| 1 | About |
| 2 | ===== |
| 3 | |
| 4 | This is a library implementing skip lists for the Go programming |
| 5 | language (http://golang.org/). |
| 6 | |
| 7 | Skip lists are a data structure that can be used in place of |
| 8 | balanced trees. Skip lists use probabilistic balancing rather than |
| 9 | strictly enforced balancing and as a result the algorithms for |
| 10 | insertion and deletion in skip lists are much simpler and |
| 11 | significantly faster than equivalent algorithms for balanced trees. |
| 12 | |
| 13 | Skip lists were first described in |
| 14 | [Pugh, William (June 1990)](ftp://ftp.cs.umd.edu/pub/skipLists/skiplists.pdf). "Skip |
| 15 | lists: a probabilistic alternative to balanced trees". Communications |
| 16 | of the ACM 33 (6): 668–676 |
| 17 | |
| 18 | [](https://travis-ci.org/ryszard/goskiplist) |
| 19 | |
| 20 | Installing |
| 21 | ========== |
| 22 | |
| 23 | $ go get github.com/ryszard/goskiplist/skiplist |
| 24 | |
| 25 | Example |
| 26 | ======= |
| 27 | |
| 28 | ```go |
| 29 | package main |
| 30 | |
| 31 | import ( |
| 32 | "fmt" |
| 33 | "github.com/ryszard/goskiplist/skiplist" |
| 34 | ) |
| 35 | |
| 36 | func main() { |
| 37 | s := skiplist.NewIntMap() |
| 38 | s.Set(7, "seven") |
| 39 | s.Set(1, "one") |
| 40 | s.Set(0, "zero") |
| 41 | s.Set(5, "five") |
| 42 | s.Set(9, "nine") |
| 43 | s.Set(10, "ten") |
| 44 | s.Set(3, "three") |
| 45 | |
| 46 | firstValue, ok := s.Get(0) |
| 47 | if ok { |
| 48 | fmt.Println(firstValue) |
| 49 | } |
| 50 | // prints: |
| 51 | // zero |
| 52 | |
| 53 | s.Delete(7) |
| 54 | |
| 55 | secondValue, ok := s.Get(7) |
| 56 | if ok { |
| 57 | fmt.Println(secondValue) |
| 58 | } |
| 59 | // prints: nothing. |
| 60 | |
| 61 | s.Set(9, "niner") |
| 62 | |
| 63 | // Iterate through all the elements, in order. |
| 64 | unboundIterator := s.Iterator() |
| 65 | for unboundIterator.Next() { |
| 66 | fmt.Printf("%d: %s\n", unboundIterator.Key(), unboundIterator.Value()) |
| 67 | } |
| 68 | // prints: |
| 69 | // 0: zero |
| 70 | // 1: one |
| 71 | // 3: three |
| 72 | // 5: five |
| 73 | // 9: niner |
| 74 | // 10: ten |
| 75 | |
| 76 | for unboundIterator.Previous() { |
| 77 | fmt.Printf("%d: %s\n", unboundIterator.Key(), unboundIterator.Value()) |
| 78 | } |
| 79 | // 9: niner |
| 80 | // 5: five |
| 81 | // 3: three |
| 82 | // 1: one |
| 83 | // 0: zero |
| 84 | |
| 85 | boundIterator := s.Range(3, 10) |
| 86 | // Iterate only through elements in some range. |
| 87 | for boundIterator.Next() { |
| 88 | fmt.Printf("%d: %s\n", boundIterator.Key(), boundIterator.Value()) |
| 89 | } |
| 90 | // prints: |
| 91 | // 3: three |
| 92 | // 5: five |
| 93 | // 9: niner |
| 94 | |
| 95 | for boundIterator.Previous() { |
| 96 | fmt.Printf("%d: %s\n", boundIterator.Key(), boundIterator.Value()) |
| 97 | } |
| 98 | // prints: |
| 99 | // 5: five |
| 100 | // 3: three |
| 101 | |
| 102 | var iterator skiplist.Iterator |
| 103 | |
| 104 | iterator = s.Seek(3) |
| 105 | fmt.Printf("%d: %s\n", iterator.Key(), iterator.Value()) |
| 106 | // prints: |
| 107 | // 3: three |
| 108 | |
| 109 | iterator = s.Seek(2) |
| 110 | fmt.Printf("%d: %s\n", iterator.Key(), iterator.Value()) |
| 111 | // prints: |
| 112 | // 3: three |
| 113 | |
| 114 | iterator = s.SeekToFirst() |
| 115 | fmt.Printf("%d: %s\n", iterator.Key(), iterator.Value()) |
| 116 | // prints: |
| 117 | // 0: zero |
| 118 | |
| 119 | iterator = s.SeekToLast() |
| 120 | fmt.Printf("%d: %s\n", iterator.Key(), iterator.Value()) |
| 121 | // prints: |
| 122 | // 10: ten |
| 123 | |
| 124 | // SkipList can also reduce subsequent forward seeking costs by reusing the |
| 125 | // same iterator: |
| 126 | |
| 127 | iterator = s.Seek(3) |
| 128 | fmt.Printf("%d: %s\n", iterator.Key(), iterator.Value()) |
| 129 | // prints: |
| 130 | // 3: three |
| 131 | |
| 132 | iterator.Seek(5) |
| 133 | fmt.Printf("%d: %s\n", iterator.Key(), iterator.Value()) |
| 134 | // prints: |
| 135 | // 5: five |
| 136 | } |
| 137 | ``` |
| 138 | |
| 139 | Full documentation |
| 140 | ================== |
| 141 | |
| 142 | Read it [online](http://godoc.org/github.com/ryszard/goskiplist/skiplist) or run |
| 143 | |
| 144 | $ go doc github.com/ryszard/goskiplist/skiplist |
| 145 | |
| 146 | Other implementations |
| 147 | ===================== |
| 148 | |
| 149 | This list is probably incomplete. |
| 150 | |
| 151 | |
| 152 | * https://github.com/huandu/skiplist |
| 153 | * https://bitbucket.org/taruti/go-skip/src |
| 154 | * http://code.google.com/p/leveldb-go/source/browse/leveldb/memdb/memdb.go |
| 155 | (part of [leveldb-go](http://code.google.com/p/leveldb-go/)) |