UNPKG

1.77 kBJavaScriptView Raw
1"use strict";
2
3var DLList;
4DLList = class DLList {
5 constructor(incr, decr) {
6 this.incr = incr;
7 this.decr = decr;
8 this._first = null;
9 this._last = null;
10 this.length = 0;
11 }
12
13 push(value) {
14 var node;
15 this.length++;
16
17 if (typeof this.incr === "function") {
18 this.incr();
19 }
20
21 node = {
22 value,
23 prev: this._last,
24 next: null
25 };
26
27 if (this._last != null) {
28 this._last.next = node;
29 this._last = node;
30 } else {
31 this._first = this._last = node;
32 }
33
34 return void 0;
35 }
36
37 shift() {
38 var value;
39
40 if (this._first == null) {
41 return;
42 } else {
43 this.length--;
44
45 if (typeof this.decr === "function") {
46 this.decr();
47 }
48 }
49
50 value = this._first.value;
51
52 if ((this._first = this._first.next) != null) {
53 this._first.prev = null;
54 } else {
55 this._last = null;
56 }
57
58 return value;
59 }
60
61 first() {
62 if (this._first != null) {
63 return this._first.value;
64 }
65 }
66
67 getArray() {
68 var node, ref, results;
69 node = this._first;
70 results = [];
71
72 while (node != null) {
73 results.push((ref = node, node = node.next, ref.value));
74 }
75
76 return results;
77 }
78
79 forEachShift(cb) {
80 var node;
81 node = this.shift();
82
83 while (node != null) {
84 cb(node), node = this.shift();
85 }
86
87 return void 0;
88 }
89
90 debug() {
91 var node, ref, ref1, ref2, results;
92 node = this._first;
93 results = [];
94
95 while (node != null) {
96 results.push((ref = node, node = node.next, {
97 value: ref.value,
98 prev: (ref1 = ref.prev) != null ? ref1.value : void 0,
99 next: (ref2 = ref.next) != null ? ref2.value : void 0
100 }));
101 }
102
103 return results;
104 }
105
106};
107module.exports = DLList;
\No newline at end of file