skiplist

package module
v0.0.0-...-2f71ae9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 28, 2014 License: MIT Imports: 3 Imported by: 1

README

Skiplist

Latest Version Build Status Go Documentation

Skiplist implementation in Go. Read more on Skip Lists

Skiplist with various additions and potential variations on the standard list.

Install

go get -u github.com/mtchavez/skiplist

Usage

Initialize a skiplist

package main

func main() {
    list := skiplist.NewList()
}

Insert Nodes

package main

func main() {
    list := skiplist.NewList()
    list.Insert(1, []byte("Node 1"))
    list.Insert(2, []byte("Node 2"))
}

Iterate through nodes

package main

import (
    "fmt"
)

func main() {
    list := skiplist.NewList()
    list.Insert(1, []byte("Node 1"))
    list.Insert(2, []byte("Node 2"))
    list.Insert(3, []byte("Node 3"))

    for i := list.Iterator(); i.Next(); {
        // Print Key
        fmt.Println(i.Key())

        // Print Value
        fmt.Println(i.Val())
    }
}

Documentation

Docs are on GoDoc

Tests

Run tests with coverage

go test --cover

TODO

  • Update to use interface{} for key/value
    • With a compare interface
  • Concurrent skiplist implementation

Documentation

Index

Examples

Constants

View Source
const (
	// ListMaxLevel is the Skiplist can have
	ListMaxLevel = 32
	// ListP is the P value for the SkipList
	ListP = 0.25
)

Variables

This section is empty.

Functions

This section is empty.

Types

type CList

type CList struct {
	sync.RWMutex
	MaxLevel int
	// contains filtered or unexported fields
}

CList is a skip list built for high concurrency

func NewCList

func NewCList() *CList

NewDupeList initializes a new skiplist with max level of 32 or 2^32 elements that allows duplicates

func NewCListWithLevel

func NewCListWithLevel(level int) *CList

NewDupeListWithLevel initializes a new skiplist with a custom max level. Level is defaulted to 32 to allow for 2^32 max elements

func (*CList) Search

func (cl *CList) Search(key int) *CNode

Search will look for a node by the key passed in and return a Node if found otherwise nil

type CNode

type CNode struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

CNode is a node for the concurrent list

type DupeList

type DupeList struct {
	sync.RWMutex
	MaxLevel int
	// contains filtered or unexported fields
}

DupeList implements the SkipList interface but allows for duplicate keys to be inserted

func NewDupeList

func NewDupeList() *DupeList

NewDupeList initializes a new skiplist with max level of 32 or 2^32 elements that allows duplicates

func NewDupeListWithLevel

func NewDupeListWithLevel(level int) *DupeList

NewDupeListWithLevel initializes a new skiplist with a custom max level. Level is defaulted to 32 to allow for 2^32 max elements

func (*DupeList) Delete

func (l *DupeList) Delete(key int) bool

Delete a node by the key provided

func (*DupeList) Insert

func (l *DupeList) Insert(key int, val []byte) *Node

Insert a node into the list given a key and a byte array value

func (*DupeList) Iterator

func (l *DupeList) Iterator() Iterator

Iterator returns an iterable from current list header

func (*DupeList) Search

func (l *DupeList) Search(key int) *Node

Search by key to find the matching node in the list For duplicate keys this will always return the last inserted node for the key

func (*DupeList) SearchKeyVal

func (l *DupeList) SearchKeyVal(key int, val []byte) *Node

SearchKeyVal allows you to search for nodes by the key and value of the node which is useful for nodes with duplicate keys

type Iterator

type Iterator interface {
	Next() (ok bool)
	Prev() (ok bool)
	Val() []byte
	Key() int
}

Iterator interface Implements an iterator to be used to navigate through the skiplist

type List

type List struct {
	sync.RWMutex
	MaxLevel int
	// contains filtered or unexported fields
}

List is a basic skip list implementation with search, delete, and insert functionality and uses a mutex to allow for multi-threaded interaction

func NewList

func NewList() *List

NewList initializes a new skiplist with max level of 32 or 2^32 elements

Example
l := NewList()
fmt.Println("Max level:", l.MaxLevel)
Output:

Max level: 32

func NewListWithLevel

func NewListWithLevel(level int) *List

NewListWithLevel initializes a new skiplist with a custom max level. Level is defaulted to 32 to allow for 2^32 max elements

Example
l := NewListWithLevel(200)
fmt.Println("Max level:", l.MaxLevel)
Output:

Max level: 200

func (*List) Delete

func (l *List) Delete(key int) bool

Delete will delete a node for the provided key will return a true/false if Node was deleted or not

Example
l := NewList()
l.Insert(1, []byte("example 1"))
l.Insert(2, []byte("example 2"))
l.Insert(3, []byte("example 3"))
l.Insert(4, []byte("example 4"))

found := l.Search(4)
fmt.Printf("Searched for 4 and got '%+v'\n", string(found.Value()))

// Delete node
fmt.Printf("Deleted key 4? %+v\n", l.Delete(4))

notFound := l.Search(4)
fmt.Printf("Searched for deleted key of 4 and got %+v\n", notFound)
Output:

Searched for 4 and got 'example 4'
Deleted key 4? true
Searched for deleted key of 4 and got <nil>

func (*List) Insert

func (l *List) Insert(key int, val []byte) *Node

Insert a new node into the skip list providing a integer key and a byte array value. Will return the inserted Node

Example
l := NewList()
l.Insert(1, []byte("example 1"))
l.Insert(2, []byte("example 2"))
l.Insert(3, []byte("example 3"))
l.Insert(4, []byte("example 4"))
fmt.Println("Size:", l.Size())
Output:

Size: 4

func (*List) Iterator

func (l *List) Iterator() Iterator

Iterator returns an iterable from the current head of the skiplist.

func (*List) Search

func (l *List) Search(key int) *Node

Search for a node in the skip list by the key will return a Node if found or nil if not found

Example
l := NewList()
l.Insert(1, []byte("example 1"))
l.Insert(2, []byte("example 2"))
l.Insert(3, []byte("example 3"))
l.Insert(4, []byte("example 4"))

// Not Found
notFound := l.Search(45)
found := l.Search(4)

fmt.Printf("Searched for 45 and got %+v\n", notFound)
fmt.Printf("Searched for 4 and got '%+v'\n", string(found.Value()))
Output:

Searched for 45 and got <nil>
Searched for 4 and got 'example 4'

func (*List) Size

func (l *List) Size() int

Size returns the length of the list

func (*List) Split

func (l *List) Split(splitKey int) *List

Split takes a key to split a list by All values less than the provided key will be in the new list which will be returned

type Node

type Node struct {
	// contains filtered or unexported fields
}

Node for a skiplist which is a linked list node containing a slice of forward referencing nodes, the previous node and the key and value of the node

func NewNode

func NewNode(level, key int, val []byte) *Node

NewNode takes a level used for the forward slice referencing linked nodes as well as the key and value of the node

func (*Node) Value

func (n *Node) Value() []byte

Value returns val of node

type SkipList

type SkipList interface {
	Search(key int) *Node
	Delete(key int) bool
	Insert(key int, val []byte) *Node
	Iterator() Iterator
}

SkipList interface defining the methods needed for a skip list

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL