This repository has been archived by the owner on Jan 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 19
/
serializeDict.ts
236 lines (204 loc) · 5.79 KB
/
serializeDict.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import { BN } from "bn.js";
import { BitString } from "../BitString";
import { Cell } from "../Cell";
import { findCommonPrefix } from "./utils/findCommonPrefix";
//
// Tree Build
//
function pad(src: string, size: number) {
while (src.length < size) {
src = '0' + src;
}
return src;
}
type Node<T> =
| {
type: 'fork',
left: Edge<T>,
right: Edge<T>
}
| {
type: 'leaf',
value: T
}
type Edge<T> = {
label: string,
node: Node<T>
};
function removePrefixMap<T>(src: Map<string, T>, length: number) {
if (length === 0) {
return src;
} else {
let res = new Map<string, T>();
for (let k of src.keys()) {
let d = src.get(k)!;
res.set(k.slice(length), d);
}
return res;
}
}
function forkMap<T>(src: Map<string, T>) {
if (src.size === 0) {
throw Error('Internal inconsistency');
}
let left = new Map<string, T>();
let right = new Map<string, T>();
for (let k of src.keys()) {
let d = src.get(k)!;
if (k.startsWith('0')) {
left.set(k.substr(1), d);
} else {
right.set(k.substr(1), d);
}
}
if (left.size === 0) {
throw Error('Internal inconsistency. Left emtpy.')
}
if (right.size === 0) {
throw Error('Internal inconsistency. Right emtpy.')
}
return { left, right };
}
function buildNode<T>(src: Map<string, T>): Node<T> {
if (src.size === 0) {
throw Error('Internal inconsistency');
}
if (src.size === 1) {
return { type: 'leaf', value: Array.from(src.values())[0] };
}
let { left, right } = forkMap(src);
return {
type: 'fork',
left: buildEdge(left),
right: buildEdge(right)
}
}
function buildEdge<T>(src: Map<string, T>): Edge<T> {
if (src.size === 0) {
throw Error('Internal inconsistency');
}
const label = findCommonPrefix(Array.from(src.keys()));
return { label, node: buildNode(removePrefixMap(src, label.length)) };
}
export function buildTree<T>(src: Map<string, T>, keyLength: number) {
// Convert map keys
let converted = new Map<string, T>();
for (let k of Array.from(src.keys())) {
const padded = pad(new BN(k).toString(2), keyLength);
converted.set(padded, src.get(k)!);
}
// Calculate root label
return buildEdge(converted);
}
//
// Serialization
//
export function writeLabelShort(src: string, to: BitString) {
// Header
to.writeBit(0);
// Unary length
for (let i = 0; i < src.length; i++) {
to.writeBit(1);
}
to.writeBit(0);
// Value
for (let i = 0; i < src.length; i++) {
to.writeBit(src[i] === '1');
}
return to;
}
function labelShortLength(src: string) {
return 1 + src.length + 1 + src.length;
}
export function writeLabelLong(src: string, keyLength: number, to: BitString) {
// Header
to.writeBit(1);
to.writeBit(0);
// Length
let length = Math.ceil(Math.log2(keyLength + 1));
to.writeUint(src.length, length);
// Value
for (let i = 0; i < src.length; i++) {
to.writeBit(src[i] === '1');
}
return to;
}
function labelLongLength(src: string, keyLength: number) {
return 1 + 1 + Math.ceil(Math.log2(keyLength + 1)) + src.length;
}
export function writeLabelSame(value: number | boolean, length: number, keyLength: number, to: BitString) {
// Header
to.writeBit(1);
to.writeBit(1);
// Value
to.writeBit(value);
// Length
let lenLen = Math.ceil(Math.log2(keyLength + 1));
to.writeUint(length, lenLen);
}
function labelSameLength(keyLength: number) {
return 1 + 1 + 1 + Math.ceil(Math.log2(keyLength + 1));
}
function isSame(src: string) {
if (src.length === 0 || src.length === 1) {
return true;
}
for (let i = 1; i < src.length; i++) {
if (src[i] !== src[0]) {
return false;
}
}
return true;
}
export function detectLabelType(src: string, keyLength: number) {
let kind: 'short' | 'long' | 'same' = 'short';
let kindLength = labelShortLength(src);
let longLength = labelLongLength(src, keyLength);
if (longLength < kindLength) {
kindLength = longLength;
kind = 'long';
}
if (isSame(src)) {
let sameLength = labelSameLength(keyLength);
if (sameLength < kindLength) {
kindLength = sameLength;
kind = 'same';
}
}
return kind;
}
function writeLabel(src: string, keyLength: number, to: BitString) {
let type = detectLabelType(src, keyLength);
if (type === 'short') {
writeLabelShort(src, to);
}
if (type === 'long') {
writeLabelLong(src, keyLength, to);
}
if (type === 'same') {
writeLabelSame(src[0] === '1', src.length, keyLength, to);
}
}
function writeNode<T>(src: Node<T>, keyLength: number, serializer: (src: T, cell: Cell) => void, to: Cell) {
if (src.type === 'leaf') {
serializer(src.value, to);
}
if (src.type === 'fork') {
const leftCell = new Cell();
const rightCell = new Cell();
writeEdge(src.left, keyLength - 1, serializer, leftCell);
writeEdge(src.right, keyLength - 1, serializer, rightCell);
to.refs.push(leftCell);
to.refs.push(rightCell);
}
}
function writeEdge<T>(src: Edge<T>, keyLength: number, serializer: (src: T, cell: Cell) => void, to: Cell) {
writeLabel(src.label, keyLength, to.bits);
writeNode(src.node, keyLength - src.label.length, serializer, to);
}
export function serializeDict<T>(src: Map<string, T>, keyLength: number, serializer: (src: T, cell: Cell) => void) {
const tree = buildTree<T>(src, keyLength);
const dest = new Cell();
writeEdge(tree, keyLength, serializer, dest);
return dest;
}