-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstack.js
More file actions
56 lines (52 loc) · 1.34 KB
/
Copy pathstack.js
File metadata and controls
56 lines (52 loc) · 1.34 KB
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
// @ts-self-types="./stack.d.ts"
import ValueList from './value-list.js';
import {addAliases} from './meta-utils.js';
import {pushValuesFront} from './list-utils.js';
export class Stack {
constructor(underlyingList = ValueList) {
// accepts a list instance or a list class
this.list = typeof underlyingList == 'function' ? new underlyingList() : underlyingList;
this.size = this.list.getLength();
}
get isEmpty() {
return this.list.isEmpty;
}
get top() {
return this.list.isEmpty ? undefined : this.list.front.value;
}
peek() {
return this.list.isEmpty ? undefined : this.list.front.value;
}
push(value) {
this.list.pushFront(value);
++this.size;
return this;
}
pop() {
if (!this.list.isEmpty) {
--this.size;
return this.list.popFront();
}
// return undefined;
}
pushValues(values) {
pushValuesFront(this, values);
return this;
}
clear() {
this.list.clear();
this.size = 0;
return this;
}
[Symbol.iterator]() {
return this.list[Symbol.iterator]();
}
getReverseIterator() {
return this.list.getReverseIterator?.();
}
static from(values, underlyingList) {
return new Stack(underlyingList).pushValues(values);
}
}
addAliases(Stack.prototype, {push: 'pushFront, add', pop: 'remove', pushValues: 'addValues'});
export default Stack;