-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathvec.ts
59 lines (46 loc) · 1.68 KB
/
vec.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
import { concat } from '@ethersproject/bytes';
import { WORD_SIZE } from '../constants';
import type { TypesOfCoder } from './abstract-coder';
import Coder from './abstract-coder';
import U64Coder from './u64';
const VEC_PROPERTY_SPACE = 3; // ptr + cap + length
type InputValueOf<TCoder extends Coder> = Array<TypesOfCoder<TCoder>['Input']>;
type DecodedValueOf<TCoder extends Coder> = Array<TypesOfCoder<TCoder>['Decoded']>;
export default class VecCoder<TCoder extends Coder> extends Coder<
InputValueOf<TCoder>,
DecodedValueOf<TCoder>
> {
coder: TCoder;
constructor(coder: TCoder) {
super('struct', `struct Vec`, 0);
this.coder = coder;
}
static getBaseOffset(): number {
return VEC_PROPERTY_SPACE * WORD_SIZE;
}
getEncodedVectorData(value: InputValueOf<TCoder>): Uint8Array {
if (!Array.isArray(value)) {
this.throwError('expected array value', value);
}
const encodedValues = Array.from(value).map((v) => this.coder.encode(v));
return concat(encodedValues);
}
encode(value: InputValueOf<TCoder>): Uint8Array {
if (!Array.isArray(value)) {
this.throwError('expected array value', value);
}
const parts: Uint8Array[] = [];
// pointer (ptr)
const pointer = this.offset || 0;
parts.push(new U64Coder().encode(pointer));
// capacity (cap)
parts.push(new U64Coder().encode(value.length));
// length (len)
parts.push(new U64Coder().encode(value.length));
return concat(parts);
}
decode(data: Uint8Array, offset: number): [DecodedValueOf<TCoder>, number] {
this.throwError('unexpected Vec decode', 'not implemented');
return [undefined as unknown as DecodedValueOf<TCoder>, offset];
}
}