etherpad-lite/static/js/skiplist.js

490 lines
12 KiB
JavaScript
Raw Normal View History

/**
* 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.
*/
var noop = require('./ace2_common').noop;
2011-03-26 14:10:41 +01:00
2011-07-07 19:59:34 +02:00
function newSkipList()
{
2011-03-26 14:10:41 +01:00
var PROFILER = window.PROFILER;
2011-07-07 19:59:34 +02:00
if (!PROFILER)
{
PROFILER = function()
{
return {
start: noop,
mark: noop,
literal: noop,
end: noop,
cancel: noop
};
};
2011-03-26 14:10:41 +01:00
}
2011-07-07 19:59:34 +02:00
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
2011-07-07 19:59:34 +02:00
var start = {
key: null,
levels: 1,
upPtrs: [null],
downPtrs: [null],
downSkips: [1],
downSkipWidths: [0]
};
var end = {
key: null,
levels: 1,
upPtrs: [null],
downPtrs: [null],
downSkips: [null],
downSkipWidths: [null]
};
2011-03-26 14:10:41 +01:00
var numNodes = 0;
var totalWidth = 0;
var keyToNodeMap = {};
start.downPtrs[0] = end;
end.upPtrs[0] = start;
// 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.
2011-07-07 19:59:34 +02:00
function _getPoint(targetLoc)
{
2011-03-26 14:10:41 +01:00
var numLevels = start.levels;
2011-07-07 19:59:34 +02:00
var lvl = numLevels - 1;
var i = -1,
ws = 0;
2011-03-26 14:10:41 +01:00
var nodes = new Array(numLevels);
var idxs = new Array(numLevels);
var widthSkips = new Array(numLevels);
nodes[lvl] = start;
idxs[lvl] = -1;
widthSkips[lvl] = 0;
2011-07-07 19:59:34 +02:00
while (lvl >= 0)
{
2011-03-26 14:10:41 +01:00
var n = nodes[lvl];
2011-07-07 19:59:34 +02:00
while (n.downPtrs[lvl] && (i + n.downSkips[lvl] < targetLoc))
{
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--;
2011-07-07 19:59:34 +02:00
if (lvl >= 0)
{
nodes[lvl] = n;
2011-03-26 14:10:41 +01:00
}
}
2011-07-07 19:59:34 +02:00
return {
nodes: nodes,
idxs: idxs,
loc: targetLoc,
widthSkips: widthSkips,
toString: function()
{
return "getPoint(" + targetLoc + ")";
}
};
2011-03-26 14:10:41 +01:00
}
2011-07-07 19:59:34 +02:00
function _getNodeAtOffset(targetOffset)
{
2011-03-26 14:10:41 +01:00
var i = 0;
var n = start;
2011-07-07 19:59:34 +02:00
var lvl = start.levels - 1;
while (lvl >= 0 && n.downPtrs[lvl])
{
while (n.downPtrs[lvl] && (i + n.downSkipWidths[lvl] <= targetOffset))
{
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);
else if (n === end) return (targetOffset == totalWidth ? (end.upPtrs[0] || null) : null);
return n;
}
2011-07-07 19:59:34 +02:00
function _entryWidth(e)
{
return (e && e.width) || 0;
}
function _insertKeyAtPoint(point, newKey, entry)
{
2011-03-26 14:10:41 +01:00
var p = PROFILER("insertKey", false);
2011-07-07 19:59:34 +02:00
var newNode = {
key: newKey,
levels: 0,
upPtrs: [],
downPtrs: [],
downSkips: [],
downSkipWidths: []
};
2011-03-26 14:10:41 +01:00
p.mark("donealloc");
var pNodes = point.nodes;
var pIdxs = point.idxs;
var pLoc = point.loc;
var widthLoc = point.widthSkips[0] + point.nodes[0].downSkipWidths[0];
var newWidth = _entryWidth(entry);
p.mark("loop1");
2011-07-07 19:59:34 +02:00
while (newNode.levels == 0 || Math.random() < 0.01)
{
2011-03-26 14:10:41 +01:00
var lvl = newNode.levels;
newNode.levels++;
2011-07-07 19:59:34 +02:00
if (lvl == pNodes.length)
{
// 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
}
var me = newNode;
var up = pNodes[lvl];
var down = up.downPtrs[lvl];
var skip1 = pLoc - pIdxs[lvl];
var skip2 = up.downSkips[lvl] + 1 - skip1;
up.downSkips[lvl] = skip1;
up.downPtrs[lvl] = me;
me.downSkips[lvl] = skip2;
me.upPtrs[lvl] = up;
me.downPtrs[lvl] = down;
down.upPtrs[lvl] = me;
var widthSkip1 = widthLoc - point.widthSkips[lvl];
var widthSkip2 = up.downSkipWidths[lvl] + newWidth - widthSkip1;
up.downSkipWidths[lvl] = widthSkip1;
me.downSkipWidths[lvl] = widthSkip2;
}
p.mark("loop2");
p.literal(pNodes.length, "PNL");
2011-07-07 19:59:34 +02:00
for (var lvl = newNode.levels; lvl < pNodes.length; lvl++)
{
2011-03-26 14:10:41 +01:00
var up = pNodes[lvl];
up.downSkips[lvl]++;
up.downSkipWidths[lvl] += newWidth;
}
p.mark("map");
2011-07-07 19:59:34 +02:00
keyToNodeMap['$KEY$' + newKey] = newNode;
2011-03-26 14:10:41 +01:00
numNodes++;
totalWidth += newWidth;
p.end();
}
2011-07-07 19:59:34 +02:00
function _getNodeAtPoint(point)
{
2011-03-26 14:10:41 +01:00
return point.nodes[0].downPtrs[0];
}
2011-07-07 19:59:34 +02:00
function _incrementPoint(point)
{
2011-03-26 14:10:41 +01:00
point.loc++;
2011-07-07 19:59:34 +02:00
for (var 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];
2011-03-26 14:10:41 +01:00
}
}
}
2011-07-07 19:59:34 +02:00
function _deleteKeyAtPoint(point)
{
2011-03-26 14:10:41 +01:00
var elem = point.nodes[0].downPtrs[0];
var elemWidth = _entryWidth(elem.entry);
2011-07-07 19:59:34 +02:00
for (var i = 0; i < point.nodes.length; i++)
{
if (i < elem.levels)
{
var up = elem.upPtrs[i];
var down = elem.downPtrs[i];
var totalSkip = up.downSkips[i] + elem.downSkips[i] - 1;
up.downPtrs[i] = down;
down.upPtrs[i] = up;
up.downSkips[i] = totalSkip;
var totalWidthSkip = up.downSkipWidths[i] + elem.downSkipWidths[i] - elemWidth;
up.downSkipWidths[i] = totalWidthSkip;
2011-03-26 14:10:41 +01:00
}
2011-07-07 19:59:34 +02:00
else
{
var up = point.nodes[i];
var down = up.downPtrs[i];
up.downSkips[i]--;
up.downSkipWidths[i] -= elemWidth;
2011-03-26 14:10:41 +01:00
}
}
2011-07-07 19:59:34 +02:00
delete keyToNodeMap['$KEY$' + elem.key];
2011-03-26 14:10:41 +01:00
numNodes--;
totalWidth -= elemWidth;
}
2011-07-07 19:59:34 +02:00
function _propagateWidthChange(node)
{
2011-03-26 14:10:41 +01:00
var oldWidth = node.downSkipWidths[0];
var newWidth = _entryWidth(node.entry);
var widthChange = newWidth - oldWidth;
var n = node;
var lvl = 0;
2011-07-07 19:59:34 +02:00
while (lvl < n.levels)
{
2011-03-26 14:10:41 +01:00
n.downSkipWidths[lvl] += widthChange;
lvl++;
2011-07-07 19:59:34 +02:00
while (lvl >= n.levels && n.upPtrs[lvl - 1])
{
n = n.upPtrs[lvl - 1];
2011-03-26 14:10:41 +01:00
}
}
totalWidth += widthChange;
}
2011-07-07 19:59:34 +02:00
function _getNodeIndex(node, byWidth)
{
2011-03-26 14:10:41 +01:00
var dist = (byWidth ? 0 : -1);
var n = node;
2011-07-07 19:59:34 +02:00
while (n !== start)
{
var 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;
}
2011-07-07 19:59:34 +02:00
/*function _debugToString() {
2011-03-26 14:10:41 +01:00
var array = [start];
while (array[array.length-1] !== end) {
array[array.length] = array[array.length-1].downPtrs[0];
}
function getIndex(node) {
if (!node) return null;
for(var i=0;i<array.length;i++) {
if (array[i] === node)
return i-1;
}
return false;
}
var processedArray = map(array, function(node) {
var x = {key:node.key, levels: node.levels, downSkips: node.downSkips,
upPtrs: map(node.upPtrs, getIndex), downPtrs: map(node.downPtrs, getIndex),
downSkipWidths: node.downSkipWidths};
return x;
});
return map(processedArray, function (x) { return x.toSource(); }).join("\n");
}*/
2011-07-07 19:59:34 +02:00
function _getNodeByKey(key)
{
return 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.
2011-07-07 19:59:34 +02:00
function _search(entryFunc)
{
2011-03-26 14:10:41 +01:00
var low = start;
2011-07-07 19:59:34 +02:00
var lvl = start.levels - 1;
2011-03-26 14:10:41 +01:00
var lowIndex = -1;
2011-07-07 19:59:34 +02:00
function 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);
}
2011-07-07 19:59:34 +02:00
while (lvl >= 0)
{
2011-03-26 14:10:41 +01:00
var nextLow = low.downPtrs[lvl];
2011-07-07 19:59:34 +02:00
while (!f(nextLow))
{
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;
2011-03-26 14:10:41 +01:00
}
/*
The skip-list contains "entries", JavaScript objects that each must have a unique "key" property
that is a string.
*/
var self = {
2011-07-07 19:59:34 +02:00
length: function()
{
return numNodes;
},
atIndex: function(i)
{
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
2011-07-07 19:59:34 +02:00
splice: function(start, deleteCount, newEntryArray)
{
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);
console.trace();
2011-03-26 14:10:41 +01:00
}
2011-07-07 19:59:34 +02:00
if (!newEntryArray) newEntryArray = [];
2011-03-26 14:10:41 +01:00
var pt = _getPoint(start);
2011-07-07 19:59:34 +02:00
for (var i = 0; i < deleteCount; i++)
{
_deleteKeyAtPoint(pt);
2011-03-26 14:10:41 +01:00
}
2011-07-07 19:59:34 +02:00
for (var i = (newEntryArray.length - 1); i >= 0; i--)
{
var entry = newEntryArray[i];
_insertKeyAtPoint(pt, entry.key, entry);
var node = _getNodeByKey(entry.key);
node.entry = entry;
2011-03-26 14:10:41 +01:00
}
},
2011-07-07 19:59:34 +02:00
next: function(entry)
{
2011-03-26 14:10:41 +01:00
return _getNodeByKey(entry.key).downPtrs[0].entry || null;
},
2011-07-07 19:59:34 +02:00
prev: function(entry)
{
2011-03-26 14:10:41 +01:00
return _getNodeByKey(entry.key).upPtrs[0].entry || null;
},
2011-07-07 19:59:34 +02:00
push: function(entry)
{
2011-03-26 14:10:41 +01:00
self.splice(numNodes, 0, [entry]);
},
2011-07-07 19:59:34 +02:00
slice: function(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;
2011-07-07 19:59:34 +02:00
dmesg(String([start, end, numNodes]));
2011-03-26 14:10:41 +01:00
if (end <= start) return [];
var n = self.atIndex(start);
var array = [n];
2011-07-07 19:59:34 +02:00
for (var i = 1; i < (end - start); i++)
{
n = self.next(n);
array.push(n);
2011-03-26 14:10:41 +01:00
}
return array;
},
2011-07-07 19:59:34 +02:00
atKey: function(key)
{
return _getNodeByKey(key).entry;
},
indexOfKey: function(key)
{
return _getNodeIndex(_getNodeByKey(key));
},
indexOfEntry: function(entry)
{
return self.indexOfKey(entry.key);
},
containsKey: function(key)
{
return !!(_getNodeByKey(key));
},
2011-03-26 14:10:41 +01:00
// gets the last entry starting at or before the offset
2011-07-07 19:59:34 +02:00
atOffset: function(offset)
{
return _getNodeAtOffset(offset).entry;
},
keyAtOffset: function(offset)
{
return self.atOffset(offset).key;
},
offsetOfKey: function(key)
{
return _getNodeIndex(_getNodeByKey(key), true);
},
offsetOfEntry: function(entry)
{
return self.offsetOfKey(entry.key);
},
setEntryWidth: function(entry, width)
{
entry.width = width;
_propagateWidthChange(_getNodeByKey(entry.key));
},
totalWidth: function()
{
return totalWidth;
},
offsetOfIndex: function(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));
},
2011-07-07 19:59:34 +02:00
indexOfOffset: function(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));
},
2011-07-07 19:59:34 +02:00
search: function(entryFunc)
{
2011-03-26 14:10:41 +01:00
return _search(entryFunc);
},
//debugToString: _debugToString,
debugGetPoint: _getPoint,
2011-07-07 19:59:34 +02:00
debugDepth: function()
{
return start.levels;
}
2011-03-26 14:10:41 +01:00
}
return self;
}
exports.newSkipList = newSkipList;