hashmap

package module
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Mar 17, 2022 License: ISC Imports: 2 Imported by: 4

README

hashmap

GoDoc

An efficient hashmap implementation in Go.

Features

Using

To start using this package, install Go and run:

$ go get github.com/tidwall/hashmap

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, and Scan.

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, and Scan.

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

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]) 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]) 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.

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]) Delete added in v1.4.1

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

Delete an item

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

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

Insert an item

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