refmap

package
v0.0.0-...-32fb1ab Latest Latest
Warning

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

Go to latest
Published: Dec 4, 2024 License: MIT Imports: 7 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 CompareFunc[K]) ref.R[Map[K, V]]

New returns an empty map wrapped in a ref.

Types

type CompareFunc

type CompareFunc[K any] func(a, b K) int

CompareFunc compares two keys, and returns -1 if a < b, 0 if a == b, 1 if a > b.

type Item

type Item[K, V any] struct {
	Key   K
	Value ref.R[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() ref.R[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 := refmap.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, does not retain the value.
	Get(key K) (ref.R[V], bool)

	// Contains returns true if the map contains a key.
	Contains(key K) bool

	// Iterator returns an iterator, the iterator does not retain the values.
	Iterator() Iterator[K, V]

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

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

	// SetRetain adds an item reference to the map, and retains it.
	SetRetain(key K, value ref.R[V])

	// Delete deletes an item by a key, releases its value.
	Delete(key K)

	// Free frees the map, releases all values.
	Free()
}

Map is an immutable sorted map which stores countable references, implemented as a btree.

The map retains/releases references internally, but does not retain them when iterating or returning.

func New

func New[K, V any](mutable bool, compare CompareFunc[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