typescript-algorithms-and-datastructures
Version:
Useful algorithms and Data structures written in typescript.
57 lines • 1.76 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 TypedStack {
constructor(TypedArray, size) {
this.top = -1;
this.max = -1;
this.stack = new TypedArray(size);
this.max = size;
}
clear() {
this.top = -1;
}
isEmpty() {
return this.top === -1;
}
search(element) {
const index = this.stack.lastIndexOf(element);
return (index < 0) ? -1 : this.top - index;
}
peek() {
if (this.isEmpty()) {
throw new Error('The stack is empty');
}
return this.stack[this.top];
}
pop() {
if (this.top === -1) {
throw new RangeError('The stack is empty.');
}
var index = this.top;
this.top--;
return this.stack[index];
}
push(element) {
if (this.top >= this.max) {
throw new RangeError('You exceeded the buffer size.');
}
this.top++;
this.stack[this.top] = element;
return this;
}
size() {
return this.top + 1;
}
}
exports.TypedStack = TypedStack;
});
//# sourceMappingURL=TypedStack.js.map