Documentation ¶
Index ¶
- Variables
- func SortedKeys[K cmp.Ordered, V any](m map[K]V) []K
- type ComparableAndOrdered
- type SynchronisedMap
- func (s *SynchronisedMap[T, U]) Bytes() ([]byte, error)
- func (s *SynchronisedMap[T, U]) Contains(id T) bool
- func (s *SynchronisedMap[T, U]) Get(id T) (U, error)
- func (s *SynchronisedMap[T, U]) GetKeys() []T
- func (s *SynchronisedMap[T, U]) Insert(k T, v U, errIfExists bool) (U, error)
- func (s *SynchronisedMap[T, U]) Len() int
- func (s *SynchronisedMap[T, U]) Merge(b []byte) error
- func (s *SynchronisedMap[T, U]) Remove(id T)
- func (s *SynchronisedMap[T, U]) String() string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ErrKeyExists = errors.New("key already exists")
ErrKeyExists is returned if Insert is called and key already exists
var ErrMissingKey = errors.New("unknown key")
ErrMissingKey is returned if the requested key is not in the map
Functions ¶
func SortedKeys ¶
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