Documentation
¶
Overview ¶
Package btree provides copy-on-write ordered maps and sets backed by a B-tree. See package aug for the ownership and concurrency rules, and the orderstat and interval packages for augmented variants.
Comparison ¶
A map or set is ordered, and its entries identified, by a comparison function: cmp(a, b) is negative, zero or positive as a sorts before, with or after b, and must be a total order (in particular, transitive). Two keys that compare equal are the same key, so an Upsert of a key equal to a stored one replaces it, and Set.Get returns the stored item for an equal probe. Data that the comparison reads must not change while the key is stored.
NewOrdered and NewOrderedSet order keys with <, as cmp.Compare does except for floating-point NaN, which they cannot place; use New with cmp.Compare for keys that may be NaN.
Index ¶
- type FreeList
- type Map
- func (m *Map[K, V]) All() iter.Seq2[K, V]
- func (m *Map[K, V]) Backward() iter.Seq2[K, V]
- func (m *Map[K, V]) Clear()
- func (m *Map[K, V]) Clone() *Map[K, V]
- func (m *Map[K, V]) Compare(a, b K) int
- func (m *Map[K, V]) Cursor() MapCursor[K, V]
- func (m *Map[K, V]) Degree() int
- func (m *Map[K, V]) Delete(k K) (removedK K, v V, found bool)
- func (m *Map[K, V]) From(lo K) iter.Seq2[K, V]
- func (m *Map[K, V]) Get(k K) (v V, ok bool)
- func (m *Map[K, V]) Height() int
- func (m *Map[K, V]) Iterator() MapIterator[K, V]
- func (m *Map[K, V]) Len() int
- func (m *Map[K, V]) Lookup(k K) (key K, v V, ok bool)
- func (m *Map[K, V]) Max() (k K, v V, ok bool)
- func (m *Map[K, V]) Min() (k K, v V, ok bool)
- func (m *Map[K, V]) Range(lo, hi K) iter.Seq2[K, V]
- func (m *Map[K, V]) String() string
- func (m *Map[K, V]) Upsert(k K, v V) (replacedK K, replacedV V, replaced bool)
- func (m *Map[K, V]) Verify() error
- type MapCursor
- type MapIterator
- type Option
- type Set
- func (s *Set[T]) All() iter.Seq[T]
- func (s *Set[T]) Backward() iter.Seq[T]
- func (s *Set[T]) Clear()
- func (s *Set[T]) Clone() *Set[T]
- func (s *Set[T]) Compare(a, b T) int
- func (s *Set[T]) Contains(item T) bool
- func (s *Set[T]) Cursor() SetCursor[T]
- func (s *Set[T]) Degree() int
- func (s *Set[T]) Delete(item T) (removed T, ok bool)
- func (s *Set[T]) From(lo T) iter.Seq[T]
- func (s *Set[T]) Get(probe T) (item T, found bool)
- func (s *Set[T]) Height() int
- func (s *Set[T]) Iterator() SetIterator[T]
- func (s *Set[T]) Len() int
- func (s *Set[T]) Max() (item T, ok bool)
- func (s *Set[T]) Min() (item T, ok bool)
- func (s *Set[T]) Range(lo, hi T) iter.Seq[T]
- func (s *Set[T]) String() string
- func (s *Set[T]) Upsert(item T) (replaced T, overwrote bool)
- func (s *Set[T]) Verify() error
- type SetCursor
- func (c *SetCursor[T]) Compare(a, b T) int
- func (c *SetCursor[T]) Delete() T
- func (c *SetCursor[T]) First() bool
- func (c *SetCursor[T]) Item() T
- func (c *SetCursor[T]) Last() bool
- func (c *SetCursor[T]) Next() bool
- func (c *SetCursor[T]) Prev() bool
- func (c *SetCursor[T]) Rekey(item T) (displaced T, ok bool)
- func (c *SetCursor[T]) Reset()
- func (c *SetCursor[T]) SeekExact(item T) bool
- func (c *SetCursor[T]) SeekGE(item T) bool
- func (c *SetCursor[T]) SeekGT(item T) bool
- func (c *SetCursor[T]) SeekLE(item T) bool
- func (c *SetCursor[T]) SeekLT(item T) bool
- func (c *SetCursor[T]) Upsert(item T) (replaced T, ok bool)
- func (c *SetCursor[T]) Valid() bool
- type SetIterator
- func (i *SetIterator[T]) Compare(a, b T) int
- func (i *SetIterator[T]) First() bool
- func (i *SetIterator[T]) Item() T
- func (i *SetIterator[T]) Last() bool
- func (i *SetIterator[T]) Next() bool
- func (i *SetIterator[T]) Prev() bool
- func (i *SetIterator[T]) Reset()
- func (i *SetIterator[T]) SeekExact(item T) bool
- func (i *SetIterator[T]) SeekGE(item T) bool
- func (i *SetIterator[T]) SeekGT(item T) bool
- func (i *SetIterator[T]) SeekLE(item T) bool
- func (i *SetIterator[T]) SeekLT(item T) bool
- func (i *SetIterator[T]) Valid() bool
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type FreeList ¶ added in v0.2.0
FreeList recycles nodes between Maps and Sets with the same key and value types.
func NewFreeList ¶ added in v0.2.0
NewFreeList returns a FreeList that retains up to size nodes. See aug.NewFreeList.
type Map ¶
Map is an ordered map from K to V. It is an aug.Map without an augmentation; the conversion between the two is free.
Example ¶
package main
import (
"fmt"
"strings"
"github.com/ajwerner/btree"
)
func main() {
m := btree.New[string, int](strings.Compare)
m.Upsert("foo", 1)
m.Upsert("bar", 2)
fmt.Println(m.Get("foo"))
fmt.Println(m.Get("baz"))
it := m.Iterator()
for it.First(); it.Valid(); it.Next() {
fmt.Println(it.Key(), it.Value())
}
}
Output: 1 true 0 false bar 2 foo 1
func New ¶ added in v0.2.0
New constructs a Map with the provided comparison function. See WithDegree and WithFreeList for the options.
func NewOrdered ¶ added in v0.2.0
NewOrdered constructs a Map keyed by a type that supports <, ordered that way. It is faster than New with cmp.Compare because nodes are searched with < directly.
func (*Map[K, V]) Backward ¶ added in v0.2.0
Backward returns an iterator over every entry in reverse key order.
func (*Map[K, V]) Clear ¶ added in v0.2.0
func (m *Map[K, V]) Clear()
Clear removes all entries, returning nodes no other map references to the free list. See aug.Map.Clear.
func (*Map[K, V]) Compare ¶ added in v0.2.0
Compare compares two keys with the map's comparison function.
func (*Map[K, V]) Cursor ¶ added in v0.2.0
Cursor returns a new cursor positioned before the first entry.
func (*Map[K, V]) Iterator ¶ added in v0.2.0
func (m *Map[K, V]) Iterator() MapIterator[K, V]
Iterator returns a new iterator positioned before the first entry.
func (*Map[K, V]) Lookup ¶ added in v0.3.0
Lookup returns the stored entry whose key compares equal to k, if any.
func (*Map[K, V]) Range ¶ added in v0.2.0
Range returns an iterator over the entries with keys in [lo, hi).
type MapCursor ¶ added in v0.2.0
MapCursor is a cursor for a Map: an iterator that can mutate the entry it is positioned on. See aug.Cursor.
Example ¶
ExampleMapCursor charges the least loaded host and moves it to its new position without a second descent.
package main
import (
"cmp"
"fmt"
"strings"
"github.com/ajwerner/btree"
)
func main() {
type host struct {
load int
name string
}
byLoad := func(a, b host) int {
if c := cmp.Compare(a.load, b.load); c != 0 {
return c
}
return strings.Compare(a.name, b.name)
}
fleet := btree.NewSet(byLoad)
fleet.Upsert(host{3, "a"})
fleet.Upsert(host{1, "b"})
fleet.Upsert(host{2, "c"})
c := fleet.Cursor()
for range 3 {
c.First()
h := c.Item()
h.load += 2
c.Rekey(h)
fmt.Println("charged", h.name, "to", h.load)
}
for h := range fleet.All() {
fmt.Println(h.name, h.load)
}
}
Output: charged b to 3 charged c to 4 charged a to 5 b 3 c 4 a 5
type MapIterator ¶
MapIterator is an iterator for a Map.
type Option ¶ added in v0.2.0
Option configures a Map or Set at construction.
func WithDegree ¶ added in v0.2.0
WithDegree sets the degree of the tree. See aug.WithDegree.
func WithFreeList ¶ added in v0.2.0
WithFreeList sets the free list a Map or Set allocates from. See aug.WithFreeList.
type Set ¶
Set is an ordered set of items of type T.
func NewOrderedSet ¶ added in v0.2.0
NewOrderedSet constructs a Set of a type that supports <, ordered that way. See NewOrdered.
func NewSet ¶ added in v0.2.0
NewSet constructs a Set with the provided comparison function. See WithDegree and WithFreeList for the options.
func (*Set[T]) Backward ¶ added in v0.2.0
Backward returns an iterator over every item in reverse order.
func (*Set[T]) Clear ¶ added in v0.2.0
func (s *Set[T]) Clear()
Clear removes all items, returning nodes no other set references to the free list.
func (*Set[T]) Compare ¶ added in v0.2.0
Compare compares two items with the set's comparison function.
func (*Set[T]) Contains ¶ added in v0.2.0
Contains reports whether an item equal to item is in the set.
func (*Set[T]) Cursor ¶ added in v0.2.0
Cursor returns a new cursor positioned before the first item.
func (*Set[T]) Get ¶ added in v0.3.0
Get returns the item in the set equal to probe, if any. Items that compare equal may differ in fields the comparison ignores.
func (*Set[T]) Iterator ¶ added in v0.2.0
func (s *Set[T]) Iterator() SetIterator[T]
Iterator returns a new iterator positioned before the first item.
type SetCursor ¶ added in v0.2.0
type SetCursor[T any] struct { // contains filtered or unexported fields }
SetCursor is a SetIterator that can also mutate the set at its position; see aug.Cursor for the rules.
func (*SetCursor[T]) Compare ¶ added in v0.3.0
Compare compares two items with the set's comparison function.
func (*SetCursor[T]) Delete ¶ added in v0.3.0
func (c *SetCursor[T]) Delete() T
Delete removes the current item and returns it, leaving the cursor on the following item or past the end; see aug.Cursor.Delete.
func (*SetCursor[T]) Item ¶ added in v0.3.0
func (c *SetCursor[T]) Item() T
Item returns the item at the cursor's position, which must be valid.
func (*SetCursor[T]) Rekey ¶ added in v0.3.0
Rekey replaces the current item with item, which may sort elsewhere, and returns any other item equal to it that was displaced. Afterwards the cursor is on item.
func (*SetCursor[T]) Reset ¶ added in v0.3.0
func (c *SetCursor[T]) Reset()
Reset marks the cursor invalid, before the first item.
func (*SetCursor[T]) SeekExact ¶ added in v0.3.0
SeekExact positions the cursor at item and reports whether it is in the set; if not, the cursor is at the first greater item.
func (*SetCursor[T]) SeekGE ¶ added in v0.3.0
SeekGE positions the cursor at the first item >= item.
type SetIterator ¶
type SetIterator[T any] struct { // contains filtered or unexported fields }
SetIterator iterates over a set. Every positioning method reports whether the iterator is now at an item.
func (*SetIterator[T]) Compare ¶ added in v0.3.0
func (i *SetIterator[T]) Compare(a, b T) int
Compare compares two items with the set's comparison function.
func (*SetIterator[T]) First ¶ added in v0.3.0
func (i *SetIterator[T]) First() bool
First positions the iterator at the smallest item.
func (*SetIterator[T]) Item ¶ added in v0.3.0
func (i *SetIterator[T]) Item() T
Item returns the item at the iterator's position, which must be valid.
func (*SetIterator[T]) Last ¶ added in v0.3.0
func (i *SetIterator[T]) Last() bool
Last positions the iterator at the largest item.
func (*SetIterator[T]) Next ¶ added in v0.3.0
func (i *SetIterator[T]) Next() bool
Next positions the iterator at the following item.
func (*SetIterator[T]) Prev ¶ added in v0.3.0
func (i *SetIterator[T]) Prev() bool
Prev positions the iterator at the preceding item.
func (*SetIterator[T]) Reset ¶ added in v0.3.0
func (i *SetIterator[T]) Reset()
Reset marks the iterator invalid, before the first item.
func (*SetIterator[T]) SeekExact ¶ added in v0.3.0
func (i *SetIterator[T]) SeekExact(item T) bool
SeekExact positions the iterator at item and reports whether it is in the set; if not, the iterator is at the first greater item.
func (*SetIterator[T]) SeekGE ¶ added in v0.3.0
func (i *SetIterator[T]) SeekGE(item T) bool
SeekGE positions the iterator at the first item >= item.
func (*SetIterator[T]) SeekGT ¶ added in v0.3.0
func (i *SetIterator[T]) SeekGT(item T) bool
SeekGT positions the iterator at the first item > item.
func (*SetIterator[T]) SeekLE ¶ added in v0.3.0
func (i *SetIterator[T]) SeekLE(item T) bool
SeekLE positions the iterator at the last item <= item.
func (*SetIterator[T]) SeekLT ¶ added in v0.3.0
func (i *SetIterator[T]) SeekLT(item T) bool
SeekLT positions the iterator at the last item < item.
func (*SetIterator[T]) Valid ¶ added in v0.3.0
func (i *SetIterator[T]) Valid() bool
Valid reports whether the iterator is at an item.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package aug implements a copy-on-write augmented B-tree.
|
Package aug implements a copy-on-write augmented B-tree. |
|
Package interval provides copy-on-write ordered maps and sets of intervals whose iterators find every interval overlapping a query.
|
Package interval provides copy-on-write ordered maps and sets of intervals whose iterators find every interval overlapping a query. |
|
Package orderstat provides copy-on-write ordered maps and sets with order-statistic queries: the rank of a key, the entry at a rank, and the number of entries in a key range, each in O(log n).
|
Package orderstat provides copy-on-write ordered maps and sets with order-statistic queries: the rank of a key, the entry at a rank, and the number of entries in a key range, each in O(log n). |