skipstruct
Version:
Set of efficient data structures based on skip lists.
84 lines (83 loc) • 2.39 kB
TypeScript
/**
* Fixed capacity skip list. It doesn't contain the values, it uses numeric keys to maintain the
* order of values (defined by the comparator). Fixed data structure allocates memory in advance
* so it is most suitable for cases where constant volume of data is expected.
*/
export class FixedSkipList {
/**
* @param {number} capacity maximum number of values that can be stored in the list
* @param {number} ratio probability of promoting a value to next layer; defines size ratio between layers; should be more than 0 and less than 1; most commonly used values are 1/2, 1/4, 1/8.
* @param {(a: number, b: number) => -1 | 0 | 1} compare comparator function that receives indices of values
*/
constructor(
capacity: number,
ratio: number,
compare: (a: number, b: number) => -1 | 0 | 1,
);
capacity: number;
ratio: number;
compare: (a: number, b: number) => -1 | 0 | 1;
/** @protected */
protected currentLevel: number;
/**
* @protected
* @type {Uint32Array}
*/
protected heads: Uint32Array;
/**
* @protected
* @type {Uint32Array}
*/
protected tails: Uint32Array;
/**
* @protected
* @type {Uint32Array}
*/
protected sizes: Uint32Array;
/**
* @protected
* @type {() => number}
*/
protected randomLevel: () => number;
/**
* @protected
* @type {Array<Uint32Array>}
*/
protected nexts: Array<Uint32Array>;
get size(): number;
get head(): number;
get tail(): number;
get next(): Uint32Array;
/**
* Whenever a new value added to a collection, insert its index to the skiplist.
*
* ```js
* let slist = new FixedSkipList(1000, 1 / 4, ascending);
* let values = [];
*
* let index = values.push(newValue) - 1;
* slist.insert(index);
* ```
*
* @param {number} index
*/
insert(index: number): void;
/**
* Remove previously inserted index from all layers.
*
* @param {number} index
*/
remove(index: number): void;
/**
* Utilizing layered structure of the skip list, find insertion point for an arbitrary predicate function.
*
* @param {(index: number) => boolean} predicate
*/
bisect(predicate: (index: number) => boolean): number;
/**
* Find first exact point that satisfies match function.
*
* @param {(index: number) => -1 | 0 | 1} match
*/
search(match: (index: number) => -1 | 0 | 1): number;
}