2020-12-21 23:23:16 +00:00
|
|
|
'use strict';
|
|
|
|
|
2011-12-04 15:33:56 +00:00
|
|
|
/**
|
2019-04-16 00:34:29 +02:00
|
|
|
* This code is mostly from the old Etherpad. Please help us to comment this code.
|
2011-12-04 15:33:56 +00:00
|
|
|
* 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 13:10:41 +00:00
|
|
|
/**
|
|
|
|
* Copyright 2009 Google Inc.
|
2011-07-07 18:59:34 +01:00
|
|
|
*
|
2011-03-26 13:10:41 +00: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 18:59:34 +01:00
|
|
|
*
|
2011-03-26 13:10:41 +00:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2011-07-07 18:59:34 +01:00
|
|
|
*
|
2011-03-26 13:10:41 +00: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.
|
|
|
|
*/
|
|
|
|
|
2024-08-17 20:14:36 +02:00
|
|
|
const _entryWidth = (e: Entry) => (e && e.width) || 0;
|
|
|
|
|
|
|
|
type Entry = {
|
|
|
|
key: string,
|
|
|
|
value?: string
|
|
|
|
width?: number
|
|
|
|
}
|
2012-03-27 11:44:21 +02:00
|
|
|
|
2021-04-11 23:50:42 -04:00
|
|
|
class Node {
|
2024-08-17 20:14:36 +02:00
|
|
|
public key: string|null
|
|
|
|
readonly entry: Entry|null
|
|
|
|
levels: number
|
|
|
|
upPtrs: Node[]
|
|
|
|
downPtrs: Node[]
|
|
|
|
downSkips: number[]
|
|
|
|
readonly downSkipWidths: number[]
|
|
|
|
|
|
|
|
constructor(entry: Entry|null, levels = 0, downSkips: number|null = 1, downSkipWidths:number|null = 0) {
|
2021-04-11 23:50:42 -04:00
|
|
|
this.key = entry != null ? entry.key : null;
|
|
|
|
this.entry = entry;
|
|
|
|
this.levels = levels;
|
|
|
|
this.upPtrs = Array(levels).fill(null);
|
|
|
|
this.downPtrs = Array(levels).fill(null);
|
|
|
|
this.downSkips = Array(levels).fill(downSkips);
|
|
|
|
this.downSkipWidths = Array(levels).fill(downSkipWidths);
|
|
|
|
}
|
2021-04-12 00:24:39 -04:00
|
|
|
|
|
|
|
propagateWidthChange() {
|
|
|
|
const oldWidth = this.downSkipWidths[0];
|
2024-08-17 20:14:36 +02:00
|
|
|
const newWidth = _entryWidth(this.entry!);
|
2021-04-12 00:24:39 -04:00
|
|
|
const widthChange = newWidth - oldWidth;
|
2024-08-17 20:14:36 +02:00
|
|
|
let n: Node = this;
|
2021-04-12 00:24:39 -04:00
|
|
|
let lvl = 0;
|
|
|
|
while (lvl < n.levels) {
|
|
|
|
n.downSkipWidths[lvl] += widthChange;
|
|
|
|
lvl++;
|
|
|
|
while (lvl >= n.levels && n.upPtrs[lvl - 1]) {
|
|
|
|
n = n.upPtrs[lvl - 1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return widthChange;
|
|
|
|
}
|
2021-04-11 23:50:42 -04:00
|
|
|
}
|
|
|
|
|
2021-04-10 15:43:22 -04:00
|
|
|
// A "point" object at index 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.
|
|
|
|
class Point {
|
2024-08-17 20:14:36 +02:00
|
|
|
private skipList: SkipList
|
|
|
|
private readonly loc: number
|
|
|
|
private readonly idxs: number[]
|
|
|
|
private readonly nodes: Node[]
|
|
|
|
private widthSkips: number[]
|
|
|
|
|
|
|
|
constructor(skipList: SkipList, loc: number) {
|
|
|
|
this.skipList = skipList;
|
2021-04-10 15:43:22 -04:00
|
|
|
this.loc = loc;
|
2024-08-17 20:14:36 +02:00
|
|
|
const numLevels = this.skipList.start.levels;
|
2021-04-10 15:43:22 -04:00
|
|
|
let lvl = numLevels - 1;
|
|
|
|
let i = -1;
|
|
|
|
let ws = 0;
|
2024-08-17 20:14:36 +02:00
|
|
|
const nodes: Node[] = new Array(numLevels);
|
|
|
|
const idxs: number[] = new Array(numLevels);
|
|
|
|
const widthSkips: number[] = new Array(numLevels);
|
|
|
|
nodes[lvl] = this.skipList.start;
|
2021-04-10 15:43:22 -04:00
|
|
|
idxs[lvl] = -1;
|
|
|
|
widthSkips[lvl] = 0;
|
|
|
|
while (lvl >= 0) {
|
|
|
|
let n = nodes[lvl];
|
|
|
|
while (n.downPtrs[lvl] && (i + n.downSkips[lvl] < this.loc)) {
|
|
|
|
i += n.downSkips[lvl];
|
|
|
|
ws += n.downSkipWidths[lvl];
|
|
|
|
n = n.downPtrs[lvl];
|
|
|
|
}
|
|
|
|
nodes[lvl] = n;
|
|
|
|
idxs[lvl] = i;
|
|
|
|
widthSkips[lvl] = ws;
|
|
|
|
lvl--;
|
|
|
|
if (lvl >= 0) {
|
|
|
|
nodes[lvl] = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.idxs = idxs;
|
|
|
|
this.nodes = nodes;
|
|
|
|
this.widthSkips = widthSkips;
|
|
|
|
}
|
|
|
|
|
|
|
|
toString() {
|
|
|
|
return `Point(${this.loc})`;
|
|
|
|
}
|
|
|
|
|
2024-08-17 20:14:36 +02:00
|
|
|
insert(entry: Entry) {
|
2021-04-12 01:35:27 -04:00
|
|
|
if (entry.key == null) throw new Error('entry.key must not be null');
|
2024-08-17 20:14:36 +02:00
|
|
|
if (this.skipList.containsKey(entry.key)) {
|
2021-04-12 01:35:27 -04:00
|
|
|
throw new Error(`an entry with key ${entry.key} already exists`);
|
|
|
|
}
|
|
|
|
|
2021-04-11 23:50:42 -04:00
|
|
|
const newNode = new Node(entry);
|
2021-04-11 21:11:30 -04:00
|
|
|
const pNodes = this.nodes;
|
|
|
|
const pIdxs = this.idxs;
|
|
|
|
const pLoc = this.loc;
|
|
|
|
const widthLoc = this.widthSkips[0] + this.nodes[0].downSkipWidths[0];
|
2020-11-23 13:24:19 -05:00
|
|
|
const newWidth = _entryWidth(entry);
|
2019-04-16 00:34:29 +02:00
|
|
|
|
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-21 23:23:16 +00:00
|
|
|
while (newNode.levels === 0 || Math.random() < 0.01) {
|
|
|
|
const lvl = newNode.levels;
|
2011-03-26 13:10:41 +00:00
|
|
|
newNode.levels++;
|
2020-12-21 23:23:16 +00:00
|
|
|
if (lvl === pNodes.length) {
|
2021-04-11 21:11:30 -04:00
|
|
|
// assume we have just passed the end of this.nodes, and reached one level greater
|
2011-07-07 18:59:34 +01:00
|
|
|
// than the skiplist currently supports
|
2024-08-17 20:14:36 +02:00
|
|
|
pNodes[lvl] = this.skipList.start;
|
2011-07-07 18:59:34 +01:00
|
|
|
pIdxs[lvl] = -1;
|
2024-08-17 20:14:36 +02:00
|
|
|
this.skipList.start.levels++;
|
|
|
|
this.skipList.end.levels++;
|
|
|
|
this.skipList.start.downPtrs[lvl] = this.skipList.end;
|
|
|
|
this.skipList.end.upPtrs[lvl] = this.skipList.start;
|
|
|
|
this.skipList.start.downSkips[lvl] = this.skipList.keyToNodeMap.size + 1;
|
|
|
|
this.skipList.start.downSkipWidths[lvl] = this.skipList._totalWidth;
|
2021-04-11 21:11:30 -04:00
|
|
|
this.widthSkips[lvl] = 0;
|
2011-03-26 13:10:41 +00:00
|
|
|
}
|
2020-11-23 13:24:19 -05:00
|
|
|
const me = newNode;
|
2020-12-21 23:23:16 +00:00
|
|
|
const up = pNodes[lvl];
|
2020-11-23 13:24:19 -05:00
|
|
|
const down = up.downPtrs[lvl];
|
|
|
|
const skip1 = pLoc - pIdxs[lvl];
|
|
|
|
const skip2 = up.downSkips[lvl] + 1 - skip1;
|
2011-03-26 13:10:41 +00: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;
|
2021-04-11 21:11:30 -04:00
|
|
|
const widthSkip1 = widthLoc - this.widthSkips[lvl];
|
2020-11-23 13:24:19 -05:00
|
|
|
const widthSkip2 = up.downSkipWidths[lvl] + newWidth - widthSkip1;
|
2011-03-26 13:10:41 +00:00
|
|
|
up.downSkipWidths[lvl] = widthSkip1;
|
|
|
|
me.downSkipWidths[lvl] = widthSkip2;
|
|
|
|
}
|
2020-12-21 23:23:16 +00:00
|
|
|
for (let lvl = newNode.levels; lvl < pNodes.length; lvl++) {
|
|
|
|
const up = pNodes[lvl];
|
2011-03-26 13:10:41 +00:00
|
|
|
up.downSkips[lvl]++;
|
|
|
|
up.downSkipWidths[lvl] += newWidth;
|
|
|
|
}
|
2024-08-17 20:14:36 +02:00
|
|
|
this.skipList.keyToNodeMap.set(newNode.key as string, newNode);
|
|
|
|
this.skipList._totalWidth += newWidth;
|
2021-04-10 15:11:12 -04:00
|
|
|
}
|
2011-07-07 18:59:34 +01:00
|
|
|
|
2021-04-11 21:11:30 -04:00
|
|
|
delete() {
|
|
|
|
const elem = this.nodes[0].downPtrs[0];
|
2024-08-17 20:14:36 +02:00
|
|
|
const elemWidth = _entryWidth(elem.entry!);
|
2021-04-11 21:11:30 -04:00
|
|
|
for (let i = 0; i < this.nodes.length; i++) {
|
2020-11-23 13:24:19 -05:00
|
|
|
if (i < elem.levels) {
|
2020-12-21 23:23:16 +00:00
|
|
|
const up = elem.upPtrs[i];
|
|
|
|
const down = elem.downPtrs[i];
|
2020-11-23 13:24:19 -05:00
|
|
|
const totalSkip = up.downSkips[i] + elem.downSkips[i] - 1;
|
2011-07-07 18:59:34 +01:00
|
|
|
up.downPtrs[i] = down;
|
|
|
|
down.upPtrs[i] = up;
|
|
|
|
up.downSkips[i] = totalSkip;
|
2020-11-23 13:24:19 -05:00
|
|
|
const totalWidthSkip = up.downSkipWidths[i] + elem.downSkipWidths[i] - elemWidth;
|
2011-07-07 18:59:34 +01:00
|
|
|
up.downSkipWidths[i] = totalWidthSkip;
|
2020-11-23 13:24:19 -05:00
|
|
|
} else {
|
2021-04-11 21:11:30 -04:00
|
|
|
const up = this.nodes[i];
|
2011-07-07 18:59:34 +01:00
|
|
|
up.downSkips[i]--;
|
|
|
|
up.downSkipWidths[i] -= elemWidth;
|
2011-03-26 13:10:41 +00:00
|
|
|
}
|
|
|
|
}
|
2024-08-17 20:14:36 +02:00
|
|
|
this.skipList.keyToNodeMap.delete(elem.key as string);
|
|
|
|
this.skipList._totalWidth -= elemWidth;
|
2021-04-11 21:11:30 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
getNode() {
|
|
|
|
return this.nodes[0].downPtrs[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The skip-list contains "entries", JavaScript objects that each must have a unique "key"
|
|
|
|
* property that is a string.
|
|
|
|
*/
|
|
|
|
class SkipList {
|
2024-08-17 20:14:36 +02:00
|
|
|
start: Node
|
|
|
|
end: Node
|
|
|
|
_totalWidth: number
|
|
|
|
keyToNodeMap: Map<string, Node>
|
|
|
|
|
|
|
|
|
2021-04-11 21:11:30 -04:00
|
|
|
constructor() {
|
|
|
|
// if there are N elements in the skiplist, "start" is element -1 and "end" is element N
|
2024-08-17 20:14:36 +02:00
|
|
|
this.start = new Node(null, 1);
|
|
|
|
this.end = new Node(null, 1, null, null);
|
2021-04-11 21:11:30 -04:00
|
|
|
this._totalWidth = 0;
|
2024-08-17 20:14:36 +02:00
|
|
|
this.keyToNodeMap = new Map();
|
|
|
|
this.start.downPtrs[0] = this.end;
|
|
|
|
this.end.upPtrs[0] = this.start;
|
2021-04-11 21:11:30 -04:00
|
|
|
}
|
|
|
|
|
2024-08-17 20:14:36 +02:00
|
|
|
_getNodeAtOffset(targetOffset: number) {
|
2021-04-11 21:11:30 -04:00
|
|
|
let i = 0;
|
2024-08-17 20:14:36 +02:00
|
|
|
let n = this.start;
|
|
|
|
let lvl = this.start.levels - 1;
|
2021-04-11 21:11:30 -04:00
|
|
|
while (lvl >= 0 && n.downPtrs[lvl]) {
|
|
|
|
while (n.downPtrs[lvl] && (i + n.downSkipWidths[lvl] <= targetOffset)) {
|
|
|
|
i += n.downSkipWidths[lvl];
|
|
|
|
n = n.downPtrs[lvl];
|
|
|
|
}
|
|
|
|
lvl--;
|
|
|
|
}
|
2024-08-17 20:14:36 +02:00
|
|
|
if (n === this.start) return (this.start.downPtrs[0] || null);
|
|
|
|
if (n === this.end) {
|
|
|
|
return targetOffset === this._totalWidth ? (this.end.upPtrs[0] || null) : null;
|
2021-04-11 21:11:30 -04:00
|
|
|
}
|
|
|
|
return n;
|
2021-04-10 15:11:12 -04:00
|
|
|
}
|
2011-07-07 18:59:34 +01:00
|
|
|
|
2024-08-17 20:14:36 +02:00
|
|
|
_getNodeIndex(node: Node, byWidth?: boolean) {
|
2020-11-23 13:24:19 -05:00
|
|
|
let dist = (byWidth ? 0 : -1);
|
|
|
|
let n = node;
|
2024-08-17 20:14:36 +02:00
|
|
|
while (n !== this.start) {
|
2020-11-23 13:24:19 -05:00
|
|
|
const lvl = n.levels - 1;
|
2011-03-26 13:10:41 +00:00
|
|
|
n = n.upPtrs[lvl];
|
|
|
|
if (byWidth) dist += n.downSkipWidths[lvl];
|
|
|
|
else dist += n.downSkips[lvl];
|
|
|
|
}
|
|
|
|
return dist;
|
2021-04-10 15:11:12 -04:00
|
|
|
}
|
2011-03-26 13:10:41 +00:00
|
|
|
|
2024-08-17 20:14:36 +02:00
|
|
|
totalWidth() { return this._totalWidth; }
|
|
|
|
|
2011-03-26 13:10:41 +00: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.
|
2024-08-17 20:14:36 +02:00
|
|
|
search(entryFunc: Function) {
|
|
|
|
let low = this.start;
|
|
|
|
let lvl = this.start.levels - 1;
|
2020-11-23 13:24:19 -05:00
|
|
|
let lowIndex = -1;
|
2011-07-07 18:59:34 +01:00
|
|
|
|
2024-08-17 20:14:36 +02:00
|
|
|
const f = (node: Node) => {
|
|
|
|
if (node === this.start) return false;
|
|
|
|
else if (node === this.end) return true;
|
2011-03-26 13:10:41 +00:00
|
|
|
else return entryFunc(node.entry);
|
2020-12-21 23:23:16 +00:00
|
|
|
};
|
|
|
|
|
2020-11-23 13:24:19 -05:00
|
|
|
while (lvl >= 0) {
|
|
|
|
let nextLow = low.downPtrs[lvl];
|
|
|
|
while (!f(nextLow)) {
|
2011-07-07 18:59:34 +01:00
|
|
|
lowIndex += low.downSkips[lvl];
|
|
|
|
low = nextLow;
|
|
|
|
nextLow = low.downPtrs[lvl];
|
2011-03-26 13:10:41 +00:00
|
|
|
}
|
|
|
|
lvl--;
|
|
|
|
}
|
2011-07-07 18:59:34 +01:00
|
|
|
return lowIndex + 1;
|
2021-04-10 15:11:12 -04:00
|
|
|
}
|
2011-03-26 13:10:41 +00:00
|
|
|
|
2024-08-17 20:14:36 +02:00
|
|
|
length() { return this.keyToNodeMap.size; }
|
2011-03-26 13:10:41 +00:00
|
|
|
|
2024-08-17 20:14:36 +02:00
|
|
|
atIndex(i: number) {
|
2021-04-10 15:11:12 -04:00
|
|
|
if (i < 0) console.warn(`atIndex(${i})`);
|
2024-08-17 20:14:36 +02:00
|
|
|
if (i >= this.keyToNodeMap.size) console.warn(`atIndex(${i}>=${this.keyToNodeMap.size})`);
|
2021-04-11 21:11:30 -04:00
|
|
|
return (new Point(this, i)).getNode().entry;
|
2021-04-10 15:11:12 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// differs from Array.splice() in that new elements are in an array, not varargs
|
2024-08-17 20:14:36 +02:00
|
|
|
splice(start: number, deleteCount: number, newEntryArray: Entry[]) {
|
2021-04-10 15:11:12 -04:00
|
|
|
if (start < 0) console.warn(`splice(${start}, ...)`);
|
2024-08-17 20:14:36 +02:00
|
|
|
if (start + deleteCount > this.keyToNodeMap.size) {
|
|
|
|
console.warn(`splice(${start}, ${deleteCount}, ...), N=${this.keyToNodeMap.size}`);
|
|
|
|
console.warn('%s %s %s', typeof start, typeof deleteCount, typeof this.keyToNodeMap.size);
|
2021-04-10 15:11:12 -04:00
|
|
|
console.trace();
|
|
|
|
}
|
2011-03-26 13:10:41 +00:00
|
|
|
|
2021-04-10 15:11:12 -04:00
|
|
|
if (!newEntryArray) newEntryArray = [];
|
2021-04-10 15:43:22 -04:00
|
|
|
const pt = new Point(this, start);
|
2021-04-11 21:11:30 -04:00
|
|
|
for (let i = 0; i < deleteCount; i++) pt.delete();
|
2021-04-10 15:11:12 -04:00
|
|
|
for (let i = (newEntryArray.length - 1); i >= 0; i--) {
|
|
|
|
const entry = newEntryArray[i];
|
2021-04-11 21:11:30 -04:00
|
|
|
pt.insert(entry);
|
2021-04-10 15:11:12 -04:00
|
|
|
}
|
|
|
|
}
|
2011-03-26 13:10:41 +00:00
|
|
|
|
2024-08-17 20:14:36 +02:00
|
|
|
next(entry: Entry) { return this.keyToNodeMap.get(entry.key)!.downPtrs[0].entry || null; }
|
|
|
|
prev(entry: Entry) { return this.keyToNodeMap.get(entry.key)!.upPtrs[0].entry || null; }
|
|
|
|
push(entry: Entry) { this.splice(this.keyToNodeMap.size, 0, [entry]); }
|
2021-04-10 15:11:12 -04:00
|
|
|
|
2024-08-17 20:14:36 +02:00
|
|
|
slice(start: number, end: number) {
|
2021-04-10 15:11:12 -04:00
|
|
|
// act like Array.slice()
|
|
|
|
if (start === undefined) start = 0;
|
2024-08-17 20:14:36 +02:00
|
|
|
else if (start < 0) start += this.keyToNodeMap.size;
|
|
|
|
if (end === undefined) end = this.keyToNodeMap.size;
|
|
|
|
else if (end < 0) end += this.keyToNodeMap.size;
|
2021-04-10 15:11:12 -04:00
|
|
|
|
|
|
|
if (start < 0) start = 0;
|
2024-08-17 20:14:36 +02:00
|
|
|
if (start > this.keyToNodeMap.size) start = this.keyToNodeMap.size;
|
2021-04-10 15:11:12 -04:00
|
|
|
if (end < 0) end = 0;
|
2024-08-17 20:14:36 +02:00
|
|
|
if (end > this.keyToNodeMap.size) end = this.keyToNodeMap.size;
|
2021-04-10 15:11:12 -04:00
|
|
|
|
|
|
|
if (end <= start) return [];
|
|
|
|
let n = this.atIndex(start);
|
|
|
|
const array = [n];
|
|
|
|
for (let i = 1; i < (end - start); i++) {
|
2024-08-17 20:14:36 +02:00
|
|
|
n = this.next(n!);
|
2021-04-10 15:11:12 -04:00
|
|
|
array.push(n);
|
|
|
|
}
|
|
|
|
return array;
|
|
|
|
}
|
|
|
|
|
2024-08-17 20:14:36 +02:00
|
|
|
atKey(key: string) { return this.keyToNodeMap.get(key)!.entry; }
|
|
|
|
indexOfKey(key: string) { return this._getNodeIndex(this.keyToNodeMap.get(key)!); }
|
|
|
|
indexOfEntry(entry: Entry) { return this.indexOfKey(entry.key); }
|
|
|
|
containsKey(key: string) { return this.keyToNodeMap.has(key); }
|
2021-04-10 15:11:12 -04:00
|
|
|
// gets the last entry starting at or before the offset
|
2024-08-17 20:14:36 +02:00
|
|
|
atOffset(offset: number) { return this._getNodeAtOffset(offset)!.entry; }
|
|
|
|
keyAtOffset(offset: number) { return this.atOffset(offset)!.key; }
|
|
|
|
offsetOfKey(key: string) { return this._getNodeIndex(this.keyToNodeMap.get(key)!, true); }
|
|
|
|
offsetOfEntry(entry: Entry) { return this.offsetOfKey(entry.key); }
|
|
|
|
setEntryWidth(entry: Entry, width: number) {
|
2021-04-10 15:11:12 -04:00
|
|
|
entry.width = width;
|
2024-08-17 20:14:36 +02:00
|
|
|
this._totalWidth += this.keyToNodeMap.get(entry.key)!.propagateWidthChange();
|
2021-04-10 15:11:12 -04:00
|
|
|
}
|
2024-08-17 20:14:36 +02:00
|
|
|
offsetOfIndex(i: number) {
|
2021-04-10 15:11:12 -04:00
|
|
|
if (i < 0) return 0;
|
2024-08-17 20:14:36 +02:00
|
|
|
if (i >= this.keyToNodeMap.size) return this._totalWidth;
|
|
|
|
return this.offsetOfEntry(this.atIndex(i)!);
|
2021-04-10 15:11:12 -04:00
|
|
|
}
|
2024-08-17 20:14:36 +02:00
|
|
|
indexOfOffset(offset: number) {
|
2021-04-10 15:11:12 -04:00
|
|
|
if (offset <= 0) return 0;
|
2024-08-17 20:14:36 +02:00
|
|
|
if (offset >= this._totalWidth) return this.keyToNodeMap.size;
|
|
|
|
return this.indexOfEntry(this.atOffset(offset)!);
|
2021-04-10 15:11:12 -04:00
|
|
|
}
|
2011-03-26 13:10:41 +00:00
|
|
|
}
|
2012-01-15 17:23:48 -08:00
|
|
|
|
2024-08-17 20:14:36 +02:00
|
|
|
export default SkipList
|