From 1cdfe9193b36b952aba391cec03c5e4aefd5c8b4 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Mon, 12 Apr 2021 00:37:17 -0400 Subject: [PATCH] skiplist: Convert `_keyToNodeMap` to a `Map` object --- src/static/js/skiplist.js | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/static/js/skiplist.js b/src/static/js/skiplist.js index 105f5902c..4b0bc82d7 100644 --- a/src/static/js/skiplist.js +++ b/src/static/js/skiplist.js @@ -146,7 +146,7 @@ class Point { up.downSkips[lvl]++; up.downSkipWidths[lvl] += newWidth; } - this._skipList._keyToNodeMap[`$KEY$${newNode.key}`] = newNode; + this._skipList._keyToNodeMap.set(newNode.key, newNode); this._skipList._numNodes++; this._skipList._totalWidth += newWidth; } @@ -170,7 +170,7 @@ class Point { up.downSkipWidths[i] -= elemWidth; } } - delete this._skipList._keyToNodeMap[`$KEY$${elem.key}`]; + this._skipList._keyToNodeMap.delete(elem.key); this._skipList._numNodes--; this._skipList._totalWidth -= elemWidth; } @@ -191,7 +191,7 @@ class SkipList { this._end = new Node(null, 1, null, null); this._numNodes = 0; this._totalWidth = 0; - this._keyToNodeMap = {}; + this._keyToNodeMap = new Map(); this._start.downPtrs[0] = this._end; this._end.upPtrs[0] = this._start; } @@ -226,8 +226,6 @@ class SkipList { return dist; } - _getNodeByKey(key) { return this._keyToNodeMap[`$KEY$${key}`]; } - // Returns index of first entry such that entryFunc(entry) is truthy, // or length() if no such entry. Assumes all falsy entries come before // all truthy entries. @@ -280,8 +278,8 @@ class SkipList { } } - next(entry) { return this._getNodeByKey(entry.key).downPtrs[0].entry || null; } - prev(entry) { return this._getNodeByKey(entry.key).upPtrs[0].entry || null; } + next(entry) { return this._keyToNodeMap.get(entry.key).downPtrs[0].entry || null; } + prev(entry) { return this._keyToNodeMap.get(entry.key).upPtrs[0].entry || null; } push(entry) { this.splice(this._numNodes, 0, [entry]); } slice(start, end) { @@ -307,14 +305,14 @@ class SkipList { return array; } - atKey(key) { return this._getNodeByKey(key).entry; } - indexOfKey(key) { return this._getNodeIndex(this._getNodeByKey(key)); } + atKey(key) { return this._keyToNodeMap.get(key).entry; } + indexOfKey(key) { return this._getNodeIndex(this._keyToNodeMap.get(key)); } indexOfEntry(entry) { return this.indexOfKey(entry.key); } - containsKey(key) { return !!this._getNodeByKey(key); } + containsKey(key) { return this._keyToNodeMap.has(key); } // gets the last entry starting at or before the offset atOffset(offset) { return this._getNodeAtOffset(offset).entry; } keyAtOffset(offset) { return this.atOffset(offset).key; } - offsetOfKey(key) { return this._getNodeIndex(this._getNodeByKey(key), true); } + offsetOfKey(key) { return this._getNodeIndex(this._keyToNodeMap.get(key), true); } offsetOfEntry(entry) { return this.offsetOfKey(entry.key); } setEntryWidth(entry, width) { entry.width = width;