Documentation
¶
Overview ¶
Package sumtree is a port of Zed's sum_tree crate: a persistent B+ tree whose nodes cache a summary of their subtree.
Index ¶
- Constants
- func FromSummary[D Dimension[D, S, C], S any, C any](summary S, cx C) D
- func PropertySumTree[T Item[S, struct{}], S Summary[S, struct{}]](values func(*rand.Rand) T, minSize, maxSize int) func(*rand.Rand) SumTree[T, S, struct{}]
- func WithAddedSummary[D Dimension[D, S, C], S any, C any](d D, summary S, cx C) D
- type Bias
- type ContextLessSummary
- type Cursor
- func (c *Cursor[T, S, C, D]) All() iter.Seq[T]
- func (c *Cursor[T, S, C, D]) Clone() *Cursor[T, S, C, D]
- func (c *Cursor[T, S, C, D]) DidSeek() bool
- func (c *Cursor[T, S, C, D]) End() D
- func (c *Cursor[T, S, C, D]) Item() (T, bool)
- func (c *Cursor[T, S, C, D]) ItemSummary() (S, bool)
- func (c *Cursor[T, S, C, D]) Next()
- func (c *Cursor[T, S, C, D]) NextItem() (T, bool)
- func (c *Cursor[T, S, C, D]) Prev()
- func (c *Cursor[T, S, C, D]) PrevItem() (T, bool)
- func (c *Cursor[T, S, C, D]) Reset()
- func (c *Cursor[T, S, C, D]) SearchBackward(filterNode func(S) bool)
- func (c *Cursor[T, S, C, D]) SearchForward(filterNode func(S) bool)
- func (c *Cursor[T, S, C, D]) Seek(pos SeekTarget[S, D, C], bias Bias) bool
- func (c *Cursor[T, S, C, D]) SeekForward(pos SeekTarget[S, D, C], bias Bias) bool
- func (c *Cursor[T, S, C, D]) Slice(end SeekTarget[S, D, C], bias Bias) SumTree[T, S, C]
- func (c *Cursor[T, S, C, D]) Start() D
- func (c *Cursor[T, S, C, D]) Suffix() SumTree[T, S, C]
- func (c *Cursor[T, S, C, D]) Summary[Output Dimension[Output, S, C]](end SeekTarget[S, D, C], bias Bias) Output
- type Dimension
- type Dimensions
- type DimensionsTarget
- type Edit
- type FilterCursor
- func (f *FilterCursor[T, S, C, D]) All() iter.Seq[T]
- func (f *FilterCursor[T, S, C, D]) End() D
- func (f *FilterCursor[T, S, C, D]) Item() (T, bool)
- func (f *FilterCursor[T, S, C, D]) ItemSummary() (S, bool)
- func (f *FilterCursor[T, S, C, D]) Next()
- func (f *FilterCursor[T, S, C, D]) Prev()
- func (f *FilterCursor[T, S, C, D]) Start() D
- type Item
- type Iter
- type KeyedItem
- type MapEntry
- type MapKey
- type MapKeyRef
- type MapSeekKey
- type MapSeekTarget
- type NoSummary
- type Node
- type OrdDimension
- type SeekTarget
- type SumTree
- func NewSumTree[T Item[S, C], S Summary[S, C], C any](cx C) SumTree[T, S, C]
- func SumTreeFromItem[T Item[S, C], S Summary[S, C], C any](item T, cx C) SumTree[T, S, C]
- func SumTreeFromIter[T Item[S, C], S Summary[S, C], C any](items []T, cx C) SumTree[T, S, C]
- func SumTreeFromParIter[T Item[S, C], S Summary[S, C], C any](items []T, cx C) SumTree[T, S, C]
- func SumTreeFromSummary[T Item[S, C], S Summary[S, C], C any](summary S) SumTree[T, S, C]
- func (t *SumTree[T, S, C]) Append(other SumTree[T, S, C], cx C)
- func (t SumTree[T, S, C]) Cursor[D Dimension[D, S, C]](cx C) *Cursor[T, S, C, D]
- func (t *SumTree[T, S, C]) Edit[K OrdDimension[K, S, C]](edits []Edit[T, K], cx C) []T
- func (t *SumTree[T, S, C]) Extend(items []T, cx C)
- func (t SumTree[T, S, C]) Extent[D Dimension[D, S, C]](cx C) D
- func (t SumTree[T, S, C]) Filter[U Dimension[U, S, C]](cx C, filterNode func(S) bool) *FilterCursor[T, S, C, U]
- func (t SumTree[T, S, C]) Find[D Dimension[D, S, C]](cx C, target SeekTarget[S, D, C], bias Bias) (D, D, T, bool)
- func (t SumTree[T, S, C]) FindExact[D Dimension[D, S, C]](cx C, target SeekTarget[S, D, C], bias Bias) (D, D, T, bool)
- func (t SumTree[T, S, C]) FindWithPrev[D Dimension[D, S, C]](cx C, target SeekTarget[S, D, C], bias Bias) (start D, end D, prev T, hasPrev bool, item T, ok bool)
- func (t SumTree[T, S, C]) First() (T, bool)
- func (t SumTree[T, S, C]) Get[K OrdDimension[K, S, C]](key K, cx C) (T, bool)
- func (t *SumTree[T, S, C]) InsertOrReplace[K OrdDimension[K, S, C]](item T, cx C) (T, bool)
- func (t SumTree[T, S, C]) IsEmpty() bool
- func (t SumTree[T, S, C]) Items(cx C) []T
- func (t SumTree[T, S, C]) Iter() iter.Seq[T]
- func (t SumTree[T, S, C]) Last() (T, bool)
- func (t SumTree[T, S, C]) LastSummary() (S, bool)
- func (t *SumTree[T, S, C]) ParExtend(items []T, cx C)
- func (t *SumTree[T, S, C]) Push(item T, cx C)
- func (t *SumTree[T, S, C]) Remove[K OrdDimension[K, S, C]](key K, cx C) (T, bool)
- func (t SumTree[T, S, C]) Summary() S
- func (t *SumTree[T, S, C]) UpdateFirst(f func(*T), cx C)
- func (t *SumTree[T, S, C]) UpdateLast(f func(*T), cx C)
- type Summary
- type TreeMap
- func (m *TreeMap[K, V]) Clear()
- func (m TreeMap[K, V]) Closest(key K) (K, V, bool)
- func (m TreeMap[K, V]) ContainsKey(key K) bool
- func (m *TreeMap[K, V]) Extend(entries iter.Seq2[K, V])
- func (m TreeMap[K, V]) First() (K, V, bool)
- func (m TreeMap[K, V]) Get(key K) (V, bool)
- func (m *TreeMap[K, V]) Insert(key K, value V)
- func (m *TreeMap[K, V]) InsertOrReplace(key K, value V) (V, bool)
- func (m *TreeMap[K, V]) InsertTree(other TreeMap[K, V])
- func (m TreeMap[K, V]) IsEmpty() bool
- func (m TreeMap[K, V]) Iter() iter.Seq2[K, V]
- func (m TreeMap[K, V]) IterFrom(from K) iter.Seq2[K, V]
- func (m TreeMap[K, V]) Last() (K, V, bool)
- func (m *TreeMap[K, V]) Remove(key K) (V, bool)
- func (m *TreeMap[K, V]) RemoveRange(start MapSeekTarget[K], end MapSeekTarget[K])
- func (m *TreeMap[K, V]) Retain(predicate func(K, V) bool)
- func (m *TreeMap[K, V]) Update(key K, f func(*V)) bool
- func (m *TreeMap[K, V]) UpdateR[R any](key K, f func(*V) R) (R, bool)
- func (m TreeMap[K, V]) Values() iter.Seq[V]
- type TreeSet
- type Unit
Constants ¶
const TreeBase = 6
TreeBase is the minimum fan-out of a node; a node holds at most 2*TreeBase children. Tests in this package run with a base of 2, as upstream's cfg(test) build does.
Variables ¶
This section is empty.
Functions ¶
func FromSummary ¶
FromSummary returns the dimension of summary measured from zero.
func PropertySumTree ¶
func PropertySumTree[T Item[S, struct{}], S Summary[S, struct{}]](values func(*rand.Rand) T, minSize, maxSize int) func(*rand.Rand) SumTree[T, S, struct{}]
PropertySumTree returns a generator of trees with between minSize and maxSize-1 items drawn from values, upstream's proptest `sum_tree` strategy without shrinking.
func WithAddedSummary ¶
WithAddedSummary returns d with summary added.
Types ¶
type Bias ¶
type Bias uint8
Bias settles ambiguities when determining positions in an ordered sequence.
type ContextLessSummary ¶
ContextLessSummary is a Summary whose context is the unit struct.
type Cursor ¶
type Cursor[T Item[S, C], S Summary[S, C], C any, D Dimension[D, S, C]] struct { Position D // contains filtered or unexported fields }
Cursor walks a SumTree, tracking its position in dimension D.
func NewCursor ¶
func NewCursor[T Item[S, C], S Summary[S, C], C any, D Dimension[D, S, C]](tree SumTree[T, S, C], cx C) *Cursor[T, S, C, D]
NewCursor returns a cursor over tree that has not yet seeked.
func (*Cursor[T, S, C, D]) All ¶
All iterates the remaining items, advancing the cursor, as upstream's Iterator impl.
func (*Cursor[T, S, C, D]) End ¶
func (c *Cursor[T, S, C, D]) End() D
End returns the position at the end of the current item.
func (*Cursor[T, S, C, D]) Item ¶
Item returns the current item; ok is false when the tree is empty or the cursor is at the end.
func (*Cursor[T, S, C, D]) ItemSummary ¶
ItemSummary returns the summary of the current item.
func (*Cursor[T, S, C, D]) Prev ¶
func (c *Cursor[T, S, C, D]) Prev()
Prev moves to the previous item.
func (*Cursor[T, S, C, D]) Reset ¶
func (c *Cursor[T, S, C, D]) Reset()
Reset returns the cursor to its unseeked state.
func (*Cursor[T, S, C, D]) SearchBackward ¶
SearchBackward moves to the previous item whose subtree summaries satisfy filterNode.
func (*Cursor[T, S, C, D]) SearchForward ¶
SearchForward moves to the next item whose subtree summaries satisfy filterNode.
func (*Cursor[T, S, C, D]) Seek ¶
func (c *Cursor[T, S, C, D]) Seek(pos SeekTarget[S, D, C], bias Bias) bool
Seek resets the cursor and moves it to target; it reports whether target was found exactly.
func (*Cursor[T, S, C, D]) SeekForward ¶
func (c *Cursor[T, S, C, D]) SeekForward(pos SeekTarget[S, D, C], bias Bias) bool
SeekForward moves the cursor forward to target; it reports whether target was found exactly.
func (*Cursor[T, S, C, D]) Slice ¶
func (c *Cursor[T, S, C, D]) Slice(end SeekTarget[S, D, C], bias Bias) SumTree[T, S, C]
Slice advances the cursor to end and returns the traversed items as a tree.
func (*Cursor[T, S, C, D]) Start ¶
func (c *Cursor[T, S, C, D]) Start() D
Start returns the position at the start of the current item.
type Dimension ¶
Dimension is a quantity measured by a Summary S that can be used to seek in a SumTree. Every Summary is a Dimension of itself.
type Dimensions ¶
type Dimensions[S any, C any, D1 Dimension[D1, S, C], D2 Dimension[D2, S, C], D3 Dimension[D3, S, C]] struct { V0 D1 V1 D2 V2 D3 }
Dimensions measures up to three dimensions at once.
func (Dimensions[S, C, D1, D2, D3]) AddSummary ¶
func (d Dimensions[S, C, D1, D2, D3]) AddSummary(summary S, cx C) Dimensions[S, C, D1, D2, D3]
AddSummary adds summary to every dimension.
func (Dimensions[S, C, D1, D2, D3]) Zero ¶
func (Dimensions[S, C, D1, D2, D3]) Zero(cx C) Dimensions[S, C, D1, D2, D3]
Zero returns the zero of every dimension.
type DimensionsTarget ¶
type DimensionsTarget[S any, C any, D1 Dimension[D1, S, C], D2 Dimension[D2, S, C], D3 Dimension[D3, S, C]] struct { Target SeekTarget[S, D1, C] }
DimensionsTarget seeks a Dimensions cursor by its first dimension.
func (DimensionsTarget[S, C, D1, D2, D3]) Cmp ¶
func (t DimensionsTarget[S, C, D1, D2, D3]) Cmp(cursorLocation Dimensions[S, C, D1, D2, D3], cx C) int
Cmp compares the target with the first dimension of cursorLocation.
type Edit ¶
Edit is an insertion of an item or a removal by key, applied by SumTree.Edit.
func EditInsert ¶
EditInsert returns an edit inserting item.
func EditRemove ¶
EditRemove returns an edit removing the item with key.
type FilterCursor ¶
type FilterCursor[T Item[S, C], S Summary[S, C], C any, D Dimension[D, S, C]] struct { // contains filtered or unexported fields }
FilterCursor is a Cursor that skips subtrees whose summary fails a filter.
func NewFilterCursor ¶
func NewFilterCursor[T Item[S, C], S Summary[S, C], C any, D Dimension[D, S, C]](tree SumTree[T, S, C], cx C, filterNode func(S) bool) *FilterCursor[T, S, C, D]
NewFilterCursor returns a filter cursor over tree.
func (*FilterCursor[T, S, C, D]) All ¶
func (f *FilterCursor[T, S, C, D]) All() iter.Seq[T]
All iterates the remaining matching items, advancing the cursor.
func (*FilterCursor[T, S, C, D]) End ¶
func (f *FilterCursor[T, S, C, D]) End() D
End returns the position at the end of the current item.
func (*FilterCursor[T, S, C, D]) Item ¶
func (f *FilterCursor[T, S, C, D]) Item() (T, bool)
Item returns the current item.
func (*FilterCursor[T, S, C, D]) ItemSummary ¶
func (f *FilterCursor[T, S, C, D]) ItemSummary() (S, bool)
ItemSummary returns the summary of the current item.
func (*FilterCursor[T, S, C, D]) Next ¶
func (f *FilterCursor[T, S, C, D]) Next()
Next moves to the next matching item.
func (*FilterCursor[T, S, C, D]) Prev ¶
func (f *FilterCursor[T, S, C, D]) Prev()
Prev moves to the previous matching item.
func (*FilterCursor[T, S, C, D]) Start ¶
func (f *FilterCursor[T, S, C, D]) Start() D
Start returns the position at the start of the current item.
type Iter ¶
type Iter[T Item[S, C], S Summary[S, C], C any] struct { // contains filtered or unexported fields }
Iter iterates the items of a SumTree in order.
type MapKey ¶
MapKey is the summary of a TreeMap subtree: its greatest key, or none.
func (MapKey[K]) AddSummary ¶
AddSummary returns summary, the greatest key so far.
type MapKeyRef ¶
MapKeyRef is the key dimension used to seek in a TreeMap.
func (MapKeyRef[K]) AddSummary ¶
AddSummary returns the key of summary.
type MapSeekKey ¶
MapSeekKey is a key used as a MapSeekTarget, upstream's blanket impl for `K: Ord`.
func (MapSeekKey[K]) CmpCursor ¶
func (k MapSeekKey[K]) CmpCursor(cursorLocation K) int
CmpCursor compares the key with cursorLocation.
type MapSeekTarget ¶
MapSeekTarget is a position in a TreeMap's key space.
type NoSummary ¶
type NoSummary struct{}
NoSummary is a catch-all Summary that carries no data.
func (NoSummary) AddSummary ¶
AddSummary returns NoSummary.
type Node ¶
type Node[T Item[S, C], S Summary[S, C], C any] struct { // contains filtered or unexported fields }
Node is one node of a SumTree: internal when it has child trees, a leaf when it has items.
type OrdDimension ¶
type OrdDimension[K any, S any, C any] interface { Dimension[K, S, C] SeekTarget[S, K, C] }
OrdDimension is a dimension that is also a seek target for itself, the Go form of upstream's `Dimension + Ord` key bound.
type SeekTarget ¶
SeekTarget is a position a cursor over dimension D can seek to. Cmp compares the target with the cursor location like cmp.Compare.
type SumTree ¶
type SumTree[T Item[S, C], S Summary[S, C], C any] struct { // contains filtered or unexported fields }
SumTree is a B+ tree whose leaves hold items and whose nodes hold the summary of their subtree. Copying a SumTree is cheap; nodes are shared and copied on write. The zero value is an empty tree under the zero context.
func NewSumTree ¶
NewSumTree returns an empty tree.
func SumTreeFromItem ¶
SumTreeFromItem returns a tree holding item.
func SumTreeFromIter ¶
SumTreeFromIter builds a tree from items in order.
func SumTreeFromParIter ¶
SumTreeFromParIter builds a tree from items in chunks, as upstream's parallel constructor does; the Go port runs the chunks sequentially.
func SumTreeFromSummary ¶
SumTreeFromSummary returns an empty tree with the given zero summary.
func (*SumTree[T, S, C]) Edit ¶
func (t *SumTree[T, S, C]) Edit[K OrdDimension[K, S, C]](edits []Edit[T, K], cx C) []T
Edit applies edits and returns the items they replaced or removed.
func (*SumTree[T, S, C]) Extend ¶
func (t *SumTree[T, S, C]) Extend(items []T, cx C)
Extend appends items.
func (SumTree[T, S, C]) Filter ¶
func (t SumTree[T, S, C]) Filter[U Dimension[U, S, C]](cx C, filterNode func(S) bool) *FilterCursor[T, S, C, U]
Filter returns a cursor that only visits subtrees whose summary satisfies filterNode.
func (SumTree[T, S, C]) Find ¶
func (t SumTree[T, S, C]) Find[D Dimension[D, S, C]](cx C, target SeekTarget[S, D, C], bias Bias) (D, D, T, bool)
Find is a faster Cursor + Seek + Item.
func (SumTree[T, S, C]) FindExact ¶
func (t SumTree[T, S, C]) FindExact[D Dimension[D, S, C]](cx C, target SeekTarget[S, D, C], bias Bias) (D, D, T, bool)
FindExact returns the start and end of the item that exactly matches target, and the item.
func (SumTree[T, S, C]) FindWithPrev ¶
func (t SumTree[T, S, C]) FindWithPrev[D Dimension[D, S, C]](cx C, target SeekTarget[S, D, C], bias Bias) (start D, end D, prev T, hasPrev bool, item T, ok bool)
FindWithPrev is Find that also returns the item before the found one.
func (SumTree[T, S, C]) Get ¶
func (t SumTree[T, S, C]) Get[K OrdDimension[K, S, C]](key K, cx C) (T, bool)
Get returns the item with the given key.
func (*SumTree[T, S, C]) InsertOrReplace ¶
func (t *SumTree[T, S, C]) InsertOrReplace[K OrdDimension[K, S, C]](item T, cx C) (T, bool)
InsertOrReplace inserts item, replacing and returning the item with the same key.
func (SumTree[T, S, C]) Items ¶
func (t SumTree[T, S, C]) Items(cx C) []T
Items returns all items in order.
func (SumTree[T, S, C]) LastSummary ¶
LastSummary returns the summary of the last item.
func (*SumTree[T, S, C]) ParExtend ¶
func (t *SumTree[T, S, C]) ParExtend(items []T, cx C)
ParExtend appends items through SumTreeFromParIter.
func (*SumTree[T, S, C]) Push ¶
func (t *SumTree[T, S, C]) Push(item T, cx C)
Push appends one item.
func (*SumTree[T, S, C]) Remove ¶
func (t *SumTree[T, S, C]) Remove[K OrdDimension[K, S, C]](key K, cx C) (T, bool)
Remove removes and returns the item with the given key.
func (SumTree[T, S, C]) Summary ¶
func (t SumTree[T, S, C]) Summary() S
Summary returns the summary of the whole tree.
func (*SumTree[T, S, C]) UpdateFirst ¶
func (t *SumTree[T, S, C]) UpdateFirst(f func(*T), cx C)
UpdateFirst applies f to the first item and refreshes the summaries above it.
func (*SumTree[T, S, C]) UpdateLast ¶
func (t *SumTree[T, S, C]) UpdateLast(f func(*T), cx C)
UpdateLast applies f to the last item and refreshes the summaries above it.
type Summary ¶
Summary describes the sum of all items in a subtree. Zero is called on the zero value of S; AddSummary returns the receiver with summary added.
type TreeMap ¶
TreeMap is a cheaply-copyable ordered map based on a SumTree.
func TreeMapFromOrderedEntries ¶
TreeMapFromOrderedEntries builds a map from entries already in key order.
func (TreeMap[K, V]) Closest ¶
Closest returns the entry with the greatest key less than or equal to key.
func (TreeMap[K, V]) ContainsKey ¶
ContainsKey reports whether key is present.
func (*TreeMap[K, V]) Insert ¶
func (m *TreeMap[K, V]) Insert(key K, value V)
Insert sets key to value.
func (*TreeMap[K, V]) InsertOrReplace ¶
InsertOrReplace sets key to value and returns the previous value.
func (*TreeMap[K, V]) InsertTree ¶
InsertTree inserts every entry of other, replacing existing keys.
func (*TreeMap[K, V]) RemoveRange ¶
func (m *TreeMap[K, V]) RemoveRange(start MapSeekTarget[K], end MapSeekTarget[K])
RemoveRange removes the entries from start up to end.
func (*TreeMap[K, V]) Update ¶
Update applies f to the value stored under key and reports whether it was present.
type TreeSet ¶
TreeSet is an ordered set based on a TreeMap.
func TreeSetFromOrderedEntries ¶
TreeSetFromOrderedEntries builds a set from keys already in order.