forked from Yomguithereal/mnemonist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vector.js
373 lines (303 loc) · 8.72 KB
/
vector.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
/**
* Mnemonist Vector
* =================
*
* Abstract implementation of a growing array that can be used with JavaScript
* typed arrays and other array-like structures.
*
* Note: should try and use ArrayBuffer.transfer when it will be available.
*/
var Iterator = require('obliterator/iterator'),
forEach = require('obliterator/foreach'),
iterables = require('./utils/iterables.js'),
typed = require('./utils/typed-arrays.js');
/**
* Defaults.
*/
var DEFAULT_GROWING_POLICY = function(currentCapacity) {
return Math.max(1, Math.ceil(currentCapacity * 1.5));
};
var pointerArrayFactory = function(capacity) {
var PointerArray = typed.getPointerArray(capacity);
return new PointerArray(capacity);
};
/**
* Vector.
*
* @constructor
* @param {function} ArrayClass - An array constructor.
* @param {number|object} initialCapacityOrOptions - Self-explanatory:
* @param {number} initialCapacity - Initial capacity.
* @param {number} initialLength - Initial length.
* @param {function} policy - Allocation policy.
*/
function Vector(ArrayClass, initialCapacityOrOptions) {
if (arguments.length < 1)
throw new Error('mnemonist/vector: expecting at least a byte array constructor.');
var initialCapacity = initialCapacityOrOptions || 0,
policy = DEFAULT_GROWING_POLICY,
initialLength = 0,
factory = false;
if (typeof initialCapacityOrOptions === 'object') {
initialCapacity = initialCapacityOrOptions.initialCapacity || 0;
initialLength = initialCapacityOrOptions.initialLength || 0;
policy = initialCapacityOrOptions.policy || policy;
factory = initialCapacityOrOptions.factory === true;
}
this.factory = factory ? ArrayClass : null;
this.ArrayClass = ArrayClass;
this.length = initialLength;
this.capacity = Math.max(initialLength, initialCapacity);
this.policy = policy;
this.array = new ArrayClass(this.capacity);
}
/**
* Method used to set a value.
*
* @param {number} index - Index to edit.
* @param {any} value - Value.
* @return {Vector}
*/
Vector.prototype.set = function(index, value) {
// Out of bounds?
if (this.length < index)
throw new Error('Vector(' + this.ArrayClass.name + ').set: index out of bounds.');
// Updating value
this.array[index] = value;
return this;
};
/**
* Method used to get a value.
*
* @param {number} index - Index to retrieve.
* @return {any}
*/
Vector.prototype.get = function(index) {
if (this.length < index)
return undefined;
return this.array[index];
};
/**
* Method used to apply the growing policy.
*
* @param {number} [override] - Override capacity.
* @return {number}
*/
Vector.prototype.applyPolicy = function(override) {
var newCapacity = this.policy(override || this.capacity);
if (typeof newCapacity !== 'number' || newCapacity < 0)
throw new Error('mnemonist/vector.applyPolicy: policy returned an invalid value (expecting a positive integer).');
if (newCapacity <= this.capacity)
throw new Error('mnemonist/vector.applyPolicy: policy returned a less or equal capacity to allocate.');
// TODO: we should probably check that the returned number is an integer
return newCapacity;
};
/**
* Method used to reallocate the underlying array.
*
* @param {number} capacity - Target capacity.
* @return {Vector}
*/
Vector.prototype.reallocate = function(capacity) {
if (capacity === this.capacity)
return this;
var oldArray = this.array;
if (capacity < this.length)
this.length = capacity;
if (capacity > this.capacity) {
if (this.factory === null)
this.array = new this.ArrayClass(capacity);
else
this.array = this.factory(capacity);
if (typed.isTypedArray(this.array)) {
this.array.set(oldArray, 0);
}
else {
for (var i = 0, l = this.length; i < l; i++)
this.array[i] = oldArray[i];
}
}
else {
this.array = oldArray.slice(0, capacity);
}
this.capacity = capacity;
return this;
};
/**
* Method used to grow the array.
*
* @param {number} [capacity] - Optional capacity to match.
* @return {Vector}
*/
Vector.prototype.grow = function(capacity) {
var newCapacity;
if (typeof capacity === 'number') {
if (this.capacity >= capacity)
return this;
// We need to match the given capacity
newCapacity = this.capacity;
while (newCapacity < capacity)
newCapacity = this.applyPolicy(newCapacity);
this.reallocate(newCapacity);
return this;
}
// We need to run the policy once
newCapacity = this.applyPolicy();
this.reallocate(newCapacity);
return this;
};
/**
* Method used to resize the array. Won't deallocate.
*
* @param {number} length - Target length.
* @return {Vector}
*/
Vector.prototype.resize = function(length) {
if (length === this.length)
return this;
if (length < this.length) {
this.length = length;
return this;
}
this.length = length;
this.reallocate(length);
return this;
};
/**
* Method used to push a value into the array.
*
* @param {any} value - Value to push.
* @return {number} - Length of the array.
*/
Vector.prototype.push = function(value) {
if (this.capacity === this.length)
this.grow();
this.array[this.length++] = value;
return this.length;
};
/**
* Method used to pop the last value of the array.
*
* @return {number} - The popped value.
*/
Vector.prototype.pop = function() {
if (this.length === 0)
return;
return this.array[--this.length];
};
/**
* Method used to create an iterator over a vector's values.
*
* @return {Iterator}
*/
Vector.prototype.values = function() {
var items = this.array,
l = this.length,
i = 0;
return new Iterator(function() {
if (i >= l)
return {
done: true
};
var value = items[i];
i++;
return {
value: value,
done: false
};
});
};
/**
* Method used to create an iterator over a vector's entries.
*
* @return {Iterator}
*/
Vector.prototype.entries = function() {
var items = this.array,
l = this.length,
i = 0;
return new Iterator(function() {
if (i >= l)
return {
done: true
};
var value = items[i];
return {
value: [i++, value],
done: false
};
});
};
/**
* Attaching the #.values method to Symbol.iterator if possible.
*/
if (typeof Symbol !== 'undefined')
Vector.prototype[Symbol.iterator] = Vector.prototype.values;
/**
* Convenience known methods.
*/
Vector.prototype.inspect = function() {
var proxy = this.array.slice(0, this.length);
proxy.type = this.array.constructor.name;
proxy.items = this.length;
proxy.capacity = this.capacity;
// Trick so that node displays the name of the constructor
Object.defineProperty(proxy, 'constructor', {
value: Vector,
enumerable: false
});
return proxy;
};
if (typeof Symbol !== 'undefined')
Vector.prototype[Symbol.for('nodejs.util.inspect.custom')] = Vector.prototype.inspect;
/**
* Static @.from function taking an arbitrary iterable & converting it into
* a vector.
*
* @param {Iterable} iterable - Target iterable.
* @param {function} ArrayClass - Byte array class.
* @param {number} capacity - Desired capacity.
* @return {Vector}
*/
Vector.from = function(iterable, ArrayClass, capacity) {
if (arguments.length < 3) {
// Attempting to guess the needed capacity
capacity = iterables.guessLength(iterable);
if (typeof capacity !== 'number')
throw new Error('mnemonist/vector.from: could not guess iterable length. Please provide desired capacity as last argument.');
}
var vector = new Vector(ArrayClass, capacity);
forEach(iterable, function(value) {
vector.push(value);
});
return vector;
};
/**
* Exporting.
*/
function subClass(ArrayClass) {
var SubClass = function(initialCapacityOrOptions) {
Vector.call(this, ArrayClass, initialCapacityOrOptions);
};
for (var k in Vector.prototype) {
if (Vector.prototype.hasOwnProperty(k))
SubClass.prototype[k] = Vector.prototype[k];
}
SubClass.from = function(iterable, capacity) {
return Vector.from(iterable, ArrayClass, capacity);
};
if (typeof Symbol !== 'undefined')
SubClass.prototype[Symbol.iterator] = SubClass.prototype.values;
return SubClass;
}
Vector.Int8Vector = subClass(Int8Array);
Vector.Uint8Vector = subClass(Uint8Array);
Vector.Uint8ClampedVector = subClass(Uint8ClampedArray);
Vector.Int16Vector = subClass(Int16Array);
Vector.Uint16Vector = subClass(Uint16Array);
Vector.Int32Vector = subClass(Int32Array);
Vector.Uint32Vector = subClass(Uint32Array);
Vector.Float32Vector = subClass(Float32Array);
Vector.Float64Vector = subClass(Float64Array);
Vector.PointerVector = subClass(pointerArrayFactory);
module.exports = Vector;