Package skiplist

import "github.com/byte-mug/golibs/skiplist"

Overview

Skip List implement in Go.

A Skip List is a data structure that allows fast search within an ordered sequence of elements. Fast search is made possible by maintaining a linked hierarchy of subsequences, each skipping over fewer elements.

Index

Constants

const (
	DefaultMaxLevel		int	= 15	//Maximal level allow to create in this skip list
	DefaultPropobility	float32	= 0.25	//Default propobility
)

-

type Skiplist

type Skiplist struct {
	Header	*Skipnode

	// List configuration
	MaxLevel	int
	Propobility	float32

	// List Comparison
	Comparator	utils.Comparator

	// List status
	Level	int	//current level of whole skiplist

	Random	*rand.Rand
}

-

func NewSkipList

func NewSkipList(comparator utils.Comparator) *Skiplist

NewSkipList : Init structure for Skit List.

func (*Skiplist) Delete

func (b *Skiplist) Delete(searchKey interface{}) error

Delete: Delete element by search key

func (*Skiplist) DisplayAll

func (b *Skiplist) DisplayAll()

DisplayAll: Display current SkipList content in console, will also print out the linked pointer.

func (*Skiplist) Insert

func (b *Skiplist) Insert(searchKey interface{}, value interface{})

Insert: Insert a search key and its value which could be interface.

func (*Skiplist) RandomLevel

func (b *Skiplist) RandomLevel() int

-

func (*Skiplist) Search

func (b *Skiplist) Search(searchKey interface{}) (interface{}, error)

Search: Search a element by search key and return the interface{}

func (*Skiplist) SetMaxLevel

func (b *Skiplist) SetMaxLevel(maxLevel int)

Change SkipList default maxlevel is 4.

type Skipnode

type Skipnode struct {
	Key	interface{}
	Val	interface{}
	Forward	[]*Skipnode
	Level	int
	// contains filtered or unexported fields
}

-

func NewHeader

func NewHeader(createLevel int, maxLevel int) *Skipnode

-

func NewNode

func NewNode(searchKey interface{}, value interface{}, createLevel int, maxLevel int) *Skipnode

-

Dependencies