hashmap

package module
v1.8.1 Latest Latest
Warning

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

Go to latest
Published: Mar 31, 2023 License: ISC Imports: 2 Imported by: 4

README

hashmap

GoDoc

An efficient hashmap implementation in Go.

Features

For ordered key-value data, check out the tidwall/btree package.

Getting Started

Installing

To start using hashmap, install Go and run go get:

go get github.com/tidwall/hashmap

This will retrieve the library.

Usage

The Map type works similar to a standard Go map, and includes the methods: Set, Get, Delete, Len, Scan, Keys, Values, and Copy.

var m hashmap.Map[string, string]
m.Set("Hello", "Dolly!")
val, _ := m.Get("Hello")
fmt.Printf("%v\n", val)
val, _ = m.Delete("Hello")
fmt.Printf("%v\n", val)
val, _ = m.Get("Hello")
fmt.Printf("%v\n", val)

// Output:
// Dolly!
// Dolly!
//

The Set type is like Map but only for keys. It includes the methods: Insert, Contains, Delete, Len, Scan and Keys.

var m hashmap.Set[string]
m.Insert("Andy")
m.Insert("Kate")
m.Insert("Janet")

fmt.Printf("%v\n", m.Contains("Kate"))
fmt.Printf("%v\n", m.Contains("Bob"))
fmt.Printf("%v\n", m.Contains("Andy"))

// Output:
// true
// false
// true

Performance

See BENCH.md for more info.

Contact

Josh Baker @tidwall

License

Source code is available under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Map

type Map[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Map is a hashmap. Like map[string]interface{}

func New

func New[K comparable, V any](cap int) *Map[K, V]

New returns a new Map. Like map[string]interface{}

func (*Map[K, V]) Copy added in v1.6.0

func (m *Map[K, V]) Copy() *Map[K, V]

Copy the hashmap.

func (*Map[K, V]) Delete

func (m *Map[K, V]) Delete(key K) (prev V, deleted bool)

Delete deletes a value for a key. Returns the deleted value, or false when no value was assigned.

func (*Map[K, V]) Get

func (m *Map[K, V]) Get(key K) (value V, ok bool)

Get returns a value for a key. Returns false when no value has been assign for key.

func (*Map[K, V]) GetPos added in v1.1.0

func (m *Map[K, V]) GetPos(pos uint64) (key K, value V, ok bool)

GetPos gets a single keys/value nearby a position. The pos param can be any valid uint64. Useful for grabbing a random item from the map.

func (*Map[K, V]) Keys added in v1.5.0

func (m *Map[K, V]) Keys() []K

Keys returns all keys as a slice

func (*Map[K, V]) Len

func (m *Map[K, V]) Len() int

Len returns the number of values in map.

func (*Map[K, V]) Scan added in v1.4.1

func (m *Map[K, V]) Scan(iter func(key K, value V) bool)

Scan iterates over all key/values. It's not safe to call or Set or Delete while scanning.

func (*Map[K, V]) Set

func (m *Map[K, V]) Set(key K, value V) (V, bool)

Set assigns a value to a key. Returns the previous value, or false when no value was assigned.

func (*Map[K, V]) Values added in v1.5.0

func (m *Map[K, V]) Values() []V

Values returns all values as a slice

type Set added in v1.4.1

type Set[K comparable] struct {
	// contains filtered or unexported fields
}

func (*Set[K]) Contains added in v1.4.1

func (tr *Set[K]) Contains(key K) bool

Get a value for key

func (*Set[K]) Copy added in v1.6.0

func (tr *Set[K]) Copy() *Set[K]

Copy the set. This is a copy-on-write operation and is very fast because it only performs a shadow copy.

func (*Set[K]) Delete added in v1.4.1

func (tr *Set[K]) Delete(key K)

Delete an item

func (*Set[K]) GetPos added in v1.8.0

func (s *Set[K]) GetPos(pos uint64) (key K, ok bool)

GetPos gets a single keys/value nearby a position. The pos param can be any valid uint64. Useful for grabbing a random item from the Set.

func (*Set[K]) Insert added in v1.4.1

func (tr *Set[K]) Insert(key K)

Insert an item

func (*Set[K]) Keys added in v1.5.0

func (tr *Set[K]) Keys() []K

Keys returns all keys as a slice

func (*Set[K]) Len added in v1.4.1

func (tr *Set[K]) Len() int

Len returns the number of items in the tree

func (*Set[K]) Scan added in v1.4.1

func (tr *Set[K]) Scan(iter func(key K) bool)

Jump to

Keyboard shortcuts

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