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.
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]) 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 bool)
- func (s *Set[T]) From(lo T) iter.Seq[T]
- 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
- type SetIterator
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]) 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.Key()
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]) Cursor ¶ added in v0.2.0
Cursor returns a new cursor positioned before the first item.
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 SetIterator ¶
type SetIterator[T any] = MapIterator[T, struct{}]
SetIterator is an iterator for a Set.
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 in O(log n + k).
|
Package interval provides copy-on-write ordered maps and sets of intervals whose iterators find every interval overlapping a query in O(log n + k). |
|
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). |