hashmap

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 16, 2023 License: Apache-2.0 Imports: 2 Imported by: 0

README

hashmap

A generic hashmap and hashset for Go.

This is a weekend project. I'm not using it for anything important yet.

The goal of the project is to explore and find a usable interface for generic maps and sets in Go.

Goals:

  1. Allow using slices as keys of a map.
  2. Allow using sets as a keys of a map.
  3. Allow using any type as a key of a map.

Choices:

  1. Equality is defined on element type and not on the container.
  2. Zero values are valid sets and maps.
  3. Sets and maps behave as pointer types.

Documentation

Overview

Package hashmap provides a generic hashmap and hashset implementation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Bool

type Bool bool

Bool is a wrapper around bool that implements the Comparable interface.

func (Bool) Equals

func (b Bool) Equals(other Bool) bool

func (Bool) Hash

func (b Bool) Hash() uint64

type Byte

type Byte = Uint8

Byte is an alias for Uint8.

type Bytes

type Bytes []byte

Bytes is a wrapper around []byte that implements the Comparable interface.

func (Bytes) Equals

func (b Bytes) Equals(other Bytes) bool

func (Bytes) Hash

func (b Bytes) Hash() uint64

type Comparable

type Comparable[T any] interface {
	Hash() uint64
	Equals(T) bool
}

Comparable is an interface that must be implemented by all types that are used as keys in a Map or Set.

type Complex128

type Complex128 complex128

Complex128 is a wrapper around complex128 that implements the Comparable interface.

func (Complex128) Equals

func (c Complex128) Equals(other Complex128) bool

func (Complex128) Hash

func (c Complex128) Hash() uint64

type Complex64

type Complex64 complex64

Complex64 is a wrapper around complex64 that implements the Comparable interface.

func (Complex64) Equals

func (c Complex64) Equals(other Complex64) bool

func (Complex64) Hash

func (c Complex64) Hash() uint64

type Float32

type Float32 float32

Float32 is a wrapper around float32 that implements the Comparable interface. It uses the IEEE 754 binary32 format for hashing and equality. NaN is considered equal to itself.

func (Float32) Equals

func (f Float32) Equals(other Float32) bool

func (Float32) Hash

func (f Float32) Hash() uint64

type Float64

type Float64 float64

Float64 is a wrapper around float64 that implements the Comparable interface. It uses the IEEE 754 binary64 format for hashing and equality. NaN is considered equal to itself.

func (Float64) Equals

func (f Float64) Equals(other Float64) bool

func (Float64) Hash

func (f Float64) Hash() uint64

type Int

type Int int

Int is a wrapper around int that implements the Comparable interface.

func (Int) Equals

func (i Int) Equals(other Int) bool

func (Int) Hash

func (i Int) Hash() uint64

type Int16

type Int16 int16

Int16 is a wrapper around int16 that implements the Comparable interface.

func (Int16) Equals

func (i Int16) Equals(other Int16) bool

func (Int16) Hash

func (i Int16) Hash() uint64

type Int32

type Int32 int32

Int32 is a wrapper around int32 that implements the Comparable interface.

func (Int32) Equals

func (i Int32) Equals(other Int32) bool

func (Int32) Hash

func (i Int32) Hash() uint64

type Int64

type Int64 int64

Int64 is a wrapper around int64 that implements the Comparable interface.

func (Int64) Equals

func (i Int64) Equals(other Int64) bool

func (Int64) Hash

func (i Int64) Hash() uint64

type Int8

type Int8 int8

Int8 is a wrapper around int8 that implements the Comparable interface.

func (Int8) Equals

func (i Int8) Equals(other Int8) bool

func (Int8) Hash

func (i Int8) Hash() uint64

type Map

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

Map is a hash map that uses open addressing with linear probing. It is not thread-safe. The zero value is an empty map ready to use.

func (*Map[K, V]) Copy

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

Copy returns a copy of the map.

func (*Map[K, V]) ForEach

func (m *Map[K, V]) ForEach(f func(K, V) error) error

ForEach calls the given function for each key/value pair in the map.

func (*Map[K, V]) Get

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

Get returns the value associated with the given key. The second return value indicates if the key was found. The value is the zero value for the value type if the key was not found.

func (*Map[K, V]) Put

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

Put adds the given key/value pair to the map. If the key already exists, the value is updated.

func (*Map[K, V]) Remove

func (m *Map[K, V]) Remove(key K)

Remove removes the given key from the map.

func (*Map[K, V]) Size

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

Size returns the number of elements in the map.

type Rune

type Rune = Int32

Rune is an alias for Int32.

type Set

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

Set is a hash set that uses open addressing with linear probing. It is not thread-safe. The zero value is an empty set ready to use. Set implements Comparable, so it can be used as a key in a Map or an element in a Set.

func (*Set[K]) Add

func (s *Set[K]) Add(key K)

Add adds the given key to the set.

func (*Set[K]) Contains

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

Contains returns true if the set contains the given key.

func (*Set[K]) Copy

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

Copy returns a copy of the set.

func (*Set[K]) Difference

func (s *Set[K]) Difference(t *Set[K]) *Set[K]

Difference returns a new set with the elements that are in the set but not in the given set.

func (*Set[K]) Equals

func (s *Set[K]) Equals(t *Set[K]) bool

Equals returns true if the set is equal to the given set.

func (*Set[K]) ForEach

func (s *Set[K]) ForEach(f func(K) error) error

ForEach calls the given function for each key in the set.

func (*Set[K]) Hash

func (s *Set[K]) Hash() uint64

Hash returns the hash code for the set.

func (*Set[K]) Intersection

func (s *Set[K]) Intersection(t *Set[K]) *Set[K]

Intersection returns a new set with the elements that are in both sets.

func (*Set[K]) IsDisjoint

func (s *Set[K]) IsDisjoint(t *Set[K]) bool

IsDisjoint returns true if the intersection of the set and the given set is empty.

func (*Set[K]) IsSubset

func (s *Set[K]) IsSubset(t *Set[K]) bool

IsSubset returns true if the set is a subset of the given set.

func (*Set[K]) Remove

func (s *Set[K]) Remove(key K)

Remove removes the given key from the set.

func (*Set[K]) Size

func (s *Set[K]) Size() int

Size returns the number of elements in the set.

func (*Set[K]) Union

func (s *Set[K]) Union(t *Set[K]) *Set[K]

Union returns a new set with all the elements in both sets.

type Slice

type Slice[T Comparable[T]] []T

Slice is a wrapper around []T that implements the Comparable interface.

func (Slice[T]) Equals

func (s Slice[T]) Equals(other Slice[T]) bool

func (Slice[T]) Hash

func (s Slice[T]) Hash() uint64

type String

type String string

String is a wrapper around string that implements the Comparable interface.

func (String) Equals

func (s String) Equals(other String) bool

func (String) Hash

func (s String) Hash() uint64

type Uint

type Uint uint

Uint is a wrapper around uint that implements the Comparable interface.

func (Uint) Equals

func (u Uint) Equals(other Uint) bool

func (Uint) Hash

func (u Uint) Hash() uint64

type Uint16

type Uint16 uint16

Uint16 is a wrapper around uint16 that implements the Comparable interface.

func (Uint16) Equals

func (u Uint16) Equals(other Uint16) bool

func (Uint16) Hash

func (u Uint16) Hash() uint64

type Uint32

type Uint32 uint32

Uint32 is a wrapper around uint32 that implements the Comparable interface.

func (Uint32) Equals

func (u Uint32) Equals(other Uint32) bool

func (Uint32) Hash

func (u Uint32) Hash() uint64

type Uint64

type Uint64 uint64

Uint64 is a wrapper around uint64 that implements the Comparable interface.

func (Uint64) Equals

func (u Uint64) Equals(other Uint64) bool

func (Uint64) Hash

func (u Uint64) Hash() uint64

type Uint8

type Uint8 uint8

Uint8 is a wrapper around uint8 that implements the Comparable interface.

func (Uint8) Equals

func (u Uint8) Equals(other Uint8) bool

func (Uint8) Hash

func (u Uint8) Hash() uint64

Jump to

Keyboard shortcuts

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