-
Notifications
You must be signed in to change notification settings - Fork 21
/
dict-view.ts
184 lines (170 loc) · 5.12 KB
/
dict-view.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
import type {
ComplexView,
ViewConstructor,
ViewInstance,
ViewSchema,
} from "./view-types.ts";
import type { View } from "./view.ts";
import type { ArrayView } from "./array-view.ts";
import type { VectorView } from "./vector-view.ts";
export class DictView<T extends object> extends DataView
implements ComplexView<T> {
static viewLength = 0;
static maxView: DataView;
static KeysView: typeof ArrayView;
static ValuesView: typeof VectorView;
static decode<T>(view: DataView, start = 0): T {
const { KeysView, ValuesView } = this;
const keyLength = KeysView.itemLength!;
const size = ValuesView.getSize(view, start);
const keysOffset = view.getUint32(start + ((1 + size) << 2), true);
const result = {} as T;
for (let i = 0; i < size; i++) {
const key = (KeysView.View as ViewConstructor<keyof T>).decode(
view,
start + keysOffset + i * keyLength,
keyLength,
);
const valueOffset = ValuesView.getOffset(i, view, start);
const value = !valueOffset
? undefined
: (ValuesView.View as ViewConstructor<T[keyof T]>).decode(
view,
start + valueOffset[0],
valueOffset[1],
);
result[key] = value!;
}
return result;
}
static encode<T extends object>(
value: T,
view: DataView,
start = 0,
length?: number,
): number {
const { KeysView, ValuesView } = this;
const keys = Object.keys(value);
const requiredLength = keys.length * KeysView.itemLength!;
const maxLength = length ? length - requiredLength : undefined;
let written = ValuesView.encode(
Object.values(value),
view,
start,
maxLength,
);
written += KeysView.encode(
keys,
view,
start + written,
requiredLength,
);
return written;
}
static from<T extends object, U extends DictView<T>>(value: T): U {
const end = this.encode<T>(value, this.maxView, 0, undefined);
return new this<T>(this.maxView.buffer.slice(0, end)) as U;
}
static getLength<T extends object>(value: T): number {
const { KeysView, ValuesView } = this;
const keyLength = KeysView.itemLength!;
// required length
let length = 4 + Object.keys(value).length * keyLength;
length += ValuesView.getLength(Object.values(value));
return length;
}
get<P extends keyof T>(key: P): T[P] | undefined {
const offset = this.getOffset(key);
return offset
? ((this.constructor as typeof DictView).ValuesView
.View as ViewConstructor<T[P]>).decode(
this,
offset[0],
offset[1],
)
: undefined;
}
getLength<P extends keyof T>(key: P): number {
const offset = this.getOffset(key);
return offset ? offset[1] : 0;
}
getOffset<P extends keyof T>(key: P): [number, number] | undefined {
const index = this.indexOf(key);
if (!~index) return undefined;
return (this.constructor as typeof DictView).ValuesView.getOffset(
index,
this,
0,
);
}
getView<P extends keyof T>(key: P): ViewInstance<T[P]> | undefined {
const offset = this.getOffset(key);
return offset
? new ((this.constructor as typeof DictView).ValuesView
.View as ViewConstructor<T[P]>)(
this.buffer,
this.byteOffset + offset[0],
offset[1],
)
: undefined;
}
indexOf<P extends keyof T>(key: P): number {
const { KeysView } = this.constructor as typeof DictView;
const amount = this.getUint32(0, true);
const keysOffset = this.getUint32((1 + amount) << 2, true);
return KeysView.indexOf(
key,
this,
0,
keysOffset,
amount * KeysView.itemLength,
);
}
set<P extends keyof T>(key: P, value: T[P]) {
const offset = this.getOffset(key);
if (!offset) return undefined;
((this.constructor as typeof DictView).ValuesView
.View as ViewConstructor<T[P]>).encode(
value,
this,
this.byteOffset + offset[0],
offset[1],
);
return;
}
setView<P extends keyof T>(key: P, value: DataView) {
const offset = this.getOffset(key);
if (!offset) return undefined;
new Uint8Array(this.buffer, this.byteOffset + offset[0], offset[1]).set(
new Uint8Array(value.buffer, value.byteOffset, value.byteLength),
);
return;
}
toJSON(): T {
return (this.constructor as typeof DictView).decode<T>(this);
}
static initialize<T extends object>(
schema: ViewSchema<T>,
Factory: View,
): ViewConstructor<T, ComplexView<T>> {
const keySchema = schema.propertyNames!;
const valueSchema = schema.additionalProperties!;
const KeysView = Factory.getArrayView(
keySchema as ViewSchema<number>,
undefined,
keySchema.maxLength,
) as typeof ArrayView;
const ValuesView = Factory.Views.get("vector")!.initialize(
valueSchema,
Factory,
valueSchema.type === "array"
? Factory.getArray(valueSchema)[0]
: undefined,
) as typeof VectorView;
return class extends this<T> {
static maxView = Factory.maxView;
static KeysView = KeysView;
static ValuesView = ValuesView;
};
}
}