UNPKG

algs-adt

Version:

An npm package for using data structures like queues or graphs in javascript or typescript

112 lines (111 loc) 4.11 kB
"use strict"; var __extends = (this && this.__extends) || (function () { var extendStatics = function (d, b) { extendStatics = Object.setPrototypeOf || ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) || function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; }; return extendStatics(d, b); }; return function (d, b) { if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null"); extendStatics(d, b); function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.ArrayList = void 0; var List_1 = require("../ADT/List"); var ArrayList = /** @class */ (function (_super) { __extends(ArrayList, _super); function ArrayList(compareFunction) { var _this = _super.call(this, compareFunction) || this; _this.elements = []; return _this; } ArrayList.prototype.firstElement = function () { if (this.isEmpty()) { throw new Error("Array indices out of range"); } return this.elements[0]; }; ArrayList.prototype.lastElement = function () { if (this.isEmpty()) { throw new Error("Array indices out of range"); } return this.elements[this.size() - 1]; }; ArrayList.prototype.getElement = function (position) { if (position > this.size() || position < 0) { throw new Error("Array indices out of range"); } return this.elements[position]; }; ArrayList.prototype.addFirst = function (element) { this.elements.unshift(element); }; ArrayList.prototype.addLast = function (element) { this.elements.push(element); }; ArrayList.prototype.addElement = function (element, position) { if (position > this.size() || position < 0) { throw new Error("Array indices out of range"); } else if (position === 0) { this.addFirst(element); } else if (position === this.size()) { this.addLast(element); } else { this.elements.splice(position, 0, element); } }; ArrayList.prototype.deleteFirst = function () { if (this.isEmpty()) { throw new Error("Array indices out of range"); } else { return this.elements.shift(); } }; ArrayList.prototype.deleteLast = function () { if (this.isEmpty()) { throw new Error("Array indices out of range"); } else { return this.elements.pop(); } }; ArrayList.prototype.deleteElement = function (position) { var element = this.elements[position]; this.elements.splice(position, 1); return element; }; ArrayList.prototype.changeInfo = function (element, position) { if (position >= this.size() || position < 0) { throw new Error("Array indices out of range"); } this.elements[position] = element; return element; }; ArrayList.prototype.forEach = function (callback) { this.elements.forEach(function (e) { return callback(e); }); }; ArrayList.prototype.isEmpty = function () { return this.size() === 0; }; ArrayList.prototype.size = function () { return this.elements.length; }; ArrayList.prototype.subList = function (position, numberElements) { var sublist = new ArrayList(this.compareFunction); for (var i = position; sublist.size() < numberElements; i++) { sublist.addLast(this.getElement(i)); } return sublist; }; return ArrayList; }(List_1.List)); exports.ArrayList = ArrayList;