pad.libre-service.eu-etherpad/src/static/js/skiplist.js

339 lines
10 KiB
JavaScript
Raw Normal View History

2020-12-22 00:23:16 +01:00
'use strict';
/**
* 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.
* TL;DR COMMENTS ON THIS FILE ARE HIGHLY APPRECIATED
*/
2011-03-26 14:10:41 +01:00
/**
* Copyright 2009 Google Inc.
2011-07-07 19:59:34 +02:00
*
2011-03-26 14:10:41 +01:00
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
2011-07-07 19:59:34 +02:00
*
2011-03-26 14:10:41 +01:00
* http://www.apache.org/licenses/LICENSE-2.0
2011-07-07 19:59:34 +02:00
*
2011-03-26 14:10:41 +01:00
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS-IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const _ = require('./underscore');
2012-03-27 11:44:21 +02:00
2021-04-10 10:12:10 +02:00
/**
* The skip-list contains "entries", JavaScript objects that each must have a unique "key"
* property that is a string.
*/
function SkipList() {
2011-03-26 14:10:41 +01:00
// if there are N elements in the skiplist, "start" is element -1 and "end" is element N
2020-11-23 19:24:19 +01:00
const start = {
2011-07-07 19:59:34 +02:00
key: null,
levels: 1,
upPtrs: [null],
downPtrs: [null],
downSkips: [1],
2020-11-23 19:24:19 +01:00
downSkipWidths: [0],
2011-07-07 19:59:34 +02:00
};
2020-11-23 19:24:19 +01:00
const end = {
2011-07-07 19:59:34 +02:00
key: null,
levels: 1,
upPtrs: [null],
downPtrs: [null],
downSkips: [null],
2020-11-23 19:24:19 +01:00
downSkipWidths: [null],
2011-07-07 19:59:34 +02:00
};
2020-11-23 19:24:19 +01:00
let numNodes = 0;
let totalWidth = 0;
const keyToNodeMap = {};
2011-03-26 14:10:41 +01:00
start.downPtrs[0] = end;
end.upPtrs[0] = start;
2021-04-10 10:12:10 +02:00
2011-03-26 14:10:41 +01:00
// a "point" object at location x allows modifications immediately after the first
// x elements of the skiplist, such as multiple inserts or deletes.
// After an insert or delete using point P, the point is still valid and points
// to the same index in the skiplist. Other operations with other points invalidate
// this point.
2020-12-22 00:23:16 +01:00
const _getPoint = (targetLoc) => {
2020-11-23 19:24:19 +01:00
const numLevels = start.levels;
let lvl = numLevels - 1;
let i = -1;
let ws = 0;
const nodes = new Array(numLevels);
const idxs = new Array(numLevels);
const widthSkips = new Array(numLevels);
2011-03-26 14:10:41 +01:00
nodes[lvl] = start;
idxs[lvl] = -1;
widthSkips[lvl] = 0;
2020-11-23 19:24:19 +01:00
while (lvl >= 0) {
let n = nodes[lvl];
while (n.downPtrs[lvl] && (i + n.downSkips[lvl] < targetLoc)) {
2011-07-07 19:59:34 +02:00
i += n.downSkips[lvl];
ws += n.downSkipWidths[lvl];
n = n.downPtrs[lvl];
2011-03-26 14:10:41 +01:00
}
nodes[lvl] = n;
idxs[lvl] = i;
widthSkips[lvl] = ws;
lvl--;
2020-11-23 19:24:19 +01:00
if (lvl >= 0) {
2011-07-07 19:59:34 +02:00
nodes[lvl] = n;
2011-03-26 14:10:41 +01:00
}
}
2011-07-07 19:59:34 +02:00
return {
2020-11-23 19:24:19 +01:00
nodes,
idxs,
2011-07-07 19:59:34 +02:00
loc: targetLoc,
2020-11-23 19:24:19 +01:00
widthSkips,
2020-12-22 00:23:16 +01:00
toString: () => `getPoint(${targetLoc})`,
2011-07-07 19:59:34 +02:00
};
2020-12-22 00:23:16 +01:00
};
2011-07-07 19:59:34 +02:00
2020-12-22 00:23:16 +01:00
const _getNodeAtOffset = (targetOffset) => {
2020-11-23 19:24:19 +01:00
let i = 0;
let n = start;
let lvl = start.levels - 1;
while (lvl >= 0 && n.downPtrs[lvl]) {
while (n.downPtrs[lvl] && (i + n.downSkipWidths[lvl] <= targetOffset)) {
2011-07-07 19:59:34 +02:00
i += n.downSkipWidths[lvl];
n = n.downPtrs[lvl];
2011-03-26 14:10:41 +01:00
}
lvl--;
}
if (n === start) return (start.downPtrs[0] || null);
2020-12-22 00:23:16 +01:00
else if (n === end) return (targetOffset === totalWidth ? (end.upPtrs[0] || null) : null);
2011-03-26 14:10:41 +01:00
return n;
2020-12-22 00:23:16 +01:00
};
2011-07-07 19:59:34 +02:00
2020-12-22 00:23:16 +01:00
const _entryWidth = (e) => (e && e.width) || 0;
2011-07-07 19:59:34 +02:00
2020-12-22 00:23:16 +01:00
const _insertKeyAtPoint = (point, newKey, entry) => {
2020-11-23 19:24:19 +01:00
const newNode = {
2011-07-07 19:59:34 +02:00
key: newKey,
levels: 0,
upPtrs: [],
downPtrs: [],
downSkips: [],
2020-11-23 19:24:19 +01:00
downSkipWidths: [],
2011-07-07 19:59:34 +02:00
};
2020-11-23 19:24:19 +01:00
const pNodes = point.nodes;
const pIdxs = point.idxs;
const pLoc = point.loc;
const widthLoc = point.widthSkips[0] + point.nodes[0].downSkipWidths[0];
const newWidth = _entryWidth(entry);
2012-04-08 21:21:52 +02:00
// The new node will have at least level 1
// With a proability of 0.01^(n-1) the nodes level will be >= n
2020-12-22 00:23:16 +01:00
while (newNode.levels === 0 || Math.random() < 0.01) {
const lvl = newNode.levels;
2011-03-26 14:10:41 +01:00
newNode.levels++;
2020-12-22 00:23:16 +01:00
if (lvl === pNodes.length) {
2011-07-07 19:59:34 +02:00
// assume we have just passed the end of point.nodes, and reached one level greater
// than the skiplist currently supports
pNodes[lvl] = start;
pIdxs[lvl] = -1;
start.levels++;
end.levels++;
start.downPtrs[lvl] = end;
end.upPtrs[lvl] = start;
start.downSkips[lvl] = numNodes + 1;
start.downSkipWidths[lvl] = totalWidth;
point.widthSkips[lvl] = 0;
2011-03-26 14:10:41 +01:00
}
2020-11-23 19:24:19 +01:00
const me = newNode;
2020-12-22 00:23:16 +01:00
const up = pNodes[lvl];
2020-11-23 19:24:19 +01:00
const down = up.downPtrs[lvl];
const skip1 = pLoc - pIdxs[lvl];
const skip2 = up.downSkips[lvl] + 1 - skip1;
2011-03-26 14:10:41 +01:00
up.downSkips[lvl] = skip1;
up.downPtrs[lvl] = me;
me.downSkips[lvl] = skip2;
me.upPtrs[lvl] = up;
me.downPtrs[lvl] = down;
down.upPtrs[lvl] = me;
2020-11-23 19:24:19 +01:00
const widthSkip1 = widthLoc - point.widthSkips[lvl];
const widthSkip2 = up.downSkipWidths[lvl] + newWidth - widthSkip1;
2011-03-26 14:10:41 +01:00
up.downSkipWidths[lvl] = widthSkip1;
me.downSkipWidths[lvl] = widthSkip2;
}
2020-12-22 00:23:16 +01:00
for (let lvl = newNode.levels; lvl < pNodes.length; lvl++) {
const up = pNodes[lvl];
2011-03-26 14:10:41 +01:00
up.downSkips[lvl]++;
up.downSkipWidths[lvl] += newWidth;
}
2020-11-23 19:24:19 +01:00
keyToNodeMap[`$KEY$${newKey}`] = newNode;
2011-03-26 14:10:41 +01:00
numNodes++;
totalWidth += newWidth;
2020-12-22 00:23:16 +01:00
};
2011-07-07 19:59:34 +02:00
2020-12-22 00:23:16 +01:00
const _getNodeAtPoint = (point) => point.nodes[0].downPtrs[0];
2011-07-07 19:59:34 +02:00
2020-12-22 00:23:16 +01:00
const _deleteKeyAtPoint = (point) => {
2020-11-23 19:24:19 +01:00
const elem = point.nodes[0].downPtrs[0];
const elemWidth = _entryWidth(elem.entry);
for (let i = 0; i < point.nodes.length; i++) {
if (i < elem.levels) {
2020-12-22 00:23:16 +01:00
const up = elem.upPtrs[i];
const down = elem.downPtrs[i];
2020-11-23 19:24:19 +01:00
const totalSkip = up.downSkips[i] + elem.downSkips[i] - 1;
2011-07-07 19:59:34 +02:00
up.downPtrs[i] = down;
down.upPtrs[i] = up;
up.downSkips[i] = totalSkip;
2020-11-23 19:24:19 +01:00
const totalWidthSkip = up.downSkipWidths[i] + elem.downSkipWidths[i] - elemWidth;
2011-07-07 19:59:34 +02:00
up.downSkipWidths[i] = totalWidthSkip;
2020-11-23 19:24:19 +01:00
} else {
2020-12-22 00:23:16 +01:00
const up = point.nodes[i];
2011-07-07 19:59:34 +02:00
up.downSkips[i]--;
up.downSkipWidths[i] -= elemWidth;
2011-03-26 14:10:41 +01:00
}
}
2020-11-23 19:24:19 +01:00
delete keyToNodeMap[`$KEY$${elem.key}`];
2011-03-26 14:10:41 +01:00
numNodes--;
totalWidth -= elemWidth;
2020-12-22 00:23:16 +01:00
};
2011-07-07 19:59:34 +02:00
2020-12-22 00:23:16 +01:00
const _propagateWidthChange = (node) => {
2020-11-23 19:24:19 +01:00
const oldWidth = node.downSkipWidths[0];
const newWidth = _entryWidth(node.entry);
const widthChange = newWidth - oldWidth;
let n = node;
let lvl = 0;
while (lvl < n.levels) {
2011-03-26 14:10:41 +01:00
n.downSkipWidths[lvl] += widthChange;
lvl++;
2020-11-23 19:24:19 +01:00
while (lvl >= n.levels && n.upPtrs[lvl - 1]) {
2011-07-07 19:59:34 +02:00
n = n.upPtrs[lvl - 1];
2011-03-26 14:10:41 +01:00
}
}
totalWidth += widthChange;
2020-12-22 00:23:16 +01:00
};
2011-07-07 19:59:34 +02:00
2020-12-22 00:23:16 +01:00
const _getNodeIndex = (node, byWidth) => {
2020-11-23 19:24:19 +01:00
let dist = (byWidth ? 0 : -1);
let n = node;
while (n !== start) {
const lvl = n.levels - 1;
2011-03-26 14:10:41 +01:00
n = n.upPtrs[lvl];
if (byWidth) dist += n.downSkipWidths[lvl];
else dist += n.downSkips[lvl];
}
return dist;
2020-12-22 00:23:16 +01:00
};
2011-03-26 14:10:41 +01:00
2020-12-22 00:23:16 +01:00
const _getNodeByKey = (key) => keyToNodeMap[`$KEY$${key}`];
2011-03-26 14:10:41 +01:00
// 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.
2020-12-22 00:23:16 +01:00
const _search = (entryFunc) => {
2020-11-23 19:24:19 +01:00
let low = start;
let lvl = start.levels - 1;
let lowIndex = -1;
2011-07-07 19:59:34 +02:00
2020-12-22 00:23:16 +01:00
const f = (node) => {
2011-03-26 14:10:41 +01:00
if (node === start) return false;
else if (node === end) return true;
else return entryFunc(node.entry);
2020-12-22 00:23:16 +01:00
};
2020-11-23 19:24:19 +01:00
while (lvl >= 0) {
let nextLow = low.downPtrs[lvl];
while (!f(nextLow)) {
2011-07-07 19:59:34 +02:00
lowIndex += low.downSkips[lvl];
low = nextLow;
nextLow = low.downPtrs[lvl];
2011-03-26 14:10:41 +01:00
}
lvl--;
}
2011-07-07 19:59:34 +02:00
return lowIndex + 1;
2020-12-22 00:23:16 +01:00
};
2011-03-26 14:10:41 +01:00
2020-11-23 19:24:19 +01:00
const self = this;
2012-03-27 11:44:21 +02:00
_.extend(this, {
2020-12-22 00:23:16 +01:00
length: () => numNodes,
atIndex: (i) => {
2020-11-23 19:24:19 +01:00
if (i < 0) console.warn(`atIndex(${i})`);
if (i >= numNodes) console.warn(`atIndex(${i}>=${numNodes})`);
2011-03-26 14:10:41 +01:00
return _getNodeAtPoint(_getPoint(i)).entry;
},
// differs from Array.splice() in that new elements are in an array, not varargs
2020-12-22 00:23:16 +01:00
splice: (start, deleteCount, newEntryArray) => {
2020-11-23 19:24:19 +01:00
if (start < 0) console.warn(`splice(${start}, ...)`);
if (start + deleteCount > numNodes) {
console.warn(`splice(${start}, ${deleteCount}, ...), N=${numNodes}`);
console.warn('%s %s %s', typeof start, typeof deleteCount, typeof numNodes);
2011-07-07 19:59:34 +02:00
console.trace();
2011-03-26 14:10:41 +01:00
}
2011-07-07 19:59:34 +02:00
if (!newEntryArray) newEntryArray = [];
2020-11-23 19:24:19 +01:00
const pt = _getPoint(start);
2020-12-22 00:23:16 +01:00
for (let i = 0; i < deleteCount; i++) {
2011-07-07 19:59:34 +02:00
_deleteKeyAtPoint(pt);
2011-03-26 14:10:41 +01:00
}
2020-12-22 00:23:16 +01:00
for (let i = (newEntryArray.length - 1); i >= 0; i--) {
2020-11-23 19:24:19 +01:00
const entry = newEntryArray[i];
2011-07-07 19:59:34 +02:00
_insertKeyAtPoint(pt, entry.key, entry);
2020-11-23 19:24:19 +01:00
const node = _getNodeByKey(entry.key);
2011-07-07 19:59:34 +02:00
node.entry = entry;
2011-03-26 14:10:41 +01:00
}
},
2020-12-22 00:23:16 +01:00
next: (entry) => _getNodeByKey(entry.key).downPtrs[0].entry || null,
prev: (entry) => _getNodeByKey(entry.key).upPtrs[0].entry || null,
push: (entry) => {
2011-03-26 14:10:41 +01:00
self.splice(numNodes, 0, [entry]);
},
2020-12-22 00:23:16 +01:00
slice: (start, end) => {
2011-03-26 14:10:41 +01:00
// act like Array.slice()
if (start === undefined) start = 0;
else if (start < 0) start += numNodes;
if (end === undefined) end = numNodes;
else if (end < 0) end += numNodes;
if (start < 0) start = 0;
if (start > numNodes) start = numNodes;
if (end < 0) end = 0;
if (end > numNodes) end = numNodes;
2020-12-22 00:23:16 +01:00
window.dmesg(String([start, end, numNodes]));
2011-03-26 14:10:41 +01:00
if (end <= start) return [];
2020-11-23 19:24:19 +01:00
let n = self.atIndex(start);
const array = [n];
for (let i = 1; i < (end - start); i++) {
2011-07-07 19:59:34 +02:00
n = self.next(n);
array.push(n);
2011-03-26 14:10:41 +01:00
}
return array;
},
2020-12-22 00:23:16 +01:00
atKey: (key) => _getNodeByKey(key).entry,
indexOfKey: (key) => _getNodeIndex(_getNodeByKey(key)),
indexOfEntry: (entry) => self.indexOfKey(entry.key),
containsKey: (key) => !!(_getNodeByKey(key)),
2011-03-26 14:10:41 +01:00
// gets the last entry starting at or before the offset
2020-12-22 00:23:16 +01:00
atOffset: (offset) => _getNodeAtOffset(offset).entry,
keyAtOffset: (offset) => self.atOffset(offset).key,
offsetOfKey: (key) => _getNodeIndex(_getNodeByKey(key), true),
offsetOfEntry: (entry) => self.offsetOfKey(entry.key),
setEntryWidth: (entry, width) => {
2011-07-07 19:59:34 +02:00
entry.width = width;
_propagateWidthChange(_getNodeByKey(entry.key));
},
2020-12-22 00:23:16 +01:00
totalWidth: () => totalWidth,
offsetOfIndex: (i) => {
2011-03-26 14:10:41 +01:00
if (i < 0) return 0;
if (i >= numNodes) return totalWidth;
return self.offsetOfEntry(self.atIndex(i));
},
2020-12-22 00:23:16 +01:00
indexOfOffset: (offset) => {
2011-03-26 14:10:41 +01:00
if (offset <= 0) return 0;
if (offset >= totalWidth) return numNodes;
return self.indexOfEntry(self.atOffset(offset));
},
2020-12-22 00:23:16 +01:00
search: (entryFunc) => _search(entryFunc),
2012-03-18 11:34:56 +01:00
});
2011-03-26 14:10:41 +01:00
}
2012-03-18 11:34:56 +01:00
module.exports = SkipList;