UNPKG

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