-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
310 lines (291 loc) · 8.35 KB
/
index.js
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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/**
* The BitList class.
* @since 1.0.0
* @author Mikhail Kormanowsky
*/
class BitList {
/**
* Constructor.
* @param {Array|Number|undefined} initialValue Initial value may be a number or an array.
* @since 1.0.0
* @author Mikhail Kormanowsky
*/
constructor(initialValue) {
// Init raw list here. Our "raw list" is just a number.
// In this class we play with the binary representation of the number.
// If we set nth bit to true, we must add pow(2, n) to the raw list.
// If we set it to false, we must subtract pow(2, n) from the raw list.
this.rawList = 0;
if (initialValue instanceof Array) {
this.setArray(initialValue);
} else if (typeof initialValue === "number") {
this.rawList = initialValue;
} else if (!(typeof initialValue === "undefined")) {
throw new TypeError(
`Unexpected initial value type. Expected an array or a number, but got ${initialValue}`
);
}
}
/**
* Returns a single bit.
* @param {Number} bit Index of the bit.
* @returns {Number} The value of the requested bit.
* @since 1.0.0
* @author Mikhail Kormanowsky
*/
getBit(bit) {
if (!(typeof bit === "number" && bit >= 0)) {
throw new TypeError(
`Unexpected bit type. Expected a non-negative number, but got ${bit}`
);
}
// We take the remainder to strip everything except the part which starts with requested bit.
// We use >= comparsion to check whether the requested bit
// is set to 1 (if so, the raw list will be >= 2 ** (requested_bit)).
// We use +() to convert boolean to number.
return +(this.rawList % 2 ** (bit + 1) >= 2 ** bit);
}
/**
* Sets given bit to given value.
* @param {Number} bit Index of the bit.
* @param {any} value Value of the bit.
* @see BitList#getBit
* @since 1.0.0
* @author Mikhail Kormanowsky
*/
setBit(bit, value) {
if (!(typeof bit === "number" && bit >= 0)) {
throw new TypeError(
`Unexpected bit type. Expected a non-negative number, but got ${bit}`
);
}
// Check if old and new value are different
if (value ^ this.getBit(bit)) {
// To set bit to true means here to add pow(2, bit) to the raw list (to set bit number bit to 1).
// To set it to false means to subtract pow(2, bit) from the raw list.
if (value) {
this.rawList += 2 ** bit;
} else {
this.rawList -= 2 ** bit;
}
}
}
/**
* Copies given number to the bit list.
* @param {Number} array Number to copy from.
* @since 1.1.0
* @author Mikhail Kormanowsky
*/
setNumber(number) {
this.rawList = number;
}
/**
* Copies values from given Array to the bit list.
* @param {Array} array Array to copy from.
* @see BitList#setBit
* @since 1.0.0
* @author Mikhail Kormanowsky
*/
setArray(array) {
array.forEach((element, index) => this.setBit(index, element));
}
/**
* Copies values from given object to the bit list.
* @param {Object} object Object to copy from.
* @param {Array} keys An array containing all possible object keys in standard order.
* @see BitList#setArray
* @since 1.0.2
* @author Mikhail Kormanowsky
*/
setObject(object, keys) {
Object.keys(object).forEach((key) => {
let keyIndex = keys.indexOf(key);
if (keyIndex === -1) {
return;
}
this.setBit(keyIndex, object[key]);
});
}
/**
* Converts this bit list to number.
* @returns {Number} A non-negative number representing this bit list.
* @since 1.0.0
* @author Mikhail Kormanowsky
*/
toNumber() {
return this.rawList;
}
/**
* Converts this bit list to Array.
* @returns {Array} Array with bits.
* @since 1.0.0
* @author Mikhail Kormanowsky
*/
toArray() {
let array = [],
rawList = this.rawList;
while (rawList > 0) {
array.push(rawList % 2);
rawList = Math.floor(rawList / 2);
}
if (!array.length) {
return [0];
}
return array;
}
/**
* Converts this bit list to Object.
* @param {Array} keys An array containing all possible object keys in standard order.
* @since 1.0.2
* @author Mikhail Kormanowsky
*/
toObject(keys) {
let array = this.toArray(),
result = {};
keys.forEach((key) => {
result[key] = 0;
});
array.forEach((bit, index) => {
if (index < keys.length) {
result[keys[index]] = bit;
}
});
return result;
}
/**
* Creates customized BitList class using given keys for objects.
* @param {Array} keys An array of keys to use for objects in generated class.
* @returns {Object} A class that extends BitList and uses given keys for objects.
* The returned class also supports Object instance as initialValue in constructor.
* @example
* ```javascript
* const BitListWithMyKeys = BitList.useKeys(["myKey1", "myKey2"]);
* // ...
* let bitListInstance = new BitListWithMyKeys({myKey1: true, myKey2: false});
* ```
* @since 1.2.0
* @author Mikhail Kormanowsky
*/
static useKeys(keys) {
/**
* A BitList with keys for objects.
* @extends BitList
* @since 1.2.0
* @author Mikhail Kormanowsky
*/
return class extends this {
/**
* Checks whether given key is correct for this class,
* if so, returns its index, otherwise throws an error.
* @param {*} key
* @returns {Number} Key's index.
* @throws Error if there is no such key.
* @since 2.2.0
* @author Mikhail Kormanowsky
*/
static checkKey(key) {
const keyIndex = keys.indexOf(key);
if (keyIndex === -1) {
throw new Error(
`Unknown key: ${key}. Available keys are ${JSON.stringify(keys)}`
);
}
return keyIndex;
}
/**
* It is the same as parent class' constructor but it supports Objects as initial value.
* @param {Array|Number|Object} initialValue The initial value.
* @see BitList#constructor
* @since 1.2.0
* @author Mikhail Kormanowsky
*/
constructor(initialValue) {
try {
super(initialValue);
} catch (error) {
if (typeof initialValue === "object" && initialValue !== null) {
super(0);
this.setObject(initialValue);
} else {
throw error;
}
}
}
/**
* @param {Object} object
* @see BitList#setObject
* @since 1.2.0
* @author Mikhail Kormanowsky
*/
setObject(object) {
return super.setObject(object, keys);
}
/**
* @see BitList#toObject
* @since 1.2.0
* @author Mikhail Kormanowsky
*/
toObject() {
return super.toObject(keys);
}
/**
* Returns the value (0/1) of given key
* @param {*} key The key.
* @returns {Number} Returns the value (0/1) of given key
* @see BitList#getBit
* @see #checkKey
* @since 2.2.0
* @author Mikhail Kormanowsky
*/
get(key) {
return this.getBit(this.constructor.checkKey(key));
}
/**
* Sets the value of given key.
* @param {*} key The key.
* @param {*} value The value.
* @see BitList#setBit
* @see #checkKey
* @since 2.2.0
* @author Mikhail Kormanowsky
*/
set(key, value) {
this.setBit(this.constructor.checkKey(key), value);
}
/**
* Returns an array of enabled keys (keys which bits are set to 1)
* @returns {Array} An array of keys as they were given to .useKeys()
* @see #get
* @since 2.2.0
* @author Mikhail Kormanowsky
*/
enabledKeys() {
return keys.filter((key) => this.get(key));
}
/**
* Returns an array of disabled keys (keys which bits are set to 0)
* @returns {Array} An array of keys as they were given to .useKeys()
* @see #get
* @since 2.2.0
* @author Mikhail Kormanowsky
*/
disabledKeys() {
return keys.filter((key) => !this.get(key));
}
/**
* Returns an array of all keys
* @returns {Array}
* @since 2.2.0
* @author Mikhail Kormanowsky
*/
keys() {
return keys;
}
};
}
}
// Browser support
if (typeof window === "object") {
window.BitList = BitList;
}
module.exports = BitList;