forked from nim-ka/aocutil
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdll.js
172 lines (139 loc) · 3.44 KB
/
dll.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
// TODO:
// - copy method
// - remove range method
DLLNode = class DLLNode {
constructor(val, skip = this, prev = this, next = this) {
this.val = val
this.skip = skip
this.prev = prev
this.next = next
}
adv(n = 1) {
let node = this == this.skip ? this.next : this
if (n < 0) {
while (n++) {
node = node.prev
if (node == this.skip) node = node.prev
}
} else {
while (n--) {
node = node.next
if (node == this.skip) node = node.next
}
}
return node
}
}
DLL = class DLL {
constructor(a = []) {
this.h = new DLLNode()
this.length = 0;
a.forEach((e, i, a) => this.insEnd(e))
}
insNodeAheadNode(old, node) {
node.next = old.next
old.next.prev = node
old.next = node
node.prev = old
return ++this.length
}
insDLLAheadNode(old, dll) {
let start = dll.getNode(0)
let end = dll.getNode(-1)
end.next = old.next
old.next.prev = end
old.next = start
start.prev = old
return this.length += dll.length
}
insAheadNode(old, val) {
let node = new DLLNode(val, this.h, old, old.next)
old.next = node
old.next.next.prev = node
return ++this.length
}
insStart(val) { return this.insAheadNode(this.h, val) }
unshift(val) { return this.insStart(val) }
insNodeBehindNode(old, node) {
node.prev = old.prev
old.prev.next = node
old.prev = node
node.next = old
return ++this.length
}
insDLLBehindNode(old, dll) {
let start = dll.getNode(0)
let end = dll.getNode(-1)
start.prev = old.prev
old.prev.next = start
old.prev = end
end.next = old
return this.length += dll.length
}
insBehindNode(old, val) {
let node = new DLLNode(val, this.h, old.prev, old)
old.prev = node
old.prev.prev.next = node
return ++this.length
}
insEnd(val) { return this.insBehindNode(this.h, val) }
push(...vals) {
let ret
vals.forEach((val) => ret = this.insEnd(val))
return ret
}
insNode(idx, node) { return this.insNodeBehindNode(this.getNode(idx), node) }
insDLL(idx, dll) { return this.insDLLBehindNode(this.getNode(idx), dll) }
ins(idx, val) { return this.insBehindNode(this.getNode(idx), val) }
insNodeAhead(idx, node) { return this.insNodeAheadNode(this.getNode(idx), node) }
insDLLAhead(idx, dll) { return this.insDLLAheadNode(this.getNode(idx), dll) }
insAhead(idx, val) { return this.insAheadNode(this.getNode(idx), val) }
removeNode(node) {
let tmp = node.next
node.prev.next = node.next
tmp.prev = node.prev
this.length--
return node
}
remove(idx) { return this.removeNode(this.getNode(idx)).val }
getNode(idx) { return this.h.adv(idx) }
get(idx) { return this.getNode(idx).val }
reverse() {
let node = this.h
do {
let next = node.next
let tmp = node.prev
node.prev = node.next
node.next = tmp
node = next
} while (node != this.h)
return this
}
rotate(rot) {
let tmp = this.h.next
this.removeNode(this.h)
this.insBehindNodeNode(tmp.adv(-rot), this.h)
}
forEachNode(f) {
let i = 0, node = this.h.next;
while (node != this.h) {
f(node, i++)
node = node.next
}
return this
}
forEach(f) { return this.forEachNode((node, idx) => f(node.val, idx)) }
mapMut(f) { return this.forEachNode((node, idx) => node.val = f(node.val, idx)) }
includes(val) {
let res = false
this.forEach((e) => res |= e == val)
return res
}
toArray() {
let arr = new Array(this.length)
this.forEach((el, idx) => arr[idx] = el)
return arr
}
toJSON() { return this.toArray() }
toString() { return this.toArray().toString() }
}