Documentation
¶
Index ¶
- type OrderedMap
- func (o *OrderedMap[K, V]) Clear()
- func (o *OrderedMap[K, V]) Delete(key K)
- func (o *OrderedMap[K, V]) Get(key K) (V, bool)
- func (o *OrderedMap[K, V]) Keys() []K
- func (o *OrderedMap[K, V]) Len() int
- func (o *OrderedMap[K, V]) Range(cb func(int, K, V) bool)
- func (o *OrderedMap[K, V]) Set(key K, value V)
- func (o *OrderedMap[K, V]) Values() []V
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type OrderedMap ¶
type OrderedMap[K comparable, V any] struct { // contains filtered or unexported fields }
OrderedMap structure that maintains the insertion order
func NewOrderedMap ¶
func NewOrderedMap[K comparable, V any]() *OrderedMap[K, V]
NewOrderedMap creates a new instance of OrderedMap
To ensure concurrency safety and avoid race conditions and deadlocks, avoid cross-instance dependencies. Ensure that operations on different OrderedMap instances are independent and do not inadvertently depend on each other. Take the following example:
var o1, o2 OrderedMap[int, string]
go func() {
o1.Set(1, "one")
o2.Get(1) // Waits on o1 indirectly
}()
go func() {
o2.Set(2, "two")
o1.Get(2) // Waits on o2 indirectly
}()
One should also be careful when using the Range method. The callback function should not modify the OrderedMap.
func (*OrderedMap[K, V]) Clear ¶
func (o *OrderedMap[K, V]) Clear()
Clear removes all key-value pairs from the OrderedMap
func (*OrderedMap[K, V]) Delete ¶
func (o *OrderedMap[K, V]) Delete(key K)
Delete removes a key-value pair from the OrderedMap
func (*OrderedMap[K, V]) Get ¶
func (o *OrderedMap[K, V]) Get(key K) (V, bool)
Get retrieves the value associated with a key
func (*OrderedMap[K, V]) Keys ¶
func (o *OrderedMap[K, V]) Keys() []K
Keys returns the keys in the order they were added
func (*OrderedMap[K, V]) Len ¶
func (o *OrderedMap[K, V]) Len() int
Len returns the number of key-value pairs in the OrderedMap
func (*OrderedMap[K, V]) Range ¶
func (o *OrderedMap[K, V]) Range(cb func(int, K, V) bool)
Range iterates over the OrderedMap in the order of insertion and applies a callback function. If the callback function returns false, the iteration stops.
The callback function should not modify the OrderedMap in any way. For example: Calling Set, Delete, or Clear inside the callback function will cause a deadlock.
func (*OrderedMap[K, V]) Set ¶
func (o *OrderedMap[K, V]) Set(key K, value V)
Set adds or updates a key-value pair in the OrderedMap
func (*OrderedMap[K, V]) Values ¶
func (o *OrderedMap[K, V]) Values() []V
Values returns the values in the order they were added