typescript-algorithms-and-datastructures
Version:
Useful algorithms and Data structures written in typescript.
56 lines • 1.85 kB
JavaScript
(function (factory) {
if (typeof module === "object" && typeof module.exports === "object") {
var v = factory(require, exports);
if (v !== undefined) module.exports = v;
}
else if (typeof define === "function" && define.amd) {
define(["require", "exports"], factory);
}
})(function (require, exports) {
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
class TypedQueue {
constructor(queue) {
this.head = 0;
this.tail = 0;
this.memoryLength = 0;
this.queue = queue;
this.memoryLength = queue.length;
}
enqueue(element) {
if (this.size() === this.memoryLength - 1) {
throw new RangeError('The queue is full');
}
this.queue[this.tail] = element;
this.tail++;
if (this.tail === this.memoryLength) {
this.tail = 0;
}
}
dequeue() {
if (this.isEmpty()) {
throw new Error('The queue is empty');
}
var element = this.queue[this.head];
this.head++;
if (this.head === this.memoryLength) {
this.head = 0;
}
return element;
}
isEmpty() {
return this.tail === this.head;
}
peek() {
if (this.isEmpty()) {
throw new Error('The stack is empty');
}
return this.queue[this.head];
}
size() {
return (this.tail < this.head) ? this.tail + this.memoryLength - this.head : this.tail - this.head;
}
}
exports.TypedQueue = TypedQueue;
});
//# sourceMappingURL=TypedQueue.js.map