-
Notifications
You must be signed in to change notification settings - Fork 21
/
bit-field.ts
244 lines (222 loc) · 6.16 KB
/
bit-field.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
237
238
239
240
241
242
243
244
import { getBitSize } from "./utilities.ts";
import type {
BitFieldConstructor,
BitFieldStructure,
} from "./bit-field-types.ts";
const SIGN_BIT = 2147483647;
const MAX_BITWISE_SIZE = 31;
class BitField<
K extends PropertyKey,
> implements BitFieldStructure<K, number> {
static schema: Record<PropertyKey, number>;
static fields: Array<PropertyKey>;
static masks: Record<PropertyKey, number>;
static offsets: Record<PropertyKey, number>;
static mask = 2 << 30;
static size = 31;
value = 0;
constructor(
data: number | BitField<K> | Array<number> | Record<K, number> = 0,
) {
this.value = typeof data === "number"
? data
: data instanceof BitField
? data.valueOf()
: (this.constructor as typeof BitField).encode(data);
}
static decode<K extends PropertyKey>(
data: number,
): Record<K, number> {
const schema = this.schema as Record<K, number>;
const fields = this.fields as Array<K>;
const masks = this.masks as Record<K, number>;
const result = {} as Record<K, number>;
let value = data;
for (let i = 0; i < fields.length; i++) {
const field = fields[i];
const size = schema[field];
result[field] = value & masks[field];
value >>= size;
}
return result;
}
static encode<K extends PropertyKey>(
data: Array<number> | Record<K, number>,
): number {
const schema = this.schema as Record<K, number>;
const fields = this.fields as Array<K>;
const array = Array.isArray(data)
? data
: fields.map((name) => data[name] || 0);
let result = 0;
for (let i = fields.length - 1; i >= 0; i--) {
const field = fields[i];
const current = array[i];
result <<= schema[field];
result |= current;
}
return result;
}
static getMatcher<K extends PropertyKey>(
matcher: Partial<Record<K, number>>,
): [number, number] {
const masks = this.masks as Record<K, number>;
const offsets = this.offsets as Record<K, number>;
const fields = Object.keys(matcher) as Array<K>;
let mask = 0;
let value = 0;
for (let i = 0; i < fields.length; i++) {
const fieldName = fields[i];
const fieldMask = masks[fieldName] << offsets[fieldName];
const fieldValue = matcher[fieldName]!;
value = (value & ~fieldMask) | (fieldValue << offsets[fieldName]);
mask |= fieldMask;
}
return [value, this.mask ^ mask];
}
static getMinSize(number: number): number {
return getBitSize(number);
}
static isValid<T extends Record<K, number>, K extends keyof T>(
data: T,
): boolean {
const masks = this.masks as T;
const fields = Object.keys(data) as Array<K>;
for (let i = 0; i < fields.length; i++) {
const field = fields[i];
const value = data[field];
if ((value & SIGN_BIT) !== value || value > masks[field]) return false;
}
return true;
}
static match(value: number, matcher: [number, number]): boolean {
return (value & matcher[1]) === matcher[0];
}
*[Symbol.iterator](): Generator<number> {
const fields = (this.constructor as BitFieldConstructor<K>).fields;
for (let i = 0; i < fields.length; i++) {
yield this.get(fields[i]);
}
}
get(field: K): number {
const { offsets, masks } = this.constructor as BitFieldConstructor<K>;
return (this.value >> offsets[field]) & masks[field];
}
has(...fields: Array<K>): boolean {
const { offsets } = this.constructor as BitFieldConstructor<K>;
let mask = 0;
for (let i = 0; i < fields.length; i++) {
mask |= 1 << offsets[fields[i]];
}
mask |= this.value;
return this.value === mask;
}
match(matcher: Partial<Record<K, number>> | [number, number]): boolean {
return (this.constructor as BitFieldConstructor<K>).match(
this.value,
Array.isArray(matcher)
? matcher
: (this.constructor as BitFieldConstructor<K>).getMatcher(matcher),
);
}
set(field: K, value = 1): this {
const { offsets, masks } = this.constructor as BitFieldConstructor<K>;
this.value = (this.value & ~(masks[field] << offsets[field])) |
(value << offsets[field]);
return this;
}
toJSON() {
return this.value;
}
toObject(): Record<K, number> {
return (this.constructor as BitFieldConstructor<K>).decode(this.value);
}
toString(): string {
return this.value.toString();
}
valueOf(): number {
return this.value;
}
}
/**
* Creates a BitField class from with a given schema. BitField uses numbers as bitfields
* to store and operate on data using bitwise operations. The size of the field is limited to 31 bits,
* for a larger bitfields consider using BigBitField class that uses bigints instead.
*
* @param schema the schema
* @returns the BitFieldClass
*
* @example
* const Field = BitFieldMixin({ width: 8, height: 8 });
* const field = new Field({ width: 100, height: 200 });
* field.get('width');
* //=> 100;
* field.get('height');
* //=> 200
* field.set('width', 18);
* field.get('width');
* //=> 18
* field.toObject();
* //=> { width: 18, height: 200 }
*/
export function BitFieldMixin<
T extends Record<K, number>,
K extends keyof T,
>(schema: T): BitFieldConstructor<K> {
const fields = Object.keys(schema) as Array<K>;
const masks = {} as Record<K, number>;
const offsets = {} as Record<K, number>;
let lastOffset = 0;
for (let i = 0; i < fields.length; i++) {
const field = fields[i];
const size = schema[field];
masks[field] = (2 << (size - 1)) - 1;
offsets[field] = lastOffset;
lastOffset += size;
}
if (lastOffset > MAX_BITWISE_SIZE) {
throw TypeError("The total size of the bitfield exceeds 31 bits.");
}
return class extends BitField<K> {
static schema = schema;
static fields = fields;
static masks = masks;
static offsets = offsets;
size = lastOffset;
mask = 2 << (lastOffset - 1);
};
}
const _BitField = BitFieldMixin({
0: 1,
1: 1,
2: 1,
3: 1,
4: 1,
5: 1,
6: 1,
7: 1,
8: 1,
9: 1,
10: 1,
11: 1,
12: 1,
13: 1,
14: 1,
15: 1,
16: 1,
17: 1,
18: 1,
19: 1,
20: 1,
21: 1,
22: 1,
23: 1,
24: 1,
25: 1,
26: 1,
27: 1,
28: 1,
29: 1,
30: 1,
});
export { _BitField as BitField };