hashmap

package module
v0.0.0-...-1ac30a6 Latest Latest
Warning

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

Go to latest
Published: Nov 30, 2017 License: MIT Imports: 0 Imported by: 0

README

Hashmap

Build Status Coverage Status Go Report Card

A Red-Black Tree Hash Map implementation in Golang that uses user-supplied hashing algorithms.

Usage

The hashmap supports the classic Get, Insert, Remove operations you'd expect.

Inserting
//hash function must take interface{} and return int64
hashFunc := func(i interface{}) int64 {
    return int64(i.(int))
}

m := hashmap.New(hashFunc)
//insertion of key, value. keep in mind the key will be used as input to your hashFunc
m.Insert(4, 0)
//you can store different types
m.Insert(19, "hello")
//panics, because the hashFunc doesn't support string keys
m.Insert("fail", "oh no")
Selecting
hashFunc := func(i interface{}) int64 {
    return int64(i.(int))
}

m := hashmap.New(hashFunc)
m.Insert(4, 0)
m.Insert(19, 10)

//returns value as interface{} and found flag (true if the key was found)
value, found := m.Get(19)
// found will be false
value, found = m.Get(123)
Removing
hashFunc := func(i interface{}) int64 {
    return int64(i.(int))
}

m := hashmap.New(hashFunc)
m.Insert(4, 0)
m.Insert(19, 10)

//returns found flag (true if the key was found)
found := m.Remove(19)
// found will be false
found = m.Remove(123)

Type safety concerns

As this hash map supports keys and values of any type (by type hinting interface{}), there could be concerns of type safety and runtime problems. The suggested way to work around this is to wrap the hash map into type-specific proxy with methods such as Get(key KeyType) (value ValueType, found bool) and do the type assertions there.

Direct support for code generation by this package is still considered but not yet implemented.

TODO

  • implement as thread safe
  • threadsafety: don't lock the whole tree but separate nodes?
  • CI
  • Performance optimizations
  • Performance tests and docs

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type RBTree

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

func New

func New(hashFunc func(i interface{}) uint64) *RBTree

New creates a new hash map with supplied hashing function

func (*RBTree) Get

func (rb *RBTree) Get(key interface{}) (value interface{}, found bool)

func (*RBTree) Insert

func (rb *RBTree) Insert(key, value interface{})

func (*RBTree) Remove

func (rb *RBTree) Remove(key interface{}) (found bool)

Jump to

Keyboard shortcuts

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