btype

package module
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 16, 2026 License: MIT Imports: 12 Imported by: 0

README

Tile38
MapSetArrayTableStackQueueDequePrique

The btype package provides btree based collection types that allow Go programmers to easily implement common data structures like maps, arrays, queues, and stacks.

It's hand-crafted with performance in mind and is generally faster than the state of the art btrees for Go, Rust, and C++. google/btree, tidwall/btree, rust/BTreeMap, frozenca/btree. [benchmarks]

Features

  • Includes collections types: map, set, queue, stack, table. Each backed by a btree structure.
  • Modern Go ergonomics with a friendly API.
  • 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

Includes the following collection 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

Map

btype.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.
Get(key, val)           # 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])
Drain()                 # Iterate and remove in ascending order.       (iter.Seq2[K,V])
DrainBackward()         # Iterate and remove in descending order.      (iter.Seq2[K,V])

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.
PopFrontIf(cond)        # Remove first item if cond returns true.
PopBackIf(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])

DeleteRange(min, max)   # Remove items within the provided sub-range. [min,max)
DeleteRangeAt(i, count) # Remove items starting at index.

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

import (
	"fmt"

	"github.com/tidwall/btype"
)

func main() {
	// Create a map
	var users btype.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

btype.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])
Drain()                 # Iterate and remove in ascending order.      (iter.Seq[K])
DrainBackward()         # Iterate and remove in descending order.     (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.
PopFrontIf(cond)        # Remove first key if cond returns true.
PopBackIf(cond)         # Remove last key if cond returns true.

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])

DeleteRange(min, max)   # Remove keys within the provided sub-range. [min,max)
DeleteRangeAt(i, count) # Remove keys starting at index.

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

import (
	"fmt"

	"github.com/tidwall/btype"
)

func main() {
	// Create a set
	var names btype.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

btype.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.
Get(i)                # Gets item at index.
Delete(i)             # Remove item at index.

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])
Drain()               # Iterate and remove in ascending order.         (iter.Seq[T])
DrainBackward()       # Iterate and remove in descending order.        (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.
PopFrontIf(cond)      # Remove first item if cond returns true.
PopBackIf(cond)       # Remove last item if cond returns true.

DeleteRange(i, count) # Remove items starting at index.

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

import (
	"fmt"

	"github.com/tidwall/btype"
)

func main() {
	// Create an array of names
	var names btype.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

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

This collection type is functionally similar to tidwall/btree.BTreeG and google/btree.BTreeG, but includes additional features and performance enhancements.

Features
Automatic type ordering

A btype.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 btype.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 btype.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 := btype.NewTableOptions(btype.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 btype:"key" designates that struct field as the key.

// Order by id
type User struct {
	id   int `btype:"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 `btype:"key"`
	first string `btype:"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 `btype:"key.1"`
	last  string `btype:"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 `btype:"key,desc"`
	first string `btype:"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 `btype:"key,binary_CI,desc"`
 	first string `btype:"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.
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])
Drain()                 # Iterate and remove in ascending order.       (iter.Seq[T])
DrainBackward()         # Iterate and remove in descending order.      (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.
PopFrontIf(cond)        # Remove first item if cond returns true.
PopBackIf(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])

DeleteRange(min, max)   # Remove items within the provided sub-range. [min,max)
DeleteRangeAt(i, count) # Remove items starting at index.

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

Stack

btype.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
PopIf(cond)  # Remove item from top stack if cond returns true.

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.
Copy()     # Copy collection, fast O(1), uses Copy-on-write shadow cloning.
Clear()    # Remove all items from collection
Release()  # Same as Clear() but optimized for copied collections.
package main

import (
	"fmt"

	"github.com/tidwall/btype"
)

func main() {
	// Create an stack of names
	var names btype.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

btype.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.
PopIf(cond)  # Remove first item if cond returns true.

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.
Copy()     # Copy collection, fast O(1), uses Copy-on-write shadow cloning.
Clear()    # Remove all items from collection
Release()  # Same as Clear() but optimized for copied collections.
Example
package main

import (
	"fmt"

	"github.com/tidwall/btype"
)

func main() {
	// Create an queue of names
	var names btype.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

btype.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.
PopFrontIf(cond) # Remove first item if cond returns true.
PopBackIf(cond)  # Remove last item if cond returns true.

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.
Copy()          # Copy collection, fast O(1), uses Copy-on-write shadow cloning.
Clear()         # Remove all items from collection
Release()       # Same as Clear() but optimized for copied collections.
Example
package main

import (
	"fmt"

	"github.com/tidwall/btype"
)

func main() {
	// Create an queue of names
	var names btype.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

btype.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.
PopIf(cond)  # Remove largest item if cond returns true.

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.
Copy()     # Copy collection, fast O(1), uses Copy-on-write shadow cloning.
Clear()    # Remove all items from collection
Release()  # Same as Clear() but optimized for copied collections.
Example
package main

import (
	"fmt"

	"github.com/tidwall/btype"
)

func main() {

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

	// Create an queue of users
	var users btype.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 btype package has various optimizations that enhance performance over existing implementations.

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

https://github.com/tidwall/btype-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/btype
insert(seq)      1,000,000 ops in   0.017 secs   17.1 ns/op   58,389,383 op/sec
insert(rand)     1,000,000 ops in   0.076 secs   76.0 ns/op   13,159,734 op/sec
get(seq)         1,000,000 ops in   0.030 secs   29.8 ns/op   33,599,978 op/sec
get(rand)        1,000,000 ops in   0.064 secs   64.4 ns/op   15,535,343 op/sec

tidwall/btree
insert(seq)      1,000,000 ops in   0.037 secs   36.9 ns/op   27,104,762 op/sec
insert(rand)     1,000,000 ops in   0.134 secs  134.3 ns/op    7,445,864 op/sec
get(seq)         1,000,000 ops in   0.041 secs   41.4 ns/op   24,141,559 op/sec
get(rand)        1,000,000 ops in   0.128 secs  127.9 ns/op    7,817,200 op/sec

google/btree
insert(seq)      1,000,000 ops in   0.070 secs   69.8 ns/op   14,321,709 op/sec
insert(rand)     1,000,000 ops in   0.153 secs  153.4 ns/op    6,518,280 op/sec
get(seq)         1,000,000 ops in   0.065 secs   64.6 ns/op   15,486,010 op/sec
get(rand)        1,000,000 ops in   0.155 secs  154.9 ns/op    6,454,916 op/sec

rust/btree
insert(seq)      1,000,000 ops in   0.051 secs   51.0 ns/op   19,624,389 op/sec
insert(rand)     1,000,000 ops in   0.098 secs   98.2 ns/op   10,187,241 op/sec
get(seq)         1,000,000 ops in   0.033 secs   32.6 ns/op   30,650,401 op/sec
get(rand)        1,000,000 ops in   0.097 secs   97.0 ns/op   10,308,321 op/sec

tidwall/bgen
insert(seq)      1,000,000 ops in   0.053 secs   52.6 ns/op   19,011,130 op/sec
insert(rand)     1,000,000 ops in   0.076 secs   75.8 ns/op   13,186,500 op/sec
get(seq)         1,000,000 ops in   0.033 secs   33.0 ns/op   30,264,262 op/sec
get(rand)        1,000,000 ops in   0.069 secs   68.9 ns/op   14,524,288 op/sec

frozenca/btree
insert(seq)      1,000,000 ops in   0.093 secs   92.7 ns/op   10,782,702 op/sec
insert(rand)     1,000,000 ops in   0.081 secs   81.5 ns/op   12,275,940 op/sec
get(seq)         1,000,000 ops in   0.044 secs   44.2 ns/op   22,636,693 op/sec
get(rand)        1,000,000 ops in   0.079 secs   79.1 ns/op   12,638,367 op/sec
uint64 keys
tidwall/btype
insert(seq)      1,000,000 ops in   0.018 secs   18.3 ns/op   54,498,410 op/sec
insert(rand)     1,000,000 ops in   0.080 secs   80.3 ns/op   12,457,898 op/sec
get(seq)         1,000,000 ops in   0.030 secs   30.1 ns/op   33,200,724 op/sec
get(rand)        1,000,000 ops in   0.072 secs   72.4 ns/op   13,819,949 op/sec

tidwall/btree
insert(seq)      1,000,000 ops in   0.039 secs   38.7 ns/op   25,824,082 op/sec
insert(rand)     1,000,000 ops in   0.146 secs  146.0 ns/op    6,849,574 op/sec
get(seq)         1,000,000 ops in   0.053 secs   52.6 ns/op   19,010,440 op/sec
get(rand)        1,000,000 ops in   0.141 secs  140.9 ns/op    7,099,716 op/sec

google/btree
insert(seq)      1,000,000 ops in   0.077 secs   76.8 ns/op   13,028,686 op/sec
insert(rand)     1,000,000 ops in   0.173 secs  172.9 ns/op    5,784,271 op/sec
get(seq)         1,000,000 ops in   0.062 secs   61.9 ns/op   16,165,628 op/sec
get(rand)        1,000,000 ops in   0.166 secs  166.4 ns/op    6,008,125 op/sec

rust/btree
insert(seq)      1,000,000 ops in   0.044 secs   43.6 ns/op   22,936,305 op/sec
insert(rand)     1,000,000 ops in   0.105 secs  105.2 ns/op    9,502,632 op/sec
get(seq)         1,000,000 ops in   0.034 secs   33.9 ns/op   29,509,841 op/sec
get(rand)        1,000,000 ops in   0.107 secs  106.8 ns/op    9,362,769 op/sec

tidwall/bgen
insert(seq)      1,000,000 ops in   0.054 secs   53.7 ns/op   18,605,963 op/sec
insert(rand)     1,000,000 ops in   0.081 secs   80.6 ns/op   12,406,050 op/sec
get(seq)         1,000,000 ops in   0.033 secs   32.6 ns/op   30,657,821 op/sec
get(rand)        1,000,000 ops in   0.075 secs   75.4 ns/op   13,269,668 op/sec

frozenca/btree
insert(seq)      1,000,000 ops in   0.094 secs   93.7 ns/op   10,668,965 op/sec
insert(rand)     1,000,000 ops in   0.094 secs   93.6 ns/op   10,688,826 op/sec
get(seq)         1,000,000 ops in   0.044 secs   43.5 ns/op   22,964,359 op/sec
get(rand)        1,000,000 ops in   0.087 secs   87.0 ns/op   11,497,710 op/sec
string keys
tidwall/btype
insert(seq)      1,000,000 ops in   0.074 secs   73.9 ns/op   13,534,682 op/sec
insert(rand)     1,000,000 ops in   0.287 secs  287.2 ns/op    3,482,420 op/sec
get(seq)         1,000,000 ops in   0.094 secs   93.6 ns/op   10,683,081 op/sec
get(rand)        1,000,000 ops in   0.328 secs  327.6 ns/op    3,052,945 op/sec

tidwall/btree
insert(seq)      1,000,000 ops in   0.124 secs  124.3 ns/op    8,043,933 op/sec
insert(rand)     1,000,000 ops in   0.402 secs  402.3 ns/op    2,485,505 op/sec
get(seq)         1,000,000 ops in   0.122 secs  122.4 ns/op    8,171,636 op/sec
get(rand)        1,000,000 ops in   0.452 secs  452.0 ns/op    2,212,330 op/sec

google/btree
insert(seq)      1,000,000 ops in   0.191 secs  191.0 ns/op    5,234,886 op/sec
insert(rand)     1,000,000 ops in   0.437 secs  437.3 ns/op    2,286,980 op/sec
get(seq)         1,000,000 ops in   0.146 secs  145.9 ns/op    6,855,693 op/sec
get(rand)        1,000,000 ops in   0.487 secs  487.0 ns/op    2,053,473 op/sec

rust/btree
insert(seq)      1,000,000 ops in   0.250 secs  250.3 ns/op    3,995,141 op/sec
insert(rand)     1,000,000 ops in   0.510 secs  510.3 ns/op    1,959,504 op/sec
get(seq)         1,000,000 ops in   0.218 secs  218.3 ns/op    4,581,355 op/sec
get(rand)        1,000,000 ops in   0.591 secs  591.0 ns/op    1,692,138 op/sec

tidwall/bgen
insert(seq)      1,000,000 ops in   0.392 secs  392.3 ns/op    2,549,279 op/sec
insert(rand)     1,000,000 ops in   0.400 secs  400.5 ns/op    2,496,899 op/sec
get(seq)         1,000,000 ops in   0.466 secs  466.3 ns/op    2,144,526 op/sec
get(rand)        1,000,000 ops in   0.478 secs  477.6 ns/op    2,093,912 op/sec

frozenca/btree
insert(seq)      1,000,000 ops in   0.636 secs  636.3 ns/op    1,571,476 op/sec
insert(rand)     1,000,000 ops in   0.633 secs  632.7 ns/op    1,580,596 op/sec
get(seq)         1,000,000 ops in   0.376 secs  376.5 ns/op    2,656,345 op/sec
get(rand)        1,000,000 ops in   0.618 secs  618.4 ns/op    1,617,120 op/sec

Documentation

Overview

https://github.com/tidwall/btype

Copyright 2026 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.

btype - B-tree based collections for go

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 Array

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

func NewArrayOptions

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

func NewList

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

func (*Array[T]) All

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

func (*Array[T]) Append

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

func (*Array[T]) Ascend

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

func (*Array[T]) Back

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

func (*Array[T]) Backward

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

func (*Array[T]) Clear

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

func (*Array[T]) Copy

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

func (*Array[T]) Delete

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

func (*Array[T]) DeleteRange

func (b *Array[T]) DeleteRange(index, count int) Slice[T]

func (*Array[T]) DeleteRangeOptions

func (b *Array[T]) DeleteRangeOptions(index, count int,
	opts DeleteRangeOptions,
) Slice[T]

func (*Array[T]) Descend

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

func (*Array[T]) Drain

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

func (*Array[T]) DrainBackward

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

func (*Array[T]) Front

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

func (*Array[T]) Get

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

func (*Array[T]) Insert

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

func (*Array[T]) Len

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

func (*Array[T]) PopBack

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

func (*Array[T]) PopBackIf added in v0.3.0

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

func (*Array[T]) PopFront

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

func (*Array[T]) PopFrontIf added in v0.3.0

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

func (*Array[T]) PushBack

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

func (*Array[T]) PushFront

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

func (*Array[T]) Release

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

func (*Array[T]) Replace

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

type ArrayOptions

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

type DeleteRangeOptions

type DeleteRangeOptions struct {
	NoReturn     bool
	MinExclusive bool
	MaxInclusive bool
}

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]

func NewDequeOptions

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

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)

func (*Deque[T]) BackMut

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

func (*Deque[T]) Backward

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

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

func (*Deque[T]) BackwardMut

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

All 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]) Copy

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

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

func (*Deque[T]) Drain

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

func (*Deque[T]) DrainBackward

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

func (*Deque[T]) Front

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

func (*Deque[T]) FrontMut

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

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)

func (*Deque[T]) PopBackIf added in v0.3.0

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

func (*Deque[T]) PopFront

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

func (*Deque[T]) PopFrontIf added in v0.3.0

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

func (*Deque[T]) PushBack

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

func (*Deque[T]) PushFront

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

func (*Deque[T]) Release

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

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

type DequeOptions

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

type Map

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

func NewMap

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

func NewMapOptions

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

func (*Map[K, V]) All

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

func (*Map[K, V]) AllMut

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

func (*Map[K, V]) Ascend

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

func (*Map[K, V]) AscendAt

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

func (*Map[K, V]) AscendAtMut

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

func (*Map[K, V]) AscendMut

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

func (*Map[K, V]) Back

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

func (*Map[K, V]) BackMut

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

func (*Map[K, V]) Backward

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

func (*Map[K, V]) BackwardMut

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

func (*Map[K, V]) Clear

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

func (*Map[K, V]) Contains

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

func (*Map[K, V]) Copy

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

func (*Map[K, V]) Delete

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

func (*Map[K, V]) DeleteAt

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

func (*Map[K, V]) DeleteRange

func (b *Map[K, V]) DeleteRange(min, max K) Slice2[K, V]

func (*Map[K, V]) DeleteRangeAt

func (b *Map[K, V]) DeleteRangeAt(index, count int) Slice2[K, V]

func (*Map[K, V]) DeleteRangeAtOptions

func (b *Map[K, V]) DeleteRangeAtOptions(index, count int,
	opts DeleteRangeOptions,
) Slice2[K, V]

func (*Map[K, V]) DeleteRangeOptions

func (b *Map[K, V]) DeleteRangeOptions(min, max K, opts DeleteRangeOptions,
) Slice2[K, V]

func (*Map[K, V]) Descend

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

func (*Map[K, V]) DescendAt

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

func (*Map[K, V]) DescendAtMut

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

func (*Map[K, V]) DescendMut

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

func (*Map[K, V]) Drain

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

func (*Map[K, V]) DrainBackward

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

func (*Map[K, V]) Front

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

func (*Map[K, V]) FrontMut

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

func (*Map[K, V]) Get

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

func (*Map[K, V]) GetAt

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

func (*Map[K, V]) GetAtMut

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

func (*Map[K, V]) GetMut

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

func (*Map[K, V]) IndexOf

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

func (*Map[K, V]) Insert

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

func (*Map[K, V]) InsertAt

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

func (*Map[K, V]) Keys

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

func (*Map[K, V]) Len

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

func (*Map[K, V]) PopBack

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

func (*Map[K, V]) PopBackIf added in v0.3.0

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

func (*Map[K, V]) PopFront

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

func (*Map[K, V]) PopFrontIf added in v0.3.0

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

func (*Map[K, V]) PushBack

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

func (*Map[K, V]) PushFront

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

func (*Map[K, V]) Release

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

func (*Map[K, V]) Replace

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

func (*Map[K, V]) ReplaceAt

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

func (*Map[K, V]) Seek

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

func (*Map[K, V]) SeekMut

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

func (*Map[K, V]) SeekNext

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

func (*Map[K, V]) SeekNextMut

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

func (*Map[K, V]) SeekPrev

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

func (*Map[K, V]) SeekPrevMut

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

func (*Map[K, V]) Set

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

func (*Map[K, V]) Values

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

func (*Map[K, V]) ValuesMut

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

type MapOptions

type MapOptions[K cmp.Ordered, V any] struct {
	Copy     func(V) V
	Release  func(V)
	NoPrefix bool
}

type Prique

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

func NewPrique

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

func NewPriqueOptions

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

func (*Prique[T]) All

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

func (*Prique[T]) AllMut

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

func (*Prique[T]) At

func (b *Prique[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 (*Prique[T]) AtMut

func (b *Prique[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 (*Prique[T]) Clear

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

Clear the queue.

func (*Prique[T]) Copy

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

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

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]) Drain

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

Drain iterates over each item in queue, popping along the way.

func (*Prique[T]) Front

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

func (*Prique[T]) FrontMut

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

func (*Prique[T]) Len

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

Len returns the number of items in queue

func (*Prique[T]) Pop

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

Pop first item from queue.

func (*Prique[T]) PopIf added in v0.3.0

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

func (*Prique[T]) Push

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

func (*Prique[T]) Release

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

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

type PriqueOptions

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

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]

func NewQueueOptions

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

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]) Copy

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

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

func (*Queue[K]) Drain

func (b *Queue[K]) Drain() iter.Seq[K]

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]) PopIf added in v0.3.0

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

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 will clear the queue and release any references. This method is functionally equivalent to Clear() but is an optimization for collections that are copied using Copy().

type QueueOptions

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

type Set

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

func NewSet

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

func (*Set[K]) All

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

func (*Set[K]) Ascend

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

func (*Set[K]) AscendAt

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

func (*Set[K]) Back

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

func (*Set[K]) Backward

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

func (*Set[K]) Clear

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

func (*Set[K]) Contains

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

func (*Set[K]) Copy

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

func (*Set[K]) Delete

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

func (*Set[K]) DeleteAt

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

func (*Set[K]) DeleteRange

func (b *Set[K]) DeleteRange(min, max K) Slice[K]

func (*Set[K]) DeleteRangeAt

func (b *Set[K]) DeleteRangeAt(index, count int) Slice[K]

func (*Set[K]) DeleteRangeAtOptions

func (b *Set[K]) DeleteRangeAtOptions(index, count int, opts DeleteRangeOptions,
) Slice[K]

func (*Set[K]) DeleteRangeOptions

func (b *Set[K]) DeleteRangeOptions(min, max K, opts DeleteRangeOptions,
) Slice[K]

func (*Set[K]) Descend

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

func (*Set[K]) DescendAt

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

func (*Set[K]) Drain

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

func (*Set[K]) DrainBackward

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

func (*Set[K]) Front

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

func (*Set[K]) GetAt

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

func (*Set[K]) IndexOf

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

func (*Set[K]) Insert

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

func (*Set[K]) InsertAt

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

func (*Set[K]) Len

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

func (*Set[K]) PopBack

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

func (*Set[K]) PopBackIf added in v0.3.0

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

func (*Set[K]) PopFront

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

func (*Set[K]) PopFrontIf added in v0.3.0

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

func (*Set[K]) PushBack

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

func (*Set[K]) PushFront

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

func (*Set[K]) Release

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

func (*Set[K]) ReplaceAt

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

func (*Set[K]) Seek

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

func (*Set[K]) SeekNext

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

func (*Set[K]) SeekPrev

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

type Slice

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

type Slice2

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

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]

func NewStackOptions

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

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]) Copy

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

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

func (*Stack[K]) Drain

func (b *Stack[K]) Drain() iter.Seq[K]

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 will clear the stack and releases any copied reference. This method is functionally equivalent to Clear() but is an optimization for collections that are copied using Copy().

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
	Release func(T)
}

type Table

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

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]) Contains

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

func (*Table[T]) Copy

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

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]) DeleteRange

func (b *Table[T]) DeleteRange(min, max T) Slice[T]

func (*Table[T]) DeleteRangeAt

func (b *Table[T]) DeleteRangeAt(index, count int) Slice[T]

func (*Table[T]) DeleteRangeAtOptions

func (b *Table[T]) DeleteRangeAtOptions(index, count int,
	opts DeleteRangeOptions,
) Slice[T]

func (*Table[T]) DeleteRangeOptions

func (b *Table[T]) DeleteRangeOptions(min, max T, opts DeleteRangeOptions,
) Slice[T]

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() iter.Seq[T]

func (*Table[T]) DrainBackward

func (b *Table[T]) DrainBackward() iter.Seq[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, bool)

func (*Table[T]) GetAt

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

func (*Table[T]) GetAtMut

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

func (*Table[T]) GetMut

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

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]) PopBack

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

func (*Table[T]) PopBackIf added in v0.3.0

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

func (*Table[T]) PopFront

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

func (*Table[T]) PopFrontIf added in v0.3.0

func (b *Table[T]) PopFrontIf(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)

type TableOptions

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

Jump to

Keyboard shortcuts

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