bst

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

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

Go to latest
Published: Mar 30, 2017 License: MIT Imports: 1 Imported by: 1

README

bst set/map

Build Status GoDoc Go Report Card License

Fast and generic Set and Map implementations using binary-search-trees.

Documentation

Full documentation is available on GoDoc

Examples

As a Set:

{
	set := bst.NewSet(5)
	set.Add(bst.Int(3))
	set.Add(bst.Int(5))
	set.Add(bst.Int(1))
	set.Add(bst.Int(3))

	for iter := set.Iterator(); iter.Next(); {
		fmt.Println(iter.Value())
	}
}

As a Map:

{
	set := bst.NewMap(5)
	set.Set(bst.Int(5), "bar")
	set.Set(bst.Int(3), "foo")

	for iter := set.Iterator(); iter.Next(); {
		fmt.Println(iter.Key(), iter.Value())
	}
}

Documentation

Overview

Package bst is a fast and generic implementation of sets/maps using binary-search-trees in Go. Performance is optimised towards reads. Both, inserts and lookups are binary searches and therefore O(log n). Others tructures, such as b-trees and skiplists may be better suited if you are looking for a more balanced performance.

Please see examples for more details on usage.

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Element

type Element interface {
	// Must return true if element is less than the other
	Less(other Element) bool
	// Must return true if element is the equivalent of the other
	Equals(other Element) bool
}

Element represents a value that is stores in the set

type Float64

type Float64 float64

Float64 element type

func (Float64) Equals

func (e Float64) Equals(other Element) bool

func (Float64) Less

func (e Float64) Less(other Element) bool

type Int

type Int int

Int element type

func (Int) Equals

func (e Int) Equals(other Element) bool

func (Int) Less

func (e Int) Less(other Element) bool

type Map

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

Map is an extension of Set and allows to store additional information in the Element. It is NOT thread-safe, please implement your own locking when reading and writing it from multiple goroutines.

Example
package main

import (
	"fmt"

	"github.com/bsm/bst"
)

func main() {
	set := bst.NewMap(5)
	set.Set(bst.Int(5), "bar")
	set.Set(bst.Int(3), "foo")

	value, ok := set.Get(bst.Int(3))
	fmt.Println(value, ok)
	value, ok = set.Get(bst.Int(4))
	fmt.Println(value, ok)
}
Output:
foo true
<nil> false

func NewMap

func NewMap(capacity int) *Map

NewMap creates a map with a given capacity

func (*Map) Add

func (m *Map) Add(key Element, value interface{}) (ok bool)

Add acts like Set, but only adds the key if it does not exist, returns true if successful.

func (*Map) Delete

func (m *Map) Delete(key Element) (ok bool)

Delete removes an item from the map, returns true if successful

func (*Map) Exists

func (m *Map) Exists(key Element) bool

Exists checks the existence

func (*Map) Get

func (m *Map) Get(key Element) (value interface{}, found bool)

Get retrieves a value from the map, returns false as a second argument to indicate that key was not found.

func (*Map) Iterator

func (m *Map) Iterator() *MapIterator

Iterator creates a new iterator

Example
package main

import (
	"fmt"

	"github.com/bsm/bst"
)

func main() {
	set := bst.NewMap(5)
	set.Set(bst.Int(5), "bar")
	set.Set(bst.Int(3), "foo")

	for iter := set.Iterator(); iter.Next(); {
		fmt.Println(iter.Key(), iter.Value())
	}
}
Output:
3 foo
5 bar

func (Map) Len

func (p Map) Len() int

Len returns the size

func (*Map) Set

func (m *Map) Set(key Element, value interface{}) (replaced bool)

Set adds a value on key, returns true if the key was already present and the value was replaced.

type MapIterator

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

MapIterator is used to iterate over a map

func (*MapIterator) Key

func (i *MapIterator) Key() Element

Key returns the current key or nil if invalid

func (*MapIterator) Next

func (i *MapIterator) Next() bool

Next advances the cursor to the next position

func (*MapIterator) Previous

func (i *MapIterator) Previous() bool

Previous advances the cursor to the previous position

func (*MapIterator) Seek

func (i *MapIterator) Seek(pivot Element) bool

Seek places the cursor to the value greater or equal to pivot

func (*MapIterator) Value

func (i *MapIterator) Value() interface{}

Value returns the current key or nil if invalid

type Set

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

Set stores elements as a sorted slice. It is NOT thread-safe, please implement your own locking when reading and writing it from multiple goroutines.

Example
package main

import (
	"fmt"

	"github.com/bsm/bst"
)

func main() {
	set := bst.NewSet(5)
	set.Add(bst.Int(3))
	set.Add(bst.Int(5))
	set.Add(bst.Int(1))
	set.Add(bst.Int(3))

	fmt.Println(set.Len())
}
Output:
3

func NewSet

func NewSet(capacity int) *Set

NewSet creates a set with a given capacity

func (*Set) Add

func (s *Set) Add(v Element) (ok bool)

Add adds a value to the set

Example
package main

import (
	"fmt"

	"github.com/bsm/bst"
)

func main() {
	set := bst.NewSet(5)

	fmt.Println(set.Add(bst.Int(1)))
	fmt.Println(set.Add(bst.Int(2)))
	fmt.Println(set.Add(bst.Int(1)))
}
Output:
true
true
false

func (*Set) Delete

func (s *Set) Delete(v Element) (ok bool)

Delete removes an item from the set, returns true if successful

func (Set) Exists

func (p Set) Exists(v Element) bool

Exists checks for the existence of the element

func (*Set) Intersects

func (s *Set) Intersects(other *Set) bool

Intersects checks if the set is intersectable with other

func (*Set) Iterator

func (s *Set) Iterator() *SetIterator

Iterator creates a new iterator

Example
package main

import (
	"fmt"

	"github.com/bsm/bst"
)

func main() {
	set := bst.NewSet(5)
	set.Add(bst.Int(3))
	set.Add(bst.Int(5))
	set.Add(bst.Int(1))
	set.Add(bst.Int(3))

	for iter := set.Iterator(); iter.Next(); {
		fmt.Println(iter.Value())
	}
}
Output:
1
3
5

func (Set) Len

func (p Set) Len() int

Len returns the size

type SetIterator

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

SetIterator is used to iterate over a set

func (*SetIterator) Next

func (i *SetIterator) Next() bool

Next advances the cursor to the next position

func (*SetIterator) Previous

func (i *SetIterator) Previous() bool

Previous advances the cursor to the previous position

func (*SetIterator) Seek

func (i *SetIterator) Seek(pivot Element) bool

Seek places the cursor to the value greater or equal to pivot

func (*SetIterator) Value

func (i *SetIterator) Value() Element

Value returns the value at the current cursor position or nil if cursor is invalid

type String

type String string

String element type

func (String) Equals

func (e String) Equals(other Element) bool

func (String) Less

func (e String) Less(other Element) bool

Jump to

Keyboard shortcuts

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