UNPKG

nowjs-core

Version:

NowCanDo Javascript Core [nowjs-core] is a library written by TypeScript code maintains under Apache 2.0 licence

253 lines (252 loc) 6.88 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const index_1 = require("../linq/index"); const index_2 = require("./index"); class LinkedList { constructor(enumerable) { this.length = 0; if (enumerable && enumerable[Symbol.iterator]) { for (const x of enumerable) { this.add(x); } } this.iterator = function* (cn) { let currentNode; if (!currentNode) { currentNode = cn.firstNode; } do { const tmp = currentNode; currentNode = tmp.Next; yield tmp.Current; } while (currentNode.Next); yield currentNode.Current; }; this.nodeIterator = function* (cn) { let currentNode; if (!currentNode) { currentNode = cn.firstNode; } do { const tmp = currentNode; currentNode = tmp.Next; yield tmp; } while (currentNode.Next); yield currentNode; }; } isEmpty() { return this.size === 0; } contains(item) { for (const xx of this) { if (item === xx) { return true; } } return false; } get size() { return this.length; } clear() { this.firstNode = null; this.lastNode = null; this.length = 0; return true; } insert(index, item) { return this.addInternal(item, index); } add(...arg1) { let res = false; if (Array.isArray(arg1)) { for (const item of arg1) { res = this.addInternal(item); } } return res; } addFirst(item) { return this.addInternal(item, 0); } addLast(item) { return this.addInternal(item); } get(index) { let ix = -1; for (const xx of this) { ix++; if (ix === index) return xx; } return null; } set(index, item) { const xx = this.getInterenal(index); if (xx) xx.Current = item; } getFirst() { return this.firstNode ? this.firstNode.Current : null; } getLast() { return this.lastNode ? this.lastNode.Current : null; } shift() { const item = this.getFirst(); this.removeFirst(); return item; } get first() { return this.getFirst(); } get last() { return this.getLast(); } indexOf(item) { let ix = -1; for (const xx of this.nodeIterator(this)) { ix++; if (xx.Current === item) return ix; } return -1; } lastIndexOf(item) { let ix = -1; let lastIndex = -1; for (const xx of this.nodeIterator(this)) { ix++; if (xx.Current === item) { lastIndex = ix; } } return lastIndex; } remove(item) { return this.removeInternal(item); } removeFirst() { return this.removeInternal(this.getFirst(), this.firstNode); } removeLast() { return this.removeInternal(this.getLast(), this.lastNode); } clone() { return new LinkedList(this); } toArray() { const array = []; for (const xx of this) { array.push(xx); } return array; } toCollection() { return new index_2.Collection(this); } toList() { return new index_2.List(this); } toSet() { return new Set(this); } linq() { return new index_1.Enumerable(this); } plinq() { return new index_1.ParallelEnumerable(this); } [Symbol.iterator]() { return this.iterator(this); } getInterenal(index) { let ix = -1; for (const xx of this.nodeIterator(this)) { ix++; if (ix === index) return xx; } return null; } getInterenalItem(item) { for (const xx of this.nodeIterator(this)) { if (xx.Current === item) return xx; } return null; } removeInternal(item, fitem) { const tmp = fitem ? fitem : this.getInterenalItem(item); if (tmp) { const tnext = tmp.Next; const tprev = tmp.Previous; if (tprev) { tprev.Next = tnext; } else { this.firstNode = tnext; if (this.firstNode && this.firstNode.Previous) { this.firstNode.Previous = null; } } if (tnext) { tnext.Previous = tprev; } else { this.lastNode = tprev; if (this.lastNode && this.lastNode.Next) { this.lastNode.Next = null; } } this.length--; return true; } else { return false; } } addInternal(item, index) { const dataItem = item; const dataIndex = index; if (!this.firstNode) { this.firstNode = { Current: dataItem }; this.lastNode = this.firstNode; } else { if (!dataIndex) { if (this.firstNode && !this.firstNode.Next) { const tmp = this.lastNode; this.lastNode = { Current: dataItem, Previous: tmp }; tmp.Next = this.lastNode; this.firstNode.Next = this.lastNode; this.firstNode.Previous = null; } else if (this.lastNode && !this.lastNode.Next) { const tmp = this.lastNode; this.lastNode = { Current: dataItem, Previous: tmp }; tmp.Next = this.lastNode; } } else { if (dataIndex === 0) { const tmp = this.firstNode; this.firstNode = { Current: dataItem, Next: tmp }; tmp.Previous = this.firstNode; } else if (dataIndex > 0) { const tmp = this.getInterenal(dataIndex); const tnext = tmp.Next; const tprev = tmp.Previous; const current = { Current: dataItem, Next: tnext, Previous: tprev }; tprev.Next = current; tnext.Previous = current; } } } this.length++; return true; } } exports.LinkedList = LinkedList;