|
| 1 | +import assert from 'assert'; |
| 2 | +import Node from './Node'; |
| 3 | +import keys from './keys'; |
| 4 | +import iter from './iter'; |
| 5 | +import downMost from './downMost'; |
| 6 | +import searchTopMost from './searchTopMost'; |
| 7 | +import bottomMostPredecessor from './bottomMostPredecessor'; |
| 8 | +import deleteFromTopMost from './deleteFromTopMost'; |
| 9 | +import insertFromBottomMostPredecessor from './insertFromBottomMostPredecessor'; |
| 10 | +import makeQuasiRandom from './makeQuasiRandom'; |
| 11 | +import makeBottomLevel from './makeBottomLevel'; |
| 12 | + |
| 13 | +/** |
| 14 | + * @param {Number} p Promotion probability in (0,1). |
| 15 | + * @param {Function} compare |
| 16 | + */ |
| 17 | +export default function SkipList(p, compare, head = new Node()) { |
| 18 | + this.head = head; // Sentinel node |
| 19 | + this.p = p; |
| 20 | + this.compare = compare; |
| 21 | +} |
| 22 | + |
| 23 | +SkipList.prototype.add = function (key) { |
| 24 | + const node = bottomMostPredecessor(this.compare, this.head, key); |
| 25 | + const pred = insertFromBottomMostPredecessor(this.p, node, key); |
| 26 | + if (pred.left === null && pred.up === null) this.head = pred; |
| 27 | +}; |
| 28 | + |
| 29 | +SkipList.prototype.get = function (key) { |
| 30 | + const node = searchTopMost(this.compare, this.head, key); |
| 31 | + return node === null ? null : node.key; |
| 32 | +}; |
| 33 | + |
| 34 | +SkipList.prototype.has = function (key) { |
| 35 | + return searchTopMost(this.compare, this.head, key) !== null; |
| 36 | +}; |
| 37 | + |
| 38 | +SkipList.prototype.remove = function (key) { |
| 39 | + const node = searchTopMost(this.compare, this.head, key); |
| 40 | + if (node === null) return false; |
| 41 | + |
| 42 | + deleteFromTopMost(node); |
| 43 | + return true; |
| 44 | +}; |
| 45 | + |
| 46 | +SkipList.prototype._predecessor = function (key) { |
| 47 | + return bottomMostPredecessor(this.compare, this.head, key); |
| 48 | +}; |
| 49 | + |
| 50 | +SkipList.prototype.levelOne = function () { |
| 51 | + return downMost(this.head); |
| 52 | +}; |
| 53 | + |
| 54 | +SkipList.prototype.levels = function () { |
| 55 | + assert(this.head !== null); |
| 56 | + let k = 0; |
| 57 | + let current = this.head; |
| 58 | + do { |
| 59 | + ++k; |
| 60 | + current = current.down; |
| 61 | + } while (current !== null); |
| 62 | + |
| 63 | + return k; |
| 64 | +}; |
| 65 | + |
| 66 | +SkipList.prototype.keys = function () { |
| 67 | + const level = this.levelOne(); |
| 68 | + return keys(level); |
| 69 | +}; |
| 70 | + |
| 71 | +SkipList.prototype.values = function () { |
| 72 | + return this.keys(); |
| 73 | +}; |
| 74 | + |
| 75 | +SkipList.prototype[Symbol.iterator] = function () { |
| 76 | + return this.keys(); |
| 77 | +}; |
| 78 | + |
| 79 | +SkipList.prototype.range = function* (left, right) { |
| 80 | + const pred = this._predecessor(left); |
| 81 | + for (const node of iter(pred)) { |
| 82 | + if (this.compare(node.key, right) >= 0) break; |
| 83 | + yield node.key; |
| 84 | + } |
| 85 | +}; |
| 86 | + |
| 87 | +SkipList.from = (compare, iterable, p = 1 / 2) => { |
| 88 | + if (p === 1 / 2) { |
| 89 | + const bottomLevelHead = makeBottomLevel(compare, iterable); |
| 90 | + const head = makeQuasiRandom(p, bottomLevelHead); |
| 91 | + return new SkipList(p, compare, head); |
| 92 | + } |
| 93 | + |
| 94 | + const list = new SkipList(p, compare); |
| 95 | + for (const key of iterable) list.add(key); |
| 96 | + return list; |
| 97 | +}; |
0 commit comments