circle-ds / Exports / SkipList
Skip lists are a probabilistic data structure that allows for efficient search, insertion, deletion, and access operations. A skip list maintains multiple layers of linked lists, with each layer representing a subset of the elements.
Skip lists use randomness to maintain balance among the levels, with the
p
property controlling the probability of level assignment for new nodes,
and maxLevel
determining the maximum height a skip list node can have.
This structure allows for operations that are efficient on average,
typically achieving logarithmic time complexity for search, insert,
and delete operations.
Name |
---|
T |
-
List
<T
>↳
SkipList
- [iterator]
- at
- clear
- delete
- entries
- fill
- forEach
- has
- keys
- pop
- push
- set
- shift
- slice
- splice
- unshift
- values
• maxLevel: number
The maximum number of levels in the skip list.
• p: number
The probability factor used to randomly determine the levels of new nodes. Should be a value between 0 and 1, where a lower value results in fewer levels on average.
• get
[toStringTag](): string
A string tag for the Collection
class that is used in Object.prototype.toString
.
string
List.[toStringTag]
• get
levels(): number
The current number of levels in the skip list.
number
• get
size(): number
Gets the number of elements contained in the Collection
.
number
List.size
▸ [iterator](): IterableIterator
<T
>
Returns the default iterator through the list's elements.
IterableIterator
<T
>
▸ at(index
): undefined
| T
Retrieves the element at the specified index in the list.
Name | Type | Description |
---|---|---|
index |
number |
The zero-based index of the element to retrieve. |
undefined
| T
The element at the specified index, or undefined
if the index is out of bounds.
▸ clear(): void
Removes all elements from the Collection
, effectively resetting it.
void
▸ delete(index
): boolean
Deletes the element at the specified index. Subsequent elements are shifted one position towards the start of the list.
Name | Type | Description |
---|---|---|
index |
number |
The zero-based index of the element to delete. |
boolean
true
if the element was successfully deleted, false
otherwise.
▸ entries(): IterableIterator
<[number
, T
]>
Returns a new iterator object that contains an array of [key, value]
pairs for each element in the Collection
.
IterableIterator
<[number
, T
]>
An iterable iterator for the entries of the collection.
▸ fill(value
, start?
, end?
): this
Fills the list with the specified value from a start index (inclusive) to an end index (exclusive).
Name | Type | Description |
---|---|---|
value |
T |
The value to fill the list with. |
start? |
number |
The start index. Defaults to 0. |
end? |
number |
The end index. Defaults to the list's length. |
this
The instance of the list for chaining.
▸ forEach(callbackfn
, thisArg?
): void
Executes a provided function once for each key-value pair in the Collection
.
Name | Type | Description |
---|---|---|
callbackfn |
(value : T , key : number , collection : this ) => void |
A function to execute for each element, receiving the value, key, and collection instance as arguments. |
thisArg? |
unknown |
An optional value to use as this when executing callbackfn . |
void
▸ has(value
): boolean
Determines whether a specific element exists within the list.
Name | Type | Description |
---|---|---|
value |
T |
The value to locate. |
boolean
true
if the value exists, false
otherwise.
▸ keys(): IterableIterator
<number
>
Returns a new iterator that contains the keys for each element
in the Collection
.
IterableIterator
<number
>
An iterable iterator for the keys of the collection.
▸ pop(): undefined
| T
Removes and returns the last element of the list.
undefined
| T
The last element of the list, or undefined
if the list is empty.
▸ push(...values
): number
Adds one or more elements to the end of the list and returns the new list length.
Name | Type | Description |
---|---|---|
...values |
T [] |
The elements to add. |
number
The new length of the list.
▸ set(index
, value
): undefined
| T
Replaces the element at the specified index with a new value.
Name | Type | Description |
---|---|---|
index |
number |
The zero-based index of the element to replace. |
value |
T |
The new value to set at the specified index. |
undefined
| T
The previous value at the index if replaced, otherwise undefined
.
▸ shift(): undefined
| T
Removes and returns the first element of the list. Subsequent elements are shifted one position towards the start of the list.
undefined
| T
The first element of the list, or undefined
if the list is empty.
▸ slice(start?
, end?
): List
<T
>
Creates a shallow copy of the list from start index (inclusive) to end index (exclusive) into a new list. Does not modify the existing list.
Name | Type | Description |
---|---|---|
start? |
number |
The start index. Defaults to 0. |
end? |
number |
The end index. Defaults to the list's length. |
List
<T
>
A new list containing the specified elements.
▸ splice(start
, deleteCount?
, ...items
): List
<T
>
Changes the contents of the list by removing existing elements and/or adding new elements at a given index.
Name | Type | Description |
---|---|---|
start |
number |
The zero-based index at which to start changing the list. |
deleteCount? |
number |
The number of elements to remove from the list. Defaults to 0. |
...items |
T [] |
The elements to add to the list beginning at start index. Defaults to []. |
List
<T
>
A new list containing the deleted elements, if any.
▸ unshift(...values
): number
Adds one or more elements to the beginning of the list and returns the new list length.
Name | Type | Description |
---|---|---|
...values |
T [] |
The elements to add to the beginning of the list. |
number
The new length of the list.
▸ values(): IterableIterator
<T
>
Returns a new iterator that contains the values for each element
in the Collection
.
IterableIterator
<T
>
An iterable iterator for the values of the collection.