Documentation ¶
Overview ¶
Package lmap provides helpers for the built in map type.
Note that there is also util/filter.Map which provides tools for performing filtering operations on a map.
Index ¶
- func FromIter[I any, Key comparable, Val any](in liter.Iter[I], fn KVGen[I, Key, Val]) map[Key]Val
- func SortKeys[K constraints.Ordered, V any](m map[K]V) slice.Slice[K]
- type IterFunc
- type KVGen
- type Map
- type Mapper
- type Safe
- type Wrapper
- func (w Wrapper[K, V]) DeleteMany(keys []K)
- func (w Wrapper[K, V]) GetVal(key K) V
- func (w Wrapper[K, V]) Keys(buf slice.Slice[K]) slice.Slice[K]
- func (w Wrapper[K, V]) MustPop(key K) V
- func (w Wrapper[K, V]) Pop(key K) (V, bool)
- func (w Wrapper[K, V]) Vals(buf slice.Slice[V]) slice.Slice[V]
- func (w Wrapper[K, V]) Wrapped() any
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SortKeys ¶
func SortKeys[K constraints.Ordered, V any](m map[K]V) slice.Slice[K]
SortKeys is a convenience function that returns the sorted keys. This is equivalent to calling m.Keys(nil).Sort(slice.LT[K]()). It assumes slice.LT for sorting and a nil buffer. If either of those assumtions are not true, use Keys and Sort explicitly.
Types ¶
type IterFunc ¶
type IterFunc[K comparable, V any] func(key K, val V, done *bool)
IterFunc is a function that can be called by Each on a Mapper. Note that "done" is a return argument. The choice was made in this case because being able to stop the iteration is useful in enough cases to justify it's inclusion, it is used infrequently. Not requiring a return argumenet cleaned up most of the instances of IterFuncs.
type KVGen ¶
type KVGen[T any, Key comparable, Val any] func(t T, idx int) (Key, Val, bool)
KVGen creates a Key-Value pair from an input. Generally this will be from an iterator
type Map ¶
type Map[K comparable, V any] map[K]V
Map fulfills Mapper using the builtin map type.
func (Map[K, V]) Delete ¶
func (m Map[K, V]) Delete(key K)
Delete fulfills Mapper, deleting the key from the underlying map.
func (Map[K, V]) Get ¶
Get fulfills Mapper, returning the value for a given key and a boolean indicating if the key was present.
type Mapper ¶
type Mapper[K comparable, V any] interface { Get(K) (V, bool) Set(K, V) Len() int Delete(K) Each(IterFunc[K, V]) Map() map[K]V }
Mapper represents the operations a Map can perform.
type Safe ¶
type Safe[K comparable, V any] struct { // contains filtered or unexported fields }
Safe provides a map with a Read-Write mutex that provides thread safe access.
func (*Safe[K, V]) Delete ¶
func (s *Safe[K, V]) Delete(key K)
Delete fulfills Mapper, deleting the key from the underlying map.
func (*Safe[K, V]) Get ¶
Get fulfills Mapper, returning the value for a given key and a boolean indicating if the key was present.
type Wrapper ¶
type Wrapper[K comparable, V any] struct { Mapper[K, V] }
Wrapper provides helpers around a Mapper.
func Empty ¶
func Empty[K comparable, V any](capacity int) Wrapper[K, V]
Empty creates a Wrapped instance of Map with the defined capacity.
func EmptySafe ¶
func EmptySafe[K comparable, V any](capacity int) Wrapper[K, V]
EmptySafe creates a new empty Wrapped instance of a Safe map with the defined capacity.
func New ¶
func New[K comparable, V any](m map[K]V) Wrapper[K, V]
New creates a Wrapped instance of Map.
func NewSafe ¶
func NewSafe[K comparable, V any](m map[K]V) Wrapper[K, V]
NewSafe creates a new Wrapped instance of a Safe map.
func Wrap ¶
func Wrap[K comparable, V any](m Mapper[K, V]) Wrapper[K, V]
Wrap a Mapper. If it is already a Wrapper, that will be returned.
func (Wrapper[K, V]) DeleteMany ¶
func (w Wrapper[K, V]) DeleteMany(keys []K)
DeleteMany deletes multiple keys.
func (Wrapper[K, V]) GetVal ¶
func (w Wrapper[K, V]) GetVal(key K) V
GetVal returns the value for a key dropping the "found" boolean.
func (Wrapper[K, V]) Keys ¶
Keys returns the keys of the map as a Slice. The provided buffer will be used if it has sufficient capacity.
func (Wrapper[K, V]) MustPop ¶
func (w Wrapper[K, V]) MustPop(key K) V
MustPop removes a key from the map and returns the value associated with it. It will panic nad the key is not found.
func (Wrapper[K, V]) Pop ¶
Pop removes a key from the map and returns the value associated with it along a with a bool indicating if the key was found.