Documentation
¶
Overview ¶
Package multimap provides multimaps keyed by Key objects: maps in which a key holds a set of values instead of a single one.
Implementations ¶
- Ordered is an adaptive radix tree. Range queries touch only the keys in range, and every iteration visits keys in ascending order.
- Hashed is a Go map. Point operations on long string keys are faster than in Ordered, but every range query scans all keys, and iteration order is unspecified. Like every Go map it does not shrink its memory after deletions.
Both keep the values of a key in a compact container: up to three values inline, then a plain array, then a hash set.
Concurrency ¶
Ordered and Hashed are not synchronized. Any number of goroutines may read concurrently, but a write must not run concurrently with any other access; callers provide that exclusion themselves. Synchronized wraps any MultiMap with a sync.RWMutex, so that all methods are safe for concurrent use. New returns a synchronized Ordered multimap.
Sets and iterators ¶
Every read method comes in two forms. The plain form returns a *set3.Set3 or a []Key that is an independent copy: it costs an allocation, but the caller may keep and modify it, and on a synchronized multimap it holds no lock once returned. The Seq form returns an iterator (iter.Seq) over the stored data without copying. It yields the values of each key in turn, so a value stored under several keys is yielded once per key, whereas the set forms hold it once. The loop body must not modify the multimap it iterates over; what the loop then yields is unspecified. On a synchronized multimap, a Seq iterator holds the read lock for the whole loop: the loop body must not call any method of the same multimap (that can deadlock), and a long loop delays writers. Use the set forms when the loop body needs the multimap or runs long.
Key order ¶
Keys are compared byte-wise lexicographically, see Key.LessThan. Range queries and ordering follow that comparison. Mixing key encodings (for example UTF-8 strings and encoded integers) may order keys in ways that differ from numeric or locale-aware expectations.
Example (BasicUsage) ¶
mm := New[int]()
// Use FromString to obtain normalized keys from user strings
mm.AddValue(FromString("Alice"), 1)
mm.AddValue(FromString("Bob"), 2)
fmt.Println(mm.NumberOfKeys())
Output: 2
Example (Iterators) ¶
mm := NewOrdered[string]() // unsynchronized; iteration in ascending key order
mm.AddValue(FromString("b"), "second")
mm.AddValue(FromString("a"), "first")
mm.AddValue(FromString("c"), "third")
for v := range mm.ValuesFromInclusiveSeq(FromString("b")) {
fmt.Println(v)
}
Output: second third
Example (RangeQuery) ¶
mm := New[int]()
mm.AddValue(FromString("a"), 1)
mm.AddValue(FromString("b"), 2)
mm.AddValue(FromString("c"), 3)
set := mm.ValuesBetweenInclusive(FromString("a"), FromString("b"))
// set is a *set3.Set3[int]; for documentation we print whether it contains 1 and 2
fmt.Println(set.Equals(set3.From(1, 2)))
Output: true
Index ¶
- type Hashed
- func (m *Hashed[T]) AddValue(key Key, value T)
- func (m *Hashed[T]) AllKeys() []Key
- func (m *Hashed[T]) AllKeysSeq() iter.Seq[Key]
- func (m *Hashed[T]) AllValues() *set3.Set3[T]
- func (m *Hashed[T]) AllValuesSeq() iter.Seq[T]
- func (m *Hashed[T]) Clear()
- func (m *Hashed[T]) ContainsKey(key Key) bool
- func (m *Hashed[T]) NumberOfKeys() uint64
- func (m *Hashed[T]) RemoveKey(key Key)
- func (m *Hashed[T]) RemoveValue(key Key, value T)
- func (m *Hashed[T]) ValuesBetweenExclusive(from, to Key) *set3.Set3[T]
- func (m *Hashed[T]) ValuesBetweenExclusiveSeq(from, to Key) iter.Seq[T]
- func (m *Hashed[T]) ValuesBetweenInclusive(from, to Key) *set3.Set3[T]
- func (m *Hashed[T]) ValuesBetweenInclusiveSeq(from, to Key) iter.Seq[T]
- func (m *Hashed[T]) ValuesFor(key Key) *set3.Set3[T]
- func (m *Hashed[T]) ValuesForSeq(key Key) iter.Seq[T]
- func (m *Hashed[T]) ValuesFromExclusive(from Key) *set3.Set3[T]
- func (m *Hashed[T]) ValuesFromExclusiveSeq(from Key) iter.Seq[T]
- func (m *Hashed[T]) ValuesFromInclusive(from Key) *set3.Set3[T]
- func (m *Hashed[T]) ValuesFromInclusiveSeq(from Key) iter.Seq[T]
- func (m *Hashed[T]) ValuesToExclusive(to Key) *set3.Set3[T]
- func (m *Hashed[T]) ValuesToExclusiveSeq(to Key) iter.Seq[T]
- func (m *Hashed[T]) ValuesToInclusive(to Key) *set3.Set3[T]
- func (m *Hashed[T]) ValuesToInclusiveSeq(to Key) iter.Seq[T]
- type Key
- func FromByte(b byte) Key
- func FromBytes(b []byte) Key
- func FromInt(i int) Key
- func FromInt8(i int8) Key
- func FromInt16(i int16) Key
- func FromInt32(i int32) Key
- func FromInt64(i int64) Key
- func FromRune(r rune) Key
- func FromString(s string) Key
- func FromUint(u uint) Key
- func FromUint8(u uint8) Key
- func FromUint16(u uint16) Key
- func FromUint32(u uint32) Key
- func FromUint64(u uint64) Key
- type MultiMap
- type Ordered
- func (m *Ordered[T]) AddValue(key Key, value T)
- func (m *Ordered[T]) AllKeys() []Key
- func (m *Ordered[T]) AllKeysSeq() iter.Seq[Key]
- func (m *Ordered[T]) AllValues() *set3.Set3[T]
- func (m *Ordered[T]) AllValuesSeq() iter.Seq[T]
- func (m *Ordered[T]) Clear()
- func (m *Ordered[T]) ContainsKey(key Key) bool
- func (m *Ordered[T]) NumberOfKeys() uint64
- func (m *Ordered[T]) RemoveKey(key Key)
- func (m *Ordered[T]) RemoveValue(key Key, value T)
- func (m *Ordered[T]) ValuesBetweenExclusive(from, to Key) *set3.Set3[T]
- func (m *Ordered[T]) ValuesBetweenExclusiveSeq(from, to Key) iter.Seq[T]
- func (m *Ordered[T]) ValuesBetweenInclusive(from, to Key) *set3.Set3[T]
- func (m *Ordered[T]) ValuesBetweenInclusiveSeq(from, to Key) iter.Seq[T]
- func (m *Ordered[T]) ValuesFor(key Key) *set3.Set3[T]
- func (m *Ordered[T]) ValuesForSeq(key Key) iter.Seq[T]
- func (m *Ordered[T]) ValuesFromExclusive(from Key) *set3.Set3[T]
- func (m *Ordered[T]) ValuesFromExclusiveSeq(from Key) iter.Seq[T]
- func (m *Ordered[T]) ValuesFromInclusive(from Key) *set3.Set3[T]
- func (m *Ordered[T]) ValuesFromInclusiveSeq(from Key) iter.Seq[T]
- func (m *Ordered[T]) ValuesToExclusive(to Key) *set3.Set3[T]
- func (m *Ordered[T]) ValuesToExclusiveSeq(to Key) iter.Seq[T]
- func (m *Ordered[T]) ValuesToInclusive(to Key) *set3.Set3[T]
- func (m *Ordered[T]) ValuesToInclusiveSeq(to Key) iter.Seq[T]
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Hashed ¶
type Hashed[T comparable] struct { // contains filtered or unexported fields }
Hashed is a multimap backed by a Go map. Point operations cost one hash lookup, which beats Ordered on long string keys. Every range query scans all keys, though, and iteration order is unspecified. Like every Go map it keeps its memory after deletions. Use it when range queries are rare.
Hashed is not synchronized: concurrent reads are safe, but a write must not run concurrently with any other access. Wrap it with Synchronized for concurrent use.
func NewHashed ¶
func NewHashed[T comparable]() *Hashed[T]
NewHashed returns an empty, unsynchronized Hashed multimap. Use it directly when access is already serialized, or wrap it with Synchronized.
func (*Hashed[T]) AllKeysSeq ¶
AllKeysSeq iterates over all keys in unspecified order. The yielded Keys must not be retained after the loop step (clone them to keep them).
func (*Hashed[T]) AllValuesSeq ¶
AllValuesSeq iterates over all values, key by key in unspecified order.
func (*Hashed[T]) ContainsKey ¶
ContainsKey reports whether key is present.
func (*Hashed[T]) NumberOfKeys ¶
NumberOfKeys returns the number of keys.
func (*Hashed[T]) RemoveValue ¶
RemoveValue removes value from the set at key, and the key once its set is empty.
func (*Hashed[T]) ValuesBetweenExclusive ¶
ValuesBetweenExclusive returns a copy of the values of all keys in (from, to).
func (*Hashed[T]) ValuesBetweenExclusiveSeq ¶
ValuesBetweenExclusiveSeq iterates over the values of all keys in (from, to).
func (*Hashed[T]) ValuesBetweenInclusive ¶
ValuesBetweenInclusive returns a copy of the values of all keys in [from, to].
func (*Hashed[T]) ValuesBetweenInclusiveSeq ¶
ValuesBetweenInclusiveSeq iterates over the values of all keys in [from, to].
func (*Hashed[T]) ValuesFor ¶
ValuesFor returns a copy of the values of key; empty if key is absent.
func (*Hashed[T]) ValuesForSeq ¶
ValuesForSeq iterates over the values of key.
func (*Hashed[T]) ValuesFromExclusive ¶
ValuesFromExclusive returns a copy of the values of all keys > from.
func (*Hashed[T]) ValuesFromExclusiveSeq ¶
ValuesFromExclusiveSeq iterates over the values of all keys > from.
func (*Hashed[T]) ValuesFromInclusive ¶
ValuesFromInclusive returns a copy of the values of all keys >= from.
func (*Hashed[T]) ValuesFromInclusiveSeq ¶
ValuesFromInclusiveSeq iterates over the values of all keys >= from.
func (*Hashed[T]) ValuesToExclusive ¶
ValuesToExclusive returns a copy of the values of all keys < to.
func (*Hashed[T]) ValuesToExclusiveSeq ¶
ValuesToExclusiveSeq iterates over the values of all keys < to.
func (*Hashed[T]) ValuesToInclusive ¶
ValuesToInclusive returns a copy of the values of all keys <= to.
type Key ¶
type Key []byte
Key is an alias for a byte slice used as a map key representation. Use the provided constructors to build Keys from primitive types or normalized strings.
Integer encoding policy ----------------------- All integer constructors produce an 8-byte big-endian representation (most-significant byte first). To ensure consistent, order-preserving comparisons across signed and unsigned types and across different integer widths, every integer constructor adds an offset of `1<<63` before encoding the numeric value. For signed constructors the value is first converted to `int64`, for unsigned constructors it is treated as `uint64`; in both cases the offset is added and the resulting unsigned 64-bit value is written big-endian into the Key.
This mapping has two useful properties:
- Lexicographic byte-wise comparison of Keys corresponds to numeric ordering of the original values (taking signedness into account).
- Values produced from different source widths are comparable (for example `FromInt32(x)` equals `FromInt64(x)` for the same numeric x).
Note: Because both signed and unsigned constructors add the same offset, `FromInt64(0)` equals `FromUint64(0)`. The smallest `int64` value (`math.MinInt64`) maps to `0` and negative signed values compare before zero/positive values as expected for numeric ordering.
func FromBytes ¶
FromBytes returns a copy of the provided byte slice as a Key. If b is nil this returns an empty (zero-length) Key (not nil).
func FromInt ¶
FromInt converts an `int` to an 8-byte big-endian Key. The signed integer range is shifted by adding 1<<63 so that negative values compare before positive values when Keys are compared lexicographically.
func FromInt8 ¶
FromInt8 converts an int8 to an 8-byte big-endian Key (value is encoded into 64 bits). The signed value is shifted by 1<<63 for order-preserving behavior across widths.
func FromInt16 ¶
FromInt16 converts an int16 to an 8-byte big-endian Key (value is encoded into 64 bits). The signed value is shifted by 1<<63 for order-preserving behavior across widths.
func FromInt32 ¶
FromInt32 converts an int32 to an 8-byte big-endian Key (value is encoded into 64 bits). The signed value is shifted by 1<<63 for order-preserving behavior across widths.
func FromInt64 ¶
FromInt64 converts an int64 to an 8-byte big-endian Key. The value is shifted by 1<<63 so that lexicographic key order matches numeric order.
func FromRune ¶
FromRune converts a rune to its UTF-8 encoding as a Key. An invalid rune (a surrogate half, or a value outside the Unicode range) is encoded as U+FFFD, the replacement character, like everywhere in Go.
func FromString ¶
FromString returns a Key produced from the provided string after normalizing it to Unicode NFC. The resulting Key contains the UTF-8 encoding of the normalized string. (FromString does not alter case or trim spaces.)
func FromUint8 ¶
FromUint8 converts a uint8 to an 8-byte big-endian Key (value encoded into 64 bits).
func FromUint16 ¶
FromUint16 converts a uint16 to an 8-byte big-endian Key (value encoded into 64 bits).
func FromUint32 ¶
FromUint32 converts a uint32 to an 8-byte big-endian Key (value encoded into 64 bits).
func FromUint64 ¶
FromUint64 converts a uint64 to an 8-byte big-endian Key (MSB first).
func (Key) LessThanOrEqual ¶
LessThanOrEqual reports whether k is lexicographically less than or equal to other.
type MultiMap ¶
type MultiMap[T comparable] interface { // AddValue adds value to the set at key. If the key does not exist in the // MultiMap it is added. The provided Key is cloned before insertion; // changes to the caller's Key afterwards do not affect the stored key. AddValue(key Key, value T) // ContainsKey reports whether the MultiMap contains the key. ContainsKey(key Key) bool // ValuesFor returns the set of values associated with key, or an empty set // if the key does not exist. The result is never nil and is an independent // copy that the caller may modify. ValuesFor(key Key) *set3.Set3[T] // ValuesForSeq iterates over the values associated with key, in // unspecified order. It yields nothing if the key does not exist. ValuesForSeq(key Key) iter.Seq[T] // ValuesBetweenInclusive returns a set with all values whose keys are // between from and to, including values stored for from and to. The // bounds need not exist as keys. If from is greater than to, the result is // empty. The result is never nil and is an independent copy. ValuesBetweenInclusive(from, to Key) *set3.Set3[T] // ValuesBetweenInclusiveSeq iterates over the values of the keys that // ValuesBetweenInclusive covers, key by key. ValuesBetweenInclusiveSeq(from, to Key) iter.Seq[T] // ValuesBetweenExclusive returns a set with all values whose keys are // strictly between from and to. The bounds need not exist as keys. The // result is never nil and is an independent copy. ValuesBetweenExclusive(from, to Key) *set3.Set3[T] // ValuesBetweenExclusiveSeq iterates over the values of the keys that // ValuesBetweenExclusive covers, key by key. ValuesBetweenExclusiveSeq(from, to Key) iter.Seq[T] // ValuesFromInclusive returns a set with all values whose keys are greater // than or equal to from. The result is never nil and is an independent // copy. ValuesFromInclusive(from Key) *set3.Set3[T] // ValuesFromInclusiveSeq iterates over the values of the keys that // ValuesFromInclusive covers, key by key. ValuesFromInclusiveSeq(from Key) iter.Seq[T] // ValuesFromExclusive returns a set with all values whose keys are // strictly greater than from. The result is never nil and is an // independent copy. ValuesFromExclusive(from Key) *set3.Set3[T] // ValuesFromExclusiveSeq iterates over the values of the keys that // ValuesFromExclusive covers, key by key. ValuesFromExclusiveSeq(from Key) iter.Seq[T] // ValuesToInclusive returns a set with all values whose keys are less than // or equal to to. The result is never nil and is an independent copy. ValuesToInclusive(to Key) *set3.Set3[T] // ValuesToInclusiveSeq iterates over the values of the keys that // ValuesToInclusive covers, key by key. ValuesToInclusiveSeq(to Key) iter.Seq[T] // ValuesToExclusive returns a set with all values whose keys are strictly // less than to. The result is never nil and is an independent copy. ValuesToExclusive(to Key) *set3.Set3[T] // ValuesToExclusiveSeq iterates over the values of the keys that // ValuesToExclusive covers, key by key. ValuesToExclusiveSeq(to Key) iter.Seq[T] // AllValues returns a set with all values currently stored. The result is // never nil and is an independent copy. AllValues() *set3.Set3[T] // AllValuesSeq iterates over all values, key by key. AllValuesSeq() iter.Seq[T] // NumberOfKeys returns the number of keys currently stored. A key whose // last value is removed is removed as well. NumberOfKeys() uint64 // AllKeys returns a slice with clones of all keys. Ordered returns them in // ascending order; for other implementations the order is unspecified. AllKeys() []Key // AllKeysSeq iterates over all keys, in the same order as AllKeys. The // yielded Keys are views into the multimap: they must not be modified or // retained after the loop step (clone them to keep them). AllKeysSeq() iter.Seq[Key] // RemoveValue removes value from the set at key. Removing a non-existent // key or value is a no-op. When the set becomes empty, the key is removed. RemoveValue(key Key, value T) // RemoveKey removes the key and all its values. Removing a non-existent // key is a no-op. RemoveKey(key Key) // Clear removes all keys and values. Clear() }
MultiMap is the behavior all multimaps of this package share: a map from Keys to sets of values. Implementations clone keys on insertion, so callers may reuse their Key buffers.
Whether the methods are safe for concurrent use depends on the implementation; see the package documentation.
func New ¶
func New[T comparable]() MultiMap[T]
New returns a MultiMap that is safe for concurrent use: an Ordered multimap wrapped by Synchronized. Use NewOrdered or NewHashed directly when the caller already serializes access or needs no locking.
func Synchronized ¶
func Synchronized[T comparable](m MultiMap[T]) MultiMap[T]
Synchronized returns a MultiMap that forwards every call to m under a sync.RWMutex, making all methods safe for concurrent use. Reads run in parallel; a write waits for all running reads and blocks new ones.
The Seq methods hold the read lock for the whole loop over the returned iterator. The loop body must not call any method of the same multimap: a write deadlocks at once, and a read deadlocks if a writer is waiting. A long loop delays all writers. Use the set-returning methods when the loop body needs the multimap or runs long; they release the lock before returning.
m must not be used directly while the wrapper is in use.
type Ordered ¶
type Ordered[T comparable] struct { // contains filtered or unexported fields }
Ordered is a multimap backed by an adaptive radix tree. Point operations descend one node per distinguishing key byte; range queries visit only the keys in range; all iteration is in ascending key order. The zero value is an empty multimap.
Ordered is not synchronized: concurrent reads are safe, but a write must not run concurrently with any other access. Wrap it with Synchronized for concurrent use.
func NewOrdered ¶
func NewOrdered[T comparable]() *Ordered[T]
NewOrdered returns an empty, unsynchronized Ordered multimap. Use it directly when access is already serialized, or wrap it with Synchronized.
func (*Ordered[T]) AllKeysSeq ¶
AllKeysSeq iterates over all keys in ascending order. The yielded Keys are views into the multimap and must not be modified or retained.
func (*Ordered[T]) AllValuesSeq ¶
AllValuesSeq iterates over all values, key by key in ascending key order.
func (*Ordered[T]) ContainsKey ¶
ContainsKey reports whether key is present.
func (*Ordered[T]) NumberOfKeys ¶
NumberOfKeys returns the number of keys.
func (*Ordered[T]) RemoveValue ¶
RemoveValue removes value from the set at key, and the key once its set is empty.
func (*Ordered[T]) ValuesBetweenExclusive ¶
ValuesBetweenExclusive returns a copy of the values of all keys in (from, to).
func (*Ordered[T]) ValuesBetweenExclusiveSeq ¶
ValuesBetweenExclusiveSeq iterates over the values of all keys in (from, to).
func (*Ordered[T]) ValuesBetweenInclusive ¶
ValuesBetweenInclusive returns a copy of the values of all keys in [from, to].
func (*Ordered[T]) ValuesBetweenInclusiveSeq ¶
ValuesBetweenInclusiveSeq iterates over the values of all keys in [from, to].
func (*Ordered[T]) ValuesFor ¶
ValuesFor returns a copy of the values of key; empty if key is absent.
func (*Ordered[T]) ValuesForSeq ¶
ValuesForSeq iterates over the values of key.
func (*Ordered[T]) ValuesFromExclusive ¶
ValuesFromExclusive returns a copy of the values of all keys > from.
func (*Ordered[T]) ValuesFromExclusiveSeq ¶
ValuesFromExclusiveSeq iterates over the values of all keys > from.
func (*Ordered[T]) ValuesFromInclusive ¶
ValuesFromInclusive returns a copy of the values of all keys >= from.
func (*Ordered[T]) ValuesFromInclusiveSeq ¶
ValuesFromInclusiveSeq iterates over the values of all keys >= from.
func (*Ordered[T]) ValuesToExclusive ¶
ValuesToExclusive returns a copy of the values of all keys < to.
func (*Ordered[T]) ValuesToExclusiveSeq ¶
ValuesToExclusiveSeq iterates over the values of all keys < to.
func (*Ordered[T]) ValuesToInclusive ¶
ValuesToInclusive returns a copy of the values of all keys <= to.
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
art
Package art is the adaptive radix tree behind multimap.Ordered.
|
Package art is the adaptive radix tree behind multimap.Ordered. |
|
swar
Package swar holds the branch-free byte search primitives of the ART nodes: SWAR ("SIMD within a register") search over 8 key bytes at a time, and rank queries on a 256-bit presence bitmap.
|
Package swar holds the branch-free byte search primitives of the ART nodes: SWAR ("SIMD within a register") search over 8 key bytes at a time, and rank queries on a 256-bit presence bitmap. |
|
vset
Package vset is the value container of one multimap key.
|
Package vset is the value container of one multimap key. |