Rename iterables.ts to structures.ts

This commit is contained in:
mindfulminun committed 2022-11-14 03:50:11 -06:00
1 parent b83668ef07
commit 2019573887
1 file changed
+44 -45
+44 -45
View File
@@ -1,3 +1,46 @@
/**
* Data structure allowing for the insertion and removal of
* elements in a LIFO manner in `O(1)` time.
* @author MindfulMinun
* @since 2022-10-22
*/
export class Stack<T> implements Iterable<T> {
#elements: Record<number, T>
#head: number
constructor(initials?: Iterable<T>) {
this.#elements = {}
this.#head = 0
if (!initials) return
for (const el of initials) this.push(el)
}
/** Add an element to the stack */
push(element: T): void {
this.#elements[this.#head++] = element
}
/** Remove an element from the stack and return it. */
pop(): T | undefined {
if (this.length === 0) return undefined
const el = this.#elements[--this.#head]
delete this.#elements[this.#head]
return el
}
peek(): T | undefined {
if (this.length === 0) return undefined
return this.#elements[this.#head - 1]
}
/** The number of elements remaining in the stack */
get length() { return this.#head }
*[Symbol.iterator]() {
while (this.length !== 0) yield this.pop()!
}
}
/**
* Data structure allowing for the insertion and removal of
* elements in a FIFO manner in `O(1)` time.
@@ -9,7 +52,7 @@ export class Queue<T> implements Iterable<T> {
#head: number
#tail: number
constructor(initials: Iterable<T>) {
constructor(initials?: Iterable<T>) {
this.#elements = {}
this.#head = 0
this.#tail = 0
@@ -42,47 +85,3 @@ export class Queue<T> implements Iterable<T> {
while (this.length !== 0) yield this.dequeue()!
}
}
/**
* Data structure allowing for the insertion and removal of
* elements in a LIFO manner in `O(1)` time.
* @author MindfulMinun
* @since 2022-10-22
*/
export class Stack<T> implements Iterable<T> {
#elements: Record<number, T>
#top: number
constructor(initials?: Iterable<T>) {
this.#elements = {}
this.#top = 0
if (!initials) return
for (const el of initials) this.push(el)
}
/** Add an element to the stack */
push(element: T): void {
this.#elements[this.#top++] = element
}
/** Remove an element from the stack and return it. */
pop(): T | undefined {
if (this.length === 0) return undefined
const el = this.#elements[--this.#top]
delete this.#elements[this.#top]
return el
}
peek(): T | undefined {
if (this.length === 0) return undefined
return this.#elements[this.#top - 1]
}
/** The number of elements remaining in the stack */
get length() { return this.#top }
*[Symbol.iterator]() {
while (this.length !== 0) yield this.pop()!
}
}