Documentation
¶
Overview ¶
Package tree contains an implementation of a B-tree Map and Set. These are similar to Go's map built-in, but keep elements in sorted order.
Index ¶
- type KVPair
- type Map
- func (m Map[K, V]) Contains(k K) bool
- func (m Map[K, V]) Cursor() *MapCursor[K, V]
- func (m Map[K, V]) Delete(k K)
- func (m Map[K, V]) First() (K, V)
- func (m Map[K, V]) Get(k K) V
- func (m Map[K, V]) Iterate() iterator.Iterator[KVPair[K, V]]
- func (m Map[K, V]) Last() (K, V)
- func (m Map[K, V]) Len() int
- func (m Map[K, V]) Put(k K, v V)
- type MapCursor
- func (c *MapCursor[K, V]) Backward() iterator.Iterator[KVPair[K, V]]
- func (c *MapCursor[K, V]) Forward() iterator.Iterator[KVPair[K, V]]
- func (c *MapCursor[K, V]) Key() K
- func (c *MapCursor[K, V]) Next()
- func (c *MapCursor[K, V]) Ok() bool
- func (c *MapCursor[K, V]) Prev()
- func (c *MapCursor[K, V]) SeekFirst()
- func (c *MapCursor[K, V]) SeekFirstGreater(k K)
- func (c *MapCursor[K, V]) SeekFirstGreaterOrEqual(k K)
- func (c *MapCursor[K, V]) SeekLast()
- func (c *MapCursor[K, V]) SeekLastLess(k K)
- func (c *MapCursor[K, V]) SeekLastLessOrEqual(k K)
- func (c *MapCursor[K, V]) Value() V
- type Set
- type SetCursor
- func (c *SetCursor[T]) Backward() iterator.Iterator[T]
- func (c *SetCursor[T]) Forward() iterator.Iterator[T]
- func (c *SetCursor[T]) Item() T
- func (c *SetCursor[T]) Next()
- func (c *SetCursor[T]) Ok() bool
- func (c *SetCursor[T]) Prev()
- func (c *SetCursor[T]) SeekFirst()
- func (c *SetCursor[T]) SeekFirstGreater(x T)
- func (c *SetCursor[T]) SeekFirstGreaterOrEqual(x T)
- func (c *SetCursor[T]) SeekLast()
- func (c *SetCursor[T]) SeekLastLess(x T)
- func (c *SetCursor[T]) SeekLastLessOrEqual(x T)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Map ¶
Map is a tree-structured key-value map, similar to Go's built-in map but keeps elements in sorted order by key.
It is safe for multiple goroutines to Put concurrently with keys that are already in the map.
func NewMap ¶
NewMap returns a Map that uses less to determine the sort order of keys. If !less(a, b) && !less(b, a), then a and b are considered the same key. The output of less must not change for any pair of keys while they are in the map.
func (Map[K, V]) First ¶
func (m Map[K, V]) First() (K, V)
First returns the lowest-keyed entry in the map according to less.
func (Map[K, V]) Get ¶
func (m Map[K, V]) Get(k K) V
Get returns the value associated with the given key if it is present in the map. Otherwise, it returns the zero-value of V.
func (Map[K, V]) Iterate ¶
Iterate returns an iterator that yields the elements of the map in sorted order by key.
The map may be safely modified during iteration and the iterator will continue from the next-lowest key. Thus the iterator will see new elements that are after the current position of the iterator according to less, but will not necessarily see a consistent snapshot of the state of the map.
type MapCursor ¶
MapCursor is a cursor into a Map.
A cursor is usable while a map is being modified. If the element the cursor is at is deleted, Key() will continue to return the key and Value() will return the zero value of V until it is moved.
func (*MapCursor[K, V]) Backward ¶
Backward returns an iterator that starts from the cursor's position and yields all of the elements less than or equal to the cursor in descending order.
This iterator's Next method is amortized O(1), unless the map changes in which case the following Next is O(log(n)) where n is the number of elements in the map.
func (*MapCursor[K, V]) Forward ¶
Forward returns an iterator that starts from the cursor's position and yields all of the elements greater than or equal to the cursor in ascending order.
This iterator's Next method is amortized O(1), unless the map changes in which case the following Next is O(log(n)) where n is the number of elements in the map.
func (*MapCursor[K, V]) Key ¶
func (c *MapCursor[K, V]) Key() K
Key returns the key of the element that the cursor is at. Panics if Ok is false.
func (*MapCursor[K, V]) Next ¶
func (c *MapCursor[K, V]) Next()
Next moves the cursor to the next element in the map.
Next is amortized O(1) unless the map has been modified since the last cursor move, in which case it's O(log(n)).
func (*MapCursor[K, V]) Ok ¶
Ok returns false if the cursor is not currently placed at an element, for example if Next advances past the last element.
func (*MapCursor[K, V]) Prev ¶
func (c *MapCursor[K, V]) Prev()
Prev moves the cursor to the previous element in the map.
Prev is amortized O(1) unless the map has been modified since the last cursor move, in which case it's O(log(n)).
func (*MapCursor[K, V]) SeekFirst ¶
func (c *MapCursor[K, V]) SeekFirst()
SeekFirst moves the cursor to the first element in the map.
SeekFirst is O(log(n)).
func (*MapCursor[K, V]) SeekFirstGreater ¶
func (c *MapCursor[K, V]) SeekFirstGreater(k K)
SeekFirstGreater moves the cursor to the element in the map just after k.
SeekFirstGreater is O(log(n)).
func (*MapCursor[K, V]) SeekFirstGreaterOrEqual ¶
func (c *MapCursor[K, V]) SeekFirstGreaterOrEqual(k K)
SeekFirstGreaterOrEqual moves the cursor to the element in the map with the least key that is greater than or equal to k.
SeetFirstGreaterOrEqual is O(log(n)).
func (*MapCursor[K, V]) SeekLast ¶
func (c *MapCursor[K, V]) SeekLast()
SeekLast moves the cursor to the last element in the map.
SeekLast is O(log(n)).
func (*MapCursor[K, V]) SeekLastLess ¶
func (c *MapCursor[K, V]) SeekLastLess(k K)
SeekLastLess moves the cursor to the element in the map just before k.
SeekLastLess is O(log(n)).
func (*MapCursor[K, V]) SeekLastLessOrEqual ¶
func (c *MapCursor[K, V]) SeekLastLessOrEqual(k K)
SeekLastLessOrEqual moves the cursor to the element in the map with the greatest key that is less than or equal to k.
SeekLastLessOrEqual is O(log(n)).
type Set ¶
type Set[T any] struct { // contains filtered or unexported fields }
Set is a tree-structured set. Sets are a collection of unique elements. Similar to Go's built-in map[T]struct{} but keeps elements in sorted order.
func NewSet ¶
NewSet returns a Set that uses less to determine the sort order of items. If !less(a, b) && !less(b, a), then a and b are considered the same item. The output of less must not change for any pair of items while they are in the set.
func (Set[T]) Add ¶
func (s Set[T]) Add(item T)
Add adds item to the set if it is not already present.
func (Set[T]) First ¶
func (s Set[T]) First() T
First returns the lowest item in the set according to less.
func (Set[T]) Iterate ¶
Iterate returns an iterator that yields the elements of the set in sorted order.
The set may be safely modified during iteration and the iterator will continue from the next-lowest item. Thus the iterator will see new items that are after the current position of the iterator according to less, but will not necessarily see a consistent snapshot of the state of the set.
type SetCursor ¶
type SetCursor[T any] struct { // contains filtered or unexported fields }
SetCursor is a cursor into a Set.
A cursor is usable while a set is being modified. If the item the cursor is at is deleted, the cursor will still return the old item until it is moved.
func (*SetCursor[T]) Backward ¶
Backward returns an iterator that starts from the cursor's position and yields all of the elements less than or equal to the cursor in descending order.
This iterator's Next method is amortized O(1), unless the map changes in which case the following Next is O(log(n)) where n is the number of elements in the map.
func (*SetCursor[T]) Forward ¶
Forward returns an iterator that starts from the cursor's position and yields all of the elements greater than or equal to the cursor in ascending order.
This iterator's Next method is amortized O(1), unless the map changes in which case the following Next is O(log(n)) where n is the number of elements in the map.
func (*SetCursor[T]) Item ¶
func (c *SetCursor[T]) Item() T
Item returns the item that the cursor is at. Panics if Ok is false.
func (*SetCursor[T]) Next ¶
func (c *SetCursor[T]) Next()
Next moves the cursor to the next item in the set.
Next is amortized O(1) unless the map has been modified since the last cursor move, in which case it's O(log(n)).
func (*SetCursor[T]) Ok ¶
Ok returns false if the cursor is not currently placed at an item, for example if Next advances past the last item.
func (*SetCursor[T]) Prev ¶
func (c *SetCursor[T]) Prev()
Prev moves the cursor to the previous item in the set.
Prev is amortized O(1) unless the map has been modified since the last cursor move, in which case it's O(log(n)).
func (*SetCursor[T]) SeekFirst ¶
func (c *SetCursor[T]) SeekFirst()
SeekFirst moves the cursor to the first item in the set.
SeekFirst is O(log(n)).
func (*SetCursor[T]) SeekFirstGreater ¶
func (c *SetCursor[T]) SeekFirstGreater(x T)
SeekFirstGreater moves the cursor to the item in the set just after x.
SeekFirstGreater is O(log(n)).
func (*SetCursor[T]) SeekFirstGreaterOrEqual ¶
func (c *SetCursor[T]) SeekFirstGreaterOrEqual(x T)
SeekFirstGreaterOrEqual moves the cursor to the least item in the set that is greater than or equal to x.
SeetFirstGreaterOrEqual is O(log(n)).
func (*SetCursor[T]) SeekLast ¶
func (c *SetCursor[T]) SeekLast()
SeekLast moves the cursor to the last item in the set.
SeekLast is O(log(n)).
func (*SetCursor[T]) SeekLastLess ¶
func (c *SetCursor[T]) SeekLastLess(x T)
SeekLastLess moves the cursor to the item in the set just before x.
SeekLastLess is O(log(n)).
func (*SetCursor[T]) SeekLastLessOrEqual ¶
func (c *SetCursor[T]) SeekLastLessOrEqual(x T)
SeekLastLessOrEqual moves the cursor to the greatest item in the set that is less than or equal to x.
SeekLastLessOrEqual is O(log(n)).