-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
core.js
364 lines (308 loc) · 9.5 KB
/
core.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
'use strict';
import {addAliases, normalizeIterator} from '../meta-utils.js';
import {ExtListBase, HeadNode} from './nodes.js';
import {append} from './basics.js';
import Ptr from './ptr.js';
export class SList extends HeadNode {
get frontPtr() {
return new Ptr(this);
}
get ptrRange() {
return this.isEmpty ? null : {from: new Ptr(this), to: this.last};
}
makePtr(node) {
if (node && !this.isNodeLike(node)) throw new Error('"node" is not a compatible node');
return new Ptr(this, node);
}
makePtrFromPrev(prev) {
if (prev && !this.isNodeLike(prev)) throw new Error('"prev" is not a compatible node');
return new Ptr(this, null, prev || this);
}
popFrontNode() {
if (this[this.nextName] === this) return undefined;
const node = this[this.nextName];
this[this.nextName] = node[this.nextName];
if (this[this.nextName] === this) this.last = this;
return (node[this.nextName] = node);
}
pushFront(value) {
const node = this.adoptValue(value);
node[this.nextName] = this[this.nextName];
this[this.nextName] = node;
if (node[this.nextName] === this) this.last = node;
return this.makePtr();
}
pushBack(value) {
const node = this.adoptValue(value);
node[this.nextName] = this;
const last = this.last;
this.last = last[this.nextName] = node;
return this.makePtrFromPrev(last);
}
pushFrontNode(nodeOrPtr) {
const node = this.adoptNode(nodeOrPtr);
node[this.nextName] = this[this.nextName];
this[this.nextName] = node;
if (node[this.nextName] === this) this.last = node;
return this.makePtr();
}
pushBackNode(nodeOrPtr) {
const node = this.adoptNode(nodeOrPtr);
node[this.nextName] = this;
const last = this.last;
this.last = last[this.nextName] = node;
return this.makePtrFromPrev(last);
}
appendFront(list) {
if (!this.isCompatible(list)) throw new Error('Incompatible lists');
if (list.isEmpty) return this;
list.last[this.nextName] = this[this.nextName];
this[this.nextName] = list[this.nextName];
if (list.last[this.nextName] === this) this.last = list.last;
list[this.nextName] = list.last = list; // clear the list
return this.makePtr();
}
appendBack(list) {
if (!this.isCompatible(list)) throw new Error('Incompatible lists');
if (list.isEmpty) return this;
this.last[this.nextName] = list[this.nextName];
list.last[this.nextName] = this;
const last = this.last;
this.last = list.last;
list[this.nextName] = list.last = list; // clear the list
return this.makePtrFromPrev(last);
}
moveToFront(ptr) {
if (!this.isCompatiblePtr(ptr)) throw new Error('Incompatible pointer');
ptr.list = this;
if (ptr.isHead) return this;
const node = ptr.removeCurrent();
ptr.prevNode = this;
return this.pushFrontNode(node);
}
moveToBack(ptr) {
if (!this.isCompatiblePtr(ptr)) throw new Error('Incompatible pointer');
ptr.list = this;
if (ptr.isHead) return this;
const node = ptr.removeCurrent();
ptr.prevNode = this.last;
return this.pushBackNode(node);
}
clear(drop) {
if (drop) {
let current = this;
do {
const next = current[this.nextName];
current[this.nextName] = current;
current = next;
} while (current !== this);
} else {
this[this.nextName] = this;
}
this.last = this;
return this;
}
removeNode(ptr) {
if (!ptr.isPrevNodeValid()) throw new Error('Cannot remove node: "prevNode" is invalid');
if (!this.isCompatiblePtr(ptr)) throw new Error('Incompatible pointer');
const node = ptr.prevNode[this.nextName];
if (node === this || node === ptr.prevNode) return null;
if (this.last === node) this.last = ptr.prevNode;
ptr.prevNode[this.nextName] = node[this.nextName];
ptr.list = this;
node[this.nextName] = node;
return node;
}
removeRange(ptrRange, drop) {
return this.extractRange(ptrRange).clear(drop);
}
extractRange(ptrRange = {}) {
const originalTo = ptrRange.to;
ptrRange = this.normalizePtrRange(ptrRange.from ? ptrRange : {...ptrRange, from: this.frontPtr});
if (!ptrRange.from.isPrevNodeValid()) throw new Error('Cannot extract range: "prevNode" is invalid');
ptrRange.to ||= this.last;
const extracted = this.make();
append(this, extracted, {prevFrom: ptrRange.from.prevNode, to: ptrRange.to});
extracted.last = ptrRange.to;
ptrRange.from.list = extracted;
if (originalTo instanceof Ptr) originalTo.list = extracted;
if (ptrRange.to === this.last) this.last = ptrRange.from.prevNode;
return extracted;
}
extractBy(condition) {
const extracted = this.make();
if (this.isEmpty) return extracted;
for (const ptr = this.frontPtr; !ptr.isHead; ) {
if (condition(ptr.node)) {
extracted.pushBackNode(ptr.removeCurrent());
} else {
ptr.next();
}
}
return extracted;
}
reverse() {
if (this.isOneOrEmpty) return this;
this.last = this[this.nextName];
let prev = this,
current = prev[this.nextName];
do {
const next = current[this.nextName];
current[this.nextName] = prev;
prev = current;
current = next;
} while (current !== this);
this[this.nextName] = prev;
return this;
}
sort(lessFn) {
if (this.isOneOrEmpty) return this;
const left = this.make(),
right = this.make();
// split into two sublists
for (let isLeft = true; !this.isEmpty; isLeft = !isLeft) {
(isLeft ? left : right).pushBackNode(this.popFrontNode());
}
// the list is empty now
// sort sublists
left.sort(lessFn);
right.sort(lessFn);
// merge sublists
while (!left.isEmpty && !right.isEmpty) {
this.pushBackNode((lessFn(left.front, right.front) ? left : right).popFrontNode());
}
if (!left.isEmpty) this.appendBack(left);
if (!right.isEmpty) this.appendBack(right);
return this;
}
releaseAsPtrRange() {
if (this.isEmpty) return null;
const head = this[this.nextName],
tail = this.last;
this.clear();
tail[this.nextName] = head;
return {from: new Ptr(this, null, tail), to: tail};
}
releaseRawList() {
if (this.isEmpty) return null;
const head = this[this.nextName],
tail = this.last;
this.clear();
tail[this.nextName] = head;
return head;
}
releaseNTList() {
if (this.isEmpty) return null;
const head = this[this.nextName],
tail = this.last;
this.clear();
tail[this.nextName] = null;
return {head, tail};
}
validateRange(range = {}) {
range = this.normalizeRange(range);
let current = range.from;
do {
if (current === this) return false;
current = current[this.nextName];
} while (current !== range.to);
return true;
}
// iterators
[Symbol.iterator]() {
let current = this[this.nextName],
readyToStop = this.isEmpty;
return normalizeIterator({
next: () => {
if (readyToStop && current === this) return {done: true};
readyToStop = true;
const value = current;
current = current[this.nextName];
return {value};
}
});
}
getNodeIterator(range = {}) {
range = this.normalizeRange(range);
const {from, to} = range;
return {
[Symbol.iterator]: () => {
let current = from || this[this.nextName],
readyToStop = this.isEmpty;
const stop = to ? to[this.nextName] : this;
return normalizeIterator({
next: () => {
if (readyToStop && current === stop) return {done: true};
readyToStop = true;
const value = current;
current = current[this.nextName];
return {value};
}
});
}
};
}
getPtrIterator(ptrRange = {}) {
if (!ptrRange.from) ptrRange = Object.assign({from: this.frontPtr}, ptrRange);
ptrRange = this.normalizePtrRange(ptrRange);
const {from: fromPtr, to} = ptrRange;
return {
[Symbol.iterator]: () => {
let current = fromPtr.clone(),
readyToStop = this.isEmpty;
const stop = to ? to[this.nextName] : this;
return normalizeIterator({
next: () => {
if (readyToStop && current.node === stop) return {done: true};
readyToStop = true;
const value = current.clone();
current = current.next();
return {value};
}
});
}
};
}
// meta helpers
make() {
return new SList(this);
}
makeFrom(values) {
return SList.from(values, this);
}
makeFromRange(range) {
return SList.fromRange(range, this);
}
static from(values, options) {
const list = new SList(options);
for (const value of values) list.pushBack(value);
return list;
}
static fromPtrRange(ptrRange, options) {
const list = new SList(options);
if (!list.isCompatiblePtrRange(ptrRange)) throw new Error('"range" is not a compatible range');
if (ptrRange) append(list, list, ptrRange);
return list;
}
static fromExtList(extList) {
if (!(extList instanceof ExtListBase)) throw new Error('Not a circular list');
const list = new SList(extList);
if (extList.isEmpty) return list;
const range = extList.range;
if (range) {
append(list, list, range);
extList.clear();
}
return list;
}
}
SList.Ptr = Ptr;
addAliases(SList.prototype, {
popFrontNode: 'popFront, pop',
popBackNode: 'popBack',
pushFront: 'push',
appendBack: 'append',
getNodeIterator: 'getIterator'
});
export {Ptr};
export default SList;