lint: skiplist
This commit is contained in:
parent
f72ce463ef
commit
29179e512f
1 changed files with 65 additions and 112 deletions
|
@ -1,3 +1,5 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This code is mostly from the old Etherpad. Please help us to comment this code.
|
* This code is mostly from the old Etherpad. Please help us to comment this code.
|
||||||
* This helps other people to understand this code better and helps them to improve it.
|
* This helps other people to understand this code better and helps them to improve it.
|
||||||
|
@ -28,15 +30,13 @@ const noop = Ace2Common.noop;
|
||||||
function SkipList() {
|
function SkipList() {
|
||||||
let PROFILER = window.PROFILER;
|
let PROFILER = window.PROFILER;
|
||||||
if (!PROFILER) {
|
if (!PROFILER) {
|
||||||
PROFILER = function () {
|
PROFILER = () => ({
|
||||||
return {
|
start: noop,
|
||||||
start: noop,
|
mark: noop,
|
||||||
mark: noop,
|
literal: noop,
|
||||||
literal: noop,
|
end: noop,
|
||||||
end: noop,
|
cancel: noop,
|
||||||
cancel: noop,
|
});
|
||||||
};
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// if there are N elements in the skiplist, "start" is element -1 and "end" is element N
|
// if there are N elements in the skiplist, "start" is element -1 and "end" is element N
|
||||||
|
@ -68,7 +68,7 @@ function SkipList() {
|
||||||
// this point.
|
// this point.
|
||||||
|
|
||||||
|
|
||||||
function _getPoint(targetLoc) {
|
const _getPoint = (targetLoc) => {
|
||||||
const numLevels = start.levels;
|
const numLevels = start.levels;
|
||||||
let lvl = numLevels - 1;
|
let lvl = numLevels - 1;
|
||||||
let i = -1;
|
let i = -1;
|
||||||
|
@ -99,13 +99,11 @@ function SkipList() {
|
||||||
idxs,
|
idxs,
|
||||||
loc: targetLoc,
|
loc: targetLoc,
|
||||||
widthSkips,
|
widthSkips,
|
||||||
toString() {
|
toString: () => `getPoint(${targetLoc})`,
|
||||||
return `getPoint(${targetLoc})`;
|
|
||||||
},
|
|
||||||
};
|
};
|
||||||
}
|
};
|
||||||
|
|
||||||
function _getNodeAtOffset(targetOffset) {
|
const _getNodeAtOffset = (targetOffset) => {
|
||||||
let i = 0;
|
let i = 0;
|
||||||
let n = start;
|
let n = start;
|
||||||
let lvl = start.levels - 1;
|
let lvl = start.levels - 1;
|
||||||
|
@ -117,16 +115,14 @@ function SkipList() {
|
||||||
lvl--;
|
lvl--;
|
||||||
}
|
}
|
||||||
if (n === start) return (start.downPtrs[0] || null);
|
if (n === start) return (start.downPtrs[0] || null);
|
||||||
else if (n === end) return (targetOffset == totalWidth ? (end.upPtrs[0] || null) : null);
|
else if (n === end) return (targetOffset === totalWidth ? (end.upPtrs[0] || null) : null);
|
||||||
return n;
|
return n;
|
||||||
}
|
};
|
||||||
|
|
||||||
function _entryWidth(e) {
|
const _entryWidth = (e) => (e && e.width) || 0;
|
||||||
return (e && e.width) || 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
function _insertKeyAtPoint(point, newKey, entry) {
|
const _insertKeyAtPoint = (point, newKey, entry) => {
|
||||||
const p = PROFILER('insertKey', false);
|
const p = PROFILER('insertKey', false); // eslint-disable-line new-cap
|
||||||
const newNode = {
|
const newNode = {
|
||||||
key: newKey,
|
key: newKey,
|
||||||
levels: 0,
|
levels: 0,
|
||||||
|
@ -145,10 +141,10 @@ function SkipList() {
|
||||||
|
|
||||||
// The new node will have at least level 1
|
// The new node will have at least level 1
|
||||||
// With a proability of 0.01^(n-1) the nodes level will be >= n
|
// With a proability of 0.01^(n-1) the nodes level will be >= n
|
||||||
while (newNode.levels == 0 || Math.random() < 0.01) {
|
while (newNode.levels === 0 || Math.random() < 0.01) {
|
||||||
var lvl = newNode.levels;
|
const lvl = newNode.levels;
|
||||||
newNode.levels++;
|
newNode.levels++;
|
||||||
if (lvl == pNodes.length) {
|
if (lvl === pNodes.length) {
|
||||||
// assume we have just passed the end of point.nodes, and reached one level greater
|
// assume we have just passed the end of point.nodes, and reached one level greater
|
||||||
// than the skiplist currently supports
|
// than the skiplist currently supports
|
||||||
pNodes[lvl] = start;
|
pNodes[lvl] = start;
|
||||||
|
@ -162,7 +158,7 @@ function SkipList() {
|
||||||
point.widthSkips[lvl] = 0;
|
point.widthSkips[lvl] = 0;
|
||||||
}
|
}
|
||||||
const me = newNode;
|
const me = newNode;
|
||||||
var up = pNodes[lvl];
|
const up = pNodes[lvl];
|
||||||
const down = up.downPtrs[lvl];
|
const down = up.downPtrs[lvl];
|
||||||
const skip1 = pLoc - pIdxs[lvl];
|
const skip1 = pLoc - pIdxs[lvl];
|
||||||
const skip2 = up.downSkips[lvl] + 1 - skip1;
|
const skip2 = up.downSkips[lvl] + 1 - skip1;
|
||||||
|
@ -179,8 +175,8 @@ function SkipList() {
|
||||||
}
|
}
|
||||||
p.mark('loop2');
|
p.mark('loop2');
|
||||||
p.literal(pNodes.length, 'PNL');
|
p.literal(pNodes.length, 'PNL');
|
||||||
for (var lvl = newNode.levels; lvl < pNodes.length; lvl++) {
|
for (let lvl = newNode.levels; lvl < pNodes.length; lvl++) {
|
||||||
var up = pNodes[lvl];
|
const up = pNodes[lvl];
|
||||||
up.downSkips[lvl]++;
|
up.downSkips[lvl]++;
|
||||||
up.downSkipWidths[lvl] += newWidth;
|
up.downSkipWidths[lvl] += newWidth;
|
||||||
}
|
}
|
||||||
|
@ -189,30 +185,17 @@ function SkipList() {
|
||||||
numNodes++;
|
numNodes++;
|
||||||
totalWidth += newWidth;
|
totalWidth += newWidth;
|
||||||
p.end();
|
p.end();
|
||||||
}
|
};
|
||||||
|
|
||||||
function _getNodeAtPoint(point) {
|
const _getNodeAtPoint = (point) => point.nodes[0].downPtrs[0];
|
||||||
return point.nodes[0].downPtrs[0];
|
|
||||||
}
|
|
||||||
|
|
||||||
function _incrementPoint(point) {
|
const _deleteKeyAtPoint = (point) => {
|
||||||
point.loc++;
|
|
||||||
for (let i = 0; i < point.nodes.length; i++) {
|
|
||||||
if (point.idxs[i] + point.nodes[i].downSkips[i] < point.loc) {
|
|
||||||
point.idxs[i] += point.nodes[i].downSkips[i];
|
|
||||||
point.widthSkips[i] += point.nodes[i].downSkipWidths[i];
|
|
||||||
point.nodes[i] = point.nodes[i].downPtrs[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function _deleteKeyAtPoint(point) {
|
|
||||||
const elem = point.nodes[0].downPtrs[0];
|
const elem = point.nodes[0].downPtrs[0];
|
||||||
const elemWidth = _entryWidth(elem.entry);
|
const elemWidth = _entryWidth(elem.entry);
|
||||||
for (let i = 0; i < point.nodes.length; i++) {
|
for (let i = 0; i < point.nodes.length; i++) {
|
||||||
if (i < elem.levels) {
|
if (i < elem.levels) {
|
||||||
var up = elem.upPtrs[i];
|
const up = elem.upPtrs[i];
|
||||||
var down = elem.downPtrs[i];
|
const down = elem.downPtrs[i];
|
||||||
const totalSkip = up.downSkips[i] + elem.downSkips[i] - 1;
|
const totalSkip = up.downSkips[i] + elem.downSkips[i] - 1;
|
||||||
up.downPtrs[i] = down;
|
up.downPtrs[i] = down;
|
||||||
down.upPtrs[i] = up;
|
down.upPtrs[i] = up;
|
||||||
|
@ -220,8 +203,7 @@ function SkipList() {
|
||||||
const totalWidthSkip = up.downSkipWidths[i] + elem.downSkipWidths[i] - elemWidth;
|
const totalWidthSkip = up.downSkipWidths[i] + elem.downSkipWidths[i] - elemWidth;
|
||||||
up.downSkipWidths[i] = totalWidthSkip;
|
up.downSkipWidths[i] = totalWidthSkip;
|
||||||
} else {
|
} else {
|
||||||
var up = point.nodes[i];
|
const up = point.nodes[i];
|
||||||
var down = up.downPtrs[i];
|
|
||||||
up.downSkips[i]--;
|
up.downSkips[i]--;
|
||||||
up.downSkipWidths[i] -= elemWidth;
|
up.downSkipWidths[i] -= elemWidth;
|
||||||
}
|
}
|
||||||
|
@ -229,9 +211,9 @@ function SkipList() {
|
||||||
delete keyToNodeMap[`$KEY$${elem.key}`];
|
delete keyToNodeMap[`$KEY$${elem.key}`];
|
||||||
numNodes--;
|
numNodes--;
|
||||||
totalWidth -= elemWidth;
|
totalWidth -= elemWidth;
|
||||||
}
|
};
|
||||||
|
|
||||||
function _propagateWidthChange(node) {
|
const _propagateWidthChange = (node) => {
|
||||||
const oldWidth = node.downSkipWidths[0];
|
const oldWidth = node.downSkipWidths[0];
|
||||||
const newWidth = _entryWidth(node.entry);
|
const newWidth = _entryWidth(node.entry);
|
||||||
const widthChange = newWidth - oldWidth;
|
const widthChange = newWidth - oldWidth;
|
||||||
|
@ -245,9 +227,9 @@ function SkipList() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
totalWidth += widthChange;
|
totalWidth += widthChange;
|
||||||
}
|
};
|
||||||
|
|
||||||
function _getNodeIndex(node, byWidth) {
|
const _getNodeIndex = (node, byWidth) => {
|
||||||
let dist = (byWidth ? 0 : -1);
|
let dist = (byWidth ? 0 : -1);
|
||||||
let n = node;
|
let n = node;
|
||||||
while (n !== start) {
|
while (n !== start) {
|
||||||
|
@ -257,27 +239,26 @@ function SkipList() {
|
||||||
else dist += n.downSkips[lvl];
|
else dist += n.downSkips[lvl];
|
||||||
}
|
}
|
||||||
return dist;
|
return dist;
|
||||||
}
|
};
|
||||||
|
|
||||||
function _getNodeByKey(key) {
|
const _getNodeByKey = (key) => keyToNodeMap[`$KEY$${key}`];
|
||||||
return keyToNodeMap[`$KEY$${key}`];
|
|
||||||
}
|
|
||||||
|
|
||||||
// Returns index of first entry such that entryFunc(entry) is truthy,
|
// Returns index of first entry such that entryFunc(entry) is truthy,
|
||||||
// or length() if no such entry. Assumes all falsy entries come before
|
// or length() if no such entry. Assumes all falsy entries come before
|
||||||
// all truthy entries.
|
// all truthy entries.
|
||||||
|
|
||||||
|
|
||||||
function _search(entryFunc) {
|
const _search = (entryFunc) => {
|
||||||
let low = start;
|
let low = start;
|
||||||
let lvl = start.levels - 1;
|
let lvl = start.levels - 1;
|
||||||
let lowIndex = -1;
|
let lowIndex = -1;
|
||||||
|
|
||||||
function f(node) {
|
const f = (node) => {
|
||||||
if (node === start) return false;
|
if (node === start) return false;
|
||||||
else if (node === end) return true;
|
else if (node === end) return true;
|
||||||
else return entryFunc(node.entry);
|
else return entryFunc(node.entry);
|
||||||
}
|
};
|
||||||
|
|
||||||
while (lvl >= 0) {
|
while (lvl >= 0) {
|
||||||
let nextLow = low.downPtrs[lvl];
|
let nextLow = low.downPtrs[lvl];
|
||||||
while (!f(nextLow)) {
|
while (!f(nextLow)) {
|
||||||
|
@ -288,7 +269,7 @@ function SkipList() {
|
||||||
lvl--;
|
lvl--;
|
||||||
}
|
}
|
||||||
return lowIndex + 1;
|
return lowIndex + 1;
|
||||||
}
|
};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
The skip-list contains "entries", JavaScript objects that each must have a unique "key" property
|
The skip-list contains "entries", JavaScript objects that each must have a unique "key" property
|
||||||
|
@ -296,16 +277,14 @@ that is a string.
|
||||||
*/
|
*/
|
||||||
const self = this;
|
const self = this;
|
||||||
_.extend(this, {
|
_.extend(this, {
|
||||||
length() {
|
length: () => numNodes,
|
||||||
return numNodes;
|
atIndex: (i) => {
|
||||||
},
|
|
||||||
atIndex(i) {
|
|
||||||
if (i < 0) console.warn(`atIndex(${i})`);
|
if (i < 0) console.warn(`atIndex(${i})`);
|
||||||
if (i >= numNodes) console.warn(`atIndex(${i}>=${numNodes})`);
|
if (i >= numNodes) console.warn(`atIndex(${i}>=${numNodes})`);
|
||||||
return _getNodeAtPoint(_getPoint(i)).entry;
|
return _getNodeAtPoint(_getPoint(i)).entry;
|
||||||
},
|
},
|
||||||
// differs from Array.splice() in that new elements are in an array, not varargs
|
// differs from Array.splice() in that new elements are in an array, not varargs
|
||||||
splice(start, deleteCount, newEntryArray) {
|
splice: (start, deleteCount, newEntryArray) => {
|
||||||
if (start < 0) console.warn(`splice(${start}, ...)`);
|
if (start < 0) console.warn(`splice(${start}, ...)`);
|
||||||
if (start + deleteCount > numNodes) {
|
if (start + deleteCount > numNodes) {
|
||||||
console.warn(`splice(${start}, ${deleteCount}, ...), N=${numNodes}`);
|
console.warn(`splice(${start}, ${deleteCount}, ...), N=${numNodes}`);
|
||||||
|
@ -315,26 +294,22 @@ that is a string.
|
||||||
|
|
||||||
if (!newEntryArray) newEntryArray = [];
|
if (!newEntryArray) newEntryArray = [];
|
||||||
const pt = _getPoint(start);
|
const pt = _getPoint(start);
|
||||||
for (var i = 0; i < deleteCount; i++) {
|
for (let i = 0; i < deleteCount; i++) {
|
||||||
_deleteKeyAtPoint(pt);
|
_deleteKeyAtPoint(pt);
|
||||||
}
|
}
|
||||||
for (var i = (newEntryArray.length - 1); i >= 0; i--) {
|
for (let i = (newEntryArray.length - 1); i >= 0; i--) {
|
||||||
const entry = newEntryArray[i];
|
const entry = newEntryArray[i];
|
||||||
_insertKeyAtPoint(pt, entry.key, entry);
|
_insertKeyAtPoint(pt, entry.key, entry);
|
||||||
const node = _getNodeByKey(entry.key);
|
const node = _getNodeByKey(entry.key);
|
||||||
node.entry = entry;
|
node.entry = entry;
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
next(entry) {
|
next: (entry) => _getNodeByKey(entry.key).downPtrs[0].entry || null,
|
||||||
return _getNodeByKey(entry.key).downPtrs[0].entry || null;
|
prev: (entry) => _getNodeByKey(entry.key).upPtrs[0].entry || null,
|
||||||
},
|
push: (entry) => {
|
||||||
prev(entry) {
|
|
||||||
return _getNodeByKey(entry.key).upPtrs[0].entry || null;
|
|
||||||
},
|
|
||||||
push(entry) {
|
|
||||||
self.splice(numNodes, 0, [entry]);
|
self.splice(numNodes, 0, [entry]);
|
||||||
},
|
},
|
||||||
slice(start, end) {
|
slice: (start, end) => {
|
||||||
// act like Array.slice()
|
// act like Array.slice()
|
||||||
if (start === undefined) start = 0;
|
if (start === undefined) start = 0;
|
||||||
else if (start < 0) start += numNodes;
|
else if (start < 0) start += numNodes;
|
||||||
|
@ -346,7 +321,7 @@ that is a string.
|
||||||
if (end < 0) end = 0;
|
if (end < 0) end = 0;
|
||||||
if (end > numNodes) end = numNodes;
|
if (end > numNodes) end = numNodes;
|
||||||
|
|
||||||
dmesg(String([start, end, numNodes]));
|
window.dmesg(String([start, end, numNodes]));
|
||||||
if (end <= start) return [];
|
if (end <= start) return [];
|
||||||
let n = self.atIndex(start);
|
let n = self.atIndex(start);
|
||||||
const array = [n];
|
const array = [n];
|
||||||
|
@ -356,56 +331,34 @@ that is a string.
|
||||||
}
|
}
|
||||||
return array;
|
return array;
|
||||||
},
|
},
|
||||||
atKey(key) {
|
atKey: (key) => _getNodeByKey(key).entry,
|
||||||
return _getNodeByKey(key).entry;
|
indexOfKey: (key) => _getNodeIndex(_getNodeByKey(key)),
|
||||||
},
|
indexOfEntry: (entry) => self.indexOfKey(entry.key),
|
||||||
indexOfKey(key) {
|
containsKey: (key) => !!(_getNodeByKey(key)),
|
||||||
return _getNodeIndex(_getNodeByKey(key));
|
|
||||||
},
|
|
||||||
indexOfEntry(entry) {
|
|
||||||
return self.indexOfKey(entry.key);
|
|
||||||
},
|
|
||||||
containsKey(key) {
|
|
||||||
return !!(_getNodeByKey(key));
|
|
||||||
},
|
|
||||||
// gets the last entry starting at or before the offset
|
// gets the last entry starting at or before the offset
|
||||||
atOffset(offset) {
|
atOffset: (offset) => _getNodeAtOffset(offset).entry,
|
||||||
return _getNodeAtOffset(offset).entry;
|
keyAtOffset: (offset) => self.atOffset(offset).key,
|
||||||
},
|
offsetOfKey: (key) => _getNodeIndex(_getNodeByKey(key), true),
|
||||||
keyAtOffset(offset) {
|
offsetOfEntry: (entry) => self.offsetOfKey(entry.key),
|
||||||
return self.atOffset(offset).key;
|
setEntryWidth: (entry, width) => {
|
||||||
},
|
|
||||||
offsetOfKey(key) {
|
|
||||||
return _getNodeIndex(_getNodeByKey(key), true);
|
|
||||||
},
|
|
||||||
offsetOfEntry(entry) {
|
|
||||||
return self.offsetOfKey(entry.key);
|
|
||||||
},
|
|
||||||
setEntryWidth(entry, width) {
|
|
||||||
entry.width = width;
|
entry.width = width;
|
||||||
_propagateWidthChange(_getNodeByKey(entry.key));
|
_propagateWidthChange(_getNodeByKey(entry.key));
|
||||||
},
|
},
|
||||||
totalWidth() {
|
totalWidth: () => totalWidth,
|
||||||
return totalWidth;
|
offsetOfIndex: (i) => {
|
||||||
},
|
|
||||||
offsetOfIndex(i) {
|
|
||||||
if (i < 0) return 0;
|
if (i < 0) return 0;
|
||||||
if (i >= numNodes) return totalWidth;
|
if (i >= numNodes) return totalWidth;
|
||||||
return self.offsetOfEntry(self.atIndex(i));
|
return self.offsetOfEntry(self.atIndex(i));
|
||||||
},
|
},
|
||||||
indexOfOffset(offset) {
|
indexOfOffset: (offset) => {
|
||||||
if (offset <= 0) return 0;
|
if (offset <= 0) return 0;
|
||||||
if (offset >= totalWidth) return numNodes;
|
if (offset >= totalWidth) return numNodes;
|
||||||
return self.indexOfEntry(self.atOffset(offset));
|
return self.indexOfEntry(self.atOffset(offset));
|
||||||
},
|
},
|
||||||
search(entryFunc) {
|
search: (entryFunc) => _search(entryFunc),
|
||||||
return _search(entryFunc);
|
|
||||||
},
|
|
||||||
// debugToString: _debugToString,
|
// debugToString: _debugToString,
|
||||||
debugGetPoint: _getPoint,
|
debugGetPoint: _getPoint,
|
||||||
debugDepth() {
|
debugDepth: () => start.levels,
|
||||||
return start.levels;
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue