-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Stack.js
226 lines (204 loc) · 5.32 KB
/
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
import { wholeSlice, resolveBegin, resolveEnd, wrapIndex } from './TrieUtils';
import { IndexedCollection } from './Collection';
import { ArraySeq } from './Seq';
import { Iterator, iteratorValue, iteratorDone } from './Iterator';
import { IS_STACK_SYMBOL, isStack } from './predicates/isStack';
import assertNotInfinite from './utils/assertNotInfinite';
import { asImmutable } from './methods/asImmutable';
import { asMutable } from './methods/asMutable';
import { wasAltered } from './methods/wasAltered';
import { withMutations } from './methods/withMutations';
export class Stack extends IndexedCollection {
// @pragma Construction
constructor(value) {
return value === undefined || value === null
? emptyStack()
: isStack(value)
? value
: emptyStack().pushAll(value);
}
static of(/*...values*/) {
return this(arguments);
}
toString() {
return this.__toString('Stack [', ']');
}
// @pragma Access
get(index, notSetValue) {
let head = this._head;
index = wrapIndex(this, index);
while (head && index--) {
head = head.next;
}
return head ? head.value : notSetValue;
}
peek() {
return this._head && this._head.value;
}
// @pragma Modification
push(/*...values*/) {
if (arguments.length === 0) {
return this;
}
const newSize = this.size + arguments.length;
let head = this._head;
for (let ii = arguments.length - 1; ii >= 0; ii--) {
head = {
value: arguments[ii],
next: head,
};
}
if (this.__ownerID) {
this.size = newSize;
this._head = head;
this.__hash = undefined;
this.__altered = true;
return this;
}
return makeStack(newSize, head);
}
pushAll(iter) {
iter = IndexedCollection(iter);
if (iter.size === 0) {
return this;
}
if (this.size === 0 && isStack(iter)) {
return iter;
}
assertNotInfinite(iter.size);
let newSize = this.size;
let head = this._head;
iter.__iterate(value => {
newSize++;
head = {
value: value,
next: head,
};
}, /* reverse */ true);
if (this.__ownerID) {
this.size = newSize;
this._head = head;
this.__hash = undefined;
this.__altered = true;
return this;
}
return makeStack(newSize, head);
}
pop() {
return this.slice(1);
}
clear() {
if (this.size === 0) {
return this;
}
if (this.__ownerID) {
this.size = 0;
this._head = undefined;
this.__hash = undefined;
this.__altered = true;
return this;
}
return emptyStack();
}
slice(begin, end) {
if (wholeSlice(begin, end, this.size)) {
return this;
}
let resolvedBegin = resolveBegin(begin, this.size);
const resolvedEnd = resolveEnd(end, this.size);
if (resolvedEnd !== this.size) {
// super.slice(begin, end);
return IndexedCollection.prototype.slice.call(this, begin, end);
}
const newSize = this.size - resolvedBegin;
let head = this._head;
while (resolvedBegin--) {
head = head.next;
}
if (this.__ownerID) {
this.size = newSize;
this._head = head;
this.__hash = undefined;
this.__altered = true;
return this;
}
return makeStack(newSize, head);
}
// @pragma Mutability
__ensureOwner(ownerID) {
if (ownerID === this.__ownerID) {
return this;
}
if (!ownerID) {
if (this.size === 0) {
return emptyStack();
}
this.__ownerID = ownerID;
this.__altered = false;
return this;
}
return makeStack(this.size, this._head, ownerID, this.__hash);
}
// @pragma Iteration
__iterate(fn, reverse) {
if (reverse) {
return new ArraySeq(this.toArray()).__iterate(
(v, k) => fn(v, k, this),
reverse
);
}
let iterations = 0;
let node = this._head;
while (node) {
if (fn(node.value, iterations++, this) === false) {
break;
}
node = node.next;
}
return iterations;
}
__iterator(type, reverse) {
if (reverse) {
return new ArraySeq(this.toArray()).__iterator(type, reverse);
}
let iterations = 0;
let node = this._head;
return new Iterator(() => {
if (node) {
const value = node.value;
node = node.next;
return iteratorValue(type, iterations++, value);
}
return iteratorDone();
});
}
}
Stack.isStack = isStack;
const StackPrototype = Stack.prototype;
StackPrototype[IS_STACK_SYMBOL] = true;
StackPrototype.shift = StackPrototype.pop;
StackPrototype.unshift = StackPrototype.push;
StackPrototype.unshiftAll = StackPrototype.pushAll;
StackPrototype.withMutations = withMutations;
StackPrototype.wasAltered = wasAltered;
StackPrototype.asImmutable = asImmutable;
StackPrototype['@@transducer/init'] = StackPrototype.asMutable = asMutable;
StackPrototype['@@transducer/step'] = function (result, arr) {
return result.unshift(arr);
};
StackPrototype['@@transducer/result'] = function (obj) {
return obj.asImmutable();
};
function makeStack(size, head, ownerID, hash) {
const map = Object.create(StackPrototype);
map.size = size;
map._head = head;
map.__ownerID = ownerID;
map.__hash = hash;
map.__altered = false;
return map;
}
let EMPTY_STACK;
function emptyStack() {
return EMPTY_STACK || (EMPTY_STACK = makeStack(0));
}