repositories
/
poolifier.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Merge branch 'master' into combined-prs-branch
[poolifier.git]
/
src
/
deque.ts
diff --git
a/src/deque.ts
b/src/deque.ts
index 4a763fc7c4b417ed56d5d5f65f6e96cbfa2503cd..52441a3ec25f4f7251575104a0884f9ffeb22d00 100644
(file)
--- a/
src/deque.ts
+++ b/
src/deque.ts
@@
-1,24
+1,27
@@
-// Copyright Jerome Benoit. 2023. All Rights Reserved.
+// Copyright Jerome Benoit. 2023
-2024
. All Rights Reserved.
-class Node<T> {
- public value: T
- public next?: Node<T>
- public prev?: Node<T>
-
- public constructor (value: T) {
- this.value = value
- }
+/**
+ * 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.
*
}
/**
* Deque.
* Implemented with a doubly linked list.
*
- * @typeParam T - Type of deque values.
+ * @typeParam T - Type of deque data.
+ * @internal
*/
export class Deque<T> {
*/
export class Deque<T> {
- private head?: Node<T>
- private tail?: Node<T>
+ private head?:
ILinkedList
Node<T>
+ private tail?:
ILinkedList
Node<T>
/** The size of the deque. */
public size!: number
/** The maximum size of the deque. */
/** The size of the deque. */
public size!: number
/** The maximum size of the deque. */
@@
-29,13
+32,13
@@
export class Deque<T> {
}
/**
}
/**
- * Appends
a value
to the deque.
+ * Appends
data
to the deque.
*
*
- * @param
value - Value
to append.
- * @returns The new size of the
que
ue.
+ * @param
data - Data
to append.
+ * @returns The new size of the
deq
ue.
*/
*/
- public push (
value
: T): number {
- const node
= new Node(value)
+ public push (
data
: T): number {
+ const node
: ILinkedListNode<T> = { data }
if (this.tail == null) {
this.head = this.tail = node
} else {
if (this.tail == null) {
this.head = this.tail = node
} else {
@@
-46,13
+49,13
@@
export class Deque<T> {
}
/**
}
/**
- * Prepends
a value
to the deque.
+ * Prepends
data
to the deque.
*
*
- * @param
value - Value
to prepend.
- * @returns The new size of the
que
ue.
+ * @param
data - Data
to prepend.
+ * @returns The new size of the
deq
ue.
*/
*/
- public unshift (
value
: T): number {
- const node
= new Node(value)
+ public unshift (
data
: T): number {
+ const node
: ILinkedListNode<T> = { data }
if (this.head == null) {
this.head = this.tail = node
} else {
if (this.head == null) {
this.head = this.tail = node
} else {
@@
-63,67
+66,67
@@
export class Deque<T> {
}
/**
}
/**
- * Pops
a value
from the deque.
+ * Pops
data
from the deque.
*
*
- * @returns The popped
value
or `undefined` if the deque is empty.
+ * @returns The popped
data
or `undefined` if the deque is empty.
*/
public pop (): T | undefined {
if (this.head == null) {
*/
public pop (): T | undefined {
if (this.head == null) {
- return
undefined
+ return
}
const tail = this.tail
}
const tail = this.tail
- this.tail =
(this.tail as Node<T>)
.prev
+ this.tail =
this.tail?
.prev
if (this.tail == null) {
if (this.tail == null) {
-
this.head = undefine
d
+
delete this.hea
d
} else {
} else {
- this.tail.next = undefined
+ delete this.tail.next
}
--this.size
}
--this.size
- return tail?.
value
+ return tail?.
data
}
/**
}
/**
- * Shifts
a value
from the deque.
+ * Shifts
data
from the deque.
*
*
- * @returns The shifted
value
or `undefined` if the deque is empty.
+ * @returns The shifted
data
or `undefined` if the deque is empty.
*/
public shift (): T | undefined {
if (this.head == null) {
*/
public shift (): T | undefined {
if (this.head == null) {
- return
undefined
+ return
}
const head = this.head
this.head = this.head.next
if (this.head == null) {
}
const head = this.head
this.head = this.head.next
if (this.head == null) {
- this.tail = undefined
+ delete this.tail
} else {
} else {
- this.head.prev = undefined
+ delete this.head.prev
}
--this.size
}
--this.size
- return head
?.value
+ return head
.data
}
/**
}
/**
- * Peeks at the first
value
.
- * @returns The first
value
or `undefined` if the deque is empty.
+ * Peeks at the first
data
.
+ * @returns The first
data
or `undefined` if the deque is empty.
*/
public peekFirst (): T | undefined {
*/
public peekFirst (): T | undefined {
- return this.head?.
value
+ return this.head?.
data
}
/**
}
/**
- * Peeks at the last
value
.
- * @returns The last
value
or `undefined` if the deque is empty.
+ * Peeks at the last
data
.
+ * @returns The last
data
or `undefined` if the deque is empty.
*/
public peekLast (): T | undefined {
*/
public peekLast (): T | undefined {
- return this.tail?.
value
+ return this.tail?.
data
}
/**
* Clears the deque.
*/
public clear (): void {
}
/**
* Clears the deque.
*/
public clear (): void {
-
this.head = undefine
d
- this.tail = undefined
+
delete this.hea
d
+ delete this.tail
this.size = 0
this.maxSize = 0
}
this.size = 0
this.maxSize = 0
}
@@
-145,10
+148,10
@@
export class Deque<T> {
}
}
const ret = {
}
}
const ret = {
- value: node.
value
,
+ value: node.
data
,
done: false
}
done: false
}
- node = node.next
as Node<T>
+ node = node.next
return ret
}
}
return ret
}
}
@@
-173,10
+176,10
@@
export class Deque<T> {
}
}
const ret = {
}
}
const ret = {
- value: node.
value
,
+ value: node.
data
,
done: false
}
done: false
}
- node = node.prev
as Node<T>
+ node = node.prev
return ret
}
}
return ret
}
}