From 78ce48ca5cdf9b2473f2b43fc1153112d9a2c8d7 Mon Sep 17 00:00:00 2001 From: Tissevert Date: Tue, 2 Aug 2022 17:57:29 +0200 Subject: [PATCH] Drafting a module to share compress grids --- js/Share.js | 78 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 78 insertions(+) create mode 100644 js/Share.js diff --git a/js/Share.js b/js/Share.js new file mode 100644 index 0000000..216d169 --- /dev/null +++ b/js/Share.js @@ -0,0 +1,78 @@ +return { + compress: compress, + naive: naive +} + +function compress(grid) { + var state = { + data: '', + stack: 0, + size: 0 + }; + var color; + var count; + for(var i = 0; i < grid.length; i++) { + for(var j = 0; j < grid.length; j++) { + if(color == undefined) { + color = grid[i][j]; + } else if(grid[i][j] != color) { + } else if(count > 6) { + } else { + count++; + } + } + } + return atob(compressed); +} + +function appendLength(state, n) { + append(state, 1); + for(var i = 0; i < 3; i++) { + append(state, n % 2); + n >>= 1; + } +} + +function appendColor(state, c) { + append(state, 0); + for(var i = 0; i < 3; i++) { + append(state, c % 2); + c >>= 1; + } +} + +function appendColorNaive(state, c) { + for(var i = 0; i < 3; i++) { + append(state, c % 2); + c >>= 1; + } +} + +function append(state, one) { + if(state.size > 7) { + flush(state); + } + state.stack = 2*state.stack + (one ? 1 : 0); + state.size++; +} + +function flush(state) { + state.data = state.data + String.fromCharCode(state.stack); + state.stack = 0; + state.size = 0; +} + +function naive(grid) { + var state = { + data: '', + stack: 0, + size: 0 + }; + for(var i = 0; i < grid.length; i++) { + for(var j = 0; j < grid.length; j++) { + appendColorNaive(state, grid[i][j]); + } + } + flush(state); + return btoa(state.data); +}