compress-integers
v0.3.0
Published
Compress a series of related integers.
Downloads
4
Readme
compress-integers
Compress a series of related integers.
Installation
npm i compress-integers -S
Usage
- on-line
IC = require('compress-integers');
IC.getCompressor({order: 2});
IC.write(10);
IC.write(10);
IC.getResult()
- off-line shortcut
buffer = IC.compress([1, 2, 3, 4, 5], {order:1});
- decompress
IC.decompress(buffer);
API
src
getCompressor (opts = {})
Get an order N compressor with options
param:
opts
{ Object={} }options
option:
RLE
{ boolean }use Run-Length Encoding
option:
order
{ number=1 }order of the compressor (0, 1 or 2)
return: { Compressor }
an order N compressor
compress (arr, options = {})
A shortcut for get a Compressor
param:
arr
{ Array }array of integers
param:
options
{ options={} }see getCompressor
return: { Buffer }
compressed data
decompress (buffer)
Decompress a buffer to an array of integers
param:
buffer
{ Buffer }buffer to compress
return: { Array }
array of integers
Compressor.write (int)
write an integer
param:
int
{ number }integer
Compressor.getResult ()
Get the result buffer
return: { Buffer }
buffer
HOW
- Integers are encoded with zig-zag variable-length coding using binbone
- A series of integers are stored as their differences. Order is the number of turns of making difference.
- Run-length encoding is use to make it better.
- This algorithm is not suitable for random integers.
For example:
[1, 2, 3, 4, 5]
- order1: [1, 1, 1, 1, 1]
- order2: [1, 1, 0, 0, 0] The second value is the base value.
[1, 1, 1, 1, 1]
- order1: [1, 0, 0, 0, 0]
- order2: [1, 0, 0, 0, 0]
Comparison
npm run compare
See result
License
MIT@Jingchen Zhao