umap

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Contains

func Contains[K comparable, T comparable](values map[K]T, e T) *T

Contains checks if map contains specified element. Returns value if found, nil otherwise.

func ContainsPredicate

func ContainsPredicate[K comparable, T any](values map[K]T, predicate func(k K, v *T) bool) *T

ContainsPredicate checks if map contains specified struct element matching a predicate. Returns value if found, nil otherwise.

func Copy

func Copy[K comparable, T any](values map[K]T) map[K]T

Copy returns a copy of a map.

func Equals

func Equals[K comparable, T comparable](m1 map[K]T, m2 map[K]T) bool

Equals returns true if maps are equal. Map order is ignored.

func EqualsP

func EqualsP[K comparable, T any](m1 map[K]T, m2 map[K]T, equals func(t1 T, t2 T) bool) bool

EqualsP returns true if maps are equal. Map order is ignored. Values are compared using predicate.

func GetOrDef

func GetOrDef[K comparable, V any](values map[K]V, key K, def V) V

GetOrDef returns m[key] if present, otherwise def.

func IfPresent

func IfPresent[K comparable, V any](values map[K]V, key K, action func(value V))

IfPresent executes action(value) if key exists in the map.

func Keys

func Keys[K comparable, T any](values map[K]T) []K

func Merge

func Merge[K comparable, T any](src map[K]T, add map[K]T) map[K]T

Merge merges two maps and returns the result. Existing keys from src map will be used.

func Values

func Values[K comparable, T any](values map[K]T) []T

Types

type HashMultiMap

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

HashMultiMap is a generic data structure that implements a multi-value map, where each key K can be associated with multiple values of type V.

This implementation allows duplicate values under the same key, as values are stored in a slice. This approach supports storing multiple identical items under the same key.

Usage: This map is particularly useful in scenarios where it is necessary to maintain a collection of items (including duplicates) for each key. It is optimized for scenarios where quick lookup, insertion, and deletion of items are required.

!IMPORTANT: This map is not safe for concurrent operations. Use ConcurrentMultiMap for concurrent operations. This map does not preserve the order of insertion and allows storing multiple identical items under the same key.

func NewHashMultiMap

func NewHashMultiMap[K comparable, V any]() *HashMultiMap[K, V]

func (*HashMultiMap[K, V]) Append

func (m *HashMultiMap[K, V]) Append(key K, values ...V) int

func (*HashMultiMap[K, V]) Clear

func (m *HashMultiMap[K, V]) Clear(key K) bool

func (*HashMultiMap[K, V]) Get

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

func (*HashMultiMap[K, V]) Iterator

func (m *HashMultiMap[K, V]) Iterator() iter.Seq2[K, []V]

func (*HashMultiMap[K, V]) Remove

func (m *HashMultiMap[K, V]) Remove(key K, predicate func(v V) bool) int

func (*HashMultiMap[K, V]) Set

func (m *HashMultiMap[K, V]) Set(key K, values ...V) int

type MultiMap

type MultiMap[K, V any] interface {
	// Get retrieves values associated with the key.
	// Returns a slice of values and a boolean indicating whether the key exists.
	// The slice may contain duplicate values.
	Get(key K) (value []V, ok bool)

	// Set replaces all values associated with the key with the provided values.
	// Returns the count of values that were already associated with the key and have been replaced.
	// This method clears any existing values and inserts the new ones, allowing duplicates among the new values.
	Set(key K, value ...V) int

	// Append adds values to the list of values associated with the key.
	// Returns the total count of values that match the appended ones, counting all occurrences including duplicates.
	// This method does not replace existing values but adds new ones, maintaining any existing duplicates.
	Append(key K, value ...V) int

	// Remove deletes values that match the predicate from the key's associated values.
	// Returns the number of deletions made, accounting for each instance of a value that matches the predicate.
	// Each matching value is checked and removed individually, including each duplicate.
	Remove(key K, predicate func(v V) bool) int

	// Clear removes all values associated with the key.
	// Returns true if there were any values associated with the key before clearing; otherwise, returns false.
	// This method effectively deletes all entries for the key, regardless of their count or duplication status.
	Clear(key K) bool

	// Iterator returns an iterator over all items
	Iterator() iter.Seq2[K, []V]
}

MultiMap defines an interface for a generic multimap. It allows multiple values, including duplicates, to be associated with a single key.

type ReflectiveMultiMap

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

ReflectiveMultiMap is a multi-value map: key K -> []V. !IMPORTANT: This map is NOT safe for concurrent use. Wrap it with your own sync if needed.

func NewReflectiveMultiMap

func NewReflectiveMultiMap[K comparable, V comparable]() *ReflectiveMultiMap[K, V]

func (*ReflectiveMultiMap[K, V]) Append

func (m *ReflectiveMultiMap[K, V]) Append(key K, values ...V) int

Append adds values to an existing key. It returns the number of duplicates encountered (counted against already stored values under that key).

func (*ReflectiveMultiMap[K, V]) Clear

func (m *ReflectiveMultiMap[K, V]) Clear(key K) bool

func (*ReflectiveMultiMap[K, V]) Get

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

func (*ReflectiveMultiMap[K, V]) Iterator

func (m *ReflectiveMultiMap[K, V]) Iterator() iter.Seq2[K, []V]

Iterator returns an iterator over all (key, []V-bucket) pairs. A single logical key K may be yielded multiple times if it has multiple buckets (i.e. multiple distinct V "hash keys").

func (*ReflectiveMultiMap[K, V]) Remove

func (m *ReflectiveMultiMap[K, V]) Remove(key K, predicate func(v V) bool) int

Remove deletes values under key that match predicate. It returns the number of removed values.

func (*ReflectiveMultiMap[K, V]) Set

func (m *ReflectiveMultiMap[K, V]) Set(key K, values ...V) int

Set replaces all values for a key. It returns the number of previously stored values that are equal to any of the provided values.

type UniqueHashMultiMap

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

UniqueHashMultiMap is the same as UniqueMultiMap, but allows to specify custom hash function.

func NewUniqueHashMultiMap

func NewUniqueHashMultiMap[K comparable, V comparable](hashMethod hash.Hash) *UniqueHashMultiMap[K, V]

func (*UniqueHashMultiMap[K, V]) Append

func (m *UniqueHashMultiMap[K, V]) Append(key K, values ...V) int

func (*UniqueHashMultiMap[K, V]) Clear

func (m *UniqueHashMultiMap[K, V]) Clear(key K) bool

func (*UniqueHashMultiMap[K, V]) Get

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

func (*UniqueHashMultiMap[K, V]) Iterator

func (m *UniqueHashMultiMap[K, V]) Iterator() iter.Seq2[K, []V]

func (*UniqueHashMultiMap[K, V]) Remove

func (m *UniqueHashMultiMap[K, V]) Remove(key K, predicate func(v V) bool) int

func (*UniqueHashMultiMap[K, V]) Set

func (m *UniqueHashMultiMap[K, V]) Set(key K, values ...V) int

type UniqueMultiMap

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

UniqueMultiMap is a generic data structure that implements a multi-value map, where each key K can be associated with multiple unique values of type V.

This implementation ensures that there are no duplicate values under the same key, as values are stored in a secondary map where the hash code of each value serves as the key, and the value itself as the map value. This approach effectively prevents storing duplicate values under the same key.

Usage: This map is particularly useful in scenarios where it is necessary to maintain a collection of unique items (like a set) for each key, but unlike a traditional set, it organizes these items by their hash codes to speed up checks for duplicates, insertion, and deletion.

!IMPORTANT: This map is not safe for concurrent operations. Use ConcurrentMultiMap for concurrent operations. This map does not preserve the order of insertion and cannot store multiple identical items under the same key. It is optimized for scenarios where quick lookup, insertion, and deletion of items are required, and where each item can be uniquely identified by a hash code.

func NewUniqueMultiMap

func NewUniqueMultiMap[K comparable, V comparable]() *UniqueMultiMap[K, V]

func (*UniqueMultiMap[K, V]) Append

func (m *UniqueMultiMap[K, V]) Append(key K, values ...V) int

func (*UniqueMultiMap[K, V]) Clear

func (m *UniqueMultiMap[K, V]) Clear(key K) bool

func (*UniqueMultiMap[K, V]) Get

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

func (*UniqueMultiMap[K, V]) Iterator

func (m *UniqueMultiMap[K, V]) Iterator() iter.Seq2[K, []V]

func (*UniqueMultiMap[K, V]) Remove

func (m *UniqueMultiMap[K, V]) Remove(key K, predicate func(v V) bool) int

func (*UniqueMultiMap[K, V]) Set

func (m *UniqueMultiMap[K, V]) Set(key K, values ...V) int

Jump to

Keyboard shortcuts

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