multimap

package module
v0.0.0-...-cc47992 Latest Latest
Warning

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

Go to latest
Published: Sep 30, 2026 License: BSD-3-Clause Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

multimap for Go

Go Report Card Go Reference Linter Tests coverage

multimap provides fast, compact multimaps for Go. A multimap is a data structure that allows multiple values to be associated with a single key, unlike a regular map where each key has exactly one value.

Implementations

Constructor Backed by Range queries Iteration order Concurrency
New[T]() adaptive radix tree visit only the keys in range ascending key order safe (read/write lock)
NewOrdered[T]() adaptive radix tree visit only the keys in range ascending key order not synchronized
NewHashed[T]() Go map scan all keys unspecified not synchronized
Synchronized[T](m) wraps any of the above as the wrapped map as the wrapped map safe (read/write lock)

Choose Ordered (the default behind New) when range queries or ordered iteration matter, or when keys are integers: there it is about as fast as Hashed for point operations. Choose Hashed for string keys when range queries are rare: point operations are one and a half to two times as fast there, but every range query scans all keys, and like every Go map it keeps its table after deletions. Both need about half the memory of a Go map or B-tree of Go sets. If every key holds exactly one value, a plain B-tree map answers range queries faster and needs about half the memory of Ordered, which remains faster for lookups and changes. The benchmarks behind these statements are in bench/README.md.

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 at the same time, but a write must not run concurrently with any other access; provide that exclusion yourself, or wrap the multimap with Synchronized, which guards every method with a sync.RWMutex (reads run in parallel, writes are exclusive). New returns a synchronized Ordered multimap.

Sets and iterators

Every read method exists twice:

  • The plain form (ValuesFor, ValuesBetweenInclusive, AllKeys, ...) returns an independent copy (*set3.Set3[T] or []Key). It allocates, but you may keep and modify the result, and on a synchronized multimap no lock is held once it returns.
  • The Seq form (ValuesForSeq, ValuesBetweenInclusiveSeq, AllKeysSeq, ...) returns an iter.Seq over the stored data without copying. It yields the values key by key, so a value stored under several keys is yielded once per key. Keys yielded by AllKeysSeq must not be modified or retained, and the loop body must not modify the multimap it iterates over.

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 (this can deadlock), and a long loop delays writers. Use the plain form when the loop body needs the multimap or runs long.

Key Characteristics

  • One-to-many mapping: Each key can have zero, one, or multiple values
  • Duplicate values: The same value can only be stored once per key but it can be stored multiple times for different keys
  • Key uniqueness: Keys themselves are still unique - there's only one entry per key

Common Use Cases

  • Indexing: Group items by category (e.g., products by brand)
  • Graph representations: Store adjacency lists where each node maps to multiple connected nodes
  • HTTP headers: Multiple values for the same header name
  • Database indexes: Multiple records sharing the same indexed value

Indexing

The multimap uses Key objects for indexing, which are internally represented as []byte arrays. This allows for efficient storage and comparison regardless of the original data type used to create the key.

Key behavior
  • Mixed types: A single multimap can contain keys created from different data types (strings, integers, custom byte arrays) without any issues.
  • Lexicographic ordering: All keys are compared using byte-wise lexicographic ordering of their internal []byte representation.
  • Custom keys: You can create keys from any []byte array using the generic constructor for your own implementations.

Important: When mixing different key types in range queries, the lexicographic ordering may be counter-intuitive. For example:

// These keys will NOT be ordered numerically when mixed with strings:
key1 := FromString("100")     // UTF-8 bytes: [0x31, 0x30, 0x30]
key2 := FromInt64(50)         // Encoded as: [0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x32]
key3 := FromString("25")      // UTF-8 bytes: [0x32, 0x35]

// Lexicographic order: key1 ("100") < key3 ("25") < key2 (50)
// This differs from intuitive numeric/alphabetic ordering!

For predictable range query behavior, use consistent key types within logical ranges. See the type-specific encoding details below.

String keys
  • Use FromString(s) to convert a string to a Key. FromString normalizes the input string using Unicode NFC so canonically equivalent strings map to the same key.
  • Key ordering (LessThan) is a byte-wise lexicographic comparison of the UTF-8 bytes — it is neither locale-aware nor rune-aware.
Numeric keys
  • Use FromInt* / FromUint* helpers to build integer keys.
  • All integer keys are encoded as fixed-width 8-byte big-endian sequences (MSB first).
  • To ensure consistent ordering across signed/unsigned types and across widths, all integer constructors add an offset of 1<<63 before encoding. Signed values are converted to int64 first; unsigned values are treated as uint64 and the same offset is added. This maps signed and unsigned values into a single uint64 namespace so that lexicographic comparison matches numeric ordering.

Examples / consequences:

  • FromInt64(0) equals FromUint64(0) because both are encoded as 0 + (1<<63).
  • The smallest int64 (-2^63) maps to [00,00,00,00,00,00,00,00] after encoding; negative signed values compare before zero/positive values as expected for numeric order.
  • Values encoded from different widths are comparable — for example FromInt32(x) is identical to FromInt64(x) for the same numeric x.

Behavior and semantics

  • AddValue(key, v) clones key before inserting; mutating the caller's Key after calling AddValue will not affect the stored key.
  • ValuesFor(key) and the other set-returning methods return copies; modifying a returned set does not affect the MultiMap's contents.
  • Removing the last value of a key removes the key.
  • Range queries: Keys are ordered in lexicographic order, allowing efficient range queries between two key boundaries. Range operations return a set of all values of all keys where the key falls within the specified range (inclusive or exclusive based on the method). The ordering follows the byte-wise comparison rules described above for string and numeric keys.

Examples

See the example_test.go in this package for runnable examples that also appear in generated GoDoc.

Acknowledgements

multimap stands on the shoulders of others. Thank you!

  • The tree behind Ordered follows the Adaptive Radix Tree by Viktor Leis, Alfons Kemper and Thomas Neumann: The Adaptive Radix Tree: ARTful Indexing for Main-Memory Databases (ICDE 2013). Nodes that grow and shrink with their number of children, path compression and lazy expansion come from there; the node sizes, the child search and the value sets in the leaves are this project's own.
  • The child search in the small nodes uses the "has zero byte" SWAR trick collected in Sean Eron Anderson's Bit Twiddling Hacks (after Alan Mycroft).
  • plar/go-adaptive-radix-tree, plar/go-hot-trie and tidwall/btree served as yardsticks while the design took shape (see bench/_archive). No code of theirs is included.
  • The benchmark measures real keys thanks to the OpenStreetMap contributors, whose German street names reach it through the OpenPLZ API project's data extract (© OpenStreetMap contributors, ODbL 1.0), and to the Debian project, whose package contents lists supply its file paths. Both corpora and their licenses are described in bench/keys/testdata.

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

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]) AddValue

func (m *Hashed[T]) AddValue(key Key, value T)

AddValue adds value to the set at key, cloning key if it is new.

func (*Hashed[T]) AllKeys

func (m *Hashed[T]) AllKeys() []Key

AllKeys returns clones of all keys in unspecified order.

func (*Hashed[T]) AllKeysSeq

func (m *Hashed[T]) AllKeysSeq() iter.Seq[Key]

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]) AllValues

func (m *Hashed[T]) AllValues() *set3.Set3[T]

AllValues returns a copy of all values.

func (*Hashed[T]) AllValuesSeq

func (m *Hashed[T]) AllValuesSeq() iter.Seq[T]

AllValuesSeq iterates over all values, key by key in unspecified order.

func (*Hashed[T]) Clear

func (m *Hashed[T]) Clear()

Clear removes all keys and values.

func (*Hashed[T]) ContainsKey

func (m *Hashed[T]) ContainsKey(key Key) bool

ContainsKey reports whether key is present.

func (*Hashed[T]) NumberOfKeys

func (m *Hashed[T]) NumberOfKeys() uint64

NumberOfKeys returns the number of keys.

func (*Hashed[T]) RemoveKey

func (m *Hashed[T]) RemoveKey(key Key)

RemoveKey removes key and all its values.

func (*Hashed[T]) RemoveValue

func (m *Hashed[T]) RemoveValue(key Key, value T)

RemoveValue removes value from the set at key, and the key once its set is empty.

func (*Hashed[T]) ValuesBetweenExclusive

func (m *Hashed[T]) ValuesBetweenExclusive(from, to Key) *set3.Set3[T]

ValuesBetweenExclusive returns a copy of the values of all keys in (from, to).

func (*Hashed[T]) ValuesBetweenExclusiveSeq

func (m *Hashed[T]) ValuesBetweenExclusiveSeq(from, to Key) iter.Seq[T]

ValuesBetweenExclusiveSeq iterates over the values of all keys in (from, to).

func (*Hashed[T]) ValuesBetweenInclusive

func (m *Hashed[T]) ValuesBetweenInclusive(from, to Key) *set3.Set3[T]

ValuesBetweenInclusive returns a copy of the values of all keys in [from, to].

func (*Hashed[T]) ValuesBetweenInclusiveSeq

func (m *Hashed[T]) ValuesBetweenInclusiveSeq(from, to Key) iter.Seq[T]

ValuesBetweenInclusiveSeq iterates over the values of all keys in [from, to].

func (*Hashed[T]) ValuesFor

func (m *Hashed[T]) ValuesFor(key Key) *set3.Set3[T]

ValuesFor returns a copy of the values of key; empty if key is absent.

func (*Hashed[T]) ValuesForSeq

func (m *Hashed[T]) ValuesForSeq(key Key) iter.Seq[T]

ValuesForSeq iterates over the values of key.

func (*Hashed[T]) ValuesFromExclusive

func (m *Hashed[T]) ValuesFromExclusive(from Key) *set3.Set3[T]

ValuesFromExclusive returns a copy of the values of all keys > from.

func (*Hashed[T]) ValuesFromExclusiveSeq

func (m *Hashed[T]) ValuesFromExclusiveSeq(from Key) iter.Seq[T]

ValuesFromExclusiveSeq iterates over the values of all keys > from.

func (*Hashed[T]) ValuesFromInclusive

func (m *Hashed[T]) ValuesFromInclusive(from Key) *set3.Set3[T]

ValuesFromInclusive returns a copy of the values of all keys >= from.

func (*Hashed[T]) ValuesFromInclusiveSeq

func (m *Hashed[T]) ValuesFromInclusiveSeq(from Key) iter.Seq[T]

ValuesFromInclusiveSeq iterates over the values of all keys >= from.

func (*Hashed[T]) ValuesToExclusive

func (m *Hashed[T]) ValuesToExclusive(to Key) *set3.Set3[T]

ValuesToExclusive returns a copy of the values of all keys < to.

func (*Hashed[T]) ValuesToExclusiveSeq

func (m *Hashed[T]) ValuesToExclusiveSeq(to Key) iter.Seq[T]

ValuesToExclusiveSeq iterates over the values of all keys < to.

func (*Hashed[T]) ValuesToInclusive

func (m *Hashed[T]) ValuesToInclusive(to Key) *set3.Set3[T]

ValuesToInclusive returns a copy of the values of all keys <= to.

func (*Hashed[T]) ValuesToInclusiveSeq

func (m *Hashed[T]) ValuesToInclusiveSeq(to Key) iter.Seq[T]

ValuesToInclusiveSeq iterates over 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 FromByte

func FromByte(b byte) Key

FromByte is an alias for FromUint8 and produces an 8-byte representation.

func FromBytes

func FromBytes(b []byte) Key

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

func FromInt(i int) Key

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

func FromInt8(i int8) Key

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

func FromInt16(i int16) Key

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

func FromInt32(i int32) Key

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

func FromInt64(i int64) Key

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

func FromRune(r rune) Key

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

func FromString(s string) Key

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 FromUint

func FromUint(u uint) Key

FromUint converts a uint to an 8-byte big-endian Key (MSB first).

func FromUint8

func FromUint8(u uint8) Key

FromUint8 converts a uint8 to an 8-byte big-endian Key (value encoded into 64 bits).

func FromUint16

func FromUint16(u uint16) Key

FromUint16 converts a uint16 to an 8-byte big-endian Key (value encoded into 64 bits).

func FromUint32

func FromUint32(u uint32) Key

FromUint32 converts a uint32 to an 8-byte big-endian Key (value encoded into 64 bits).

func FromUint64

func FromUint64(u uint64) Key

FromUint64 converts a uint64 to an 8-byte big-endian Key (MSB first).

func (Key) Bytes

func (k Key) Bytes() []byte

Bytes returns a copy of the Key as a byte slice.

func (Key) Clone

func (k Key) Clone() Key

Clone returns an independent copy of the Key. If k is nil, Clone returns nil.

func (Key) Equal

func (k Key) Equal(other Key) bool

Equal reports whether k and other have the same contents.

func (Key) IsEmpty

func (k Key) IsEmpty() bool

IsEmpty returns whether the Key is empty or nil.

func (Key) LessThan

func (k Key) LessThan(other Key) bool

LessThan reports whether k is lexicographically less than other.

func (Key) LessThanOrEqual

func (k Key) LessThanOrEqual(other Key) bool

LessThanOrEqual reports whether k is lexicographically less than or equal to other.

func (Key) String

func (k Key) String() string

String returns the Key as a string consisting of uppercase hex tuples per byte, separated by commas and surrounded by `[]` (e.g. `[01,AB,00]`).

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]) AddValue

func (m *Ordered[T]) AddValue(key Key, value T)

AddValue adds value to the set at key, cloning key if it is new.

func (*Ordered[T]) AllKeys

func (m *Ordered[T]) AllKeys() []Key

AllKeys returns clones of all keys in ascending order.

func (*Ordered[T]) AllKeysSeq

func (m *Ordered[T]) AllKeysSeq() iter.Seq[Key]

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]) AllValues

func (m *Ordered[T]) AllValues() *set3.Set3[T]

AllValues returns a copy of all values.

func (*Ordered[T]) AllValuesSeq

func (m *Ordered[T]) AllValuesSeq() iter.Seq[T]

AllValuesSeq iterates over all values, key by key in ascending key order.

func (*Ordered[T]) Clear

func (m *Ordered[T]) Clear()

Clear removes all keys and values.

func (*Ordered[T]) ContainsKey

func (m *Ordered[T]) ContainsKey(key Key) bool

ContainsKey reports whether key is present.

func (*Ordered[T]) NumberOfKeys

func (m *Ordered[T]) NumberOfKeys() uint64

NumberOfKeys returns the number of keys.

func (*Ordered[T]) RemoveKey

func (m *Ordered[T]) RemoveKey(key Key)

RemoveKey removes key and all its values.

func (*Ordered[T]) RemoveValue

func (m *Ordered[T]) RemoveValue(key Key, value T)

RemoveValue removes value from the set at key, and the key once its set is empty.

func (*Ordered[T]) ValuesBetweenExclusive

func (m *Ordered[T]) ValuesBetweenExclusive(from, to Key) *set3.Set3[T]

ValuesBetweenExclusive returns a copy of the values of all keys in (from, to).

func (*Ordered[T]) ValuesBetweenExclusiveSeq

func (m *Ordered[T]) ValuesBetweenExclusiveSeq(from, to Key) iter.Seq[T]

ValuesBetweenExclusiveSeq iterates over the values of all keys in (from, to).

func (*Ordered[T]) ValuesBetweenInclusive

func (m *Ordered[T]) ValuesBetweenInclusive(from, to Key) *set3.Set3[T]

ValuesBetweenInclusive returns a copy of the values of all keys in [from, to].

func (*Ordered[T]) ValuesBetweenInclusiveSeq

func (m *Ordered[T]) ValuesBetweenInclusiveSeq(from, to Key) iter.Seq[T]

ValuesBetweenInclusiveSeq iterates over the values of all keys in [from, to].

func (*Ordered[T]) ValuesFor

func (m *Ordered[T]) ValuesFor(key Key) *set3.Set3[T]

ValuesFor returns a copy of the values of key; empty if key is absent.

func (*Ordered[T]) ValuesForSeq

func (m *Ordered[T]) ValuesForSeq(key Key) iter.Seq[T]

ValuesForSeq iterates over the values of key.

func (*Ordered[T]) ValuesFromExclusive

func (m *Ordered[T]) ValuesFromExclusive(from Key) *set3.Set3[T]

ValuesFromExclusive returns a copy of the values of all keys > from.

func (*Ordered[T]) ValuesFromExclusiveSeq

func (m *Ordered[T]) ValuesFromExclusiveSeq(from Key) iter.Seq[T]

ValuesFromExclusiveSeq iterates over the values of all keys > from.

func (*Ordered[T]) ValuesFromInclusive

func (m *Ordered[T]) ValuesFromInclusive(from Key) *set3.Set3[T]

ValuesFromInclusive returns a copy of the values of all keys >= from.

func (*Ordered[T]) ValuesFromInclusiveSeq

func (m *Ordered[T]) ValuesFromInclusiveSeq(from Key) iter.Seq[T]

ValuesFromInclusiveSeq iterates over the values of all keys >= from.

func (*Ordered[T]) ValuesToExclusive

func (m *Ordered[T]) ValuesToExclusive(to Key) *set3.Set3[T]

ValuesToExclusive returns a copy of the values of all keys < to.

func (*Ordered[T]) ValuesToExclusiveSeq

func (m *Ordered[T]) ValuesToExclusiveSeq(to Key) iter.Seq[T]

ValuesToExclusiveSeq iterates over the values of all keys < to.

func (*Ordered[T]) ValuesToInclusive

func (m *Ordered[T]) ValuesToInclusive(to Key) *set3.Set3[T]

ValuesToInclusive returns a copy of the values of all keys <= to.

func (*Ordered[T]) ValuesToInclusiveSeq

func (m *Ordered[T]) ValuesToInclusiveSeq(to Key) iter.Seq[T]

ValuesToInclusiveSeq iterates over the values of all keys <= to.

Directories

Path Synopsis
art
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.

Jump to

Keyboard shortcuts

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