Skip to content

Instantly share code, notes, and snippets.

@jba
Last active April 13, 2026 15:32
Show Gist options
  • Select an option

  • Save jba/794cd8f3777e427307b24ff7a1777c45 to your computer and use it in GitHub Desktop.

Select an option

Save jba/794cd8f3777e427307b24ff7a1777c45 to your computer and use it in GitHub Desktop.
tree.Map API
package tree // import "github.com/jba/omap/tree"
Package tree implements an in-memory ordered map using a tree. The map supports
arbitrary keys and comparison functions.
TYPES
type Map[K, V any] struct {
// Has unexported fields.
}
A Map is a map[K]V ordered according to an arbitrary comparison function.
The zero value of a Map is not meaningful since it has no comparison
function. Use NewMap to create a Map. A nil *Map, like a nil Go map,
can be read but not written and contains no entries.
func NewMap[K, V any](cmp func(K, K) int) *Map[K, V]
NewMap returns a new MapFunc[K, V] ordered according to cmp.
func (m *Map[K, V]) Above(lo K) MapSpan[K, V]
Above returns a MapSpan with lower bound lo, exclusive, and no upper bound.
func (m *Map[K, V]) All() iter.Seq2[K, V]
All returns an iterator over the map m from smallest to largest key. If m is
modified during the iteration, All guarantees that when a key is yielded,
it is the successor of the key that was previously yielded (or the minimum
key in the map, if it is the first key).
For example, if the map contains keys 10 and 20, the iterator has yielded
10, and then 15 is inserted, then the next yielded key will be 15.
Another example: if the map contains keys 10, 20 and 30, the iterator has
yielded 10, and then 20 is deleted, then the next yielded key will be 30.
func (m *Map[K, V]) At(key K) V
At returns the value of m[key], or the zero value if key is not present.
func (m *Map[K, V]) Backward() iter.Seq2[K, V]
Backward returns an iterator over the map m from largest to smallest key.
See Map.All for the guarantee provided if m is modified during the
iteration.
func (m *Map[K, V]) Below(hi K) MapSpan[K, V]
Below returns a MapSpan with upper bound hi, exclusive, and no lower bound.
func (m *Map[K, V]) Clear()
Clear removes all entries from the map.
func (m *Map[K, V]) Clone() *Map[K, V]
Clone returns a shallow copy of m.
func (m *Map[K, V]) Contains(key K) bool
Contains reports whether the map contains an entry with the given key.
func (m *Map[K, V]) ContainsAll(keys iter.Seq[K]) bool
ContainsAll reports whether the map contains an entry for each key in the
sequence.
func (m *Map[K, V]) Delete(key K) (V, bool)
Delete removes the entry with the given key, if present. It reports whether
the map changed, and returns the previous value, if any.
func (m *Map[K, V]) DeleteAll(keys iter.Seq[K]) bool
DeleteAll removes all the entries whose keys are in the sequence. It reports
whether the map changed.
func (m *Map[K, V]) From(lo K) MapSpan[K, V]
From returns a MapSpan with lower bound lo, inclusive, and no upper bound.
func (m *Map[K, V]) Get(key K) (V, bool)
Get returns the value of m[key] and reports whether it exists.
func (m *Map[K, V]) Index(key K) int
Index returns the index of key in m, or -1 if key is not present.
func (m *Map[K, V]) Keys() iter.Seq[K]
Keys returns an iterator over the keys in m from smallest to largest. See
Map.All for the guarantee provided if m is modified during the iteration.
func (m *Map[K, V]) Len() int
Len returns the number of keys in m.
func (m *Map[K, V]) Max() (K, V, bool)
Max returns the maximum key in m, its value, and true. If m is empty,
the third return value is false.
func (m *Map[K, V]) Min() (K, V, bool)
Min returns the minimum key in m, its value, and true. If m is empty,
the third return value is false.
func (m *Map[K, V]) Nth(i int) (K, V)
Nth returns the key and value at index i. It panics if i < 0 or i >=
m.Len().
func (m *Map[K, V]) Set(key K, val V) (old V, added bool)
Set sets m[key] = val. If the entry was present, Set returns the former
value and false. Otherwise it returns the zero value and true.
func (m *Map[K, V]) SetAll(seq iter.Seq2[K, V]) bool
SetAll calls Set(k, v) for each key/value pair in the sequence. It reports
whether the number of map entries increased.
func (m *Map[K, V]) String() string
String returns a string representation of m in the form "{k1: v1, k2: v2}".
func (m *Map[K, V]) To(hi K) MapSpan[K, V]
To returns a MapSpan with upper bound hi, inclusive, and no lower bound.
func (m *Map[K, V]) Values() iter.Seq[V]
Values returns an iterator over the values in m from smallest to largest
key. See Map.All for the guarantee provided if m is modified during the
iteration.
type MapSpan[K, V any] struct {
// Has unexported fields.
}
A MapSpan is a subsequence of keys in a Map.
func (s MapSpan[K, V]) Above(lo K) MapSpan[K, V]
Above returns a MapSpan with lower bound lo, exclusive and the same upper
bound as r, if lo is within r's current lower bound. Otherwise, it returns
r.
func (s MapSpan[K, V]) All() iter.Seq2[K, V]
All returns an iterator over r's underlying map from smallest to largest
key in r. See Map.All for the guarantee provided if m is modified during the
iteration.
func (s MapSpan[K, V]) Backward() iter.Seq2[K, V]
Backward returns an iterator over r's underlying map from largest to
smallest key in r. See Map.All for the guarantee provided if m is modified
during the iteration.
func (s MapSpan[K, V]) Below(hi K) MapSpan[K, V]
Below returns a MapSpan with upper bound hi, exclusive and the same lower
bound as r, if hi is within r's current upper bound. Otherwise, it returns
r.
func (s MapSpan[K, V]) Clear()
Clear deletes all the entries in r from r's underlying map.
func (s MapSpan[K, V]) Clone() *Map[K, V]
Clone returns a new Map containing only the keys in r.
func (s MapSpan[K, V]) From(lo K) MapSpan[K, V]
From returns a MapSpan with lower bound lo, inclusive and the same upper
bound as r, if lo is within r's current lower bound. Otherwise, it returns
r.
func (s MapSpan[K, V]) Index(key K) int
Index returns the index of key within r, or -1 if key is not present or not
in bounds.
func (s MapSpan[K, V]) Keys() iter.Seq[K]
Keys returns an iterator over the keys in r from smallest to largest. See
Map.All for the guarantee provided if m is modified during the iteration.
func (s MapSpan[K, V]) Len() int
Len returns the number of keys in r.
func (s MapSpan[K, V]) Max() (K, V, bool)
Max returns the maximum key from r's underlying map that is in r, its value,
and true. If m is empty, the third return value is false.
func (s MapSpan[K, V]) Min() (K, V, bool)
Min returns the minimum key from r's underlying map that is in r, its value,
and true. If m is empty, the third return value is false.
func (s MapSpan[K, V]) Nth(i int) (K, V)
Nth returns the key and value at index i within r. It panics if i < 0 or i
>= r.Len().
func (s MapSpan[K, V]) To(hi K) MapSpan[K, V]
To returns a MapSpan with upper bound hi, inclusive and the same lower bound
as r, if hi is within r's current upper bound. Otherwise, it returns r.
func (s MapSpan[K, V]) Values() iter.Seq[V]
Values returns an iterator over the values in r from smallest to largest
key. See Map.All for the guarantee provided if m is modified during the
iteration.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment