Details
ResourceFunction["SkipListStructure"] is an implementation of a values-sorted association, which is optimized to have logarithmic
Insert and
KeyDrop times. In terms of functionality and design, this skiplist structure most closely follows
DataStructure (but without optimizations due to compiling code).
An initialization
init can be either an
Association or a
List. If the latter, the keys are taken to be the corresponding
Part indices.
ResourceFunction["SkipListStructure"] accepts the option
"OrderFunction", with default value
Order. User-specified alternatives should behave like built-in
Order by taking an adjacent pair of input values and returning 1 if the values are correctly ordered, -1 if they need to be transposed, or 0 if they are equivalent. An initialization of the structure can be done using this option.
In addition to primary usages above, ResourceFunction["SkipListStructure"] allows a wide variety of auxiliary usages:
SkipListStructure[data, "InsertPosition", value] | searches data and returns before and after indices pointing where to insert value. |
SkipListStructure[data, "Lookup", key,default] | attempts a Lookup, and returns default when key is not set. |
SkipListStructure[data, "Normal", n] | returns a values-sorted Association of only those key-value pairs occurring on the nth level of the data structure. |
SkipListStructure[data, "Last"] | returns the last element of data in the given ordering. |
SkipListStructure[data, "PopLast"] | returns the last element and removes it from data. |
SkipListStructure[data, "FirstLast"] | returns both first and last elements of data. |
SkipListStructure[data, "MemberQ", value] | returns True if data already contains value, and False otherwise. |
SkipListStructure[data, "EmptyQ"] | returns True if data contains no key-value pairs, and False otherwise. |
SkipListStructure[data, "Validate"] | checks local structure and Ordering, returning True for valid data, and False otherwise. |
Desired complexity statistics are achieved by emulating 2,3-Trees with a linked list. In fact, the linked list has both horizontal and vertical links, so it can also be accurately described as a linked array with dimensions (log n)⨯ n.
All valid ResourceFunction["SkipListStructure"] linked arrays are consistent with a constraint on branching ratio per-level. One or two vertices occur between every pair of adjacent horizontal links.
The linked array of a particular ResourceFunction["SkipListStructure"] can be depicted through the "Visualization" method. Five or six different colors are used to distinguish between inequivalent vertex figures:
Color | Vertical? | Next Color |
| Yes | |
| No | ,, or |
| Yes | |
| No | |
| No | ,, or |
| Yes | None |
It is relatively easy to delete a blue or purple vertex, or to insert around a yellow vertex. Deleting a yellow vertex or inserting around a blue or purple vertex sometimes requires non-trivial recursion and heuristic decision making.
The leading depth dimension
log n is predictive of
Insert /
KeyDrop times. This method iterates through levels, making local changes only, until a halting condition is reached when the
ResourceFunction["SkipListStructure"] is proven valid around every vertex.
ResourceFunction["SkipListStructure"] is path dependent with regard to sequences of insertions and deletions. Two
ResourceFunction["SkipListStructure"] objects can fail a
SameQ test, while having the same normal form.