-
-
Notifications
You must be signed in to change notification settings - Fork 92
/
fixed-deque.js
357 lines (282 loc) · 6.89 KB
/
fixed-deque.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
/**
* Mnemonist FixedDeque
* =====================
*
* Fixed capacity double-ended queue implemented as ring deque.
*/
var iterables = require('./utils/iterables.js'),
Iterator = require('obliterator/iterator');
/**
* FixedDeque.
*
* @constructor
*/
function FixedDeque(ArrayClass, capacity) {
if (arguments.length < 2)
throw new Error('mnemonist/fixed-deque: expecting an Array class and a capacity.');
if (typeof capacity !== 'number' || capacity <= 0)
throw new Error('mnemonist/fixed-deque: `capacity` should be a positive number.');
this.ArrayClass = ArrayClass;
this.capacity = capacity;
this.items = new ArrayClass(this.capacity);
this.clear();
}
/**
* Method used to clear the structure.
*
* @return {undefined}
*/
FixedDeque.prototype.clear = function() {
// Properties
this.start = 0;
this.size = 0;
};
/**
* Method used to append a value to the deque.
*
* @param {any} item - Item to append.
* @return {number} - Returns the new size of the deque.
*/
FixedDeque.prototype.push = function(item) {
if (this.size === this.capacity)
throw new Error('mnemonist/fixed-deque.push: deque capacity (' + this.capacity + ') exceeded!');
var index = this.start + this.size;
if (index >= this.capacity)
index -= this.capacity;
this.items[index] = item;
return ++this.size;
};
/**
* Method used to prepend a value to the deque.
*
* @param {any} item - Item to prepend.
* @return {number} - Returns the new size of the deque.
*/
FixedDeque.prototype.unshift = function(item) {
if (this.size === this.capacity)
throw new Error('mnemonist/fixed-deque.unshift: deque capacity (' + this.capacity + ') exceeded!');
var index = this.start - 1;
if (this.start === 0)
index = this.capacity - 1;
this.items[index] = item;
this.start = index;
return ++this.size;
};
/**
* Method used to pop the deque.
*
* @return {any} - Returns the popped item.
*/
FixedDeque.prototype.pop = function() {
if (this.size === 0)
return;
this.size--;
var index = this.start + this.size;
if (index >= this.capacity)
index -= this.capacity;
return this.items[index];
};
/**
* Method used to shift the deque.
*
* @return {any} - Returns the shifted item.
*/
FixedDeque.prototype.shift = function() {
if (this.size === 0)
return;
var index = this.start;
this.size--;
this.start++;
if (this.start === this.capacity)
this.start = 0;
return this.items[index];
};
/**
* Method used to peek the first value of the deque.
*
* @return {any}
*/
FixedDeque.prototype.peekFirst = function() {
if (this.size === 0)
return;
return this.items[this.start];
};
/**
* Method used to peek the last value of the deque.
*
* @return {any}
*/
FixedDeque.prototype.peekLast = function() {
if (this.size === 0)
return;
var index = this.start + this.size - 1;
if (index >= this.capacity)
index -= this.capacity;
return this.items[index];
};
/**
* Method used to get the desired value of the deque.
*
* @param {number} index
* @return {any}
*/
FixedDeque.prototype.get = function(index) {
if (this.size === 0 || index >= this.capacity)
return;
index = this.start + index;
if (index >= this.capacity)
index -= this.capacity;
return this.items[index];
};
/**
* Method used to iterate over the deque.
*
* @param {function} callback - Function to call for each item.
* @param {object} scope - Optional scope.
* @return {undefined}
*/
FixedDeque.prototype.forEach = function(callback, scope) {
scope = arguments.length > 1 ? scope : this;
var c = this.capacity,
l = this.size,
i = this.start,
j = 0;
while (j < l) {
callback.call(scope, this.items[i], j, this);
i++;
j++;
if (i === c)
i = 0;
}
};
/**
* Method used to convert the deque to a JavaScript array.
*
* @return {array}
*/
// TODO: optional array class as argument?
FixedDeque.prototype.toArray = function() {
// Optimization
var offset = this.start + this.size;
if (offset < this.capacity)
return this.items.slice(this.start, offset);
var array = new this.ArrayClass(this.size),
c = this.capacity,
l = this.size,
i = this.start,
j = 0;
while (j < l) {
array[j] = this.items[i];
i++;
j++;
if (i === c)
i = 0;
}
return array;
};
/**
* Method used to create an iterator over the deque's values.
*
* @return {Iterator}
*/
FixedDeque.prototype.values = function() {
var items = this.items,
c = this.capacity,
l = this.size,
i = this.start,
j = 0;
return new Iterator(function() {
if (j >= l)
return {
done: true
};
var value = items[i];
i++;
j++;
if (i === c)
i = 0;
return {
value: value,
done: false
};
});
};
/**
* Method used to create an iterator over the deque's entries.
*
* @return {Iterator}
*/
FixedDeque.prototype.entries = function() {
var items = this.items,
c = this.capacity,
l = this.size,
i = this.start,
j = 0;
return new Iterator(function() {
if (j >= l)
return {
done: true
};
var value = items[i];
i++;
if (i === c)
i = 0;
return {
value: [j++, value],
done: false
};
});
};
/**
* Attaching the #.values method to Symbol.iterator if possible.
*/
if (typeof Symbol !== 'undefined')
FixedDeque.prototype[Symbol.iterator] = FixedDeque.prototype.values;
/**
* Convenience known methods.
*/
FixedDeque.prototype.inspect = function() {
var array = this.toArray();
array.type = this.ArrayClass.name;
array.capacity = this.capacity;
// Trick so that node displays the name of the constructor
Object.defineProperty(array, 'constructor', {
value: FixedDeque,
enumerable: false
});
return array;
};
if (typeof Symbol !== 'undefined')
FixedDeque.prototype[Symbol.for('nodejs.util.inspect.custom')] = FixedDeque.prototype.inspect;
/**
* Static @.from function taking an arbitrary iterable & converting it into
* a deque.
*
* @param {Iterable} iterable - Target iterable.
* @param {function} ArrayClass - Array class to use.
* @param {number} capacity - Desired capacity.
* @return {FiniteStack}
*/
FixedDeque.from = function(iterable, ArrayClass, capacity) {
if (arguments.length < 3) {
capacity = iterables.guessLength(iterable);
if (typeof capacity !== 'number')
throw new Error('mnemonist/fixed-deque.from: could not guess iterable length. Please provide desired capacity as last argument.');
}
var deque = new FixedDeque(ArrayClass, capacity);
if (iterables.isArrayLike(iterable)) {
var i, l;
for (i = 0, l = iterable.length; i < l; i++)
deque.items[i] = iterable[i];
deque.size = l;
return deque;
}
iterables.forEach(iterable, function(value) {
deque.push(value);
});
return deque;
};
/**
* Exporting.
*/
module.exports = FixedDeque;