@evolplus/evo-utils
Version:
Simple utilities solution.
109 lines (108 loc) • 3.47 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Dequeue = void 0;
/**
* Class representing a Double Ended Queue (Deque).
* Allows insertion and removal of elements from both ends of the queue.
*
* @template T The type of elements that the deque will store.
*/
class Dequeue {
/**
* Creates an instance of the Deque class with a specified capacity.
* Initializes the deque with an empty list and sets head and tail pointers.
*
* @param {number} capacity - The initial capacity of the deque.
*/
constructor(capacity) {
if (capacity <= 0)
throw new Error('Capacity must be greater than zero.');
this.capacity = capacity;
this.list = new Array(capacity);
this.head = 0;
this.tail = 0;
}
/**
* Checks if the deque is full.
*
* @returns {boolean} True if the deque has reached its capacity, otherwise false.
*/
isEmpty() {
return this.head == this.tail;
}
/**
* Appends an element at the end of the deque.
*
* @param {T} item - The element to be appended.
* @returns {boolean} Returns true if successfully, otherwise (the deque is full) it will returns false.
*/
add(value) {
let next = (this.tail + 1) % this.capacity;
if (next == this.head) {
// queue is already full, not add new element and return false
return false;
}
this.list[this.tail] = value;
this.tail = next;
return true;
}
/**
* Returns the element at the front of the deque without removing it.
*
* @returns {T | undefined} The element at the front of the deque, or undefined if the deque is empty.
*/
peekFirst() {
if (this.tail != this.head) {
return this.list[this.head];
}
}
/**
* Returns the element at the end of the deque without removing it.
*
* @returns {T | undefined} The element at the front of the deque, or undefined if the deque is empty.
*/
peekLast() {
if (this.tail != this.head) {
return this.list[(this.tail + this.capacity - 1) % this.capacity];
}
}
/**
* Removes and returns the element at the front of the deque.
*
* @returns {T | undefined} The element at the front of the deque, or undefined if the deque is empty.
*/
shift() {
if (this.tail != this.head) {
let val = this.list[this.head];
this.head = (this.head + 1) % this.capacity;
return val;
}
}
/**
* Removes and returns the element at the end of the deque.
*
* @returns {T | undefined} The element at the end of the deque, or undefined if the deque is empty.
*/
pop() {
if (this.tail != this.head) {
let last = (this.tail + this.capacity - 1) % this.capacity;
this.tail = last;
return this.list[last];
}
}
/**
* Returns the current number of elements in the deque.
*
* @returns {number} The number of elements currently stored in the deque.
*/
count() {
return (this.tail + this.capacity - this.head) % this.capacity;
}
/**
* Remove all elements of the current deque.
*/
clear() {
this.head = this.tail = 0; // Just do it simple by resetting head and tail
}
}
exports.Dequeue = Dequeue;