syncmap

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 5, 2023 License: MIT Imports: 8 Imported by: 1

README

Go Doc Go Report Card

syncmap

SynchronisedMap provides a simple implementation of a concurrency safe map.

The map supports the usual operations, with Insert() operating as both Add and Update.

Maps can be serialised to []byte and then this slice merged into another map instance. Merge will only insert values for missing keys, so that the receiving map's existing state is not affected.

func main() {
	c := New(map[string]int{"x": 0, "y": 0})

	c.Insert("z", 1, false)

	c.Remove("y")

	fmt.Println(c)
	// Output: map[x:0 z:1]
}

Documentation

Index

Examples

Constants

This section is empty.

Variables

View Source
var ErrKeyExists = errors.New("key already exists")

ErrKeyExists is returned if Insert is called and key already exists

View Source
var ErrMissingKey = errors.New("unknown key")

ErrMissingKey is returned if the requested key is not in the map

Functions

func SortedKeys

func SortedKeys[K cmp.Ordered, V any](m map[K]V) []K

SortedKeys returns a sorted slice of the map's keys

Types

type ComparableAndOrdered

type ComparableAndOrdered interface {
	comparable
	cmp.Ordered
}

ComparableAndOrdered defines the type constraints for SynchronisedMap

type SynchronisedMap

type SynchronisedMap[T ComparableAndOrdered, U any] struct {
	// contains filtered or unexported fields
}

SynchronisedMap provides a concurrency safe map

func New

func New[T ComparableAndOrdered, U any](init map[T]U) *SynchronisedMap[T, U]

New returns an instance of SynchronisedMap, containing the contents of the init map

Example
c := New(map[string]int{"x": 0, "y": 0})

// Adds z
c.Insert("a", 1, false)

// Updates z without raising an error
c.Insert("a", 2, false)

c.Remove("y")

fmt.Println(c)
Output:

map[a:2 x:0]

func (*SynchronisedMap[T, U]) Bytes

func (s *SynchronisedMap[T, U]) Bytes() ([]byte, error)

Bytes serialises the current contents of the map

func (*SynchronisedMap[T, U]) Contains

func (s *SynchronisedMap[T, U]) Contains(id T) bool

Contains returns true if the key is found

func (*SynchronisedMap[T, U]) Get

func (s *SynchronisedMap[T, U]) Get(id T) (U, error)

Get returns the value associated with the key, or a key missing error

func (*SynchronisedMap[T, U]) GetKeys

func (s *SynchronisedMap[T, U]) GetKeys() []T

GetKeys returns the keys, sorted, within the map

func (*SynchronisedMap[T, U]) Insert

func (s *SynchronisedMap[T, U]) Insert(k T, v U, errIfExists bool) (U, error)

Insert adds the value at the specified key. If errIfExists is true and the key exists, then an error is raised. Otherwise the value is inserted at the key, and any pre-existing value returned.

func (*SynchronisedMap[T, U]) Len

func (s *SynchronisedMap[T, U]) Len() int

Len returns the current length

func (*SynchronisedMap[T, U]) Merge

func (s *SynchronisedMap[T, U]) Merge(b []byte) error

Merge attempts to decode the slice, assuming it is of the same type as returned by Bytes(). If successful, then adds any missing key/value pairs into this instance of the map.

func (*SynchronisedMap[T, U]) Remove

func (s *SynchronisedMap[T, U]) Remove(id T)

Remove deletes the key from the map

func (*SynchronisedMap[T, U]) String

func (s *SynchronisedMap[T, U]) String() string

String returns the contents of the map as a string, with entries ordered based on the key type T

Jump to

Keyboard shortcuts

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