From 53e8124d28dca5c492cacb68201bc7e1efa5dd39 Mon Sep 17 00:00:00 2001 From: Tyler Perkins Date: Mon, 26 Dec 2022 19:24:02 -0500 Subject: [PATCH] Add skip lists --- Skip lists.html | 63 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 Skip lists.html diff --git a/Skip lists.html b/Skip lists.html new file mode 100644 index 0000000..61186d3 --- /dev/null +++ b/Skip lists.html @@ -0,0 +1,63 @@ +
+
+ +

Skiplists are possibly one of the most useful data structures that I don't +see talked about all that often. They are a special type of list that is +probabilistic in nature, and allows for O(log n) indexing and searching, and +O(1) insertion when provided an iterator. They are always sorted, and therefore +have great ordered traversal. Its insane just how fast these things are.

+ +

The general concept starts with a normal linked list, usually singly linked +lists. From there we make it so that each node in the list has pointers to more +than just the next element. With this we can skip forward through the last much +faster. By randomly placing pointers forward when traversing normally, we can +build up a well distributed set of pointers, allowing us to skip forward large +amounts at first, then slowing getting more precise as we find the element we +want.

+ +
+
+
+ +
Diagram of a skiplist, from Wikipedia +
+
+
+
+
+
+ +

Complexities

+ +
Average case
+ +
    +
  • Search - log n
  • +
  • Insert - log n
  • +
  • Delete - log n
  • +
  • Index - log n (indexable skiplist implementation)
  • +
  • Space usage - n log n
  • +
+ +
Use cases
+ +

The use case that lead me to find this was implementing a cache for often +repeated malloc() operations. I have a use case where textures need to be +allocated with strings on those textures. The set of string will repeat itself +often, however the strings to be shown are somewhat unpredictable. Therefore, I +am keeping a hash table of pointers to the allocated textures, and a skip list +associating the age of the entry with the string on the texture. Using a +skiplist allows for log n lookup and removal of the entries so we can keep +accurate track of what entries are how old. Due to its fast access of its +elements, and having O(1) access time to the head element, there is a slight +performance advantage to using this versus a Heap.

+ +
Implementations
+ +

I am using this +implementation right now to benchmark this cache system in context, then I +plan to build my own skip list. I hope this helps in your own projects!

+ +
+