stablemap

package module
v1.5.2 Latest Latest
Warning

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

Go to latest
Published: Oct 17, 2025 License: MIT Imports: 9 Imported by: 4

README

Stable Map

Stable Map is a map that provides ordered, deterministic iteration of key-value pairs. It's concurrency safe. Insertion order of key-value pairs is preserved. It marshals and unmarshals itself into deterministic bytes, using MessagePack.

Stable Map is especially useful for equality comparison.

Getting Started

package main

import (
	"fmt"

	smap "github.com/sean9999/go-stable-map"
)

func main() {

	m := smap.New[string, string]()

	m.Set("foo", "bar")
	m.Set("bing", "bat")

	//	dump the map 10 times. See that the order is always the same
	for range 10 {
		for k, v := range m.Entries() {
			fmt.Printf("%s:\t%s\n", k, v)
		}
		
		//	see that the binary representation is always the same
		bin, _ := m.MarshalBinary()
		fmt.Printf("hex:\t%x\n\n", bin)
	}

}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LexicalRange added in v1.3.0

func LexicalRange[K cmp.Ordered, V any](sm Map[K, V]) iter.Seq2[K, V]

LexicalRange can be used to iterate over a Map in lexical order.

Types

type ActiveMap added in v0.0.4

type ActiveMap[K comparable, V any] struct {
	*Map[K, V]
	// contains filtered or unexported fields
}

ActiveMap is a Map that emits events when it mutates.

func NewActiveMap added in v0.0.4

func NewActiveMap[K comparable, V any]() *ActiveMap[K, V]

NewActiveMap instantiates a new ActiveMap

func (*ActiveMap[K, V]) Delete added in v0.0.4

func (am *ActiveMap[K, V]) Delete(k K) error

func (*ActiveMap[K, V]) Events added in v0.0.4

func (am *ActiveMap[K, V]) Events() <-chan Result[K, V]

func (*ActiveMap[K, V]) Set added in v0.0.4

func (am *ActiveMap[K, V]) Set(k K, v V, fn func(res Result[K, V]) string) error

type LexicalMap added in v1.5.1

type LexicalMap[K cmp.Ordered, V any] struct {
	*Map[K, V]
}

LexicalMap is a Map that cares about lexical order rather than insertion order.

func LexicalFrom added in v1.4.2

func LexicalFrom[K cmp.Ordered, V any](m map[K]V) *LexicalMap[K, V]

func NewLexicalMap added in v1.5.1

func NewLexicalMap[K cmp.Ordered, V any]() *LexicalMap[K, V]

func (*LexicalMap[K, V]) Entries added in v1.5.1

func (lm *LexicalMap[K, V]) Entries() iter.Seq2[K, V]

func (*LexicalMap[K, V]) MarshalBinary added in v1.5.1

func (lm *LexicalMap[K, V]) MarshalBinary() ([]byte, error)

type Map added in v1.5.1

type Map[K comparable, V any] struct {
	*sync.RWMutex
	// contains filtered or unexported fields
}

Map is a map whose keys are ordered, and whose operations are concurrency safe and which marshals itself into predictable, deterministic bytes.

func From added in v0.0.3

func From[K comparable, V any](m map[K]V) *Map[K, V]

func New

func New[K comparable, V any]() *Map[K, V]

func (*Map[K, V]) AsMap added in v1.5.1

func (sm *Map[K, V]) AsMap() map[K]V

func (*Map[K, V]) Clone added in v1.5.1

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

Clone performs a safe clone

func (*Map[K, V]) Delete added in v1.5.1

func (sm *Map[K, V]) Delete(k K) error

Delete deletes an element by key

func (*Map[K, V]) DeleteAt added in v1.5.1

func (sm *Map[K, V]) DeleteAt(i int)

delete the element at a specific index, or panic

func (*Map[K, V]) Entries added in v1.5.1

func (sm *Map[K, V]) Entries() iter.Seq2[K, V]

Entries provides a stable "range over" iteration, preserving insertion order.

func (*Map[K, V]) Get added in v1.5.1

func (sm *Map[K, V]) Get(k K) (V, bool)

Get gets a value and a boolean indicating if there actually was something there

func (*Map[K, V]) GetAt added in v1.5.1

func (sm *Map[K, V]) GetAt(i int) V

get the element at a particular index, or panic

func (*Map[K, V]) Import added in v1.5.1

func (sm *Map[K, V]) Import(b *Map[K, V])

Import imports another Map

func (*Map[K, V]) Incorporate added in v1.5.1

func (sm *Map[K, V]) Incorporate(m map[K]V)

Incorporate incorporates a map, merging it with existing entries

func (*Map[K, V]) IndexOf added in v1.5.1

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

get the index value of a particular key

func (*Map[K, V]) Length added in v1.5.1

func (sm *Map[K, V]) Length() int

func (*Map[K, V]) MarshalBinary added in v1.5.1

func (sm *Map[K, V]) MarshalBinary() ([]byte, error)

func (Map[K, V]) MarshalJSON added in v1.5.1

func (sm Map[K, V]) MarshalJSON() ([]byte, error)

func (*Map[K, V]) Set added in v1.5.1

func (sm *Map[K, V]) Set(key K, val V) error

Set adds or updates an element in a thread-safe manner

func (*Map[K, V]) UnmarshalBinary added in v1.5.1

func (sm *Map[K, V]) UnmarshalBinary(p []byte) error

func (*Map[K, V]) UnmarshalJSON added in v1.5.1

func (sm *Map[K, V]) UnmarshalJSON(b []byte) error

func (*Map[K, V]) Unshift added in v1.5.1

func (sm *Map[K, V]) Unshift(key K, val V)

place this pair at the head

type Maybe added in v1.3.0

type Maybe[T any] struct {
	Val T
	Is  bool
	Err error
}

type Result added in v0.0.4

type Result[K comparable, V any] struct {
	Action string
	Key    K
	OldVal V
	NewVal V
	Msg    string
}

A Result is emitted whenever an ActiveMap mutates

Jump to

Keyboard shortcuts

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