hamt

package module
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 28, 2022 License: Unlicense Imports: 0 Imported by: 0

README

hamt

GitHub Action Codecov Go Report Card License

Immutable and Memory Efficient Maps and Sets in Go.

This package hamt provides immutable collection types of maps (associative arrays) and sets implemented as Hash-Array Mapped Tries (HAMTs). All operations of the collections, such as insert and delete, are immutable and create new ones keeping original ones unmodified.

Hash-Array Mapped Trie (HAMT) is a data structure popular as a map (a.k.a. associative array or dictionary) or set. Its immutable variant is adopted widely by functional programming languages like Scala and Clojure to implement immutable and memory-efficient associative arrays and sets.

Installation

go get github.com/raviqqe/hamt

Documentation

GoDoc

Technical notes

The implementation canonicalizes tree structures of HAMTs by eliminating intermediate nodes during delete operations as described in the CHAMP paper.

References

License

The Unlicense

Documentation

Overview

Package hamt provides immutable collection types of maps (associative arrays) and sets implemented as Hash-Array Mapped Tries (HAMTs). All operations of the collections, such as insert and delete, are immutable and create new ones keeping original ones unmodified.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Entry

type Entry[T any] interface {
	Hash() uint32
	Equal(T) bool
}

Entry represents an entry in a collection, which can be compared to values of type T (which is typically also the underlying type of the Entry).

type Map

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

Map represents a map (associative array).

func NewMap

func NewMap[K Entry[K], V any]() Map[K, V]

NewMap creates a new map.

func (Map[K, V]) Delete

func (m Map[K, V]) Delete(k K) Map[K, V]

Delete deletes a pair of a key and a value from a map.

func (Map[K, V]) Find

func (m Map[K, V]) Find(k K) *V

Find finds a value corresponding to a given key from a map. It returns nil if no value is found.

func (Map[K, V]) FirstRest

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

FirstRest returns a key-value pair in a map and a rest of the map. This method is useful for iteration. The key and value would be nil if the map is empty.

func (Map[K, V]) ForEach

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

func (Map[K, V]) Include

func (m Map[K, V]) Include(k K) bool

Include returns true if a key-value pair corresponding with a given key is included in a map, or false otherwise.

func (Map[K, V]) Insert

func (m Map[K, V]) Insert(k K, v V) Map[K, V]

Insert inserts a key-value pair into a map.

func (Map[K, V]) Merge

func (m Map[K, V]) Merge(n Map[K, V]) Map[K, V]

Merge merges 2 maps into one.

func (Map[K, V]) Size

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

Size returns a size of a map.

type Set

type Set[T Entry[T]] struct {
	// contains filtered or unexported fields
}

Set represents a set.

func NewSet

func NewSet[T Entry[T]]() Set[T]

NewSet creates a new set.

func (Set[T]) Delete

func (s Set[T]) Delete(e T) Set[T]

Delete deletes a value from a set.

func (Set[T]) FirstRest

func (s Set[T]) FirstRest() (*T, Set[T])

FirstRest returns a pointer to a value in a set and a rest of the set. This method is useful for iteration.

func (Set[T]) ForEach

func (s Set[T]) ForEach(cb func(T) error) error

func (Set[T]) Include

func (s Set[T]) Include(e T) bool

Include returns true if a given entry is included in a set, or false otherwise.

func (Set[T]) Insert

func (s Set[T]) Insert(e T) Set[T]

Insert inserts a value into a set.

func (Set[T]) Merge

func (s Set[T]) Merge(t Set[T]) Set[T]

Merge merges 2 sets into one.

func (Set[T]) Size

func (s Set[T]) Size() int

Size returns a size of a set.

Jump to

Keyboard shortcuts

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