UNPKG

7.17 kBTypeScriptView Raw
1/**
2 * A BinaryEncoder handles the encoding to an Uint8Array.
3 */
4export class Encoder {
5 cpos: number;
6 cbuf: Uint8Array;
7 /**
8 * @type {Array<Uint8Array>}
9 */
10 bufs: Array<Uint8Array>;
11}
12export function createEncoder(): Encoder;
13export function length(encoder: Encoder): number;
14export function toUint8Array(encoder: Encoder): Uint8Array;
15export function verifyLen(encoder: Encoder, len: number): void;
16export function write(encoder: Encoder, num: number): void;
17export function set(encoder: Encoder, pos: number, num: number): void;
18export function writeUint8(encoder: Encoder, num: number): void;
19export function setUint8(encoder: Encoder, pos: number, num: number): void;
20export function writeUint16(encoder: Encoder, num: number): void;
21export function setUint16(encoder: Encoder, pos: number, num: number): void;
22export function writeUint32(encoder: Encoder, num: number): void;
23export function writeUint32BigEndian(encoder: Encoder, num: number): void;
24export function setUint32(encoder: Encoder, pos: number, num: number): void;
25export function writeVarUint(encoder: Encoder, num: number): void;
26export function writeVarInt(encoder: Encoder, num: number): void;
27export function _writeVarStringNative(encoder: Encoder, str: string): void;
28export function _writeVarStringPolyfill(encoder: Encoder, str: string): void;
29export function writeVarString(encoder: Encoder, str: string): void;
30export function writeBinaryEncoder(encoder: Encoder, append: Encoder): void;
31export function writeUint8Array(encoder: Encoder, uint8Array: Uint8Array): void;
32export function writeVarUint8Array(encoder: Encoder, uint8Array: Uint8Array): void;
33export function writeOnDataView(encoder: Encoder, len: number): DataView;
34export function writeFloat32(encoder: Encoder, num: number): void;
35export function writeFloat64(encoder: Encoder, num: number): void;
36export function writeBigInt64(encoder: Encoder, num: bigint): any;
37export function writeBigUint64(encoder: Encoder, num: bigint): any;
38export function writeAny(encoder: Encoder, data: undefined | null | number | bigint | boolean | string | {
39 [x: string]: any;
40} | Array<any> | Uint8Array): void;
41/**
42 * Now come a few stateful encoder that have their own classes.
43 */
44/**
45 * Basic Run Length Encoder - a basic compression implementation.
46 *
47 * Encodes [1,1,1,7] to [1,3,7,1] (3 times 1, 1 time 7). This encoder might do more harm than good if there are a lot of values that are not repeated.
48 *
49 * It was originally used for image compression. Cool .. article http://csbruce.com/cbm/transactor/pdfs/trans_v7_i06.pdf
50 *
51 * @note T must not be null!
52 *
53 * @template T
54 */
55export class RleEncoder<T> extends Encoder {
56 /**
57 * @param {function(Encoder, T):void} writer
58 */
59 constructor(writer: (arg0: Encoder, arg1: T) => void);
60 /**
61 * The writer
62 */
63 w: (arg0: Encoder, arg1: T) => void;
64 /**
65 * Current state
66 * @type {T|null}
67 */
68 s: T | null;
69 count: number;
70 /**
71 * @param {T} v
72 */
73 write(v: T): void;
74}
75/**
76 * Basic diff decoder using variable length encoding.
77 *
78 * Encodes the values [3, 1100, 1101, 1050, 0] to [3, 1097, 1, -51, -1050] using writeVarInt.
79 */
80export class IntDiffEncoder extends Encoder {
81 /**
82 * @param {number} start
83 */
84 constructor(start: number);
85 /**
86 * Current state
87 * @type {number}
88 */
89 s: number;
90 /**
91 * @param {number} v
92 */
93 write(v: number): void;
94}
95/**
96 * A combination of IntDiffEncoder and RleEncoder.
97 *
98 * Basically first writes the IntDiffEncoder and then counts duplicate diffs using RleEncoding.
99 *
100 * Encodes the values [1,1,1,2,3,4,5,6] as [1,1,0,2,1,5] (RLE([1,0,0,1,1,1,1,1]) ⇒ RleIntDiff[1,1,0,2,1,5])
101 */
102export class RleIntDiffEncoder extends Encoder {
103 /**
104 * @param {number} start
105 */
106 constructor(start: number);
107 /**
108 * Current state
109 * @type {number}
110 */
111 s: number;
112 count: number;
113 /**
114 * @param {number} v
115 */
116 write(v: number): void;
117}
118/**
119 * Optimized Rle encoder that does not suffer from the mentioned problem of the basic Rle encoder.
120 *
121 * Internally uses VarInt encoder to write unsigned integers. If the input occurs multiple times, we write
122 * write it as a negative number. The UintOptRleDecoder then understands that it needs to read a count.
123 *
124 * Encodes [1,2,3,3,3] as [1,2,-3,3] (once 1, once 2, three times 3)
125 */
126export class UintOptRleEncoder {
127 encoder: Encoder;
128 /**
129 * @type {number}
130 */
131 s: number;
132 count: number;
133 /**
134 * @param {number} v
135 */
136 write(v: number): void;
137 toUint8Array(): Uint8Array;
138}
139/**
140 * Increasing Uint Optimized RLE Encoder
141 *
142 * The RLE encoder counts the number of same occurences of the same value.
143 * The IncUintOptRle encoder counts if the value increases.
144 * I.e. 7, 8, 9, 10 will be encoded as [-7, 4]. 1, 3, 5 will be encoded
145 * as [1, 3, 5].
146 */
147export class IncUintOptRleEncoder {
148 encoder: Encoder;
149 /**
150 * @type {number}
151 */
152 s: number;
153 count: number;
154 /**
155 * @param {number} v
156 */
157 write(v: number): void;
158 toUint8Array(): Uint8Array;
159}
160/**
161 * A combination of the IntDiffEncoder and the UintOptRleEncoder.
162 *
163 * The count approach is similar to the UintDiffOptRleEncoder, but instead of using the negative bitflag, it encodes
164 * in the LSB whether a count is to be read. Therefore this Encoder only supports 31 bit integers!
165 *
166 * Encodes [1, 2, 3, 2] as [3, 1, 6, -1] (more specifically [(1 << 1) | 1, (3 << 0) | 0, -1])
167 *
168 * Internally uses variable length encoding. Contrary to normal UintVar encoding, the first byte contains:
169 * * 1 bit that denotes whether the next value is a count (LSB)
170 * * 1 bit that denotes whether this value is negative (MSB - 1)
171 * * 1 bit that denotes whether to continue reading the variable length integer (MSB)
172 *
173 * Therefore, only five bits remain to encode diff ranges.
174 *
175 * Use this Encoder only when appropriate. In most cases, this is probably a bad idea.
176 */
177export class IntDiffOptRleEncoder {
178 encoder: Encoder;
179 /**
180 * @type {number}
181 */
182 s: number;
183 count: number;
184 diff: number;
185 /**
186 * @param {number} v
187 */
188 write(v: number): void;
189 toUint8Array(): Uint8Array;
190}
191/**
192 * Optimized String Encoder.
193 *
194 * Encoding many small strings in a simple Encoder is not very efficient. The function call to decode a string takes some time and creates references that must be eventually deleted.
195 * In practice, when decoding several million small strings, the GC will kick in more and more often to collect orphaned string objects (or maybe there is another reason?).
196 *
197 * This string encoder solves the above problem. All strings are concatenated and written as a single string using a single encoding call.
198 *
199 * The lengths are encoded using a UintOptRleEncoder.
200 */
201export class StringEncoder {
202 /**
203 * @type {Array<string>}
204 */
205 sarr: Array<string>;
206 s: string;
207 lensE: UintOptRleEncoder;
208 /**
209 * @param {string} string
210 */
211 write(string: string): void;
212 toUint8Array(): Uint8Array;
213}
214//# sourceMappingURL=encoding.d.ts.map
\No newline at end of file