forked from Yomguithereal/mnemonist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfinite-stack.js
227 lines (188 loc) · 4.94 KB
/
finite-stack.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
/**
* Mnemonist FiniteStack
* ======================
*
* The finite stack is a stack whose capacity is defined beforehand and that
* cannot be exceeded. This class is really useful when combined with
* byte arrays to save up some memory and avoid memory re-allocation, hence
* speeding up computations.
*
* This has however a downside: you need to know the maximum size you stack
* can have during your iteration (which is not too difficult to compute when
* performing, say, a DFS on a balanced binary tree).
*/
var Iterator = require('obliterator/iterator'),
iterate = require('./utils/iterate.js');
/**
* FiniteStack
*
* @constructor
* @param {function} ArrayClass - Array class to use.
* @param {number} capacity - Desired capacity.
*/
function FiniteStack(ArrayClass, capacity) {
if (arguments.length < 2)
throw new Error('mnemonist/finite-stack: expecting an Array class and a capacity.');
if (typeof capacity !== 'number' || capacity <= 0)
throw new Error('mnemonist/finite-stack: `capacity` should be a positive number.');
this.capacity = capacity;
this.ArrayClass = ArrayClass;
this.items = new this.ArrayClass(this.capacity);
this.clear();
}
/**
* Method used to clear the stack.
*
* @return {undefined}
*/
FiniteStack.prototype.clear = function() {
// Properties
this.size = 0;
};
/**
* Method used to add an item to the stack.
*
* @param {any} item - Item to add.
* @return {number}
*/
FiniteStack.prototype.push = function(item) {
if (this.size === this.capacity)
throw new Error('mnemonist/finite-stack: stack capacity (' + this.capacity + ') exceeded!');
this.items[this.size++] = item;
return this.size;
};
/**
* Method used to retrieve & remove the last item of the stack.
*
* @return {any}
*/
FiniteStack.prototype.pop = function() {
if (this.size === 0)
return;
return this.items[--this.size];
};
/**
* Method used to get the last item of the stack.
*
* @return {any}
*/
FiniteStack.prototype.peek = function() {
return this.items[this.size - 1];
};
/**
* Method used to iterate over the stack.
*
* @param {function} callback - Function to call for each item.
* @param {object} scope - Optional scope.
* @return {undefined}
*/
FiniteStack.prototype.forEach = function(callback, scope) {
scope = arguments.length > 1 ? scope : this;
for (var i = 0, l = this.items.length; i < l; i++)
callback.call(scope, this.items[l - i - 1], i, this);
};
/**
* Method used to convert the stack to a JavaScript array.
*
* @return {array}
*/
FiniteStack.prototype.toArray = function() {
var array = new this.ArrayClass(this.size),
l = this.size - 1,
i = this.size;
while (i--)
array[i] = this.items[l - i];
return array;
};
/**
* Method used to create an iterator over a stack's values.
*
* @return {Iterator}
*/
FiniteStack.prototype.values = function() {
var items = this.items,
l = this.size,
i = 0;
return new Iterator(function() {
if (i >= l)
return {
done: true
};
var value = items[l - i - 1];
i++;
return {
value: value,
done: false
};
});
};
/**
* Method used to create an iterator over a stack's entries.
*
* @return {Iterator}
*/
FiniteStack.prototype.entries = function() {
var items = this.items,
l = this.size,
i = 0;
return new Iterator(function() {
if (i >= l)
return {
done: true
};
var value = items[l - i - 1];
return {
value: [i++, value],
done: false
};
});
};
/**
* Attaching the #.values method to Symbol.iterator if possible.
*/
if (typeof Symbol !== 'undefined')
FiniteStack.prototype[Symbol.iterator] = FiniteStack.prototype.values;
/**
* Convenience known methods.
*/
FiniteStack.prototype.toString = function() {
return this.toArray().join(',');
};
FiniteStack.prototype.toJSON = function() {
return this.toArray();
};
FiniteStack.prototype.inspect = function() {
var array = this.toArray();
array.capacity = this.capacity;
// Trick so that node displays the name of the constructor
Object.defineProperty(array, 'constructor', {
value: FiniteStack,
enumerable: false
});
return array;
};
/**
* Static @.from function taking an abitrary iterable & converting it into
* a stack.
*
* @param {Iterable} iterable - Target iterable.
* @param {function} ArrayClass - Array class to use.
* @param {number} capacity - Desired capacity.
* @return {FiniteStack}
*/
FiniteStack.from = function(iterable, ArrayClass, capacity) {
if (arguments.length < 3) {
capacity = iterate.guessLength(iterable);
if (typeof capacity !== 'number')
throw new Error('mnemonist/finite-stack.from: could not guess iterable length. Please provide desired capacity as last argument.');
}
var stack = new FiniteStack(ArrayClass, capacity);
iterate(iterable, function(value) {
stack.push(value);
});
return stack;
};
/**
* Exporting.
*/
module.exports = FiniteStack;