-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
list.js
201 lines (186 loc) · 3.39 KB
/
list.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
import { id } from './function.js'
import * as error from './error.js'
export class ListNode {
constructor () {
/**
* @type {this|null}
*/
this.next = null
/**
* @type {this|null}
*/
this.prev = null
}
}
/**
* @template {ListNode} N
*/
export class List {
constructor () {
/**
* @type {N | null}
*/
this.start = null
/**
* @type {N | null}
*/
this.end = null
this.len = 0
}
}
/**
* @note The queue implementation is experimental and unfinished.
* Don't use this in production yet.
*
* @template {ListNode} N
*
* @return {List<N>}
*/
export const create = () => new List()
/**
* @template {ListNode} N
*
* @param {List<N>} queue
*/
export const isEmpty = queue => queue.start === null
/**
* Remove a single node from the queue. Only works with Queues that operate on Doubly-linked lists of nodes.
*
* @template {ListNode} N
*
* @param {List<N>} queue
* @param {N} node
*/
export const remove = (queue, node) => {
const prev = node.prev
const next = node.next
if (prev) {
prev.next = next
} else {
queue.start = next
}
if (next) {
next.prev = prev
} else {
queue.end = prev
}
queue.len--
return node
}
/**
* @deprecated @todo remove in next major release
*/
export const removeNode = remove
/**
* @template {ListNode} N
*
* @param {List<N>} queue
* @param {N| null} left
* @param {N| null} right
* @param {N} node
*/
export const insertBetween = (queue, left, right, node) => {
/* c8 ignore start */
if (left != null && left.next !== right) {
throw error.unexpectedCase()
}
/* c8 ignore stop */
if (left) {
left.next = node
} else {
queue.start = node
}
if (right) {
right.prev = node
} else {
queue.end = node
}
node.prev = left
node.next = right
queue.len++
}
/**
* Remove a single node from the queue. Only works with Queues that operate on Doubly-linked lists of nodes.
*
* @template {ListNode} N
*
* @param {List<N>} queue
* @param {N} node
* @param {N} newNode
*/
export const replace = (queue, node, newNode) => {
insertBetween(queue, node, node.next, newNode)
remove(queue, node)
}
/**
* @template {ListNode} N
*
* @param {List<N>} queue
* @param {N} n
*/
export const pushEnd = (queue, n) =>
insertBetween(queue, queue.end, null, n)
/**
* @template {ListNode} N
*
* @param {List<N>} queue
* @param {N} n
*/
export const pushFront = (queue, n) =>
insertBetween(queue, null, queue.start, n)
/**
* @template {ListNode} N
*
* @param {List<N>} list
* @return {N| null}
*/
export const popFront = list =>
list.start ? removeNode(list, list.start) : null
/**
* @template {ListNode} N
*
* @param {List<N>} list
* @return {N| null}
*/
export const popEnd = list =>
list.end ? removeNode(list, list.end) : null
/**
* @template {ListNode} N
* @template M
*
* @param {List<N>} list
* @param {function(N):M} f
* @return {Array<M>}
*/
export const map = (list, f) => {
/**
* @type {Array<M>}
*/
const arr = []
let n = list.start
while (n) {
arr.push(f(n))
n = n.next
}
return arr
}
/**
* @template {ListNode} N
*
* @param {List<N>} list
*/
export const toArray = list => map(list, id)
/**
* @template {ListNode} N
* @template M
*
* @param {List<N>} list
* @param {function(N):M} f
*/
export const forEach = (list, f) => {
let n = list.start
while (n) {
f(n)
n = n.next
}
}