3
0
mirror of https://github.com/ergochat/ergo.git synced 2024-12-25 04:02:38 +01:00
ergo/vendor/github.com/tidwall/btree/btree.go

274 lines
6.2 KiB
Go
Raw Normal View History

2020-11-08 23:55:22 +01:00
// Copyright 2020 Joshua J Baker. All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
package btree
2020-11-08 23:55:22 +01:00
type BTree struct {
2023-01-15 14:26:32 +01:00
base *BTreeG[any]
}
2020-11-08 23:55:22 +01:00
// New returns a new BTree
2023-01-15 14:26:32 +01:00
func New(less func(a, b any) bool) *BTree {
2022-06-16 19:36:02 +02:00
if less == nil {
panic("nil less")
}
return &BTree{
2023-01-15 14:26:32 +01:00
base: NewBTreeG(less),
2022-06-16 19:36:02 +02:00
}
2021-08-03 07:46:20 +02:00
}
// NewNonConcurrent returns a new BTree which is not safe for concurrent
// write operations by multiple goroutines.
//
// This is useful for when you do not need the BTree to manage the locking,
// but would rather do it yourself.
2023-01-15 14:26:32 +01:00
func NewNonConcurrent(less func(a, b any) bool) *BTree {
2020-11-08 23:55:22 +01:00
if less == nil {
panic("nil less")
}
2022-06-16 19:36:02 +02:00
return &BTree{
2023-01-15 14:26:32 +01:00
base: NewBTreeGOptions(less,
Options{
NoLocks: true,
}),
2022-06-16 19:36:02 +02:00
}
}
2020-11-08 23:55:22 +01:00
// Less is a convenience function that performs a comparison of two items
// using the same "less" function provided to New.
2023-01-15 14:26:32 +01:00
func (tr *BTree) Less(a, b any) bool {
2022-06-16 19:36:02 +02:00
return tr.base.Less(a, b)
}
2021-08-03 07:46:20 +02:00
2022-06-16 19:36:02 +02:00
// Set or replace a value for a key
2023-01-15 14:26:32 +01:00
// Returns the value for the replaced item or nil if the key was not found.
func (tr *BTree) Set(item any) (prev any) {
2022-06-16 19:36:02 +02:00
return tr.SetHint(item, nil)
}
2020-11-08 23:55:22 +01:00
// SetHint sets or replace a value for a key using a path hint
2023-01-15 14:26:32 +01:00
// Returns the value for the replaced item or nil if the key was not found.
func (tr *BTree) SetHint(item any, hint *PathHint) (prev any) {
2020-11-08 23:55:22 +01:00
if item == nil {
panic("nil item")
}
2022-06-16 19:36:02 +02:00
v, ok := tr.base.SetHint(item, hint)
if !ok {
return nil
}
2022-06-16 19:36:02 +02:00
return v
}
2023-01-15 14:26:32 +01:00
// Get a value for key.
// Returns nil if the key was not found.
func (tr *BTree) Get(key any) any {
2022-06-16 19:36:02 +02:00
return tr.GetHint(key, nil)
2020-11-08 23:55:22 +01:00
}
2023-01-15 14:26:32 +01:00
// GetHint gets a value for key using a path hint.
// Returns nil if the item was not found.
func (tr *BTree) GetHint(key any, hint *PathHint) (value any) {
2022-06-16 19:36:02 +02:00
if key == nil {
return nil
}
2022-06-16 19:36:02 +02:00
v, ok := tr.base.GetHint(key, hint)
if !ok {
return nil
}
2022-06-16 19:36:02 +02:00
return v
}
2020-11-08 23:55:22 +01:00
// Len returns the number of items in the tree
func (tr *BTree) Len() int {
2022-06-16 19:36:02 +02:00
return tr.base.Len()
2020-11-08 23:55:22 +01:00
}
2023-01-15 14:26:32 +01:00
// Delete an item for a key.
// Returns the deleted value or nil if the key was not found.
func (tr *BTree) Delete(key any) (prev any) {
2020-11-08 23:55:22 +01:00
return tr.DeleteHint(key, nil)
}
// DeleteHint deletes a value for a key using a path hint
2023-01-15 14:26:32 +01:00
// Returns the deleted value or nil if the key was not found.
func (tr *BTree) DeleteHint(key any, hint *PathHint) (prev any) {
2022-06-16 19:36:02 +02:00
if key == nil {
return nil
}
2022-06-16 19:36:02 +02:00
v, ok := tr.base.DeleteHint(key, nil)
if !ok {
return nil
}
2022-06-16 19:36:02 +02:00
return v
}
2020-11-08 23:55:22 +01:00
// Ascend the tree within the range [pivot, last]
// Pass nil for pivot to scan all item in ascending order
// Return false to stop iterating
2023-01-15 14:26:32 +01:00
func (tr *BTree) Ascend(pivot any, iter func(item any) bool) {
2020-11-08 23:55:22 +01:00
if pivot == nil {
2022-06-16 19:36:02 +02:00
tr.base.Scan(iter)
} else {
tr.base.Ascend(pivot, iter)
}
}
2020-11-08 23:55:22 +01:00
// Descend the tree within the range [pivot, first]
// Pass nil for pivot to scan all item in descending order
// Return false to stop iterating
2023-01-15 14:26:32 +01:00
func (tr *BTree) Descend(pivot any, iter func(item any) bool) {
2020-11-08 23:55:22 +01:00
if pivot == nil {
2022-06-16 19:36:02 +02:00
tr.base.Reverse(iter)
} else {
tr.base.Descend(pivot, iter)
}
}
2020-11-08 23:55:22 +01:00
// Load is for bulk loading pre-sorted items
2023-01-15 14:26:32 +01:00
// If the load replaces and existing item then the value for the replaced item
// is returned.
func (tr *BTree) Load(item any) (prev any) {
2020-11-08 23:55:22 +01:00
if item == nil {
panic("nil item")
}
2022-06-16 19:36:02 +02:00
v, ok := tr.base.Load(item)
if !ok {
return nil
}
2022-06-16 19:36:02 +02:00
return v
}
2020-11-08 23:55:22 +01:00
// Min returns the minimum item in tree.
// Returns nil if the tree has no items.
2023-01-15 14:26:32 +01:00
func (tr *BTree) Min() any {
2022-06-16 19:36:02 +02:00
v, ok := tr.base.Min()
if !ok {
return nil
}
2022-06-16 19:36:02 +02:00
return v
}
2020-11-08 23:55:22 +01:00
// Max returns the maximum item in tree.
// Returns nil if the tree has no items.
2023-01-15 14:26:32 +01:00
func (tr *BTree) Max() any {
2022-06-16 19:36:02 +02:00
v, ok := tr.base.Max()
if !ok {
return nil
}
2022-06-16 19:36:02 +02:00
return v
}
2020-11-08 23:55:22 +01:00
// PopMin removes the minimum item in tree and returns it.
// Returns nil if the tree has no items.
2023-01-15 14:26:32 +01:00
func (tr *BTree) PopMin() any {
2022-06-16 19:36:02 +02:00
v, ok := tr.base.PopMin()
if !ok {
return nil
}
2022-06-16 19:36:02 +02:00
return v
}
2023-01-15 14:26:32 +01:00
// PopMax removes the maximum item in tree and returns it.
2020-11-08 23:55:22 +01:00
// Returns nil if the tree has no items.
2023-01-15 14:26:32 +01:00
func (tr *BTree) PopMax() any {
2022-06-16 19:36:02 +02:00
v, ok := tr.base.PopMax()
if !ok {
return nil
}
2022-06-16 19:36:02 +02:00
return v
2021-08-03 07:46:20 +02:00
}
// GetAt returns the value at index.
// Return nil if the tree is empty or the index is out of bounds.
2023-01-15 14:26:32 +01:00
func (tr *BTree) GetAt(index int) any {
2022-06-16 19:36:02 +02:00
v, ok := tr.base.GetAt(index)
if !ok {
2021-08-03 07:46:20 +02:00
return nil
}
2022-06-16 19:36:02 +02:00
return v
2021-08-03 07:46:20 +02:00
}
// DeleteAt deletes the item at index.
// Return nil if the tree is empty or the index is out of bounds.
2023-01-15 14:26:32 +01:00
func (tr *BTree) DeleteAt(index int) any {
2022-06-16 19:36:02 +02:00
v, ok := tr.base.DeleteAt(index)
if !ok {
2021-08-03 07:46:20 +02:00
return nil
}
2022-06-16 19:36:02 +02:00
return v
2020-11-08 23:55:22 +01:00
}
// Height returns the height of the tree.
// Returns zero if tree has no items.
func (tr *BTree) Height() int {
2022-06-16 19:36:02 +02:00
return tr.base.Height()
2020-11-08 23:55:22 +01:00
}
// Walk iterates over all items in tree, in order.
// The items param will contain one or more items.
2023-01-15 14:26:32 +01:00
func (tr *BTree) Walk(iter func(items []any)) {
tr.base.Walk(func(items []any) bool {
2022-06-16 19:36:02 +02:00
iter(items)
return true
})
2020-11-08 23:55:22 +01:00
}
2022-06-16 19:36:02 +02:00
// Copy the tree. This is a copy-on-write operation and is very fast because
// it only performs a shadowed copy.
func (tr *BTree) Copy() *BTree {
return &BTree{base: tr.base.Copy()}
}
2022-06-16 19:36:02 +02:00
type Iter struct {
2023-01-15 14:26:32 +01:00
base GenericIter[any]
}
2021-08-03 07:46:20 +02:00
2022-06-16 19:36:02 +02:00
// Iter returns a read-only iterator.
// The Release method must be called finished with iterator.
func (tr *BTree) Iter() Iter {
return Iter{tr.base.Iter()}
2021-08-03 07:46:20 +02:00
}
2022-06-16 19:36:02 +02:00
// Seek to item greater-or-equal-to key.
// Returns false if there was no item found.
2023-01-15 14:26:32 +01:00
func (iter *Iter) Seek(key any) bool {
2022-06-16 19:36:02 +02:00
return iter.base.Seek(key)
2021-08-03 07:46:20 +02:00
}
2022-06-16 19:36:02 +02:00
// First moves iterator to first item in tree.
// Returns false if the tree is empty.
func (iter *Iter) First() bool {
return iter.base.First()
}
// Last moves iterator to last item in tree.
// Returns false if the tree is empty.
func (iter *Iter) Last() bool {
return iter.base.Last()
}
// First moves iterator to first item in tree.
// Returns false if the tree is empty.
func (iter *Iter) Release() {
iter.base.Release()
}
// Next moves iterator to the next item in iterator.
// Returns false if the tree is empty or the iterator is at the end of
// the tree.
func (iter *Iter) Next() bool {
return iter.base.Next()
}
// Prev moves iterator to the previous item in iterator.
// Returns false if the tree is empty or the iterator is at the beginning of
// the tree.
func (iter *Iter) Prev() bool {
return iter.base.Prev()
2021-08-03 07:46:20 +02:00
}
2022-06-16 19:36:02 +02:00
// Item returns the current iterator item.
2023-01-15 14:26:32 +01:00
func (iter *Iter) Item() any {
2022-06-16 19:36:02 +02:00
return iter.base.Item()
2021-08-03 07:46:20 +02:00
}