Documentation ¶
Index ¶
- func Contains[K comparable, T comparable](e T, values map[K]T) *T
- func ContainsPredicate[K comparable, T any](predicate func(k K, v *T) bool, values map[K]T) *T
- func Copy[K comparable, T any](m1 map[K]T) map[K]T
- func Equals[K comparable, T comparable](m1 map[K]T, m2 map[K]T) bool
- func EqualsP[K comparable, T any](m1 map[K]T, m2 map[K]T, equals func(t1 T, t2 T) bool) bool
- func GetOrDef[K comparable, V any](m map[K]V, key K, def V) V
- func IfPresent[K comparable, V any](m map[K]V, key K, action func(value V))
- func Keys[K comparable, T any](m map[K]T) []K
- func Merge[K comparable, T any](src map[K]T, add map[K]T) map[K]T
- func Values[K comparable, T any](m map[K]T) []T
- type HashMultiMap
- func (m *HashMultiMap[K, V]) Append(key K, values ...V) int
- func (m *HashMultiMap[K, V]) Clear(key K) bool
- func (m *HashMultiMap[K, V]) Get(key K) ([]V, bool)
- func (m *HashMultiMap[K, V]) Iterator() iter.Seq2[K, []V]
- func (m *HashMultiMap[K, V]) Remove(key K, predicate func(v V) bool) int
- func (m *HashMultiMap[K, V]) Set(key K, values ...V) int
- type MultiMap
- type ReflectiveMultiMap
- func (m *ReflectiveMultiMap[K, V]) Append(key K, values ...V) int
- func (m *ReflectiveMultiMap[K, V]) Clear(key K) bool
- func (m *ReflectiveMultiMap[K, V]) Get(key K) ([]V, bool)
- func (m *ReflectiveMultiMap[K, V]) Iterator() iter.Seq2[K, []V]
- func (m *ReflectiveMultiMap[K, V]) Remove(key K, predicate func(v V) bool) int
- func (m *ReflectiveMultiMap[K, V]) Set(key K, values ...V) int
- type UniqueHashMultiMap
- func (m *UniqueHashMultiMap[K, V]) Append(key K, values ...V) int
- func (m *UniqueHashMultiMap[K, V]) Clear(key K) bool
- func (m *UniqueHashMultiMap[K, V]) Get(key K) (value []V, ok bool)
- func (m *UniqueHashMultiMap[K, V]) Iterator() iter.Seq2[K, []V]
- func (m *UniqueHashMultiMap[K, V]) Remove(key K, predicate func(v V) bool) int
- func (m *UniqueHashMultiMap[K, V]) Set(key K, values ...V) int
- type UniqueMultiMap
- func (m *UniqueMultiMap[K, V]) Append(key K, values ...V) int
- func (m *UniqueMultiMap[K, V]) Clear(key K) bool
- func (m *UniqueMultiMap[K, V]) Get(key K) ([]V, bool)
- func (m *UniqueMultiMap[K, V]) Iterator() iter.Seq2[K, []V]
- func (m *UniqueMultiMap[K, V]) Remove(key K, predicate func(v V) bool) int
- func (m *UniqueMultiMap[K, V]) Set(key K, values ...V) int
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Contains ¶
func Contains[K comparable, T comparable](e T, values map[K]T) *T
Contains checks if map contains specified element. Returns value if found, nil otherwise.
func ContainsPredicate ¶
func ContainsPredicate[K comparable, T any](predicate func(k K, v *T) bool, values map[K]T) *T
ContainsPredicate checks if map contains specified struct element matching a predicate. Returns value if found, nil otherwise.
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 ¶ added in v1.21.1
func GetOrDef[K comparable, V any](m map[K]V, key K, def V) V
GetOrDef retrieves the value associated with the specified key from the map `m`. If the key is not present, it returns the provided `defaultValue`.
This function simplifies accessing map values by providing a default value when a key is absent, reducing the need for explicit checks in your code.
Type Parameters:
- K: The type of the keys in the map. It must be a comparable type.
- V: The type of the values in the map.
Parameters:
- m map[K]V: The map from which to retrieve the value.
- key K: The key whose associated value is to be returned.
- defaultValue V: The value to return if the key is not present in the map.
Returns:
- V: The value associated with the key, or `defaultValue` if the key is not found.
Example Usage:
package main import ( "fmt" "your_module/ucast" ) func main() { m := map[string]int{ "apple": 1, "banana": 2, } value := ucast.GetOrDef(m, "orange", 0) fmt.Println("Value:", value) // Output: Value: 0 }
func IfPresent ¶ added in v1.21.1
func IfPresent[K comparable, V any](m map[K]V, key K, action func(value V))
IfPresent checks if the specified key exists in the map `m`. If the key is present, it executes the provided `action` function with the associated value.
This function helps avoid boilerplate code when working with maps, especially when you need to perform an action only if a key is present.
Type Parameters:
- K: The type of the keys in the map. It must be a comparable type.
- V: The type of the values in the map.
Parameters:
- m map[K]V: The map to check for the presence of the key.
- key K: The key to look for in the map.
- action func(value V): The function to execute if the key is present. It receives the value associated with the key.
Example Usage:
package main import ( "fmt" "your_module/ucast" ) func main() { m := map[string]int{ "apple": 1, "banana": 2, } ucast.IfPresent(m, "apple", func(value int) { fmt.Println("Value:", value) }) // Output: // Value: 1 }
func Keys ¶
func Keys[K comparable, T any](m 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](m map[K]T) []T
Types ¶
type HashMultiMap ¶ added in v1.17.4
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 ¶ added in v1.17.4
func NewHashMultiMap[K comparable, V any]() *HashMultiMap[K, V]
func (*HashMultiMap[K, V]) Append ¶ added in v1.17.4
func (m *HashMultiMap[K, V]) Append(key K, values ...V) int
func (*HashMultiMap[K, V]) Clear ¶ added in v1.17.4
func (m *HashMultiMap[K, V]) Clear(key K) bool
func (*HashMultiMap[K, V]) Get ¶ added in v1.17.4
func (m *HashMultiMap[K, V]) Get(key K) ([]V, bool)
func (*HashMultiMap[K, V]) Iterator ¶ added in v1.20.1
func (m *HashMultiMap[K, V]) Iterator() iter.Seq2[K, []V]
func (*HashMultiMap[K, V]) Remove ¶ added in v1.17.4
func (m *HashMultiMap[K, V]) Remove(key K, predicate func(v V) bool) int
func (*HashMultiMap[K, V]) Set ¶ added in v1.17.4
func (m *HashMultiMap[K, V]) Set(key K, values ...V) int
type MultiMap ¶ added in v1.14.0
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 ¶ added in v1.14.0
type ReflectiveMultiMap[K comparable, V any] struct { // contains filtered or unexported fields }
ReflectiveMultiMap is a concurrent-safe data structure that implements a multi-value map. It associates keys of type K with multiple values of type V. and values under the same key // can have duplicates if they produce the same hash code. The structure employs a hash.Hash instance specified during creation to handle hash calculations for efficient storage and retrieval. This implementation ensures thread-safety using a sync.Mutex for concurrent access.
Usage: ReflectiveMultiMap is beneficial in scenarios where keys need to map to multiple values, and concurrent access safety is essential. It provides methods to get, set, append, remove, and clear values associated with keys. The implementation handles potential hash collisions and allows efficient storage and retrieval of values under keys.
!IMPORTANT: This map is not safe for concurrent operations. Use ConcurrentMultiMap for concurrent operations. This implementation does not preserve the order of insertion, and values under the same key can have duplicates if they produce the same hash code. It is optimized for concurrent access and efficient storage/retrieval operations, suitable for scenarios requiring a multi-value map with concurrency support.
func NewReflectiveMultiMap ¶ added in v1.14.0
func NewReflectiveMultiMap[K comparable, V any]() *ReflectiveMultiMap[K, V]
func (*ReflectiveMultiMap[K, V]) Append ¶ added in v1.14.0
func (m *ReflectiveMultiMap[K, V]) Append(key K, values ...V) int
func (*ReflectiveMultiMap[K, V]) Clear ¶ added in v1.14.0
func (m *ReflectiveMultiMap[K, V]) Clear(key K) bool
func (*ReflectiveMultiMap[K, V]) Get ¶ added in v1.14.0
func (m *ReflectiveMultiMap[K, V]) Get(key K) ([]V, bool)
func (*ReflectiveMultiMap[K, V]) Iterator ¶ added in v1.20.1
func (m *ReflectiveMultiMap[K, V]) Iterator() iter.Seq2[K, []V]
func (*ReflectiveMultiMap[K, V]) Remove ¶ added in v1.14.0
func (m *ReflectiveMultiMap[K, V]) Remove(key K, predicate func(v V) bool) int
func (*ReflectiveMultiMap[K, V]) Set ¶ added in v1.14.0
func (m *ReflectiveMultiMap[K, V]) Set(key K, values ...V) int
type UniqueHashMultiMap ¶ added in v1.14.1
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 ¶ added in v1.14.1
func NewUniqueHashMultiMap[K comparable, V comparable](hashMethod hash.Hash) *UniqueHashMultiMap[K, V]
func (*UniqueHashMultiMap[K, V]) Append ¶ added in v1.14.1
func (m *UniqueHashMultiMap[K, V]) Append(key K, values ...V) int
func (*UniqueHashMultiMap[K, V]) Clear ¶ added in v1.14.1
func (m *UniqueHashMultiMap[K, V]) Clear(key K) bool
func (*UniqueHashMultiMap[K, V]) Get ¶ added in v1.14.1
func (m *UniqueHashMultiMap[K, V]) Get(key K) (value []V, ok bool)
func (*UniqueHashMultiMap[K, V]) Iterator ¶ added in v1.20.1
func (m *UniqueHashMultiMap[K, V]) Iterator() iter.Seq2[K, []V]
func (*UniqueHashMultiMap[K, V]) Remove ¶ added in v1.14.1
func (m *UniqueHashMultiMap[K, V]) Remove(key K, predicate func(v V) bool) int
func (*UniqueHashMultiMap[K, V]) Set ¶ added in v1.14.1
func (m *UniqueHashMultiMap[K, V]) Set(key K, values ...V) int
type UniqueMultiMap ¶ added in v1.14.1
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 ¶ added in v1.14.1
func NewUniqueMultiMap[K comparable, V comparable]() *UniqueMultiMap[K, V]
func (*UniqueMultiMap[K, V]) Append ¶ added in v1.14.1
func (m *UniqueMultiMap[K, V]) Append(key K, values ...V) int
func (*UniqueMultiMap[K, V]) Clear ¶ added in v1.14.1
func (m *UniqueMultiMap[K, V]) Clear(key K) bool
func (*UniqueMultiMap[K, V]) Get ¶ added in v1.14.1
func (m *UniqueMultiMap[K, V]) Get(key K) ([]V, bool)
func (*UniqueMultiMap[K, V]) Iterator ¶ added in v1.20.1
func (m *UniqueMultiMap[K, V]) Iterator() iter.Seq2[K, []V]
func (*UniqueMultiMap[K, V]) Remove ¶ added in v1.14.1
func (m *UniqueMultiMap[K, V]) Remove(key K, predicate func(v V) bool) int
func (*UniqueMultiMap[K, V]) Set ¶ added in v1.14.1
func (m *UniqueMultiMap[K, V]) Set(key K, values ...V) int