|
| 1 | + |
| 2 | +import { BinaryHeap } from './heap.js'; |
| 3 | + |
| 4 | +export { HuffmanCoder } |
| 5 | + |
| 6 | +class HuffmanCoder{ |
| 7 | + |
| 8 | + stringify(node){ |
| 9 | + if(typeof(node[1])==="string"){ |
| 10 | + return '\''+node[1]; |
| 11 | + } |
| 12 | + |
| 13 | + return '0' + this.stringify(node[1][0]) + '1' + this.stringify(node[1][1]); |
| 14 | + } |
| 15 | + |
| 16 | + display(node, modify, index=1){ |
| 17 | + if(modify){ |
| 18 | + node = ['',node]; |
| 19 | + if(node[1].length===1) |
| 20 | + node[1] = node[1][0]; |
| 21 | + } |
| 22 | + |
| 23 | + if(typeof(node[1])==="string"){ |
| 24 | + return String(index) + " = " + node[1]; |
| 25 | + } |
| 26 | + |
| 27 | + let left = this.display(node[1][0], modify, index*2); |
| 28 | + let right = this.display(node[1][1], modify, index*2+1); |
| 29 | + let res = String(index*2)+" <= "+index+" => "+String(index*2+1); |
| 30 | + return res + '\n' + left + '\n' + right; |
| 31 | + } |
| 32 | + destringify(data){ |
| 33 | + let node = []; |
| 34 | + if(data[this.ind]==='\''){ |
| 35 | + this.ind++; |
| 36 | + node.push(data[this.ind]); |
| 37 | + this.ind++; |
| 38 | + return node; |
| 39 | + } |
| 40 | + |
| 41 | + this.ind++; |
| 42 | + let left = this.destringify(data); |
| 43 | + node.push(left); |
| 44 | + this.ind++; |
| 45 | + let right = this.destringify(data); |
| 46 | + node.push(right); |
| 47 | + |
| 48 | + return node; |
| 49 | + } |
| 50 | + |
| 51 | + |
| 52 | + getMappings(node, path){ |
| 53 | + if(typeof(node[1])==="string"){ |
| 54 | + this.mappings[node[1]] = path; |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + this.getMappings(node[1][0], path+"0"); |
| 59 | + this.getMappings(node[1][1], path+"1"); |
| 60 | + } |
| 61 | + |
| 62 | + encode(data){ |
| 63 | + |
| 64 | + this.heap = new BinaryHeap(); |
| 65 | + |
| 66 | + const mp = new Map(); |
| 67 | + for(let i=0;i<data.length;i++){ |
| 68 | + if(data[i] in mp){ |
| 69 | + mp[data[i]] = mp[data[i]] + 1; |
| 70 | + } else{ |
| 71 | + mp[data[i]] = 1; |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + for(const key in mp){ |
| 76 | + this.heap.insert([-mp[key], key]); |
| 77 | + } |
| 78 | + |
| 79 | + while(this.heap.size() > 1){ |
| 80 | + const node1 = this.heap.extractMax(); |
| 81 | + const node2 = this.heap.extractMax(); |
| 82 | + |
| 83 | + const node = [node1[0]+node2[0],[node1,node2]]; |
| 84 | + this.heap.insert(node); |
| 85 | + } |
| 86 | + const huffman_encoder = this.heap.extractMax(); |
| 87 | + |
| 88 | + this.mappings = {}; |
| 89 | + this.getMappings(huffman_encoder, ""); |
| 90 | + |
| 91 | + let binary_string = ""; |
| 92 | + for(let i=0;i<data.length;i++) { |
| 93 | + binary_string = binary_string + this.mappings[data[i]]; |
| 94 | + } |
| 95 | + |
| 96 | + let rem = (8 - binary_string.length%8)%8; |
| 97 | + let padding = ""; |
| 98 | + for(let i=0;i<rem;i++) |
| 99 | + padding = padding + "0"; |
| 100 | + binary_string = binary_string + padding; |
| 101 | + |
| 102 | + let result = ""; |
| 103 | + for(let i=0;i<binary_string.length;i+=8){ |
| 104 | + let num = 0; |
| 105 | + for(let j=0;j<8;j++){ |
| 106 | + num = num*2 + (binary_string[i+j]-"0"); |
| 107 | + } |
| 108 | + result = result + String.fromCharCode(num); |
| 109 | + } |
| 110 | + |
| 111 | + let final_res = this.stringify(huffman_encoder) + '\n' + rem + '\n' + result; |
| 112 | + let info = "Compression Ratio : " + data.length/final_res.length; |
| 113 | + info = "Compression complete and file sent for download" + '\n' + info; |
| 114 | + return [final_res, this.display(huffman_encoder, false), info]; |
| 115 | + } |
| 116 | + |
| 117 | + decode(data){ |
| 118 | + data = data.split('\n'); |
| 119 | + if(data.length===4){ |
| 120 | + // Handling new line |
| 121 | + data[0] = data[0] + '\n' + data[1]; |
| 122 | + data[1] = data[2]; |
| 123 | + data[2] = data[3]; |
| 124 | + data.pop(); |
| 125 | + } |
| 126 | + |
| 127 | + this.ind = 0; |
| 128 | + const huffman_decoder = this.destringify(data[0]); |
| 129 | + const text = data[2]; |
| 130 | + |
| 131 | + let binary_string = ""; |
| 132 | + for(let i=0;i<text.length;i++){ |
| 133 | + let num = text[i].charCodeAt(0); |
| 134 | + let bin = ""; |
| 135 | + for(let j=0;j<8;j++){ |
| 136 | + bin = num%2 + bin; |
| 137 | + num = Math.floor(num/2); |
| 138 | + } |
| 139 | + binary_string = binary_string + bin; |
| 140 | + } |
| 141 | + binary_string = binary_string.substring(0,binary_string.length-data[1]); |
| 142 | + |
| 143 | + console.log(binary_string.length); |
| 144 | + |
| 145 | + let res = ""; |
| 146 | + let node = huffman_decoder; |
| 147 | + for(let i=0;i<binary_string.length;i++){ |
| 148 | + if(binary_string[i]==='0'){ |
| 149 | + node = node[0]; |
| 150 | + } else{ |
| 151 | + node = node[1]; |
| 152 | + } |
| 153 | + |
| 154 | + if(typeof(node[0])==="string"){ |
| 155 | + res += node[0]; |
| 156 | + node = huffman_decoder; |
| 157 | + } |
| 158 | + } |
| 159 | + let info = "Decompression complete and file sent for download"; |
| 160 | + return [res, this.display(huffman_decoder, true), info]; |
| 161 | + } |
| 162 | +} |
0 commit comments