Skip to content

Commit

Permalink
Provide reverse iteration and arbitrary seeking.
Browse files Browse the repository at this point in the history
The ``SkipList`` protocol has been updated to reflect two new
features:

``SkipList.Seek(key interface{}) iterator``

This enables seeking to arbitrary positions in the list, similar to
the behavior found in ``SkipList.Get`` in _O(log n)_ time.

``Iterator.Prev() bool``

Skip List ``node`` elements are now doubly-linked to enable
backtracking to previous elements.
  • Loading branch information
matttproud committed Jan 14, 2013
1 parent 1f6d14d commit 49ac016
Show file tree
Hide file tree
Showing 5 changed files with 360 additions and 63 deletions.
5 changes: 3 additions & 2 deletions AUTHORS
@@ -1,4 +1,4 @@
# This is the official list of GoMock authors for copyright purposes.
# This is the official list of goskiplist authors for copyright purposes.
# This file is distinct from the CONTRIBUTORS files.
# See the latter for an explanation.

Expand All @@ -8,4 +8,5 @@

# Please keep the list sorted.

Google Inc.
Google Inc.
SoundCloud, Ltd.
3 changes: 2 additions & 1 deletion CONTRIBUTORS
Expand Up @@ -31,4 +31,5 @@

# Please keep the list sorted.

Ric Szopa (Ryszard) <ryszard.szopa@gmail.com>
Matt T. Proud (mtp) <matt.proud@gmail.com>
Ric Szopa (Ryszard) <ryszard.szopa@gmail.com>
80 changes: 54 additions & 26 deletions README.markdown
Expand Up @@ -19,16 +19,16 @@ Installing
==========

$ go get github.com/ryszard/goskiplist/skiplist

Example
=======

```go
package main

import (
"github.com/ryszard/goskiplist/skiplist"
"fmt"
"github.com/ryszard/goskiplist/skiplist"
)

func main() {
Expand All @@ -41,52 +41,80 @@ func main() {
s.Set(10, "ten")
s.Set(3, "three")

value, ok := s.Get(0)
firstValue, ok := s.Get(0)
if ok {
fmt.Println(value)
fmt.Println(firstValue)
}
// prints:
// zero

// prints:
// zero

s.Delete(7)

value, ok := s.Get(7)
secondValue, ok := s.Get(7)
if ok {
fmt.Println(value)
fmt.Println(secondValue)
}
// prints: nothing.

s.Set(9, "niner")

// Iterate through all the elements, in order.
for i := s.Iterator(); i.Next(); {
fmt.Printf("%d: %s\n", i.Key(), i.Value())
unboundIterator := s.Iterator()
for unboundIterator.Next() {
fmt.Printf("%d: %s\n", unboundIterator.Key(), unboundIterator.Value())
}
// prints:
// 0: zero
// 1: one
// 3: three
// 5: five
// 9: niner
// 10: ten

for unboundIterator.Previous() {
fmt.Printf("%d: %s\n", unboundIterator.Key(), unboundIterator.Value())
}
// prints:
// 0: zero
// 1: one
// 3: three
// 5: five
// 9: niner
// 10: ten
// 9: niner
// 5: five
// 3: three
// 1: one
// 0: zero

boundIterator := s.Range(3, 10)
// Iterate only through elements in some range.
for i := s.Range(3, 10); i.Next(); {
fmt.Printf("%d: %s\n", i.Key(), i.Value())
for boundIterator.Next() {
fmt.Printf("%d: %s\n", boundIterator.Key(), boundIterator.Value())
}
// prints:
// 3: three
// 5: five
// 9: niner
// prints:
// 3: three
// 5: five
// 9: niner

for boundIterator.Previous() {
fmt.Printf("%d: %s\n", boundIterator.Key(), boundIterator.Value())
}
// prints:
// 5: five
// 3: three

var iterator skiplist.Iterator

iterator = s.Seek(3)
fmt.Printf("%d: %s\n", iterator.Key(), iterator.Value())
// prints:
// 3: three

iterator = s.Seek(2)
fmt.Printf("%d: %s\n", iterator.Key(), iterator.Value())
// prints:
// 3: three
}
```

Full documentation
==================

Read it [online](http://go.pkgdoc.org/github.com/ryszard/goskiplist/skiplist) or run
Read it [online](http://go.pkgdoc.org/github.com/ryszard/goskiplist/skiplist) or run

$ go doc github.com/ryszard/goskiplist/skiplist

Expand All @@ -98,5 +126,5 @@ This list is probably incomplete.

* https://github.com/huandu/skiplist
* https://bitbucket.org/taruti/go-skip/src
* http://code.google.com/p/leveldb-go/source/browse/leveldb/memdb/memdb.go
* http://code.google.com/p/leveldb-go/source/browse/leveldb/memdb/memdb.go
(part of [leveldb-go](http://code.google.com/p/leveldb-go/))

0 comments on commit 49ac016

Please sign in to comment.