-
-
Notifications
You must be signed in to change notification settings - Fork 13
/
deque.ts
197 lines (184 loc) · 4.07 KB
/
deque.ts
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
// Copyright Jerome Benoit. 2023-2024. All Rights Reserved.
/**
* Linked list node interface.
*
* @typeParam T - Type of linked list node data.
* @internal
*/
export interface ILinkedListNode<T> {
data: T
next?: ILinkedListNode<T>
prev?: ILinkedListNode<T>
}
/**
* Deque.
* Implemented with a doubly linked list.
*
* @typeParam T - Type of deque data.
* @internal
*/
export class Deque<T> {
private head?: ILinkedListNode<T>
private tail?: ILinkedListNode<T>
/** The size of the deque. */
public size!: number
/** The maximum size of the deque. */
public maxSize!: number
public constructor () {
this.clear()
}
/**
* Appends data to the deque.
*
* @param data - Data to append.
* @returns The new size of the deque.
*/
public push (data: T): number {
const node: ILinkedListNode<T> = { data }
if (this.tail == null) {
this.head = this.tail = node
} else {
node.prev = this.tail
this.tail = this.tail.next = node
}
return this.incrementSize()
}
/**
* Prepends data to the deque.
*
* @param data - Data to prepend.
* @returns The new size of the deque.
*/
public unshift (data: T): number {
const node: ILinkedListNode<T> = { data }
if (this.head == null) {
this.head = this.tail = node
} else {
node.next = this.head
this.head = this.head.prev = node
}
return this.incrementSize()
}
/**
* Pops data from the deque.
*
* @returns The popped data or `undefined` if the deque is empty.
*/
public pop (): T | undefined {
if (this.head == null) {
return
}
const tail = this.tail
this.tail = this.tail?.prev
if (this.tail == null) {
delete this.head
} else {
delete this.tail.next
}
--this.size
return tail?.data
}
/**
* Shifts data from the deque.
*
* @returns The shifted data or `undefined` if the deque is empty.
*/
public shift (): T | undefined {
if (this.head == null) {
return
}
const head = this.head
this.head = this.head.next
if (this.head == null) {
delete this.tail
} else {
delete this.head.prev
}
--this.size
return head.data
}
/**
* Peeks at the first data.
* @returns The first data or `undefined` if the deque is empty.
*/
public peekFirst (): T | undefined {
return this.head?.data
}
/**
* Peeks at the last data.
* @returns The last data or `undefined` if the deque is empty.
*/
public peekLast (): T | undefined {
return this.tail?.data
}
/**
* Clears the deque.
*/
public clear (): void {
delete this.head
delete this.tail
this.size = 0
this.maxSize = 0
}
/**
* Returns an iterator for the deque.
*
* @returns An iterator for the deque.
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
*/
[Symbol.iterator] (): Iterator<T> {
let node = this.head
return {
next: () => {
if (node == null) {
return {
value: undefined,
done: true
}
}
const ret = {
value: node.data,
done: false
}
node = node.next
return ret
}
}
}
/**
* Returns an backward iterator for the deque.
*
* @returns An backward iterator for the deque.
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols
*/
backward (): Iterable<T> {
return {
[Symbol.iterator]: (): Iterator<T> => {
let node = this.tail
return {
next: () => {
if (node == null) {
return {
value: undefined,
done: true
}
}
const ret = {
value: node.data,
done: false
}
node = node.prev
return ret
}
}
}
}
}
private incrementSize (): number {
++this.size
if (this.size > this.maxSize) {
this.maxSize = this.size
}
return this.size
}
}