-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.js
157 lines (109 loc) · 3.27 KB
/
utils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
const { tobase64, tobinary } = require('./lookup.js');
const MinHeap = require('./minheap.js');
function dec2bin(dec){
let bin = (dec >>> 0).toString(2);
let padding = '';
for(let i = 0; i< (6-bin.length);i++){
padding+='0';
}
return padding + bin;
}
function decodeMsg(stream,Graph){
let currentNode = Graph;
let message = '';
[...stream].forEach((bit)=>{
let isTerminal = !(currentNode.left && currentNode.right);
if (isTerminal){
message+=currentNode.symbol;
currentNode = Graph
}
currentNode = (bit == 0) ? currentNode.left : currentNode.right;
})
return message;
}
class Node {
constructor(freq, symbol, left, right) {
this.freq = freq;
this.symbol = symbol;
this.left = left;
this.right = right;
}
setFreq(freq){
this.freq = freq;
}
setSymbol(symbol){
this.symbol = symbol;
}
setLeft(left){
this.left = left;
}
setRight(right){
this.right = right;
}
}
function getFrequency(string) {
var freq = {};
for (var i=0; i<string.length;i++) {
var character = string.charAt(i);
if (freq[character]) {
freq[character]++;
} else {
freq[character] = 1;
}
}
let freqArr = [];
for (const a in freq){
freqArr.push({symbol:a,freq: freq[a]});
}
return freqArr;
};
function createEncoder(train){
let freqArr = getFrequency(train);
//create min heap structure
let Heap = new MinHeap();
freqArr.forEach(val=>Heap.insert(new Node(val.freq, val.symbol, null, null)));
//create encoder
for (let i = 0 ; i< freqArr.length - 1;i++){
let node = new Node();
let x = Heap.remove();
let y = Heap.remove();
node.setLeft(x);
node.setRight(y);
node.setFreq(x.freq + y.freq);
node.setSymbol(x.symbol + y.symbol);
Heap.insert(node)
}
return Heap.heap[1];
}
function getBitstring(char, Graph) {
let isLeftTerminal = !(Graph.left.left && Graph.left.right);
let isRightTerminal = !(Graph.right.left && Graph.right.right);
if(Graph.left.symbol.includes(char)){
if(isLeftTerminal){
return '0';
}else{
return '0' + getBitstring(char, Graph.left);
}
}else if (Graph.right.symbol.includes(char)){
if(isRightTerminal){
return '1'
}else{
return '1' + getBitstring(char, Graph.right);
}
}
}
function encodeConfig(config,encoder){
let code = '';
[...config].forEach(val=>code = code + getBitstring(val, encoder));
let len = (6 - (code.length % 6))%6; //padding
for (let i = 0; i<len; i++)code = code + '0';
let bytesArr = code.match(/.{1,6}/g);
let encodedParam = bytesArr.map(byte=>tobase64[parseInt(byte,2)]);
return encodedParam.reduce((acc,curr)=>acc+curr);
}
function decodeConfig(param,encoder){
let decodedText = [...param].map(val=>dec2bin(tobinary[val])) ;
return decodeMsg(decodedText.reduce((acc,cur)=>acc+cur),encoder)
}
module.exports = { getBitstring, getFrequency, Node, decodeConfig,
decodeMsg, dec2bin, encodeConfig, createEncoder}