immap

package
v0.0.0-...-bf4279b Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewRef

func NewRef[K, V any](mutable bool, compare compare.Compare[K]) ref.R[Map[K, V]]

New returns an empty map wrapped in a ref.

Types

type Item

type Item[K, V any] struct {
	Key   K
	Value V
}

Item is a map item.

type Iterator

type Iterator[K any, V any] interface {
	// OK returns true when the iterator points to a valid item, or false on end.
	OK() bool

	// Key returns the current key or zero, the key is valid until the next iteration.
	Key() K

	// Value returns the current value or zero, the value is valid until the next iteration.
	Value() V

	// Next moves to the next item.
	Next() bool

	// Previous moves to the previous item.
	Previous() bool

	// SeekToStart positions the iterator at the start.
	SeekToStart() bool

	// SeekToEnd positions the iterator at the end.
	SeekToEnd() bool

	// SeekBefore positions the iterator before an item with key >= key.
	SeekBefore(key K) bool

	// Free frees the iterator, implements the ref.Free interface.
	Free()
}

Iterator sequentially iterates over sorted map items.

Usage:

it := immap.Iterator()
defer it.Free()

it.SeekToStart()

for it.Next() {
	key := it.Key()
	value := it.Value()
}

type Map

type Map[K, V any] interface {
	// Empty returns true if the map is empty.
	Empty() bool

	// Length returns the number of items in this map, this can be an estimate.
	Length() int64

	// Mutable returns true if the map is mutable.
	Mutable() bool

	// Clone returns a mutable clone of the map.
	Clone() Map[K, V]

	// Freeze makes the map immutable.
	Freeze()

	// Get returns an item by a key.
	Get(key K) (V, bool)

	// Contains returns true if a key exists.
	Contains(key K) bool

	// Iterator returns an iterator.
	Iterator() Iterator[K, V]

	// Keys returns all keys.
	Keys() []K

	// Set adds an item to the map.
	Set(key K, value V)

	// Move moves an item from one key to another, or returns false if the key does not exist.
	Move(key K, newKey K) bool

	// Delete deletes an item by a key.
	Delete(key K)

	// Free frees the map.
	Free()
}

Map is an immutable sorted map, implemented as a btree.

func New

func New[K, V any](mutable bool, compare compare.Compare[K]) Map[K, V]

New returns an empty map.

Jump to

Keyboard shortcuts

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