mirror of
https://github.com/ether/etherpad-lite.git
synced 2025-02-01 03:12:42 +01:00
skiplist: Define a new Node
class
This commit is contained in:
parent
9fc88f3601
commit
fc103e7f2a
1 changed files with 15 additions and 25 deletions
|
@ -24,6 +24,18 @@
|
||||||
|
|
||||||
const _entryWidth = (e) => (e && e.width) || 0;
|
const _entryWidth = (e) => (e && e.width) || 0;
|
||||||
|
|
||||||
|
class Node {
|
||||||
|
constructor(entry, levels = 0, downSkips = 1, downSkipWidths = 0) {
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// A "point" object at index x allows modifications immediately after the first x elements of the
|
// 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
|
// 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
|
// is still valid and points to the same index in the skiplist. Other operations with other points
|
||||||
|
@ -67,15 +79,7 @@ class Point {
|
||||||
}
|
}
|
||||||
|
|
||||||
insert(entry) {
|
insert(entry) {
|
||||||
const newNode = {
|
const newNode = new Node(entry);
|
||||||
key: entry.key,
|
|
||||||
entry,
|
|
||||||
levels: 0,
|
|
||||||
upPtrs: [],
|
|
||||||
downPtrs: [],
|
|
||||||
downSkips: [],
|
|
||||||
downSkipWidths: [],
|
|
||||||
};
|
|
||||||
const pNodes = this.nodes;
|
const pNodes = this.nodes;
|
||||||
const pIdxs = this.idxs;
|
const pIdxs = this.idxs;
|
||||||
const pLoc = this.loc;
|
const pLoc = this.loc;
|
||||||
|
@ -162,22 +166,8 @@ class Point {
|
||||||
class SkipList {
|
class SkipList {
|
||||||
constructor() {
|
constructor() {
|
||||||
// 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
|
||||||
this._start = {
|
this._start = new Node(null, 1);
|
||||||
key: null,
|
this._end = new Node(null, 1, null, null);
|
||||||
levels: 1,
|
|
||||||
upPtrs: [null],
|
|
||||||
downPtrs: [null],
|
|
||||||
downSkips: [1],
|
|
||||||
downSkipWidths: [0],
|
|
||||||
};
|
|
||||||
this._end = {
|
|
||||||
key: null,
|
|
||||||
levels: 1,
|
|
||||||
upPtrs: [null],
|
|
||||||
downPtrs: [null],
|
|
||||||
downSkips: [null],
|
|
||||||
downSkipWidths: [null],
|
|
||||||
};
|
|
||||||
this._numNodes = 0;
|
this._numNodes = 0;
|
||||||
this._totalWidth = 0;
|
this._totalWidth = 0;
|
||||||
this._keyToNodeMap = {};
|
this._keyToNodeMap = {};
|
||||||
|
|
Loading…
Reference in a new issue