lmap

package
v0.0.0-...-73d4566 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2025 License: GPL-3.0 Imports: 5 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func ForAll

func ForAll[In, Out any](fn func(In) Out) func(In) (Out, bool)

ForAll is a helper function. The output will generally be fed into either TransformVal or TransformKey.

func FromIter

func FromIter[I any, Key comparable, Val any](in liter.Iter[I], fn KVGen[I, Key, Val]) map[Key]Val

FromIter creates a map from an iterator and a generator function.

func SliceTransform

func SliceTransform[K comparable, V any, Out any](m Mapper[K, V], buf []Out, fn SliceTransformFunc[K, V, Out]) slice.Slice[Out]

SliceTransform takes a function for producing a single value from a given key and value produces a map.

func SliceTransformMap

func SliceTransformMap[K comparable, V any, Out any](m map[K]V, fn SliceTransformFunc[K, V, Out]) slice.Slice[Out]

SliceTransformMap takes a function for producing a single value from a given key and value produces a map.

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]) Each

func (m Map[K, V]) Each(fn IterFunc[K, V])

Each calls fn for every key-value pair.

func (Map[K, V]) Get

func (m Map[K, V]) Get(key K) (V, bool)

Get fulfills Mapper, returning the value for a given key and a boolean indicating if the key was present.

func (Map[K, V]) Len

func (m Map[K, V]) Len() int

Len fulfills Mapper, returning the length of the underlying map.

func (Map[K, V]) Map

func (m Map[K, V]) Map() map[K]V

Map fulfills Mapper and returns the underlying map.

func (Map[K, V]) New

func (m Map[K, V]) New() Mapper[K, V]

New fulfills Mapper, returning a new Map

func (Map[K, V]) Set

func (m Map[K, V]) Set(key K, val V)

Set fulfills Mapper, setting the key and value in the underlying map.

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
	// New creates a new Mapper with the same underlying structure.
	New() Mapper[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]) Each

func (s *Safe[K, V]) Each(fn IterFunc[K, V])

Each calls fn for every key-value pair.

func (*Safe[K, V]) Get

func (s *Safe[K, V]) Get(key K) (V, bool)

Get fulfills Mapper, returning the value for a given key and a boolean indicating if the key was present.

func (*Safe[K, V]) Len

func (s *Safe[K, V]) Len() int

Len fulfills Mapper, returning the length of the underlying map.

func (*Safe[K, V]) Map

func (s *Safe[K, V]) Map() map[K]V

Map fulfills Mapper and returns the underlying map.

func (*Safe[K, V]) New

func (m *Safe[K, V]) New() Mapper[K, V]

New fulfills Mapper, returning a new Safe map

func (*Safe[K, V]) Set

func (s *Safe[K, V]) Set(key K, val V)

Set fulfills Mapper, setting the key and value in the underlying map.

type SliceTransformFunc

type SliceTransformFunc[K comparable, V any, Out any] func(k K, v V) (out Out, include bool)

SliceTransformFunc is used to transform the values in a map to slice.

func NewSliceTransformFunc

func NewSliceTransformFunc[K comparable, V any, Out any](fn func(k K, v V) (out Out, include bool)) SliceTransformFunc[K, V, Out]

NewSliceTransformFunc is just a helper for converting a function to the the SliceTransformFunc type.

func (SliceTransformFunc[K, V, Out]) Transform

func (fn SliceTransformFunc[K, V, Out]) Transform(m Mapper[K, V], buf []Out) slice.Slice[Out]

Transform uses the SliceTransformFunc to transform the map to a slice.

func (SliceTransformFunc[K, V, Out]) TransformMap

func (fn SliceTransformFunc[K, V, Out]) TransformMap(m map[K]V) slice.Slice[Out]

TransformMap uses the SliceTransformFunc to transform the map to a slice.

type TransformFunc

type TransformFunc[KIn comparable, VIn any, KOut comparable, VOut any] func(k KIn, v VIn) (kOut KOut, vOut VOut, include bool)

TransformFunc converts the key and value types. Only key/values pairs for which include is true are used.

func NewTransformFunc

func NewTransformFunc[KIn comparable, VIn any, KOut comparable, VOut any](fn func(k KIn, v VIn) (KOut, VOut, bool)) TransformFunc[KIn, VIn, KOut, VOut]

NewTransformFunc is just a helper for converting a function to the the TransformFunc type.

func TransformKey

func TransformKey[V any, KIn, KOut comparable](fn func(k KIn) (KOut, bool)) TransformFunc[KIn, V, KOut, V]

TransformKey is a helper that applies the given function to the key.

func TransformVal

func TransformVal[Key comparable, VIn any, VOut any](fn func(v VIn) (VOut, bool)) TransformFunc[Key, VIn, Key, VOut]

TransformVal is a helper that applies the given function to the value.

func (TransformFunc[KIn, VIn, KOut, VOut]) Map

func (fn TransformFunc[KIn, VIn, KOut, VOut]) Map(m map[KIn]VIn) Wrapper[KOut, VOut]

Map takes in a plain map and creates an instance of lmap.Map.

func (TransformFunc[KIn, VIn, KOut, VOut]) Transform

func (fn TransformFunc[KIn, VIn, KOut, VOut]) Transform(m Mapper[KIn, VIn], buf Mapper[KOut, VOut]) Wrapper[KOut, VOut]

Transform applies the TransformFunc to m and sets the results on buf when include is true. Note that buf is not cleared, so this can perform as and append. If buf is nil a lmap.Map is created sized to m.

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 Transform

func Transform[KIn comparable, VIn any, KOut comparable, VOut any](m Mapper[KIn, VIn], buf Mapper[KOut, VOut], fn TransformFunc[KIn, VIn, KOut, VOut]) Wrapper[KOut, VOut]

Transform applies the TransformFunc to m and sets the results on buf when include is true. Note that buf is not cleared, so this can perform as and append. If buf is nil a lmap.Map is created sized to m.

func TransformMap

func TransformMap[KIn comparable, VIn any, KOut comparable, VOut any](m map[KIn]VIn, fn TransformFunc[KIn, VIn, KOut, VOut]) Wrapper[KOut, VOut]

TransformMap takes in a plain map and uses the TransformFunc to create an instance of lmap.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]) Copy

func (w Wrapper[K, V]) Copy() map[K]V

Copy the underlying map

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

func (w Wrapper[K, V]) Keys(buf slice.Slice[K]) slice.Slice[K]

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

func (w Wrapper[K, V]) Pop(key K) (V, bool)

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.

func (Wrapper[K, V]) Vals

func (w Wrapper[K, V]) Vals(buf slice.Slice[V]) slice.Slice[V]

Vals returns the values of the map as a Slice. The provided buffer will be used if it has sufficient capacity.

func (Wrapper[K, V]) WrapNew

func (w Wrapper[K, V]) WrapNew() Wrapper[K, V]

WrapNew returns a wrapper from the the underlying Mapper.New method.

func (Wrapper[K, V]) Wrapped

func (w Wrapper[K, V]) Wrapped() any

Wrapped fulfills upgrade.Wrapper.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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