UNPKG

list-runner

Version:

a lightweight linked-list implementation that offers both Singly (next) and Doubly data structures (next and previous)

152 lines (123 loc) 4.84 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.StemDoubly = void 0; var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck")); var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass")); var _possibleConstructorReturn2 = _interopRequireDefault(require("@babel/runtime/helpers/possibleConstructorReturn")); var _getPrototypeOf2 = _interopRequireDefault(require("@babel/runtime/helpers/getPrototypeOf")); var _get2 = _interopRequireDefault(require("@babel/runtime/helpers/get")); var _inherits2 = _interopRequireDefault(require("@babel/runtime/helpers/inherits")); var _StemSingly2 = require("./StemSingly"); var _SentinelDoubly = require("./SentinelDoubly"); var StemDoubly = /*#__PURE__*/ function (_StemSingly) { (0, _inherits2["default"])(StemDoubly, _StemSingly); function StemDoubly(head) { var _this; (0, _classCallCheck2["default"])(this, StemDoubly); _this = (0, _possibleConstructorReturn2["default"])(this, (0, _getPrototypeOf2["default"])(StemDoubly).call(this, head, _SentinelDoubly.SentinelDoubly)); head.setNext(_this.sentinelTail); head.setPrev(_this.sentinelHead); _this.sentinelTail.setPrev(head); return _this; } (0, _createClass2["default"])(StemDoubly, [{ key: "getTail", value: function getTail() { return this.sentinelTail.getPrev(); } }, { key: "interlink", value: function interlink(cells) { var looper; for (looper = 0; looper < cells.length - 1; looper++) { cells[looper].setNext(cells[looper + 1]); } for (looper = 1; looper < cells.length; looper++) { cells[looper].setPrev(cells[looper - 1]); } var lastCellIndex = cells.length - 1; this.sentinelTail.setPrev(cells[lastCellIndex]); cells[lastCellIndex].setNext(this.sentinelTail); } // interlinks the chain of prev pointers for the provided cells }, { key: "interlinkPrev", value: function interlinkPrev(cell1, cell2, cell3) { cell3.setPrev(cell2); cell2.setPrev(cell1); } // cell2 (the cell between cell1 and cell3) gets dereferenced (ie. cell2 is deleted from the stem) }, { key: "unlinkPrev", value: function unlinkPrev(cell1, cell3) { cell3.setPrev(cell1); } }, { key: "insert", value: function insert(cell, baseline) { // NOTE: if the baseline cell is this.sentinelTail then we are at the end of the stem and cannot insert the new cell if (!(0, _get2["default"])((0, _getPrototypeOf2["default"])(StemDoubly.prototype), "insert", this).call(this, cell, baseline)) { return false; } this.interlinkPrev(baseline, baseline.getNext(), baseline.getNext().getNext()); return true; } }, { key: "extract", value: function extract(baseline) { var extractionResult = (0, _get2["default"])((0, _getPrototypeOf2["default"])(StemDoubly.prototype), "extract", this).call(this, baseline); if (!extractionResult) { return false; } // what was previously getNext().getNext() is now getNext() due to CellSingly unlinkNext this.unlinkPrev(baseline, baseline.getNext()); return extractionResult; } }, { key: "push", value: function push(cell) { return this.insert(cell, this.sentinelTail.getPrev()); } }, { key: "pop", value: function pop() { return this.extract(this.sentinelTail.getPrev().getPrev()); } }, { key: "unshift", value: function unshift(cell) { return this.insert(cell, this.sentinelHead); } }, { key: "shift", value: function shift() { return this.extract(this.sentinelHead); } }, { key: "replace", value: function replace(cell, baseline) { // NOTE: if baseline is this.sentinelTail or the previous cell then we are at the end of the stem and cannot perform the replacement if (!(0, _get2["default"])((0, _getPrototypeOf2["default"])(StemDoubly.prototype), "replace", this).call(this, cell, baseline)) { return false; } this.interlinkPrev(baseline, baseline.getNext(), baseline.getNext().getNext()); return true; } }, { key: "delete", value: function _delete(baseline) { if (baseline === this.sentinelHead || baseline === this.sentinelTail) { return false; } var newSentinel = new _SentinelDoubly.SentinelDoubly(); baseline.setNext(newSentinel); newSentinel.setPrev(baseline); this.sentinelTail = newSentinel; return true; } }]); return StemDoubly; }(_StemSingly2.StemSingly); // eslint-disable-line import/prefer-default-export exports.StemDoubly = StemDoubly;