skipList

package
v0.0.0-...-5389b97 Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2018 License: MIT Imports: 2 Imported by: 0

Documentation

Overview

Package skipList provide an implementation of skip list. But is not thread-safe in concurrency.

Index

Constants

View Source
const (
	MAX_LEVEL   = 32
	PROBABILITY = 0.25
)

Comes from redis's implementation. Also you can see more detail in William Pugh's paper <Skip Lists: A Probabilistic Alternative to Balanced Trees>. The paper is in ftp://ftp.cs.umd.edu/pub/skipLists/skiplists.pdf

Variables

This section is empty.

Functions

func Hash

func Hash(input []byte) uint64

Hash will calculate the input's hash value using xxHash algorithm. It can be used to calculate the index of skip list. See more detail in https://cyan4973.github.io/xxHash/

Types

type SkipList

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

func NewSkipList

func NewSkipList(level int) *SkipList

NewSkipList will create and initialize a skip list with the given level. Level must between 1 to 32. If not, the level will set as 32. To determine the level, you can see the paper ftp://ftp.cs.umd.edu/pub/skipLists/skiplists.pdf. A simple way to determine the level is L(N) = log(1/PROBABILITY)(N). N is the count of the skip list which you can estimate. PROBABILITY is 0.25 in this case. For example, if you expect the skip list contains 10000000 elements, then N = 10000000, L(N) ≈ 12. After initialization, the head field's level equal to level parameter and point to tail field.

func (*SkipList) Delete

func (s *SkipList) Delete(index uint64)

Delete will find the index is existed or not firstly. If existed, delete it, otherwise do nothing.

func (*SkipList) ForEach

func (s *SkipList) ForEach(f func(index uint64, value interface{}) bool)

ForEach will iterate the whole skip list and do the function f for each index and value. Function f will not modify the index and value in skip list. Don't Insert or Delete element in ForEach function.

func (*SkipList) Insert

func (s *SkipList) Insert(index uint64, value interface{})

Insert will insert a node into skip list. If skip has these this index, overwrite the value, otherwise add it.

func (*SkipList) Length

func (s *SkipList) Length() int32

Length will return the length of skip list.

func (*SkipList) Level

func (s *SkipList) Level() int

Level will return the level of skip list.

func (*SkipList) Search

func (s *SkipList) Search(index uint64) interface{}

Search will search the skip list with the given index. If the index exists, return the value, otherwise return nil.

Jump to

Keyboard shortcuts

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