UNPKG

ng2-bootstrap

Version:
240 lines 7.64 kB
export var LinkedList = (function () { function LinkedList() { this.length = 0; this.asArray = []; } LinkedList.prototype.getNode = function (position) { if (this.length === 0 || position < 0 || position >= this.length) { throw new Error('Position is out of the list'); } var current = this.head; for (var index = 0; index < position; index++) { current = current.next; } return current; }; LinkedList.prototype.createInternalArrayRepresentation = function () { var outArray = []; var current = this.head; while (current) { outArray.push(current.value); current = current.next; } this.asArray = outArray; }; LinkedList.prototype.get = function (position) { if (this.length === 0 || position < 0 || position >= this.length) { return void 0; } var current = this.head; for (var index = 0; index < position; index++) { current = current.next; } return current.value; }; LinkedList.prototype.add = function (value, position) { if (position === void 0) { position = this.length; } if (position < 0 || position > this.length) { throw new Error('Position is out of the list'); } var node = { value: value, next: undefined, previous: undefined }; if (this.length === 0) { this.head = node; this.tail = node; this.current = node; } else { if (position === 0) { // first node node.next = this.head; this.head.previous = node; this.head = node; } else if (position === this.length) { // last node this.tail.next = node; node.previous = this.tail; this.tail = node; } else { // node in middle var currentPreviousNode = this.getNode(position - 1); var currentNextNode = currentPreviousNode.next; currentPreviousNode.next = node; currentNextNode.previous = node; node.previous = currentPreviousNode; node.next = currentNextNode; } } this.length++; this.createInternalArrayRepresentation(); }; LinkedList.prototype.remove = function (position) { if (position === void 0) { position = 0; } if (this.length === 0 || position < 0 || position >= this.length) { throw new Error('Position is out of the list'); } if (position === 0) { // first node this.head = this.head.next; if (this.head) { // there is no second node this.head.previous = undefined; } else { // there is no second node this.tail = undefined; } } else if (position === this.length - 1) { // last node this.tail = this.tail.previous; this.tail.next = undefined; } else { // middle node var removedNode = this.getNode(position); removedNode.next.previous = removedNode.previous; removedNode.previous.next = removedNode.next; } this.length--; this.createInternalArrayRepresentation(); }; LinkedList.prototype.set = function (position, value) { if (this.length === 0 || position < 0 || position >= this.length) { throw new Error('Position is out of the list'); } var node = this.getNode(position); node.value = value; this.createInternalArrayRepresentation(); }; LinkedList.prototype.toArray = function () { return this.asArray; }; LinkedList.prototype.findAll = function (fn) { var current = this.head; var result = []; for (var index = 0; index < this.length; index++) { if (fn(current.value, index)) { result.push({ index: index, value: current.value }); } current = current.next; } return result; }; // Array methods overriding start LinkedList.prototype.push = function () { var _this = this; var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i - 0] = arguments[_i]; } args.forEach(function (arg) { _this.add(arg); }); return this.length; }; LinkedList.prototype.pop = function () { if (this.length === 0) { return undefined; } var last = this.tail; this.remove(this.length - 1); return last.value; }; LinkedList.prototype.unshift = function () { var _this = this; var args = []; for (var _i = 0; _i < arguments.length; _i++) { args[_i - 0] = arguments[_i]; } args.reverse(); args.forEach(function (arg) { _this.add(arg, 0); }); return this.length; }; LinkedList.prototype.shift = function () { if (this.length === 0) { return undefined; } var lastItem = this.head.value; this.remove(); return lastItem; }; LinkedList.prototype.forEach = function (fn) { var current = this.head; for (var index = 0; index < this.length; index++) { fn(current.value, index); current = current.next; } }; LinkedList.prototype.indexOf = function (value) { var current = this.head; var position = 0; for (var index = 0; index < this.length; index++) { if (current.value === value) { position = index; break; } current = current.next; } return position; }; LinkedList.prototype.some = function (fn) { var current = this.head; var result = false; while (current && !result) { if (fn(current.value)) { result = true; break; } current = current.next; } return result; }; LinkedList.prototype.every = function (fn) { var current = this.head; var result = true; while (current && result) { if (!fn(current.value)) { result = false; } current = current.next; } return result; }; LinkedList.prototype.toString = function () { return '[Linked List]'; }; LinkedList.prototype.find = function (fn) { var current = this.head; var result; for (var index = 0; index < this.length; index++) { if (fn(current.value, index)) { result = current.value; break; } current = current.next; } return result; }; LinkedList.prototype.findIndex = function (fn) { var current = this.head; var result; for (var index = 0; index < this.length; index++) { if (fn(current.value, index)) { result = index; break; } current = current.next; } return result; }; return LinkedList; }()); //# sourceMappingURL=linked-list.class.js.map