Documentation
¶
Overview ¶
Package ds provides common interfaces and implementations for various data structures. This file defines the Map interface, which is the basic abstraction for all key-value mapping implementations.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ArrayDeque ¶
type ArrayDeque[T any] = arraydeque.ArrayDeque[T]
ArrayDeque is an alias for arraydeque.ArrayDeque, providing slice-based double-ended queue functionality.
func NewArrayDeque ¶
func NewArrayDeque[T any]() *ArrayDeque[T]
NewArrayDeque creates a new slice-based double-ended queue instance.
type ArrayStack ¶
type ArrayStack[T any] = arraystack.ArrayStack[T]
ArrayStack is an alias for arraystack.ArrayStack, providing slice-based stack functionality.
func NewArrayStack ¶
func NewArrayStack[T any]() *ArrayStack[T]
NewArrayStack creates a new slice-based stack instance.
type BTree ¶
BTree is an alias for btree.BTree, providing ordered data storage functionality.
func NewBTree ¶
NewBTree creates a new B-tree instance. comparator is used to compare element sizes and cannot be nil.
func NewOrderedBTree ¶
NewOrderedBTree creates a new ordered B-tree instance. The element type must implement the cmp.Ordered interface.
type BTreeMap ¶
BTreeMap is a type alias for btreemap.BTreeMap, providing ordered key-value mapping functionality.
func NewBTreeMap ¶
NewBTreeMap creates a new B-tree map instance. comparator is used to compare key sizes, returning negative, zero, or positive to indicate whether the first key is less than, equal to, or greater than the second key.
func NewOrderedBTreeMap ¶
NewOrderedBTreeMap creates a new ordered B-tree map instance. The key type must implement the cmp.Ordered interface.
type BTreeSet ¶
BTreeSet is a type alias for btreeset.BTreeSet, providing ordered set functionality.
func NewBTreeSet ¶
NewBTreeSet creates a new B-tree set instance. comparator is used to compare element sizes, returning negative, zero, or positive to indicate whether the first element is less than, equal to, or greater than the second element.
func NewOrderedBTreeSet ¶
NewOrderedBTreeSet creates a new ordered B-tree set instance. The element type must implement the cmp.Ordered interface.
type DefaultHasher ¶
type DefaultHasher[E comparable] = hashutil.Default[E]
DefaultHasher is a default hasher implementation for comparable types. It uses Go's standard library maphash package for hash calculations.
type HashMap ¶
HashMap is an alias for hashmap.HashMap, providing key-value mapping functionality.
func NewComparableHashMap ¶
func NewComparableHashMap[K comparable, V any]() *HashMap[K, V, hashutil.Default[K]]
NewComparableHashMap creates a new hash map instance with a default hasher for comparable key types.
func NewHashMap ¶
NewHashMap creates a new hash map instance. h is the hasher used to calculate key hash values.
func NewHashMapFromMap ¶
func NewHashMapFromMap[K comparable, V any, M ~map[K]V](m M) *HashMap[K, V, hashutil.Default[K]]
NewHashMapFromMap creates a new hash map instance from an existing map.
type HashSet ¶
HashSet is an alias for hashset.HashSet, providing set functionality.
func NewComparableHashSet ¶
func NewComparableHashSet[T comparable]() *HashSet[T, hashutil.Default[T]]
NewComparableHashSet creates a new hash set instance with a default hasher for comparable key types.
type Hasher ¶
Hasher is an alias for hashutil.Hasher, providing a generic interface for comparing and hashing values of any type.
type LinkedList ¶
type LinkedList[T any] = linkedlist.LinkedList[T]
LinkedList is an alias for linkedlist.LinkedList, providing linked list-based functionality.
func NewLinkedList ¶
func NewLinkedList[T any]() *LinkedList[T]
NewLinkedList creates a new linked list instance.
type Map ¶
type Map[K any, V any] interface { // Get retrieves the value associated with the specified key. // Parameters: // - key: The key to look up // // Return values: // - If the key exists, returns the associated value and true // - If the key does not exist, returns the zero value and false Get(key K) (V, bool) // GetMut retrieves a mutable reference to the value associated with the specified key, allowing in-place modification. // Parameters: // - key: The key to look up // // Return values: // - If the key exists, returns a pointer to the value and true // - If the key does not exist, returns nil and false GetMut(key K) (*V, bool) // GetKeyValue returns the key, value, and an existence flag. // Parameters: // - key: The key to look up // // Return values: // - If the key exists, returns the key, associated value, and true // - If the key does not exist, returns the zero key, zero value, and false GetKeyValue(key K) (K, V, bool) // Insert inserts or updates a key-value pair. // Parameters: // - key: The key to insert or update // - value: The value to associate // // Return values: // - If the key already exists, returns the old value and true // - If the key does not exist, returns the zero value and false Insert(key K, value V) (V, bool) // Remove deletes the key-value pair for the specified key. // Parameters: // - key: The key to delete // // Return values: // - If the key exists, returns the deleted value and true // - If the key does not exist, returns the zero value and false Remove(key K) (V, bool) // ContainsKey checks if the map contains the specified key. // Parameters: // - key: The key to check // // Return value: // - Returns true if the key exists, otherwise false ContainsKey(key K) bool // Len returns the number of key-value pairs in the map. // Return value: // - The number of elements in the map Len() int // IsEmpty checks if the map is empty. // Return value: // - Returns true if the map is empty, otherwise false IsEmpty() bool // Clear removes all key-value pairs from the map, making it empty. Clear() // Extend adds another iterable collection of key-value pairs to the current map. // Parameters: // - iter: Iterator providing key-value pairs // // Behavior: // - For each key-value pair, updates the value if the key already exists, otherwise adds a new key-value pair Extend(iter iter.Seq2[K, V]) // Keys returns an iterator over all keys in the map. // Return value: // - Iterator over keys, of type iter.Seq[K] // // Note: For ordered maps, keys are returned in order; for unordered maps, the order is uncertain. Keys() iter.Seq[K] // Values returns an iterator over all values in the map. // Return value: // - Iterator over values, of type iter.Seq[V] // // Note: For ordered maps, values are returned in key order; for unordered maps, the order is uncertain. Values() iter.Seq[V] // ValuesMut returns an iterator over mutable references to all values in the map. // Return value: // - Iterator over mutable references to values, of type iter.Seq[*V] // // Note: For ordered maps, values are returned in key order; for unordered maps, the order is uncertain. ValuesMut() iter.Seq[*V] // Iter returns an iterator over all key-value pairs in the map. // Return value: // - Iterator over all key-value pairs, of type iter.Seq2[K, V] // // Note: For ordered maps, key-value pairs are returned in key order; for unordered maps, the order is uncertain. Iter() iter.Seq2[K, V] // IterMut returns a mutable iterator over all key-value pairs in the map. // Return value: // - Mutable iterator over all key-value pairs, of type iter.Seq2[K, *V] // // Note: For ordered maps, key-value pairs are returned in key order; for unordered maps, the order is uncertain. IterMut() iter.Seq2[K, *V] }
Map defines a common interface for key-value mappings. All types implementing this interface provide key-to-value mapping functionality, supporting basic operations like addition, deletion, modification, and lookup, as well as iteration.
Type parameters:
- K: Key type
- V: Value type
Example:
// Using a concrete type that implements the Map interface
var m ds.Map[string, int]
m = hashmap.NewHashMap[string, int](hashutil.StrHasher{})
// Using interface methods
m.Insert("apple", 5)
val, found := m.Get("apple")
if found {
fmt.Println(val) // Output: 5
}
// Iterating over the map
for k, v := range m.Iter() {
fmt.Printf("%s: %d\n", k, v)
}
type MapHasher ¶
type MapHasher[E ~map[K]V, K comparable, V any, H Hasher[V]] = hashutil.MapHasher[E, K, V, H]
MapHasher is a hasher implementation for map types. It uses the key type's Hasher and value type's Hasher to calculate the hash for the entire map.
type OrderedMap ¶
type OrderedMap[K any, V any] interface { Map[K, V] // First returns the first (smallest key) key-value pair in the map. // Return values: // - If the map is non-empty, returns the key, value, and true // - If the map is empty, returns zero key, zero value, and false First() (K, V, bool) // Last returns the last (largest key) key-value pair in the map. // Return values: // - If the map is non-empty, returns the key, value, and true // - If the map is empty, returns zero key, zero value, and false Last() (K, V, bool) // PopFirst removes and returns the first (smallest key) key-value pair from the map. // Return values: // - If the map is non-empty, returns the removed key, value, and true // - If the map is empty, returns zero key, zero value, and false PopFirst() (K, V, bool) // PopLast removes and returns the last (largest key) key-value pair from the map. // Return values: // - If the map is non-empty, returns the removed key, value, and true // - If the map is empty, returns zero key, zero value, and false PopLast() (K, V, bool) // Range returns an iterator over key-value pairs where the key is in the [lowerBound, upperBound) range. // Parameters: // - lowerBound: Lower bound of the range (inclusive), nil means no lower bound // - upperBound: Upper bound of the range (exclusive), nil means no upper bound // // Return value: // - Iterator over key-value pairs within the specified range, sorted by key in ascending order Range(lowerBound, upperBound *K) iter.Seq2[K, V] // RangeMut returns a mutable iterator over key-value pairs where the key is in the [lowerBound, upperBound) range. // Parameters: // - lowerBound: Lower bound of the range (inclusive), nil means no lower bound // - upperBound: Upper bound of the range (exclusive), nil means no upper bound // // Return value: // - Mutable iterator over key-value pairs within the specified range, sorted by key in ascending order RangeMut(lowerBound, upperBound *K) iter.Seq2[K, *V] // IterBack returns an iterator over all key-value pairs in the map in reverse order. // Return value: // - Iterator over all key-value pairs in reverse order, of type iter.Seq2[K, V] // // Note: For ordered maps, key-value pairs are returned in reverse key order; for unordered maps, the order is uncertain. IterBack() iter.Seq2[K, V] // IterBackMut returns a mutable iterator over all key-value pairs in the map in reverse order. // Return value: // - Mutable iterator over all key-value pairs in reverse order, of type iter.Seq2[K, *V] // // Note: For ordered maps, key-value pairs are returned in reverse key order; for unordered maps, the order is uncertain. IterBackMut() iter.Seq2[K, *V] // KeysBack returns an iterator over all keys in the map in reverse order. // Return value: // - Iterator over keys in reverse order, of type iter.Seq[K] // // Note: For ordered maps, keys are returned in reverse order; for unordered maps, the order is uncertain. KeysBack() iter.Seq[K] // ValuesBack returns an iterator over all values in the map in reverse order. // Return value: // - Iterator over values in reverse order, of type iter.Seq[V] // // Note: For ordered maps, values are returned in reverse key order; for unordered maps, the order is uncertain. ValuesBack() iter.Seq[V] // ValuesBackMut returns a mutable iterator over all values in the map in reverse order. // Return value: // - Mutable iterator over values in reverse order, of type iter.Seq[*V] // // Note: For ordered maps, values are returned in reverse key order; for unordered maps, the order is uncertain. ValuesBackMut() iter.Seq[*V] }
OrderedMap defines an interface for ordered key-value mappings, extending the basic Map interface. Ordered maps guarantee the iteration order of key-value pairs, typically by key sorting order.
Type parameters:
- K: Key type
- V: Value type
Note: Not all Map implementations support ordered operations.
type PriorityQueue ¶
type PriorityQueue[T any] = priorityqueue.PriorityQueue[T]
PriorityQueue is an alias for priorityqueue.PriorityQueue.
func NewMaxOrderedPriorityQueue ¶
func NewMaxOrderedPriorityQueue[T cmp.Ordered]() *PriorityQueue[T]
NewMaxOrderedPriorityQueue creates a new max-heap priority queue, where the element type must implement the cmp.Ordered interface.
func NewMaxPriorityQueue ¶
func NewMaxPriorityQueue[T any](cmp func(T, T) int) *PriorityQueue[T]
NewMaxPriorityQueue creates a new max-heap priority queue. cmp is a comparison function: returns negative when a < b, 0 when a == b, positive when a > b. The largest element has the highest priority in a max-heap.
func NewMinOrderedPriorityQueue ¶
func NewMinOrderedPriorityQueue[T cmp.Ordered]() *PriorityQueue[T]
NewMinOrderedPriorityQueue creates a new min-heap priority queue, where the element type must implement the cmp.Ordered interface.
func NewMinPriorityQueue ¶
func NewMinPriorityQueue[T any](cmp func(T, T) int) *PriorityQueue[T]
NewMinPriorityQueue creates a new min-heap priority queue. cmp is a comparison function: returns negative when a < b, 0 when a == b, positive when a > b. The largest element has the highest priority in a max-heap.
type SkipMap ¶
SkipMap is a type alias for skipmap.SkipMap, providing skip list-based key-value mapping functionality.
func NewOrderedSkipMap ¶
NewOrderedSkipMap creates a new ordered skip list map instance. The key type must implement the cmp.Ordered interface.
func NewSkipMap ¶
NewSkipMap creates a new skip list map instance. comparator is used to compare key sizes, returning negative, zero, or positive to indicate whether the first key is less than, equal to, or greater than the second key.
type SkipSet ¶
SkipSet is a type alias for skipset.SkipSet, providing skip list-based ordered set functionality.
func NewOrderedSkipSet ¶
NewOrderedSkipSet creates a new ordered skip list set instance. The element type must implement the cmp.Ordered interface.
func NewSkipSet ¶
NewSkipSet creates a new skip list set instance. comparator is used to compare element sizes, returning negative, zero, or positive to indicate whether the first element is less than, equal to, or greater than the second element.
type SliceHasher ¶
type SliceHasher[E ~[]T, T any, H Hasher[T]] = hashutil.SliceHasher[E, T, H]
SliceHasher is a hasher implementation for slice types. It uses the element type's Hasher to calculate the hash for the entire slice.
type TrieMap ¶
TrieMap is an alias for trie.TrieMap, providing trie-based key-value mapping functionality.
func NewOrderedTrieMap ¶
NewOrderedTrieMap creates a new trie map instance for ordered key types. The key type must implement the cmp.Ordered interface.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package arraydeque implements a generic double-ended queue data structure based on a linear slice.
|
Package arraydeque implements a generic double-ended queue data structure based on a linear slice. |
|
Package arraystack implements a generic stack data structure using ArrayDeque as the underlying storage.
|
Package arraystack implements a generic stack data structure using ArrayDeque as the underlying storage. |
|
Package btree implements a generic B-tree data structure.
|
Package btree implements a generic B-tree data structure. |
|
Package btreemap implements an ordered key-value map based on B-trees.
|
Package btreemap implements an ordered key-value map based on B-trees. |
|
Package btreeset implements a B-tree based ordered set data structure.
|
Package btreeset implements a B-tree based ordered set data structure. |
|
Package hashmap implements a generic hash map data structure.
|
Package hashmap implements a generic hash map data structure. |
|
Package hashset implements an unordered set data structure based on hash tables.
|
Package hashset implements an unordered set data structure based on hash tables. |
|
Package hashutil provides generic hashing utilities for hash computation and equality comparison of various types.
|
Package hashutil provides generic hashing utilities for hash computation and equality comparison of various types. |
|
internal
|
|
|
Package linkedlist implements a generic doubly linked list data structure.
|
Package linkedlist implements a generic doubly linked list data structure. |
|
Package priorityqueue implements a generic priority queue data structure.
|
Package priorityqueue implements a generic priority queue data structure. |
|
Package skipmap implements ordered key-value mapping based on skip lists.
|
Package skipmap implements ordered key-value mapping based on skip lists. |
|
Package skipset implements a sorted set based on skip lists.
|
Package skipset implements a sorted set based on skip lists. |
|
Package trie implements a generic trie (prefix tree) data structure.
|
Package trie implements a generic trie (prefix tree) data structure. |