btree

package module
v2.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 2, 2026 License: MIT Imports: 13 Imported by: 2 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

[!IMPORTANT] Version 2 was released on 2 Nov 2026. There are major changes if you coming from an older version.

btree

GoDoc

An efficient B-tree for Go.

Features

  • Includes a variety of common collections types. All backed by a btree structure
  • Modern Go ergonomics with a friendly API
  • Adopts Go generic collection types, _Abstract(Collection/Map/Set)
  • All data operations are O(log n) complexity
  • Instant copy-on-write (shadow clones), providing O(1) snapshots
  • Uses btree counting for O(log n) random access
  • Exhaustively tested code with 100% coverage
  • Optimized for high performance and low memory

Types

  • Map: Key value pairs. Sorting ordered by key
  • Set: Like Map, but only for storing keys. No values
  • Array: Dynamic array of unsorted data
  • Table: Data sorted by key fields or a custom compare function
  • Stack: LIFO (last-in, first-out) data structure
  • Queue: FIFO (first-in, first-out) data structure
  • Deque: Double-ended queue
  • Prique: Priority queue

Using

go get github.com/tidwall/btree/v2

Migrating from v1 to v2

Major changes to be aware of when migrating from v1 to v2.

  • Iteration now uses the modern Go iter.Seq instead of callbacks
  • Get() now returns one value. For the two value return use Lookup()
  • The btree.BtreeG type is now btree.Table
  • DeleteRange() has been renamed to Drain()
  • Insert() and Replace() are available along with the traditional Set()
  • Locking and mutexes have been removed from all btrees
  • The import is now github.com/tidwall/btree/v2

Map

btree.Map is a sorted associative collection of key-value pairs with unique keys. The keys adhere to the parameter type cmp.Ordered and are naturally sorted using cmp.Compare.

Operations
Insert(key, val)        # Insert an item. (does not replace if already exists)
Replace(key, val)       # Replace an existing item. (does not insert if not exists)
Set(key, val)           # Insert or replace an item.
Lookup(key)             # Get an existing item, returning false if not exists
Get(key)                # Get an existing item.
Contains(key)           # Test if an item exists.
Delete(key)             # Remove an item.

Seek(key)               # Searches for the first item that is >= to key.
SeekNext(key)           # Searches for the first item that is > key.
SeekPrev(key)           # Searches for the first item that is < key.

All()                   # Iterate items in ascending order.            (iter.Seq2[K,V])
Backward()              # Iterate items in descending order.           (iter.Seq2[K,V])
Ascend(key)             # Iterate items in ascending order >= to key.  (iter.Seq2[K,V])
Descend(key)            # Iterate items in descending order <= to key. (iter.Seq2[K,V])
Keys()                  # Iterate key only in ascending order.         (iter.Seq[K])
Values()                # Iterate values only in ascending order.      (iter.Seq[K])

PushFront(key, val)     # Insert item to front of map.
PushBack(key, val)      # Insert item to back of map.
PopFront()              # Remove the first item.
PopBack()               # Remove the last item.
Front()                 # Get the first item.
Back()                  # Get the last item.
PopFrontFunc(cond)      # Remove first item if cond returns true.
PopBackFunc(cond)       # Remove last item if cond returns true.

InsertAt(i, key, val)   # Inserts item at index. (collection size grows by one)
ReplaceAt(i, key, val)  # Replace an item at index.
GetAt(i)                # Get an item at index.
IndexOf(key)            # Get the index of an item.
DeleteAt(i)             # Remove an item at index.
AscendAt(i)             # Iterate items in ascending order >= to index.  (iter.Seq2[K,V])
DescendAt(i)            # Iterate items in descending order <= to index. (iter.Seq2[K,V])

Drain(min, max)         # Remove and extract items within the provided sub-range. [min,max)
DrainAt(i, count)       # Remove and extract items starting at index.

Action(key, actfunc)    # Perform action (replace/delete) using custom function
DeleteFunc(delfunc)     # Delete item using custom function

Len()                   # Get the number of items in map.
Clone()                 # Clone map, fast O(1), uses copy-on-write shadow cloning.
Clear()                 # Remove all items from map
Release()               # Same as Clear() but optimized for cloned collections.

String()                # String representation
Example
package main

import (
	"fmt"

	"github.com/tidwall/btree/v2"
)

func main() {
	// Create a map
	var users btree.Map[string, string]

	// Add some users
	users.Insert("user:4", "Andrea")
	users.Insert("user:6", "Andy")
	users.Insert("user:2", "Andy")
	users.Insert("user:1", "Jane")
	users.Insert("user:5", "Janet")
	users.Insert("user:3", "Steve")

	// Iterate over the map and print each user
	for key, value := range users.All() {
		fmt.Printf("%s %s\n", key, value)
	}
	fmt.Printf("\n")

	// Delete a couple users
	users.Delete("user:5")
	users.Delete("user:1")

	// Print the map again
	for key, value := range users.All() {
		fmt.Printf("%s %s\n", key, value)
	}
	fmt.Printf("\n")
}

// Output:
// user:1 Jane
// user:2 Andy
// user:3 Steve
// user:4 Andrea
// user:5 Janet
// user:6 Andy
//
// user:2 Andy
// user:3 Steve
// user:4 Andrea
// user:6 Andy

Set

btree.Set is an associative collection of unique sorted keys. The keys adhere to the parameter type cmp.Ordered and are naturally sorted using cmp.Compare.

Operations
Insert(key)             # Insert key
Contains(key)           # Check if key exists
Delete(key)             # Remove key
Len()                   # Get the number of keys in the collection

All()                   # Iterate keys in ascending order.            (iter.Seq[K])
Backward()              # Iterate keys in descending order.           (iter.Seq[K])
Ascend(key)             # Iterate keys in ascending order >= to key.  (iter.Seq[K])
Descend(key)            # Iterate keys in descending order <= to key. (iter.Seq[K])

Seek(key)               # Searches for the first key that is >= to key
SeekNext(key)           # Searches for the first key that is > key
SeekPrev(key)           # Searches for the first key that is < key

PushFront(key)          # Insert key to front of collection
PushBack(key)           # Insert key to back of collection
PopFront()              # Remove the first key
PopBack()               # Remove the last key
Front()                 # Get the first key
Back()                  # Get the last key

InsertAt(i, key)        # Insert key at index. (collection size grows by one)
ReplaceAt(i, key)       # Replace key at index
GetAt(i)                # Gets key at index
IndexOf(key)            # Get the index of key
DeleteAt(i)             # Remove key at index
AscendAt(i)             # Iterate keys in ascending order >= to index. (iter.Seq[K])
DescendAt(i)            # Iterate key in descending order <= to index. (iter.Seq[K])

Drain(min, max)         # Remove and extract keys within the provided sub-range. [min,max)
DrainAt(i, count)       # Remove and extract keys starting at index

DeleteFunc(delfunc)     # Delete item using custom function

Len()                   # Get the number of items in collection
Clone()                 # Clone collection, fast O(1), uses Copy-on-write shadow cloning
Clear()                 # Remove all items from collection
Release()               # Same as Clear() but optimized for cloned collections

String()                # String representation

Difference(s2)          # Returns new set of keys that are in set but not in other
DifferenceWith(s2)      # Removes any key from set that are in other
Equal(s2)               # Check if two sets are equal
Intersection(s2)        # Returns new set containing item that are in both sets
IntersectionWith(s2)    # Removes any items from set that are not in other
Intersects(s2)          # Checks if sets share any keys
SymmetricDifference(s2) # Returns new set with items that are in either, but not both sets.
SymmetricDifferenceWith(s2) # Update set to the symmetric difference of both sets
Union(set)              # Returns new set containing all keys of both sets.
UnionWith(set)          # Inserts all keys of other set.

package main

import (
	"fmt"

	"github.com/tidwall/btree/v2"
)

func main() {
	// Create a set
	var names btree.Set[string]

	// Add some names
	names.Insert("Jane")
	names.Insert("Andrea")
	names.Insert("Steve")
	names.Insert("Andy")
	names.Insert("Janet")
	names.Insert("Andy")

	// Iterate over the set and print each name
	for key := range names.All() {
		fmt.Printf("%s\n", key)
	}
	fmt.Printf("\n")

	// Delete a couple names
	names.Delete("Steve")
	names.Delete("Andy")

	// Print the names again
	for key := range names.All() {
		fmt.Printf("%s\n", key)
	}
	fmt.Printf("\n")
}

// Output:
// Andrea
// Andy
// Jane
// Janet
// Steve
//
// Andrea
// Jane
// Janet

Array

btree.Array is a dynamic resizable array of unsorted items. It provides random access with O(log n) complexity to all data operations, including inserting and deleting items in the middle of the array.

Operations
Insert(i, item)       # Insert item at index. (collection size grows by one)
Replace(i, item)      # Replace existing item at index.
Lookup(i)             # Gets item at index. Returns false it not exists
Get(i)                # Gets item at index.
Delete(i)             # Remove item at index.
Contains(item)        # Checks if item exists

All()                 # Iterate items in ascending order.              (iter.Seq[T])
Backward()            # Iterate items in descending order.             (iter.Seq[T])
Ascend(i)             # Iterate items in ascending order >= to index.  (iter.Seq[T])
Descend(i)            # Iterate items in descending order <= to index. (iter.Seq[T])

PushFront(item)       # Insert item to front of collection.
PushBack(item)        # Insert item to back of collection.
PopFront()            # Remove the first item.
PopBack()             # Remove the last item.
Front()               # Get the first item.
Back()                # Get the last item.
PopFrontFunc(cond)    # Remove first item if cond returns true.
PopBackFunc(cond)     # Remove last item if cond returns true.

Drain(i, count)       # Remove and extract items starting at index.

Len()                 # Get the number of items in collection.
Clone()               # Clone collection, fast O(1), uses Copy-on-write shadow cloning.
Clear()               # Remove all items from collection
Release()             # Same as Clear() but optimized for cloned collections.

String()              # String representation
Example
package main

import (
	"fmt"

	"github.com/tidwall/btree/v2"
)

func main() {
	// Create an array of names
	var names btree.Array[string]

	// Add some names
	names.Insert(0, "Andrea")
	names.Insert(1, "Tom")
	names.Insert(2, "Andy")
	names.Insert(3, "Jane")
	names.Insert(4, "Janet")
	names.Insert(5, "Steve")

	// Iterate over the array and print each name
	for name := range names.All() {
		fmt.Printf("%s\n", name)
	}
	fmt.Printf("\n")

	// Delete a couple names
	names.Delete(3)
	names.Delete(1)

	// Print the names again
	for name := range names.All() {
		fmt.Printf("%s\n", name)
	}
	fmt.Printf("\n")
}

// Output:
// Andrea
// Tom
// Andy
// Jane
// Janet
// Steve
//
// Andrea
// Andy
// Janet
// Steve

Table

btree.Table is a general purpose btree collection for storing sorted data.

Features
Automatic type ordering

A btree.Table will automatically detect the ordering of the data type.

cmp.Ordered data type

When the data type is cmp.Ordered then cmp.Compare is used to sort the data.

var names btree.Table[string]

names.Insert("Andrea")
names.Insert("Tom")
names.Insert("Andy")
names.Insert("Jane")
names.Insert("Janet")
names.Insert("Steve")

for name := range names.All() {
	fmt.Printf("%s\n", name)
}

// Output:
// Andrea
// Andy
// Jane
// Janet
// Steve
// Tom
Struct field detection

When the data type is a struct, or pointer to struct, then the data is sorted by the first struct field that adheres to cmp.Ordered.

type User struct {
	id   int
	name string
}

var users btree.Table[User]
users.Insert(User{4, "Andrea"})
users.Insert(User{6, "Andy"})
users.Insert(User{2, "Andy"})
users.Insert(User{1, "Jane"})
users.Insert(User{5, "Janet"})
users.Insert(User{3, "Steve"})

for user := range users.All() {
	fmt.Printf("%d %s\n", user.id, user.name)
}

// Output:
// 1 Jane
// 2 Andy
// 3 Steve
// 4 Andrea
// 5 Janet
// 6 Andy
Custom comparator

A custom comparator may be used to override automatic detection.

type User struct {
	age  int
	name string
}

// sort by name, then age
users := btree.NewTableOptions(btree.TableOptions[User]{
	Compare: func(a, b User) int {
		c := cmp.Compare(a.name, b.name)
		if c == 0 {
			c = cmp.Compare(a.age, b.age)
		}
		return c
	},
})
users.Insert(User{27, "Andrea"})
users.Insert(User{54, "Andy"})
users.Insert(User{31, "Andy"})
users.Insert(User{43, "Jane"})
users.Insert(User{29, "Janet"})
users.Insert(User{62, "Steve"})

for user := range users.All() {
	fmt.Printf("%d %s\n", user.age, user.name)
}

// Output:
// 27 Andrea
// 31 Andy
// 54 Andy
// 43 Jane
// 29 Janet
// 62 Steve
Tagged struct keys

Structs may include tagged keys, which is a simple and explicit way to define key order. The syntax resembling a traditional database table.

The tag btree:"key" designates that struct field as the key.

// Order by id
type User struct {
	id   int `btree:"key"`
	name string
}

A composite key can be made by adding two or more tags.

// Order by last, then first
type User struct {
	last  string `btree:"key"`
	first string `btree:"key"`
}

By default, order of the keys are automatic. But it's possible to define the order manually by adding a .{index} to the tag.

// Order by last, then first
type User struct {
	first string `btree:"key.1"`
	last  string `btree:"key.0"`
}

Keys may be ordered ascending or descending using asc and desc, respectively. Ascending is the default.

// Order by last descending, then first ascending
type User struct {
	last  string `btree:"key,desc"`
	first string `btree:"key,asc"`
}

Text collation may be added to string keys.

// Use case-insensitive binary collation.
// Order by last descending, then first ascending.
type User struct {
 	last  string `btree:"key,binary_CI,desc"`
 	first string `btree:"key,binary_CI"`
}

Available collations:

  • binary_CS: Case sensitive binary strings (default)
  • binary_CI: Case insensitive binary strings
  • utf8_CS: Case sensitive utf8 unicode
  • utf8_CI: Case insensitive utf8 unicode

Also bin_CS, CS, bin_CI, and CI are available as shorthand for binary_CS and binary_CI.

More collations may be added in the future.

Operations

Table operations include:

Insert(item)            # Insert an item. (does not replace if already exists)
Replace(item)           # Replace an existing item. (does not insert if not exists)
Set(item)               # Insert or replace an item.
Lookup(key)             # Get an existing item. Return false if not exists
Get(key)                # Get an existing item.
Contains(key)           # Test if an item exists.
Delete(key)             # Remove an item.

Seek(key)               # Searches for the first item that is >= to key.
SeekNext(key)           # Searches for the first item that is > key.
SeekPrev(key)           # Searches for the first item that is < key.

All()                   # Iterate items in ascending order.            (iter.Seq[T])
Backward()              # Iterate items in descending order.           (iter.Seq[T])
Ascend(key)             # Iterate items in ascending order >= to key.  (iter.Seq[T])
Descend(key)            # Iterate items in descending order <= to key. (iter.Seq[T])

PushFront(item)         # Insert item to front of table.
PushBack(item)          # Insert item to back of table.
PopFront()              # Remove the first item.
PopBack()               # Remove the last item.
Front()                 # Get the first item.
Back()                  # Get the last item.
PopFrontFunc(cond)      # Remove first item if cond returns true.
PopBackFunc(cond)       # Remove last item if cond returns true.

InsertAt(i, item)       # Inserts item at index. (collection size grows by one)
ReplaceAt(i, item)      # Replace an item at index.
GetAt(i)                # Get an item at index.
IndexOf(key)            # Get the index of an item.
DeleteAt(i)             # Remove an item at index.
AscendAt(i)             # Iterate items in ascending order >= to index.  (iter.Seq[T])
DescendAt(i)            # Iterate items in descending order <= to index. (iter.Seq[T])

Drain(min, max)         # Remove and extract items within the provided sub-range. [min,max)
DrainAt(i, count)       # Remove and extract items starting at index.

Len()                   # Get the number of items in collection.
Clone()                 # Clone collection, fast O(1), uses Copy-on-write shadow cloning.
Clear()                 # Remove all items from collection
Release()               # Same as Clear() but optimized for cloned collections.

String()                # String representation

Stack

btree.Stack is a collection with the functionality of a stack - specifically, a LIFO (last-in, first-out) data structure.

Operations
Push(item)     # Insert item to top of stack.
Pop()          # Remove item from top stack.
Top()          # Get the top item in stack
PopFunc(cond)  # Remove item from top stack if cond returns true.
Contains(item) # Check if item exists

All()          # Iterate items in ascending order.      (iter.Seq[T])
Drain()        # Iterate and remove in ascending order. (iter.Seq[T])

Len()          # Get the number of items in collection.
Clone()        # Clone collection, fast O(1), uses Copy-on-write shadow cloning.
Clear()        # Remove all items from collection
Release()      # Same as Clear() but optimized for cloned collections.

String()       # String representation
package main

import (
	"fmt"

	"github.com/tidwall/btree/v2"
)

func main() {
	// Create an stack of names
	var names btree.Stack[string]

	// Add some names
	names.Push("Andrea")
	names.Push("Tom")
	names.Push("Andy")
	names.Push("Jane")
	names.Push("Janet")
	names.Push("Steve")

	// Iterate over the stack and print each name
	for name := range names.All() {
		fmt.Printf("%s\n", name)
	}
	fmt.Printf("\n")

	// Pop a the top two names
	var name string
	name, _ = names.Pop()
	fmt.Printf("%s\n", name)
	name, _ = names.Pop()
	fmt.Printf("%s\n", name)
	fmt.Printf("\n")

	// Print the names again
	for name := range names.All() {
		fmt.Printf("%s\n", name)
	}
	fmt.Printf("\n")
}

// Output:
// Steve
// Janet
// Jane
// Andy
// Tom
// Andrea
//
// Steve
// Janet
//
// Jane
// Andy
// Tom
// Andrea

Queue

btree.Queue is a collection with the functionality of a queue - specifically, a FIFO (first-in, first-out) data structure.

Operations
Push(item)     # Insert item at the end of queue.
Pop()          # Remove the first item.
Front()        # Get the first item.
PopFunc(cond)  # Remove first item if cond function returns true.
Contains(item) # Checks if item exists

All()          # Iterate items in ascending order.      (iter.Seq[T])
Drain()        # Iterate and remove in ascending order. (iter.Seq[T])

Len()          # Get the number of items in collection.
Clone()        # Clone collection, fast O(1), uses Copy-on-write shadow cloning.
Clear()        # Remove all items from collection
Release()      # Same as Clear() but optimized for cloned collections.

String()       # String representation
Example
package main

import (
	"fmt"

	"github.com/tidwall/btree/v2"
)

func main() {
	// Create an queue of names
	var names btree.Queue[string]

	// Add some names
	names.Push("Andrea")
	names.Push("Tom")
	names.Push("Andy")
	names.Push("Jane")
	names.Push("Janet")
	names.Push("Steve")

	// Iterate over the array and print each name
	for name := range names.All() {
		fmt.Printf("%s\n", name)
	}
	fmt.Printf("\n")

	// Pop the first two names
	var name string
	name, _ = names.Pop()
	fmt.Printf("%s\n", name)
	name, _ = names.Pop()
	fmt.Printf("%s\n", name)
	fmt.Printf("\n")

	// Print the names again
	for name := range names.All() {
		fmt.Printf("%s\n", name)
	}
	fmt.Printf("\n")
}

// Output:
// Andrea
// Tom
// Andy
// Jane
// Janet
// Steve
//
// Andrea
// Tom
//
// Andy
// Jane
// Janet
// Steve

Deque

btree.Deque is a double-ended queue.

Operations
Push(item)         # Insert item at the end of queue.
PopFront()         # Remove the first item.
PopBack()          # Remove the last item.
Front()            # Get the first item.
Back( )            # Get the last item.
PopFrontFunc(cond) # Remove first item if cond returns true.
PopBackFunc(cond)  # Remove last item if cond returns true.
Contains(item)     # Checks if item exists

All()              # Iterate items in ascending order.       (iter.Seq[T])
Backward()         # Iterate items in desending order.       (iter.Seq[T])
Drain()            # Iterate and remove in ascending order.  (iter.Seq[T])
DrainBackward()    # Iterate and remove in descending order. (iter.Seq[T])

Len()              # Get the number of items in collection.
Clone()            # Clone collection, fast O(1), uses Copy-on-write shadow cloning.
Clear()            # Remove all items from collection
Release()          # Same as Clear() but optimized for cloned collections.

String()           # String representation
Example
package main

import (
	"fmt"

	"github.com/tidwall/btree/v2"
)

func main() {
	// Create an queue of names
	var names btree.Deque[string]

	// Add some names
	names.PushFront("Andrea")
	names.PushBack("Tom")
	names.PushFront("Andy")
	names.PushBack("Jane")
	names.PushFront("Janet")
	names.PushBack("Steve")

	// Iterate over the array and print each name
	for name := range names.All() {
		fmt.Printf("%s\n", name)
	}
	fmt.Printf("\n")

	// Pop the first two names
	var name string
	name, _ = names.PopFront()
	fmt.Printf("%s\n", name)
	name, _ = names.PopBack()
	fmt.Printf("%s\n", name)
	fmt.Printf("\n")

	// Print the names again
	for name := range names.All() {
		fmt.Printf("%s\n", name)
	}
	fmt.Printf("\n")
}

// Output:
// Janet
// Andy
// Andrea
// Tom
// Jane
// Steve
//
// Janet
// Steve
//
// Andy
// Andrea
// Tom
// Jane

Prique

btree.Prique is a priority queue collection that sorts items by largest to smallest. Data operations, such as Push() and Pop(), are O(log n).

This collection has support for duplicate items. It also inherits the Table collection, allowing for Tagged struct keys.

Operations
Push(item)     # Insert item into queue.
Pop()          # Remove the largest item.
Front()        # Get the largest item.
PopFunc(cond)  # Remove largest item if cond returns true.
Contains(item) # Checks if item exists

All()          # Iterate items in order of largest to smallest.      (iter.Seq[T])
Drain()        # Iterate and remove in order of largest to smallest. (iter.Seq[T])

Len()          # Get the number of items in collection.
Clone()        # Clone collection, fast O(1), uses Copy-on-write shadow cloning.
Clear()        # Remove all items from collection
Release()      # Same as Clear() but optimized for cloned collections.

String()       # String representation
Example
package main

import (
	"fmt"

	"github.com/tidwall/btree/v2"
)

func main() {

	type User struct {
		age  int `btree:"key"`
		name string
	}

	// Create an queue of users
	var users btree.Prique[User]

	// Add some names
	users.Push(User{27, "Andrea"})
	users.Push(User{54, "Tom"})
	users.Push(User{31, "Andy"})
	users.Push(User{43, "Jane"})
	users.Push(User{29, "Janet"})
	users.Push(User{62, "Steve"})
	users.Push(User{31, "Morton"})

	// Iterate over the array and print each name
	for user := range users.All() {
		fmt.Printf("%v %v\n", user.name, user.age)
	}
	fmt.Printf("\n")

	// Pop the first two names
	var user User
	user, _ = users.Pop()
	fmt.Printf("%v %v\n", user.name, user.age)
	user, _ = users.Pop()
	fmt.Printf("%v %v\n", user.name, user.age)
	fmt.Printf("\n")

	// Print the names again
	for user := range users.All() {
		fmt.Printf("%v %v\n", user.name, user.age)
	}
	fmt.Printf("\n")
}

// Output:
// Steve 62
// Tom 54
// Jane 43
// Morton 31
// Andy 31
// Janet 29
// Andrea 27
// 
// Steve 62
// Tom 54
// 
// Jane 43
// Morton 31
// Andy 31
// Janet 29
// Andrea 27

Performance

The btree package has various optimizations that enhance performance over existing implementations.

  • Branch node counting
  • Branch string prefixing
  • Auto-detected search method
  • Separate keys and values for maps
  • Last-first search algo. (fast bounds check, fewer conditions, fast bulk loads)
  • Reference counted COW
Benchmarks

https://github.com/tidwall/btree-bench

  • CPU: Ryzen 9 5950X 16-Core Processor
  • Go: go version go1.26.0 linux/amd64
  • Rust: rustc 1.95.0 (59807616e 2026-04-14)
  • C: gcc (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0
  • C++: g++ (Ubuntu 11.4.0-1ubuntu1~22.04.3) 11.4.0
Implementations

Benchmarking 1,000,000 items, 50 runs, taking the average result.

int32 keys
tidwall/btree
insert(seq)      1,000,000 ops in   0.017 secs   17.4 ns/op   57,467,393 op/sec
insert(rand)     1,000,000 ops in   0.075 secs   74.6 ns/op   13,398,545 op/sec
get(seq)         1,000,000 ops in   0.033 secs   32.9 ns/op   30,397,367 op/sec
get(rand)        1,000,000 ops in   0.071 secs   71.0 ns/op   14,083,456 op/sec

tidwall/btree v1 (previous version)
insert(seq)      1,000,000 ops in   0.037 secs   37.3 ns/op   26,814,294 op/sec
insert(rand)     1,000,000 ops in   0.140 secs  140.0 ns/op    7,140,927 op/sec
get(seq)         1,000,000 ops in   0.043 secs   43.3 ns/op   23,097,759 op/sec
get(rand)        1,000,000 ops in   0.133 secs  133.1 ns/op    7,513,402 op/sec

google/btree
insert(seq)      1,000,000 ops in   0.072 secs   71.7 ns/op   13,942,242 op/sec
insert(rand)     1,000,000 ops in   0.162 secs  162.0 ns/op    6,173,920 op/sec
get(seq)         1,000,000 ops in   0.064 secs   64.2 ns/op   15,578,194 op/sec
get(rand)        1,000,000 ops in   0.161 secs  160.5 ns/op    6,229,728 op/sec

rust/btree
insert(seq)      1,000,000 ops in   0.050 secs    49.8 ns/op  20,068,231 op/sec
insert(rand)     1,000,000 ops in   0.096 secs    95.8 ns/op  10,441,029 op/sec
get(seq)         1,000,000 ops in   0.033 secs    32.7 ns/op  30,542,744 op/sec
get(rand)        1,000,000 ops in   0.095 secs    94.7 ns/op  10,559,327 op/sec

tidwall/btree.h
insert(seq)      1,000,000 ops in   0.055 secs    54.7 ns/op  18,286,839 op/sec
insert(rand)     1,000,000 ops in   0.079 secs    78.6 ns/op  12,725,242 op/sec
get(seq)         1,000,000 ops in   0.033 secs    33.0 ns/op  30,292,159 op/sec
get(rand)        1,000,000 ops in   0.071 secs    71.3 ns/op  14,018,519 op/sec

frozenca/btree
insert(seq)      1,000,000 ops in   0.097 secs    97.2 ns/op  10,292,365 op/sec
insert(rand)     1,000,000 ops in   0.084 secs    83.9 ns/op  11,920,619 op/sec
get(seq)         1,000,000 ops in   0.045 secs    45.3 ns/op  22,063,150 op/sec
get(rand)        1,000,000 ops in   0.081 secs    81.4 ns/op  12,291,098 op/sec
uint64 keys
tidwall/btree
insert(seq)      1,000,000 ops in   0.017 secs   17.4 ns/op   57,622,752 op/sec
insert(rand)     1,000,000 ops in   0.079 secs   78.7 ns/op   12,706,623 op/sec
get(seq)         1,000,000 ops in   0.032 secs   31.9 ns/op   31,376,282 op/sec
get(rand)        1,000,000 ops in   0.072 secs   72.3 ns/op   13,832,691 op/sec

tidwall/btree v1 (previous version)
insert(seq)      1,000,000 ops in   0.039 secs   38.8 ns/op   25,786,643 op/sec
insert(rand)     1,000,000 ops in   0.145 secs  144.9 ns/op    6,902,377 op/sec
get(seq)         1,000,000 ops in   0.049 secs   49.3 ns/op   20,299,522 op/sec
get(rand)        1,000,000 ops in   0.142 secs  142.3 ns/op    7,026,596 op/sec

google/btree
insert(seq)      1,000,000 ops in   0.083 secs   83.4 ns/op   11,987,214 op/sec
insert(rand)     1,000,000 ops in   0.181 secs  181.0 ns/op    5,526,075 op/sec
get(seq)         1,000,000 ops in   0.074 secs   74.3 ns/op   13,456,882 op/sec
get(rand)        1,000,000 ops in   0.177 secs  177.3 ns/op    5,639,904 op/sec

rust/btree
insert(seq)      1,000,000 ops in   0.043 secs    42.8 ns/op  23,366,123 op/sec
insert(rand)     1,000,000 ops in   0.103 secs   103.2 ns/op   9,691,143 op/sec
get(seq)         1,000,000 ops in   0.033 secs    33.5 ns/op  29,862,334 op/sec
get(rand)        1,000,000 ops in   0.106 secs   106.0 ns/op   9,436,187 op/sec

tidwall/btree.h
insert(seq)      1,000,000 ops in   0.056 secs    55.5 ns/op  18,016,899 op/sec
insert(rand)     1,000,000 ops in   0.083 secs    83.4 ns/op  11,983,995 op/sec
get(seq)         1,000,000 ops in   0.034 secs    33.7 ns/op  29,713,098 op/sec
get(rand)        1,000,000 ops in   0.078 secs    78.0 ns/op  12,818,845 op/sec

frozenca/btree
insert(seq)      1,000,000 ops in   0.096 secs    95.6 ns/op  10,455,311 op/sec
insert(rand)     1,000,000 ops in   0.096 secs    96.4 ns/op  10,369,131 op/sec
get(seq)         1,000,000 ops in   0.045 secs    44.9 ns/op  22,291,360 op/sec
get(rand)        1,000,000 ops in   0.090 secs    89.6 ns/op  11,160,886 op/sec
string keys
tidwall/btree
insert(seq)      1,000,000 ops in   0.074 secs   73.5 ns/op   13,596,412 op/sec
insert(rand)     1,000,000 ops in   0.284 secs  283.8 ns/op    3,523,962 op/sec
get(seq)         1,000,000 ops in   0.090 secs   90.4 ns/op   11,059,582 op/sec
get(rand)        1,000,000 ops in   0.324 secs  323.8 ns/op    3,088,182 op/sec

tidwall/btree v1 (previous version)
insert(seq)      1,000,000 ops in   0.126 secs  125.6 ns/op    7,958,640 op/sec
insert(rand)     1,000,000 ops in   0.402 secs  401.8 ns/op    2,489,088 op/sec
get(seq)         1,000,000 ops in   0.123 secs  122.7 ns/op    8,148,680 op/sec
get(rand)        1,000,000 ops in   0.444 secs  444.4 ns/op    2,250,378 op/sec

google/btree
insert(seq)      1,000,000 ops in   0.189 secs  188.9 ns/op    5,294,058 op/sec
insert(rand)     1,000,000 ops in   0.450 secs  449.8 ns/op    2,223,166 op/sec
get(seq)         1,000,000 ops in   0.150 secs  149.7 ns/op    6,680,306 op/sec
get(rand)        1,000,000 ops in   0.497 secs  497.4 ns/op    2,010,590 op/sec

rust/btree
insert(seq)      1,000,000 ops in   0.247 secs   247.3 ns/op   4,043,459 op/sec
insert(rand)     1,000,000 ops in   0.499 secs   498.7 ns/op   2,005,129 op/sec
get(seq)         1,000,000 ops in   0.240 secs   239.8 ns/op   4,169,707 op/sec
get(rand)        1,000,000 ops in   0.619 secs   618.6 ns/op   1,616,658 op/sec

tidwall/btree.h
insert(seq)      1,000,000 ops in   0.409 secs   409.0 ns/op   2,444,810 op/sec
insert(rand)     1,000,000 ops in   0.414 secs   413.7 ns/op   2,417,073 op/sec
get(seq)         1,000,000 ops in   0.482 secs   482.4 ns/op   2,073,149 op/sec
get(rand)        1,000,000 ops in   0.493 secs   493.2 ns/op   2,027,549 op/sec

frozenca/btree
insert(seq)      1,000,000 ops in   0.654 secs   654.1 ns/op   1,528,730 op/sec
insert(rand)     1,000,000 ops in   0.655 secs   654.6 ns/op   1,527,708 op/sec
get(seq)         1,000,000 ops in   0.383 secs   383.0 ns/op   2,610,922 op/sec
get(rand)        1,000,000 ops in   0.628 secs   628.3 ns/op   1,591,558 op/sec

Documentation

Overview

Package btree implements B-tree based collections for Go

See https://github.com/tidwall/btree for more information

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CompareFor

func CompareFor[T any]() func(T, T) int

Return a compare function for type or nil if type is not comparable

Types

type Action

type Action int

Action type used by the Action() function

const (
	// ActionGet takes no action, just a simple Get.
	ActionGet Action = iota
	// ActionDelete deletes item
	ActionDelete
	// ActionReplace replaces item
	ActionReplace
)

type Array

type Array[T any] struct {
	// contains filtered or unexported fields
}

Array is btree backed array for storing arbritary items.

func NewArray

func NewArray[T any]() *Array[T]

NewArray returns a new array.

func NewArrayOptions

func NewArrayOptions[T any](opts ArrayOptions[T]) *Array[T]

NewArrayOptions returns a new array with options

func (*Array[T]) All

func (b *Array[T]) All() iter.Seq[T]

All returns an iterator over the items.

func (*Array[T]) AllMut

func (b *Array[T]) AllMut() iter.Seq[T]

AllMut returns an iterator over the items. MUTABLE OPERATION

func (*Array[T]) Append

func (b *Array[T]) Append(items ...T)

Append items to array

func (*Array[T]) Ascend

func (b *Array[T]) Ascend(index int) iter.Seq[T]

Ascend returns an iterator starting at index.

func (*Array[T]) AscendMut

func (b *Array[T]) AscendMut(index int) iter.Seq[T]

AscendMut returns an iterator starting at index. MUTABLE OPERATION

func (*Array[T]) Back

func (b *Array[T]) Back() (T, bool)

Back returns the last item in array.

func (*Array[T]) BackMut

func (b *Array[T]) BackMut() (T, bool)

BackMut returns the last item in array. MUTABLE OPERATION

func (*Array[T]) Backward

func (b *Array[T]) Backward() iter.Seq[T]

Backward returns an iterator over the items in reverse order

func (*Array[T]) BackwardMut

func (b *Array[T]) BackwardMut() iter.Seq[T]

Backward returns an iterator over the items in reverse order MUTABLE OPERATION

func (*Array[T]) Clear

func (b *Array[T]) Clear()

Clear the array

func (*Array[T]) Clone

func (b *Array[T]) Clone() *Array[T]

Clone the array. This is a fast O(1) operation using a copy-on-write method.

func (*Array[T]) Contains

func (b *Array[T]) Contains(item T) bool

Contains checks if array contains item.

func (*Array[T]) Delete

func (b *Array[T]) Delete(index int) (T, bool)

Delete item at index. Returns item and true if inserted or false if index is out of bounds

func (*Array[T]) Descend

func (b *Array[T]) Descend(index int) iter.Seq[T]

Descend returns an iterator starting at index, in reverse order.

func (*Array[T]) DescendMut

func (b *Array[T]) DescendMut(index int) iter.Seq[T]

DescendMut returns an iterator starting at index, in reverse order. MUTABLE OPERATION

func (*Array[T]) Drain

func (b *Array[T]) Drain(index, count int) Extract[T]

DrainOptions will delete all items within the provided index and count sub-range. Returns the deleted items as an ordered extract that can be iterated over.

func (*Array[T]) DrainOptions

func (b *Array[T]) DrainOptions(index, count int,
	opts DrainOptions,
) Extract[T]

DrainOptions will delete all items within the provided index and count sub-range. Returns the deleted items as an ordered extract that can be iterated over.

func (*Array[T]) Front

func (b *Array[T]) Front() (T, bool)

Front returns the first item in array.

func (*Array[T]) FrontMut

func (b *Array[T]) FrontMut() (T, bool)

FrontMut returns the first item in array. MUTABLE OPERATION

func (*Array[T]) Get

func (b *Array[T]) Get(index int) T

Get returns item at index.

func (*Array[T]) GetMut

func (b *Array[T]) GetMut(index int) T

Get returns item at index. MUTABLE OPERTAION

func (*Array[T]) Insert

func (b *Array[T]) Insert(index int, item T) bool

Insert an item at index, moving existing items over by one. Returns true if item was inserted, or false if index is out of bounds. Use index=0 to insert front and index=arr.Len() to insert back.

func (*Array[T]) Len

func (b *Array[T]) Len() int

Len returns the number item in array.

func (*Array[T]) Lookup

func (b *Array[T]) Lookup(index int) (T, bool)

Lookup returns item at index or false if out of bounds.

func (*Array[T]) LookupMut

func (b *Array[T]) LookupMut(index int) (T, bool)

Lookup returns item at index or false if out of bounds. MUTABLE OPERTAION

func (*Array[T]) PopBack

func (b *Array[T]) PopBack() (T, bool)

PopBack removes the last item in array.

func (*Array[T]) PopBackFunc

func (b *Array[T]) PopBackFunc(cond func(item T) bool) (T, bool)

PopBackFunc removed last item if allowed by the provided function.

func (*Array[T]) PopFront

func (b *Array[T]) PopFront() (T, bool)

PopFront removes the first item in array.

func (*Array[T]) PopFrontFunc

func (b *Array[T]) PopFrontFunc(cond func(item T) bool) (T, bool)

PopFrontFunc removed first item if allowed by the provided function.

func (*Array[T]) PushBack

func (b *Array[T]) PushBack(item T)

PushBack adds item to back of array.

func (*Array[T]) PushFront

func (b *Array[T]) PushFront(item T)

PushFront adds item to front of array.

func (*Array[T]) Release

func (b *Array[T]) Release()

Release clears the array and release any references. This method is functionally equivalent to Clear() but is an optimization for collections that are cloned using Clone().

func (*Array[T]) Replace

func (b *Array[T]) Replace(index int, item T) (T, bool)

Replace an item at index. Returns tru if item was replaced

func (*Array[T]) String

func (b *Array[T]) String() string

String returns a string representation of array.

type ArrayOptions

type ArrayOptions[T any] struct {
	Copy    func(T) T // Copy item function
	Release func(T)   // Release item function
}

ArrayOptions are options for NewArrayOptions()

type Deque

type Deque[T any] struct {
	// contains filtered or unexported fields
}

Deque is a double-ended queue

func NewDeque

func NewDeque[T any]() *Deque[T]

NewDeque returns a new deque

func NewDequeOptions

func NewDequeOptions[T any](opts DequeOptions[T]) *Deque[T]

NewDequeOptions returns a new deque

func (*Deque[T]) All

func (b *Deque[T]) All() iter.Seq[T]

All returns an iterator of all items starting with the first.

func (*Deque[T]) AllMut

func (b *Deque[T]) AllMut() iter.Seq[T]

All returns an iterator of all items starting with the first. MUTABLE OPERATION.

func (*Deque[T]) At

func (b *Deque[T]) At(pos int) (T, bool)

At returns the item At position, after first, in queue. Returns false if no item is found At position.

func (*Deque[T]) AtMut

func (b *Deque[T]) AtMut(pos int) (T, bool)

AtMut returns the item at position, after first, in queue. Returns false if no item is found at position. MUTABLE OPERATION.

func (*Deque[T]) Back

func (b *Deque[T]) Back() (T, bool)

Back returns last item

func (*Deque[T]) BackMut

func (b *Deque[T]) BackMut() (T, bool)

BackMut returns last item MUTABLE OPERATION

func (*Deque[T]) Backward

func (b *Deque[T]) Backward() iter.Seq[T]

Backward returns an iterator of all items starting with the last.

func (*Deque[T]) BackwardMut

func (b *Deque[T]) BackwardMut() iter.Seq[T]

BackwardMut returns an iterator of all items starting with the last. MUTABLE OPERATION.

func (*Deque[T]) Clear

func (b *Deque[T]) Clear()

Clear the queue.

func (*Deque[T]) Clone

func (b *Deque[T]) Clone() *Deque[T]

Clone the queue. This is a fast O(1) operation using a copy-on-write method.

func (*Deque[T]) Contains

func (b *Deque[T]) Contains(item T) bool

Contains returns true if queue contains item.

func (*Deque[T]) Front

func (b *Deque[T]) Front() (T, bool)

Front returns first item

func (*Deque[T]) FrontMut

func (b *Deque[T]) FrontMut() (T, bool)

Front returns first item MUTABLE OPERATION

func (*Deque[T]) Len

func (b *Deque[T]) Len() int

Len returns the number of items in queue

func (*Deque[T]) PopBack

func (b *Deque[T]) PopBack() (T, bool)

PopFront removes last item in queue

func (*Deque[T]) PopBackFunc

func (b *Deque[T]) PopBackFunc(cond func(item T) bool) (T, bool)

PopBackFunc removes last item if provided function allows.

func (*Deque[T]) PopFront

func (b *Deque[T]) PopFront() (T, bool)

PopFront removes first item in queue

func (*Deque[T]) PopFrontFunc

func (b *Deque[T]) PopFrontFunc(cond func(item T) bool) (T, bool)

PopFrontFunc removes first item if provided function allows.

func (*Deque[T]) PushBack

func (b *Deque[T]) PushBack(item T)

PushFront inserts item to back of queue

func (*Deque[T]) PushFront

func (b *Deque[T]) PushFront(item T)

PushFront inserts item to front of queue

func (*Deque[T]) Release

func (b *Deque[T]) Release()

Release clears the queue and release any references. This method is functionally equivalent to Clear() but is an optimization for collections that are copied using Copy().

func (*Deque[T]) String

func (b *Deque[T]) String() string

String returns a string representation of queue.

type DequeOptions

type DequeOptions[T any] struct {
	Copy    func(T) T // Copy item function
	Release func(T)   // Release item function
}

DequeOptions are options for deque collection

type DrainOptions

type DrainOptions struct {
	NoReturn     bool // Do not return items
	MinExclusive bool // Exclude the min arg
	MaxInclusive bool // Include the max arg
}

DrainOptions for deleting a range of items from a collection.

type Extract

type Extract[V any] interface {
	Len() int
	All() iter.Seq[V]
	Backward() iter.Seq[V]
}

Extract is a slice of items that can be iterated forward or backward.

type Extract2

type Extract2[K, V any] interface {
	Len() int
	All() iter.Seq2[K, V]
	Backward() iter.Seq2[K, V]
	Keys() iter.Seq[K]
	Values() iter.Seq[V]
}

Extract2 is a slice of items that can be iterated forward or backward.

type Map

type Map[K cmp.Ordered, V any] struct {
	// contains filtered or unexported fields
}

Map is sorted collection of key value pairs

func NewMap

func NewMap[K cmp.Ordered, V any]() *Map[K, V]

NewMap returns a new map

func NewMapOptions

func NewMapOptions[K cmp.Ordered, V any](opts MapOptions[K, V]) *Map[K, V]

NewMapOptions returns a new map

func (*Map[K, V]) Action

func (b *Map[K, V]) Action(key K, action func(V) (V, Action))

Action performs an action on item with key. The "action" function is called when the item is found. Returning an Action type from action function to: - ActionGet: do nothing - ActionDelete: delete item - ActionReplace: replace with returned value

func (*Map[K, V]) All

func (b *Map[K, V]) All() iter.Seq2[K, V]

All returns an iterator of all items in map.

func (*Map[K, V]) AllMut

func (b *Map[K, V]) AllMut() iter.Seq2[K, V]

AllMut returns an iterator of all items in map. MUTABLE OPERATION

func (*Map[K, V]) Ascend

func (b *Map[K, V]) Ascend(key K) iter.Seq2[K, V]

Ascend returns an iterator starting at key

func (*Map[K, V]) AscendAt

func (b *Map[K, V]) AscendAt(index int) iter.Seq2[K, V]

AscendAt returns an iterator starting at index

func (*Map[K, V]) AscendAtMut

func (b *Map[K, V]) AscendAtMut(index int) iter.Seq2[K, V]

AscendAtMut returns an iterator starting at index MUTABLE OPERATION

func (*Map[K, V]) AscendMut

func (b *Map[K, V]) AscendMut(key K) iter.Seq2[K, V]

AscendMut returns an iterator starting at key MUTABLE OPERATION

func (*Map[K, V]) Back

func (b *Map[K, V]) Back() (K, V, bool)

Back returns first item

func (*Map[K, V]) BackMut

func (b *Map[K, V]) BackMut() (K, V, bool)

BackMut returns first item MUTABLE OPERATION

func (*Map[K, V]) Backward

func (b *Map[K, V]) Backward() iter.Seq2[K, V]

Backward returns an iterator of all items in map, in reverse order.

func (*Map[K, V]) BackwardMut

func (b *Map[K, V]) BackwardMut() iter.Seq2[K, V]

BackwardMut returns an iterator of all items in map, in reverse order. MUTABLE OPERATION

func (*Map[K, V]) Clear

func (b *Map[K, V]) Clear()

Clear the map

func (*Map[K, V]) Clone

func (b *Map[K, V]) Clone() *Map[K, V]

Clone the map. This is a fast O(1) operation using a copy-on-write method.

func (*Map[K, V]) Contains

func (b *Map[K, V]) Contains(key K) bool

Contains returns true if item exists

func (*Map[K, V]) Delete

func (b *Map[K, V]) Delete(key K) (V, bool)

Delete item from map.

func (*Map[K, V]) DeleteAt

func (b *Map[K, V]) DeleteAt(index int) (K, V, bool)

DeleteAt deletes item at index

func (*Map[K, V]) DeleteFunc

func (b *Map[K, V]) DeleteFunc(del func(K, V) bool) bool

DeleteFunc delete item from map if its allowed by del function.

func (*Map[K, V]) Descend

func (b *Map[K, V]) Descend(key K) iter.Seq2[K, V]

Decend returns an iterator with items less than key, in reverse order.

func (*Map[K, V]) DescendAt

func (b *Map[K, V]) DescendAt(index int) iter.Seq2[K, V]

DescendAt returns an iterator starting at index, in reverse order

func (*Map[K, V]) DescendAtMut

func (b *Map[K, V]) DescendAtMut(index int) iter.Seq2[K, V]

DescendAtMut returns an iterator starting at index, in reverse order MUTABLE OPERATION

func (*Map[K, V]) DescendMut

func (b *Map[K, V]) DescendMut(key K) iter.Seq2[K, V]

DescendMut returns an iterator with items less than key, in reverse order. MUTABLE OPERATION

func (*Map[K, V]) Drain

func (b *Map[K, V]) Drain(min, max K) Extract2[K, V]

Drain will delete all items within the provided min (inclusive) and max (exclusive) sub-range. Returns the deleted items as an ordered extract that can be iterated over.

func (*Map[K, V]) DrainAt

func (b *Map[K, V]) DrainAt(index, count int) Extract2[K, V]

DrainAt will delete all items within the provided index and count sub-range. Returns the deleted items as an ordered extract that can be iterated over.

func (*Map[K, V]) DrainAtOptions

func (b *Map[K, V]) DrainAtOptions(index, count int, opts DrainOptions,
) Extract2[K, V]

DrainAtOptions will delete all items within the provided index and count sub-range. Returns the deleted items as an ordered extract that can be iterated over.

func (*Map[K, V]) DrainOptions

func (b *Map[K, V]) DrainOptions(min, max K, opts DrainOptions) Extract2[K, V]

DrainOptions will delete all items within the provided min (inclusive) and max (exclusive) sub-range. Returns the deleted items as an ordered extract that can be iterated over.

func (*Map[K, V]) Front

func (b *Map[K, V]) Front() (K, V, bool)

Front returns first item

func (*Map[K, V]) FrontMut

func (b *Map[K, V]) FrontMut() (K, V, bool)

Front returns first item

func (*Map[K, V]) Get

func (b *Map[K, V]) Get(key K) V

Get an item

func (*Map[K, V]) GetAt

func (b *Map[K, V]) GetAt(index int) (K, V)

GetAt returns item at index

func (*Map[K, V]) GetAtMut

func (b *Map[K, V]) GetAtMut(index int) (K, V)

GetAtMut returns item at index MUTABLE OPERATION

func (*Map[K, V]) IndexOf

func (b *Map[K, V]) IndexOf(key K) (int, bool)

IndexOf returns the index of key

func (*Map[K, V]) Insert

func (b *Map[K, V]) Insert(key K, value V) (V, bool)

Insert new item. Returns true if inserted or false if already exists.

func (*Map[K, V]) InsertAt

func (b *Map[K, V]) InsertAt(index int, key K, value V) bool

InsertAt inserts item at index

func (*Map[K, V]) Keys

func (b *Map[K, V]) Keys() iter.Seq[K]

Keys returns an iterator of all keys.

func (*Map[K, V]) Len

func (b *Map[K, V]) Len() int

Len returns the number of items in map

func (*Map[K, V]) Lookup

func (b *Map[K, V]) Lookup(key K) (V, bool)

Lookup an item Returns true if found or false if not found

func (*Map[K, V]) LookupAt

func (b *Map[K, V]) LookupAt(index int) (K, V, bool)

LookupAt returns item at index

func (*Map[K, V]) LookupAtMut

func (b *Map[K, V]) LookupAtMut(index int) (K, V, bool)

LookupAtMut returns item at index MUTABLE OPERATION

func (*Map[K, V]) LookupMut

func (b *Map[K, V]) LookupMut(key K) (V, bool)

Lookup an item Returns true if found or false if not found MUTABLE OPERATION

func (*Map[K, V]) PopBack

func (b *Map[K, V]) PopBack() (K, V, bool)

PopBack removes last item.

func (*Map[K, V]) PopBackFunc

func (b *Map[K, V]) PopBackFunc(cond func(key K, value V) bool) (K, V, bool)

PopBackFunc removes last item if provided function allows.

func (*Map[K, V]) PopFront

func (b *Map[K, V]) PopFront() (K, V, bool)

PopFront removes first item.

func (*Map[K, V]) PopFrontFunc

func (b *Map[K, V]) PopFrontFunc(cond func(key K, value V) bool) (K, V, bool)

PopFrontFunc removes first item if provided function allows.

func (*Map[K, V]) PushBack

func (b *Map[K, V]) PushBack(key K, value V) bool

PushBack insertes item to back of map.

func (*Map[K, V]) PushFront

func (b *Map[K, V]) PushFront(key K, value V) bool

PushFront insertes item to front of map.

func (*Map[K, V]) Release

func (b *Map[K, V]) Release()

Release clears the map and release any references. This method is functionally equivalent to Clear() but is an optimization for collections that are cloned.

func (*Map[K, V]) Replace

func (b *Map[K, V]) Replace(key K, value V) (V, bool)

Replace existing item Returns true if replaced, of false if not exists.

func (*Map[K, V]) ReplaceAt

func (b *Map[K, V]) ReplaceAt(index int, key K, value V) (K, V, bool)

ReplaceAt replaces item at index

func (*Map[K, V]) Seek

func (b *Map[K, V]) Seek(key K) (K, V, bool)

Seek returns the first item that is >= key

func (*Map[K, V]) SeekMut

func (b *Map[K, V]) SeekMut(key K) (K, V, bool)

SeekMut returns the first item that is >= key MUTABLE OPERATION

func (*Map[K, V]) SeekNext

func (b *Map[K, V]) SeekNext(key K) (K, V, bool)

SeekNext returns the first item that is > key

func (*Map[K, V]) SeekNextMut

func (b *Map[K, V]) SeekNextMut(key K) (K, V, bool)

SeekNextMut returns the first item that is > key MUTABLE OPERATION

func (*Map[K, V]) SeekPrev

func (b *Map[K, V]) SeekPrev(key K) (K, V, bool)

SeekPrev returns the first item that is < key

func (*Map[K, V]) SeekPrevMut

func (b *Map[K, V]) SeekPrevMut(key K) (K, V, bool)

SeekPrevMut returns the first item that is < key MUTABLE OPERATION

func (*Map[K, V]) Set

func (b *Map[K, V]) Set(key K, value V) (V, bool)

Set inserts or replaces item. Returns true if replaced or false if inserted

func (*Map[K, V]) String

func (b *Map[K, V]) String() string

String returns a string representation of map

func (*Map[K, V]) Values

func (b *Map[K, V]) Values() iter.Seq[V]

Values returns an iterator of all values.

func (*Map[K, V]) ValuesMut

func (b *Map[K, V]) ValuesMut() iter.Seq[V]

ValuesMut returns an iterator of all values. MUTABLE OPERATION

type MapOptions

type MapOptions[K cmp.Ordered, V any] struct {
	Copy     func(V) V // Copy function
	Release  func(V)   // Release function
	NoPrefix bool      // Do not use string prefixes
}

MapOptions for NewMapOptions

type Prique

type Prique[T any] struct {
	// contains filtered or unexported fields
}

Prique is a priority queue

func NewPrique

func NewPrique[T any]() *Prique[T]

NewPrique returns a new priority queue

func NewPriqueOptions

func NewPriqueOptions[T any](opts PriqueOptions[T]) *Prique[T]

NewPriqueOptions returns a new priority queue

func (*Prique[T]) All

func (b *Prique[T]) All() iter.Seq[T]

All returns an iterator of all items in queue

func (*Prique[T]) AllMut

func (b *Prique[T]) AllMut() iter.Seq[T]

AllMut returns an iterator of all items in queue MUTABLE OPERATION

func (*Prique[T]) Clear

func (b *Prique[T]) Clear()

Clear the queue.

func (*Prique[T]) Clone

func (b *Prique[T]) Clone() *Prique[T]

Clone the queue. This is a fast O(1) operation using a copy-on-write method.

func (*Prique[T]) Contains

func (b *Prique[T]) Contains(key T) bool

Contains return true if item exists in queue

func (*Prique[T]) Delete

func (b *Prique[T]) Delete(key T) (T, bool)

Delete item with the provided key. If duplicate items with the same key exist, only one will be deleted; specifically the oldest duplicate item is deleted.

func (*Prique[T]) Front

func (b *Prique[T]) Front() (T, bool)

Front returns next item in queue.

func (*Prique[T]) FrontMut

func (b *Prique[T]) FrontMut() (T, bool)

FrontMut returns next item in queue. MUTABLE OPERATION

func (*Prique[T]) GetAt

func (b *Prique[T]) GetAt(pos int) T

GetAt returns the item at position, after first, in queue.

func (*Prique[T]) GetAtMut

func (b *Prique[T]) GetAtMut(pos int) T

GetAtMut returns the item at position, after first, in queue. MUTABLE OPERATION.

func (*Prique[T]) Len

func (b *Prique[T]) Len() int

Len returns the number of items in queue

func (*Prique[T]) LookupAt

func (b *Prique[T]) LookupAt(pos int) (T, bool)

LookupAt returns the item at position, after first, in queue. Returns false if no item is found at position.

func (*Prique[T]) LookupAtMut

func (b *Prique[T]) LookupAtMut(pos int) (T, bool)

LookupAtMut returns the item at position, after first, in queue. Returns false if no item is found at position. MUTABLE OPERATION.

func (*Prique[T]) Pop

func (b *Prique[T]) Pop() (T, bool)

Pop next item from queue.

func (*Prique[T]) PopFunc

func (b *Prique[T]) PopFunc(cond func(item T) bool) (T, bool)

PopFunc deletes next item if provided function allows.

func (*Prique[T]) Push

func (b *Prique[T]) Push(item T) bool

Push item into queue. Returns true if inserted or false if already exists

func (*Prique[T]) Release

func (b *Prique[T]) Release()

Release clears the queue and release any references. This method is functionally equivalent to Clear() but is an optimization for collections that are cloned.

func (*Prique[T]) String

func (b *Prique[T]) String() string

String returns a string representation of queue

type PriqueOptions

type PriqueOptions[T any] struct {
	Compare func(T, T) int  // Standard comparator
	Less    func(T, T) bool // Less comparator
	Copy    func(T) T       // Copy function
	Release func(T)         // Release function
}

PriqueOptions for NewPriqueOptions()

type Queue

type Queue[T any] struct {
	// contains filtered or unexported fields
}

Queue provides the functionality of a queue - specifically, a FIFO (first-in, first-out) data structure.

func NewQueue

func NewQueue[T any]() *Queue[T]

NewQueue returns a new queue

func NewQueueOptions

func NewQueueOptions[T any](opts QueueOptions[T]) *Queue[T]

NewQueueOptions returns a new queue

func (*Queue[T]) All

func (b *Queue[T]) All() iter.Seq[T]

All returns an iterator of all items starting with the first.

func (*Queue[T]) AllMut

func (b *Queue[T]) AllMut() iter.Seq[T]

All returns an iterator of all items starting with the first. MUTABLE OPERATION.

func (*Queue[T]) At

func (b *Queue[T]) At(pos int) (T, bool)

At returns the item At position, after first, in queue. Returns false if no item is found At position.

func (*Queue[T]) AtMut

func (b *Queue[T]) AtMut(pos int) (T, bool)

AtMut returns the item at position, after first, in queue. Returns false if no item is found at position. MUTABLE OPERATION.

func (*Queue[T]) Clear

func (b *Queue[T]) Clear()

Clear the queue.

func (*Queue[T]) Clone

func (b *Queue[T]) Clone() *Queue[T]

Clone the queue. This is a fast O(1) operation using a copy-on-write method.

func (*Queue[T]) Contains

func (b *Queue[T]) Contains(item T) bool

Contains returns true if item exists.

func (*Queue[T]) Front

func (b *Queue[T]) Front() (T, bool)

Front returns the first item in queue, or false if queue is empty.

func (*Queue[T]) FrontMut

func (b *Queue[T]) FrontMut() (T, bool)

FrontMut returns the first item in queue, or false if queue is empty. MUTABLE OPERATION.

func (*Queue[T]) Len

func (b *Queue[T]) Len() int

Len returns the number of items in queue

func (*Queue[T]) Pop

func (b *Queue[T]) Pop() (T, bool)

Pop first item from queue.

func (*Queue[T]) PopFunc

func (b *Queue[T]) PopFunc(cond func(item T) bool) (T, bool)

PopFunc deletes first item if provided function allows.

func (*Queue[T]) Push

func (b *Queue[T]) Push(item T)

Push an item to end of queue.

func (*Queue[T]) Release

func (b *Queue[T]) Release()

Release clears the queue and release any references. This method is functionally equivalent to Clear() but is an optimization for collections that are copied using Clone().

func (*Queue[T]) String

func (b *Queue[T]) String() string

String representation of queue

type QueueOptions

type QueueOptions[T any] struct {
	Copy    func(T) T // Copy function
	Release func(T)   // Release function
}

QueueOptions are options for NewQueueOptoins

type Set

type Set[K cmp.Ordered] struct {
	// contains filtered or unexported fields
}

Set is a b-tree set of keys

func NewSet

func NewSet[K cmp.Ordered]() *Set[K]

NewSet returns a new set

func (*Set[K]) All

func (b *Set[K]) All() iter.Seq[K]

All returns an iterator of all keys.

func (*Set[K]) Ascend

func (b *Set[K]) Ascend(key K) iter.Seq[K]

Ascend returns an iterator starting at index.

func (*Set[K]) AscendAt

func (b *Set[K]) AscendAt(index int) iter.Seq[K]

AscendAt returns iterator starting at index.

func (*Set[K]) Back

func (b *Set[K]) Back() (K, bool)

Back returns last key

func (*Set[K]) Backward

func (b *Set[K]) Backward() iter.Seq[K]

Backward returns an iterator of all keys in reverse order.

func (*Set[K]) Clear

func (b *Set[K]) Clear()

Clear the set

func (*Set[K]) Clone

func (b *Set[K]) Clone() *Set[K]

Clone the set This is a fast O(1) operation using a copy-on-write method.

func (*Set[K]) Contains

func (b *Set[K]) Contains(key K) bool

Contains returns true if key exists

func (*Set[K]) Delete

func (b *Set[K]) Delete(key K) bool

Delete a key

func (*Set[K]) DeleteAt

func (b *Set[K]) DeleteAt(index int) (K, bool)

DeleteAt deletes key at index

func (*Set[K]) DeleteFunc

func (b *Set[K]) DeleteFunc(del func(K) bool) bool

DeleteFunc delete item if its allowed by del function.

func (*Set[K]) Descend

func (b *Set[K]) Descend(key K) iter.Seq[K]

Descend returns an iterator starting at index in reverse order.

func (*Set[K]) DescendAt

func (b *Set[K]) DescendAt(index int) iter.Seq[K]

AscendAt returns iterator starting index in reverse order

func (*Set[K]) Difference

func (b *Set[K]) Difference(other *Set[K]) *Set[K]

Difference returns a new set containing keys that are in target set but not in other.

func (*Set[K]) DifferenceWith

func (b *Set[K]) DifferenceWith(other *Set[K])

DifferenceWith removes any items from set that are in other.

func (*Set[K]) Drain

func (b *Set[K]) Drain(min, max K) Extract[K]

Drain will delete all items within the provided min (inclusive) and max (exclusive) sub-range. Returns the deleted items as an ordered extract that can be iterated over.

func (*Set[K]) DrainAt

func (b *Set[K]) DrainAt(index, count int) Extract[K]

DeleteRangeOptions will delete all items within the provided min (inclusive) and max (exclusive) sub-range. Returns the deleted items as an ordered extract that can be iterated over.

func (*Set[K]) DrainAtOptions

func (b *Set[K]) DrainAtOptions(index, count int, opts DrainOptions,
) Extract[K]

DrainAtOptions will delete all items within the provided index and count sub-range. Returns the deleted items as an ordered extract that can be iterated over.

func (*Set[K]) DrainOptions

func (b *Set[K]) DrainOptions(min, max K, opts DrainOptions,
) Extract[K]

DrainOptions will delete all items within the provided min (inclusive) and max (exclusive) sub-range. Returns the deleted items as an ordered extract that can be iterated over.

func (*Set[K]) Equal

func (b *Set[K]) Equal(other *Set[K]) bool

Equal returs true if boths sets contains the same items

func (*Set[K]) Front

func (b *Set[K]) Front() (K, bool)

Front returns first key

func (*Set[K]) GetAt

func (b *Set[K]) GetAt(index int) K

GetAt returns key at index

func (*Set[K]) IndexOf

func (b *Set[K]) IndexOf(key K) (int, bool)

IndexOf returns the index of key.

func (*Set[K]) Insert

func (b *Set[K]) Insert(key K) bool

Insert a key

func (*Set[K]) InsertAt

func (b *Set[K]) InsertAt(index int, key K) bool

InsertAt inserts key at index. Returns false if out of order or out of bounds

func (*Set[K]) Intersection

func (b *Set[K]) Intersection(other *Set[K]) *Set[K]

Intersection returns a new set containing item that are in both sets.

func (*Set[K]) IntersectionWith

func (b *Set[K]) IntersectionWith(other *Set[K])

IntersectionWith removes any items from set that are not in other.

func (*Set[K]) Intersects

func (b *Set[K]) Intersects(other *Set[K]) bool

Intersects returns true if other and s share any items.

func (*Set[K]) Len

func (b *Set[K]) Len() int

Len returns the number of keys in set

func (*Set[K]) LookupAt

func (b *Set[K]) LookupAt(index int) (K, bool)

LookupAt returns key at index.

func (*Set[K]) PopBack

func (b *Set[K]) PopBack() (K, bool)

PopBack removes last key

func (*Set[K]) PopFront

func (b *Set[K]) PopFront() (K, bool)

PopFront removes first key

func (*Set[K]) PushBack

func (b *Set[K]) PushBack(key K) bool

PushBack pushes key to back of set

func (*Set[K]) PushFront

func (b *Set[K]) PushFront(key K) bool

PushFront pushes key to front of set

func (*Set[K]) Release

func (b *Set[K]) Release()

Release clears the set and release any references. This method is functionally equivalent to Clear() but is an optimization for collections that are cloned.

func (*Set[K]) ReplaceAt

func (b *Set[K]) ReplaceAt(index int, key K) (K, bool)

ReplaceAt replaces key at index.

func (*Set[K]) Seek

func (b *Set[K]) Seek(key K) (K, bool)

Seek returns the first key that is >= provided key.

func (*Set[K]) SeekNext

func (b *Set[K]) SeekNext(key K) (K, bool)

SeekNext returns first key that is > provided key.

func (*Set[K]) SeekPrev

func (b *Set[K]) SeekPrev(key K) (K, bool)

SeekPrev returns first key that is < provided key.

func (*Set[K]) String

func (b *Set[K]) String() string

String representation

func (*Set[K]) SymmetricDifference

func (b *Set[K]) SymmetricDifference(other *Set[K]) *Set[K]

SymmetricDifference returns a new set containing items that are in either, but not both.

func (*Set[K]) SymmetricDifferenceWith

func (b *Set[K]) SymmetricDifferenceWith(other *Set[K])

SymmetricDifferenceWith update set to the symmetric difference of both sets

func (*Set[K]) Union

func (b *Set[K]) Union(other *Set[K]) *Set[K]

Union returns a new set containing all keys of both sets.

func (*Set[K]) UnionWith

func (b *Set[K]) UnionWith(other *Set[K])

UnionWith inserts all keys of other set.

type Stack

type Stack[T any] struct {
	// contains filtered or unexported fields
}

Stack provides the functionality of a stack - specifically, a LIFO (last-in, first-out) data structure.

func NewStack

func NewStack[T any]() *Stack[T]

NewStack returns a new stack

func NewStackOptions

func NewStackOptions[T any](opts StackOptions[T]) *Stack[T]

NewStackOptions returns a new stack

func (*Stack[T]) All

func (b *Stack[T]) All() iter.Seq[T]

All returns an iterator of all items starting from the top of the stack.

func (*Stack[T]) AllMut

func (b *Stack[T]) AllMut() iter.Seq[T]

AllMut returns an iterator of all items starting from the top of the stack. MUTABLE OPERATION.

func (*Stack[T]) At

func (b *Stack[T]) At(pos int) (T, bool)

At returns the item At position, after top, in Stack. Returns false if no item is found At position.

func (*Stack[T]) AtMut

func (b *Stack[T]) AtMut(pos int) (T, bool)

AtMut returns the item at position, after top, in Stack. Returns false if no item is found at position. MUTABLE OPERATION.

func (*Stack[T]) Clear

func (b *Stack[T]) Clear()

Clear the stack.

func (*Stack[T]) Clone

func (b *Stack[T]) Clone() *Stack[T]

Clone the stack. This is a fast O(1) operation using a copy-on-write method.

func (*Stack[K]) Contains

func (b *Stack[K]) Contains(item K) bool

func (*Stack[T]) Len

func (b *Stack[T]) Len() int

Len returns the number of items in stack.

func (*Stack[T]) Pop

func (b *Stack[T]) Pop() (T, bool)

Pop the top item from top stack.

func (*Stack[T]) Push

func (b *Stack[T]) Push(item T)

Push an item to top of stack.

func (*Stack[T]) Release

func (b *Stack[T]) Release()

Release clears the stack and releases any copied reference. This method is functionally equivalent to Clear() but is an optimization for collections that are copied using Clone().

func (*Stack[K]) String

func (b *Stack[K]) String() string

func (*Stack[T]) Top

func (b *Stack[T]) Top() (T, bool)

Top returns the top item in stack, or false if stack is empty.

func (*Stack[T]) TopMut

func (b *Stack[T]) TopMut() (T, bool)

TopMut returns the top item in stack, or false if stack is empty. MUTABLE OPERATION.

type StackOptions

type StackOptions[T any] struct {
	Copy    func(T) T // Copy function
	Release func(T)   // Release function
}

StackOptions for NewStackOptions()

type Table

type Table[T any] struct {
	// contains filtered or unexported fields
}

Table is a B-tree backed in-memory collection for general purpose items.

func NewTable

func NewTable[T any]() *Table[T]

func NewTableOptions

func NewTableOptions[T any](opts TableOptions[T]) *Table[T]

func (*Table[T]) All

func (b *Table[T]) All() iter.Seq[T]

func (*Table[T]) AllMut

func (b *Table[T]) AllMut() iter.Seq[T]

func (*Table[T]) Ascend

func (b *Table[T]) Ascend(pivot T) iter.Seq[T]

func (*Table[T]) AscendAt

func (b *Table[T]) AscendAt(index int) iter.Seq[T]

func (*Table[T]) AscendAtMut

func (b *Table[T]) AscendAtMut(index int) iter.Seq[T]

func (*Table[T]) AscendMut

func (b *Table[T]) AscendMut(pivot T) iter.Seq[T]

func (*Table[T]) Back

func (b *Table[T]) Back() (T, bool)

func (*Table[T]) BackMut

func (b *Table[T]) BackMut() (T, bool)

func (*Table[T]) Backward

func (b *Table[T]) Backward() iter.Seq[T]

func (*Table[T]) BackwardMut

func (b *Table[T]) BackwardMut() iter.Seq[T]

func (*Table[T]) Clear

func (b *Table[T]) Clear()

func (*Table[T]) Clone

func (b *Table[T]) Clone() *Table[T]

func (*Table[T]) Contains

func (b *Table[T]) Contains(key T) bool

func (*Table[T]) Delete

func (b *Table[T]) Delete(key T) (T, bool)

func (*Table[T]) DeleteAt

func (b *Table[T]) DeleteAt(index int) (T, bool)

func (*Table[T]) Descend

func (b *Table[T]) Descend(pivot T) iter.Seq[T]

func (*Table[T]) DescendAt

func (b *Table[T]) DescendAt(index int) iter.Seq[T]

func (*Table[T]) DescendAtMut

func (b *Table[T]) DescendAtMut(index int) iter.Seq[T]

func (*Table[T]) DescendMut

func (b *Table[T]) DescendMut(pivot T) iter.Seq[T]

func (*Table[T]) Drain

func (b *Table[T]) Drain(min, max T) Extract[T]

func (*Table[T]) DrainAt

func (b *Table[T]) DrainAt(index, count int) Extract[T]

func (*Table[T]) DrainAtOptions

func (b *Table[T]) DrainAtOptions(index, count int, opts DrainOptions,
) Extract[T]

func (*Table[T]) DrainOptions

func (b *Table[T]) DrainOptions(min, max T, opts DrainOptions) Extract[T]

func (*Table[T]) Front

func (b *Table[T]) Front() (T, bool)

func (*Table[T]) FrontMut

func (b *Table[T]) FrontMut() (T, bool)

func (*Table[T]) Get

func (b *Table[T]) Get(key T) T

func (*Table[T]) GetAt

func (b *Table[T]) GetAt(index int) T

func (*Table[T]) GetAtMut

func (b *Table[T]) GetAtMut(index int) T

func (*Table[T]) GetMut

func (b *Table[T]) GetMut(key T) T

func (*Table[T]) IndexOf

func (b *Table[T]) IndexOf(key T) (int, bool)

func (*Table[T]) Insert

func (b *Table[T]) Insert(item T) (T, bool)

func (*Table[T]) InsertAt

func (b *Table[T]) InsertAt(index int, item T) bool

func (*Table[T]) Len

func (b *Table[T]) Len() int

func (*Table[T]) Lookup

func (b *Table[T]) Lookup(key T) (T, bool)

func (*Table[T]) LookupAt

func (b *Table[T]) LookupAt(index int) (T, bool)

func (*Table[T]) LookupAtMut

func (b *Table[T]) LookupAtMut(index int) (T, bool)

func (*Table[T]) LookupMut

func (b *Table[T]) LookupMut(key T) (T, bool)

func (*Table[T]) PopBack

func (b *Table[T]) PopBack() (T, bool)

func (*Table[T]) PopBackFunc

func (b *Table[T]) PopBackFunc(cond func(item T) bool) (T, bool)

func (*Table[T]) PopFront

func (b *Table[T]) PopFront() (T, bool)

func (*Table[T]) PopFrontFunc

func (b *Table[T]) PopFrontFunc(cond func(item T) bool) (T, bool)

func (*Table[T]) PushBack

func (b *Table[T]) PushBack(item T) bool

func (*Table[T]) PushFront

func (b *Table[T]) PushFront(item T) bool

func (*Table[T]) Release

func (b *Table[T]) Release()

func (*Table[T]) Replace

func (b *Table[T]) Replace(item T) (T, bool)

func (*Table[T]) ReplaceAt

func (b *Table[T]) ReplaceAt(index int, item T) (T, bool)

func (*Table[T]) Seek

func (b *Table[T]) Seek(key T) (T, bool)

func (*Table[T]) SeekMut

func (b *Table[T]) SeekMut(key T) (T, bool)

func (*Table[T]) SeekNext

func (b *Table[T]) SeekNext(key T) (T, bool)

func (*Table[T]) SeekNextMut

func (b *Table[T]) SeekNextMut(key T) (T, bool)

func (*Table[T]) SeekPrev

func (b *Table[T]) SeekPrev(key T) (T, bool)

func (*Table[T]) SeekPrevMut

func (b *Table[T]) SeekPrevMut(key T) (T, bool)

func (*Table[T]) Set

func (b *Table[T]) Set(item T) (T, bool)

func (*Table[T]) String

func (b *Table[T]) String() string

type TableOptions

type TableOptions[T any] struct {
	Compare func(T, T) int  // Standard comparator
	Less    func(T, T) bool // Less comparator
	Copy    func(T) T       // Copy function
	Release func(T)         // Release function
}

TableOptions for NewTableOptions()

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL