mapsets

package
v0.0.0-...-966f5ea Latest Latest
Warning

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

Go to latest
Published: May 28, 2024 License: Apache-2.0 Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CycleIterator

type CycleIterator[K Key, V any] struct {
	// contains filtered or unexported fields
}

CycleIterator for an OrderedMap. See OrderedMap.Cycle for more details.

func (*CycleIterator[K, V]) Next

func (c *CycleIterator[K, V]) Next() (K, V, bool)

Next returns the next key and value of the OrderedMap and a bool describing the iteration state. If the OrderedMap is empty (and thus can't provide any keys/values, including for this iteration), false will be returned in the third value.

func (*CycleIterator[K, V]) Reset

func (c *CycleIterator[K, V]) Reset()

Reset the iteration to the beginning of the ordered map.

type Key

type Key interface {
	comparable
	cmp.Ordered
}

Key must be implemented by keys used by OrderedMap. Most 'typical' key types (string, integers, etc.) already implement it.

type KeyValue

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

KeyValue represents a value V at a key K of an OrderedMap.

type OrderedMap

type OrderedMap[K Key, V any] struct {
	// contains filtered or unexported fields
}

OrderedMap is a map from K to V which provides total ordering as defined by the keys used.

The K type used must implement total ordering and comparability per the Key interface, and must not be mutated after being inserted (or at least, must keep the same ordering expressed as part of its Key implementation). A string (or string wrapping type) is a good key, as strings in Go are immutable and act like pointers.

The values used will be copied by the map implementation. For structured types, defining V to be a pointer to a structure is probably the correct choice.

An empty OrderedMap is ready to use and will contain no elements.

It is not safe to be used by multiple goroutines. It can be locked behind a sync.RWMutex, with a read lock taken for any method that does not mutate the map, and with a write lock taken for any method that does mutate it.

func (*OrderedMap[K, V]) Clear

func (s *OrderedMap[K, V]) Clear()

Clear removes all keys/values from the OrderedMap.

func (*OrderedMap[K, V]) Clone

func (s *OrderedMap[K, V]) Clone() OrderedMap[K, V]

Clone (perform a deep copy) of the OrderedMap, copying all keys and values.

func (*OrderedMap[K, V]) Count

func (s *OrderedMap[K, V]) Count() int

Count returns the number of keys/values in this OrderedMap.

func (*OrderedMap[K, V]) Cycle

func (s *OrderedMap[K, V]) Cycle() CycleIterator[K, V]

Cycle returns a CycleIterator referencing this OrderedMap.

This iterator will cycle through all the keys/values of the OrderedMap, then wrap around again when it reaches the end of the OrderedMap. This behaviour is designed to be used in round-robin mechanisms.

As the OrderedMap changes, the returned CycleIterator will also change:

  • If a key is removed or added, the CycleIterator is guaranteed to return it within one entire cycle through all the values.
  • If a value is changed, the new value is guaranteed to be returned on the next iteration when the previous value would have been returned.

Generally, it is safe to hold on to OrderedMap pointers and mutate the underlying OrderedMap.

However, concurrent access to the OrderedMap and CycleIterator from different goroutines is not safe. Access to the CycleIterator should be guarded behind the same mutual exclusion mechanism as access to the OrderedMap.

func (*OrderedMap[K, V]) Delete

func (s *OrderedMap[K, V]) Delete(k K)

Delete the value at the given key, if present.

This method mutates the map.

func (*OrderedMap[K, V]) Get

func (s *OrderedMap[K, V]) Get(k K) (V, bool)

Get a value at a given key. If there is no value for the given key, an empty V and false will be returned.

This returns a copy of the stored value.

func (*OrderedMap[K, V]) Insert

func (s *OrderedMap[K, V]) Insert(k K, v V)

Insert a given value at a given key. If a value at this key already exists, it will be overwritten with the new value.

This method mutates the map.

func (*OrderedMap[K, V]) Keys

func (s *OrderedMap[K, V]) Keys() []K

Keys returns a copy of the keys of this map, ordered according to the Key implementation used.

func (*OrderedMap[K, V]) Replace

func (s *OrderedMap[K, V]) Replace(o *OrderedMap[K, V])

Replace all contents of this map with the contents of another map. The other map must not be concurrently accessed.

This method mutates the map.

func (*OrderedMap[K, V]) Values

func (s *OrderedMap[K, V]) Values() []KeyValue[K, V]

Values returns a copy of all the keys and values of this map, ordered according to the key implementation used.

Jump to

Keyboard shortcuts

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