rbt

package module
v0.0.0-...-de6e2b4 Latest Latest
Warning

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

Go to latest
Published: Apr 25, 2016 License: MIT Imports: 3 Imported by: 13

README

RbTree

An iterable basic Red Black tree implementation in Golang.

Installation

go get github.com/ocdogan/rbt

Doc

type RbIterator
type RbIterator interface {
    // All iterates on all items of the RbTree
    All() (int, error)
    // Between iterates on the items of the RbTree that the key of the item
    // is less or equal to loKey and greater or equal to hiKey
    Between(loKey RbKey, hiKey RbKey) (int, error)
    // ClearData clears all the data stored on the iterator
    ClearData()
    // Close closes the current iteration, so the iteration stops iterating
    Close()
    // Closed gives the state of the iterator, 'true' if closed
    Closed() bool
    // CurrentCount gives the count of the items that match the iteration case
    CurrentCount() int
    // LessOrEqual iterates on the items of the RbTree that the key of the item
    // is less or equal to the given key
    LessOrEqual(key RbKey) (int, error)
    // LessThan iterates on the items of the RbTree that the key of the item
    // is less than the given key
    LessThan(key RbKey) (int, error)
    // GetData returns the data stored on the iterator with the dataKey
    GetData(dataKey string) (interface{}, bool)
    // GreaterOrEqual iterates on the items of the RbTree that the key of the item
    // is greater or equal to the given key
    GreaterOrEqual(key RbKey) (int, error)
    // GreaterThan iterates on the items of the RbTree that the key of the item
    // is greater than the given key
    GreaterThan(key RbKey) (int, error)
    // RemoveData deletes the data stored on the iterator with the dataKey
    RemoveData(dataKey string)
    // SetData stores the data with the dataKey on the iterator
    SetData(dataKey string, value interface{})
    // Tree returns the RbTree that the iterator is iterating on
    Tree() *RbTree
}

RbIterator interface used for iterating on a RbTree

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

RbTree structure

func NewRbTree
func NewRbTree(onInsert, onDelete KeyValueEvent) *RbTree

NewRbTree creates a new RbTree and returns its address

func (*RbTree) Ceiling
func (tree *RbTree) Ceiling(key RbKey) (RbKey, interface{})

Ceiling returns the smallest key in the tree greater than or equal to key

func (*RbTree) Count
func (tree *RbTree) Count() int

Count returns if count of the nodes stored.

func (*RbTree) Delete
func (tree *RbTree) Delete(key RbKey)

Delete deletes the given key from the tree

func (*RbTree) Floor
func (tree *RbTree) Floor(key RbKey) (RbKey, interface{})

Floor returns the largest key in the tree less than or equal to key

func (*RbTree) Get
func (tree *RbTree) Get(key RbKey) (interface{}, bool)

Get returns the stored value if key found and 'true', otherwise returns 'false' with second return param if key not found

func (*RbTree) Insert
func (tree *RbTree) Insert(key RbKey, value interface{})

Insert inserts the given key and value into the tree

func (*RbTree) IsEmpty
func (tree *RbTree) IsEmpty() bool

IsEmpty returns if the tree has any node.

func (*RbTree) Max
func (tree *RbTree) Max() (RbKey, interface{})

Max returns the largest key in the tree.

func (*RbTree) Min
func (tree *RbTree) Min() (RbKey, interface{})

Min returns the smallest key in the tree.

func (*RbTree) NewRbIterator
func (tree *RbTree) NewRbIterator(callback RbIterationCallback) (RbIterator, error)

NewRbIterator creates a new iterator for the given RbTree

 

Examples

Add, delete, get operations.

package main

import (
    "fmt"
    "runtime"
    "testing"
    "time"

    "github.com/ocdogan/rbt"
)

func main() {
    mem1 := new(runtime.MemStats)
    runtime.ReadMemStats(mem1)

    tree := NewRbTree(nil, nil)
    t1 := time.Now()

    for i := 0; i < 1000000; i++ {
        key := IntKey(i)
        tree.Insert(&key, 10 + i)
    }

    t2 := time.Now()
    fmt.Printf("Insert time: %.5f sec\n", 
        float64(t2.Sub(t1).Nanoseconds())/float64(time.Second.Nanoseconds()))

    for i := 0; i < 1500000; i++ {
        key := IntKey(i)
        tree.Get(&key)
    }

    t3 := time.Now()
    fmt.Printf("Search time: %.5f sec\n",
        float64(t3.Sub(t2).Nanoseconds())/float64(time.Second.Nanoseconds()))

    for i := 1; i < 1000000; i++ {
        key := IntKey(i)
        tree.Delete(&key)
    }

    t4 := time.Now()
    fmt.Printf("Delete time: %.5f sec\n",
        float64(t4.Sub(t3).Nanoseconds())/float64(time.Second.Nanoseconds()))

    mem2 := new(runtime.MemStats)
    runtime.ReadMemStats(mem2)
    fmt.Printf("Mem allocated: %9.3f MB\n",
        float64(mem2.Alloc - mem1.Alloc)/(1024*1024))
}

 

Iterating on the tree.

package main

import (
    "fmt"
    "runtime"
    "testing"
    "time"

    "github.com/ocdogan/rbt"
)

func main() {
    mem1 := new(runtime.MemStats)
    runtime.ReadMemStats(mem1)

    tree := NewRbTree(nil, nil)
    t1 := time.Now()

    // Insert    
    for i := 1; i <= 1000000; i++ {
        key := IntKey(i)
        tree.Insert(&key, 10 + i)
    }

    fmt.Printf("Insert time: %.5f sec\n",
        float64(time.Now().Sub(t1).Nanoseconds())
            /float64(time.Second.Nanoseconds()))

    // Initialize iterator    
    count := 0
    iterator, err := tree.NewRbIterator(func(iterator RbIterator, 
        key RbKey, value interface{}){
        count++
    })

    if err != nil {
        return
    }

    // All    
    t1 = time.Now()
    count = 0
    iterator.All()
    fmt.Printf("All completed in: %.5f sec with count %d\n",
        float64(time.Now().Sub(t1).Nanoseconds())
            /float64(time.Second.Nanoseconds()), count)

    // Between    
    t1 = time.Now()
    count = 0
    loKey, hiKey := IntKey(0), IntKey(2000000)
    iterator.Between(&loKey, &hiKey)
    fmt.Printf("Between completed in: %.5f sec with count %d\n",
        float64(time.Now().Sub(t1).Nanoseconds())
            /float64(time.Second.Nanoseconds()), count)

    // LessThan    
    t1 = time.Now()
    count = 0
    key := IntKey(900001)
    iterator.LessThan(&key)
    fmt.Printf("LessThan completed in: %.5f sec with count %d\n",
        float64(time.Now().Sub(t1).Nanoseconds())
            /float64(time.Second.Nanoseconds()), count)

    // GreaterThan    
    t1 = time.Now()
    count = 0
    key = IntKey(100000)
    iterator.GreaterThan(&key)
    fmt.Printf("GreaterThan completed in: %.5f sec with count %d\n",
        float64(time.Now().Sub(t1).Nanoseconds())
            /float64(time.Second.Nanoseconds()), count)

    // LessOrEqual    
    t1 = time.Now()
    count = 0
    key = IntKey(1000000)
    iterator.LessOrEqual(&key)
    fmt.Printf("LessOrEqual completed in: %.5f sec with count %d\n",
        float64(time.Now().Sub(t1).Nanoseconds())
            /float64(time.Second.Nanoseconds()), count)

    // GreaterOrEqual    
    t1 = time.Now()
    count = 0
    key = IntKey(0)
    iterator.GreaterOrEqual(&key)
    fmt.Printf("GreaterOrEqual completed in: %.5f sec with count %d\n",
        float64(time.Now().Sub(t1).Nanoseconds())
            /float64(time.Second.Nanoseconds()), count)

    mem2 := new(runtime.MemStats)
    runtime.ReadMemStats(mem2)
    fmt.Printf("Mem allocated: %9.3f MB\n", 
        float64(mem2.Alloc -    mem1.Alloc)/(1024*1024))
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrArgumentNil used if the function parameter is nil
	ErrArgumentNil = NewError(ErrNoArgumentNil)
	// ErrEnumeratorModified is used when the tree gets modified while iterating
	ErrEnumeratorModified = NewError(ErrNoEnumeratorModified)
	// ErrIteratorAlreadyRunning used if the iterator is already iterating
	ErrIteratorAlreadyRunning = NewError(ErrNoIteratorAlreadyRunning)
	// ErrIteratorClosed used if the iterator is closed
	ErrIteratorClosed = NewError(ErrNoIteratorClosed)
	// ErrIteratorUninitialized used if the iterator is uninitialized
	ErrIteratorUninitialized = NewError(ErrNoIteratorUninitialized)
)

Functions

func ArgumentNilError

func ArgumentNilError(arg string) error

ArgumentNilError creates a new error with ErrNoArgumentNilWithName error no and named function parameter

func NewError

func NewError(err ErrNo) error

NewError creates a new error with the given error no

func NewErrorDetailed

func NewErrorDetailed(err ErrNo, msg string) error

NewErrorDetailed creates a new error with the given error no and message

Types

type BoolKey

type BoolKey bool

BoolKey is the boolean key for RbKey

func (*BoolKey) ComparedTo

func (bkey *BoolKey) ComparedTo(key RbKey) KeyComparison

ComparedTo compares the given RbKey with its self

type ByteKey

type ByteKey byte

ByteKey is the byte key for RbKey

func (*ByteKey) ComparedTo

func (bkey *ByteKey) ComparedTo(key RbKey) KeyComparison

ComparedTo compares the given RbKey with its self

type DeleteEvent

type DeleteEvent func(key RbKey, oldValue interface{}) (updatedValue interface{})

DeleteEvent function used on Insert or Delete operations

type ErrNo

type ErrNo uintptr

ErrNo struct is used for the error code of the error

const (
	// ErrNoArgumentNil is used if the function parameter is nil
	ErrNoArgumentNil ErrNo = iota + 1
	// ErrNoArgumentNilWithName is used if the named function parameter is nil
	ErrNoArgumentNilWithName
	// ErrNoEnumeratorModified is used when the tree gets modified while iterating
	ErrNoEnumeratorModified
	// ErrNoIteratorAlreadyRunning is used if the iterator is already running
	ErrNoIteratorAlreadyRunning
	// ErrNoIteratorClosed is used if the iterator is closed
	ErrNoIteratorClosed
	// ErrNoIteratorUninitialized is used if the iterator is uninitialized
	ErrNoIteratorUninitialized
)

type Float32Key

type Float32Key float32

Float32Key is the float32 key for RbKey

func (*Float32Key) ComparedTo

func (fkey *Float32Key) ComparedTo(key RbKey) KeyComparison

ComparedTo compares the given RbKey with its self

type Float64Key

type Float64Key float64

Float64Key is the float64 key for RbKey

func (*Float64Key) ComparedTo

func (fkey *Float64Key) ComparedTo(key RbKey) KeyComparison

ComparedTo compares the given RbKey with its self

type InsertEvent

type InsertEvent func(key RbKey, oldValue interface{}, newValue interface{}) (updatedValue interface{})

InsertEvent function used on Insert or Delete operations

type Int8Key

type Int8Key int8

Int8Key is the int8 key for RbKey

func (*Int8Key) ComparedTo

func (ikey *Int8Key) ComparedTo(key RbKey) KeyComparison

ComparedTo compares the given RbKey with its self

type Int16Key

type Int16Key int16

Int16Key is the int16 key for RbKey

func (*Int16Key) ComparedTo

func (ikey *Int16Key) ComparedTo(key RbKey) KeyComparison

ComparedTo compares the given RbKey with its self

type Int32Key

type Int32Key int32

Int32Key is the int32 key for RbKey

func (*Int32Key) ComparedTo

func (ikey *Int32Key) ComparedTo(key RbKey) KeyComparison

ComparedTo compares the given RbKey with its self

type Int64Key

type Int64Key int64

Int64Key is the int64 key for RbKey

func (*Int64Key) ComparedTo

func (ikey *Int64Key) ComparedTo(key RbKey) KeyComparison

ComparedTo compares the given RbKey with its self

type IntKey

type IntKey int

IntKey is the integer key for RbKey

func (*IntKey) ComparedTo

func (ikey *IntKey) ComparedTo(key RbKey) KeyComparison

ComparedTo compares the given RbKey with its self

type KeyComparison

type KeyComparison int8

KeyComparison structure used as result of comparing two keys

const (
	// KeyIsLess is returned as result of key comparison if the first key is less than the second key
	KeyIsLess KeyComparison = iota - 1
	// KeysAreEqual is returned as result of key comparison if the first key is equal to the second key
	KeysAreEqual
	// KeyIsGreater is returned as result of key comparison if the first key is greater than the second key
	KeyIsGreater
)

func (KeyComparison) String

func (tree KeyComparison) String() string

type NilKey

type NilKey struct{}

NilKey is the nil value key for RbKey

func (*NilKey) ComparedTo

func (nkey *NilKey) ComparedTo(key RbKey) KeyComparison

ComparedTo compares the given RbKey with its self

type RbIterationCallback

type RbIterationCallback func(iterator RbIterator, key RbKey, value interface{})

RbIterationCallback is the function used to by the RbIterator with will be called on iteration match

type RbIterator

type RbIterator interface {
	// All iterates on all items of the RbTree
	All() (int, error)
	// Between iterates on the items of the RbTree that the key of the item
	// is less or equal to loKey and greater or equal to hiKey
	Between(loKey RbKey, hiKey RbKey) (int, error)
	// ClearData clears all the data stored on the iterator
	ClearData()
	// Close closes the current iteration, so the iteration stops iterating
	Close()
	// Closed gives the state of the iterator, 'true' if closed
	Closed() bool
	// CurrentCount gives the count of the items that match the iteration case
	CurrentCount() int
	// LessOrEqual iterates on the items of the RbTree that the key of the item
	// is less or equal to the given key
	LessOrEqual(key RbKey) (int, error)
	// LessThan iterates on the items of the RbTree that the key of the item
	// is less than the given key
	LessThan(key RbKey) (int, error)
	// GetData returns the data stored on the iterator with the dataKey
	GetData(dataKey string) (interface{}, bool)
	// GreaterOrEqual iterates on the items of the RbTree that the key of the item
	// is greater or equal to the given key
	GreaterOrEqual(key RbKey) (int, error)
	// GreaterThan iterates on the items of the RbTree that the key of the item
	// is greater than the given key
	GreaterThan(key RbKey) (int, error)
	// RemoveData deletes the data stored on the iterator with the dataKey
	RemoveData(dataKey string)
	// SetData stores the data with the dataKey on the iterator
	SetData(dataKey string, value interface{})
	// Tree returns the RbTree that the iterator is iterating on
	Tree() *RbTree
}

RbIterator interface used for iterating on a RbTree

type RbKey

type RbKey interface {
	ComparedTo(key RbKey) KeyComparison
}

RbKey interface

type RbTree

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

RbTree structure

func NewRbTree

func NewRbTree() *RbTree

NewRbTree creates a new RbTree and returns its address

func NewRbTreeWithEvents

func NewRbTreeWithEvents(onInsert InsertEvent, onDelete DeleteEvent) *RbTree

NewRbTreeWithEvents creates a new RbTree assigning its insert and delete events and returns its address

func (*RbTree) Ceiling

func (tree *RbTree) Ceiling(key RbKey) (RbKey, interface{})

Ceiling returns the smallest key in the tree greater than or equal to key

func (*RbTree) Count

func (tree *RbTree) Count() int

Count returns if count of the nodes stored.

func (*RbTree) Delete

func (tree *RbTree) Delete(key RbKey)

Delete deletes the given key from the tree

func (*RbTree) Exists

func (tree *RbTree) Exists(key RbKey) bool

Exists returns the node if key found, otherwise returns nil

func (*RbTree) Floor

func (tree *RbTree) Floor(key RbKey) (RbKey, interface{})

Floor returns the largest key in the tree less than or equal to key

func (*RbTree) Get

func (tree *RbTree) Get(key RbKey) (interface{}, bool)

Get returns the stored value if key found and 'true', otherwise returns 'false' with second return param if key not found

func (*RbTree) Insert

func (tree *RbTree) Insert(key RbKey, value interface{})

Insert inserts the given key and value into the tree

func (*RbTree) IsEmpty

func (tree *RbTree) IsEmpty() bool

IsEmpty returns if the tree has any node.

func (*RbTree) Max

func (tree *RbTree) Max() (RbKey, interface{})

Max returns the largest key in the tree.

func (*RbTree) Min

func (tree *RbTree) Min() (RbKey, interface{})

Min returns the smallest key in the tree.

func (*RbTree) NewRbIterator

func (tree *RbTree) NewRbIterator(callback RbIterationCallback) (RbIterator, error)

NewRbIterator creates a new iterator for the given RbTree

type StringKey

type StringKey string

StringKey is the string key for RbKey

func (*StringKey) ComparedTo

func (skey *StringKey) ComparedTo(key RbKey) KeyComparison

ComparedTo compares the given RbKey with its self

type Uint8Key

type Uint8Key uint8

Uint8Key is the uint8 key for RbKey

func (*Uint8Key) ComparedTo

func (ikey *Uint8Key) ComparedTo(key RbKey) KeyComparison

ComparedTo compares the given RbKey with its self

type Uint16Key

type Uint16Key uint16

Uint16Key is the uint16 key for RbKey

func (*Uint16Key) ComparedTo

func (ikey *Uint16Key) ComparedTo(key RbKey) KeyComparison

ComparedTo compares the given RbKey with its self

type Uint32Key

type Uint32Key uint32

Uint32Key is the uint32 key for RbKey

func (*Uint32Key) ComparedTo

func (ikey *Uint32Key) ComparedTo(key RbKey) KeyComparison

ComparedTo compares the given RbKey with its self

type Uint64Key

type Uint64Key uint64

Uint64Key is the uint64 key for RbKey

func (*Uint64Key) ComparedTo

func (ikey *Uint64Key) ComparedTo(key RbKey) KeyComparison

ComparedTo compares the given RbKey with its self

type UintKey

type UintKey uint

UintKey is the uint key for RbKey

func (*UintKey) ComparedTo

func (ikey *UintKey) ComparedTo(key RbKey) KeyComparison

ComparedTo compares the given RbKey with its self

Jump to

Keyboard shortcuts

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