lmap

package
v0.0.0-...-8199c21 Latest Latest
Warning

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

Go to latest
Published: Mar 18, 2026 License: GPL-3.0 Imports: 7 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 SortKeys

func SortKeys[K cmp.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 Each

type Each[K comparable, V any] struct {
	Func func(EachFunc[K, V])
	L    int
}

func (Each[K, V]) Each

func (e Each[K, V]) Each(fn EachFunc[K, V])

func (Each[K, V]) Len

func (e Each[K, V]) Len() int

type EachFunc

type EachFunc[K comparable, V any] = func(key K, val V, done *bool)

EachFunc 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 EachFunc.

func All

func All[K comparable, V any](fn func(K, V)) EachFunc[K, V]

type Eacher

type Eacher[K comparable, V any] interface {
	Each(EachFunc[K, V])
}

type IdxEacher

type IdxEacher[K comparable, V any] interface {
	Each(EachFunc[int, KeyVal[K, V]])
}

type KeyPair

type KeyPair[K, V any] = struct {
	Key K
	Val V
}

type KeyVal

type KeyVal[K comparable, V any] = KeyPair[K, V]

func NewKV

func NewKV[K comparable, V any](k K, v V) KeyVal[K, V]

type Lener

type Lener interface {
	Len() int
}

type Less

type Less[T any] = slice.Less[T]

func KeyLessKP

func KeyLessKP[V, K any](less Less[K]) Less[KeyPair[K, V]]

KeyLessKP is a helper that converts a Less function on the Key type to a Less funciton on the KeyPair type.

func ValLessKP

func ValLessKP[K, V any](less Less[V]) Less[KeyPair[K, V]]

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 EachFunc[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 MapReader

type MapReader[K comparable, V any] interface {
	Get(K) (V, bool)
	Len() int
	Eacher[K, V]
	Map() map[K]V
	// New creates a new Mapper with the same underlying structure.
	New() Mapper[K, V]
}

type Mapper

type Mapper[K comparable, V any] interface {
	MapReader[K, V]
	Set(K, V)
	Delete(K)
}

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 EachFunc[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 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 FromEach

func FromEach[K comparable, V any](fn func(EachFunc[int, KeyVal[K, V]])) Wrapper[K, V]

func FromEacher

func FromEacher[K comparable, V any](e IdxEacher[K, V]) Wrapper[K, V]

func FromIter

func FromIter[K comparable, V any](i liter.Iter[KeyVal[K, V]]) (out Wrapper[K, V])

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

func (w Wrapper[K, V]) All(fn func(k K, v V))

func (Wrapper[K, V]) AppendEach

func (w Wrapper[K, V]) AppendEach(fn func(EachFunc[int, KeyVal[K, V]])) Wrapper[K, V]

func (Wrapper[K, V]) AppendEacher

func (w Wrapper[K, V]) AppendEacher(e IdxEacher[K, V]) Wrapper[K, V]

func (Wrapper[K, V]) Contains

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

Contains returns true if the underlying Mapper contains the key.

func (Wrapper[K, V]) Copy

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

Copy the underlying map. The capacity can be set with cp.

func (Wrapper[K, V]) DeleteMany

func (w Wrapper[K, V]) DeleteMany(keys []K)

DeleteMany deletes multiple keys.

func (Wrapper[K, V]) Each

func (w Wrapper[K, V]) Each(fn EachFunc[K, V])

Each adds a nil check before calling Mapper.Each.

func (Wrapper[K, V]) EachKey

func (w Wrapper[K, V]) EachKey(i liter.Iter[K]) Each[K, V]

EachKey invokes the EachFunc for every

func (Wrapper[K, V]) GetVal

func (w Wrapper[K, V]) GetVal(key K) (v V)

GetVal returns the value for a key dropping the "found" boolean.

func (Wrapper[K, V]) KeyLessKP

func (w Wrapper[K, V]) KeyLessKP(less Less[K]) Less[KeyPair[K, V]]

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

func (w Wrapper[K, V]) Len() int

Len adds a nil check before calling Mapper.Len.

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 V, found 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]) Slice

func (w Wrapper[K, V]) Slice(less Less[KeyPair[K, V]], buf []KeyPair[K, V]) slice.Slice[KeyPair[K, V]]

Slice converts the underlying map to a slice of keyPairs. If less is provided, it will be sorted.

func (Wrapper[K, V]) SortKeys

func (w Wrapper[K, V]) SortKeys(less Less[K], buf []K) slice.Slice[K]

SortKeys creates a sorted slice of the keys.

func (Wrapper[K, V]) SortedEachKey

func (w Wrapper[K, V]) SortedEachKey(less Less[K], buf []K, fn EachFunc[K, V]) Each[K, V]

SortedEachKey is shorthand for invoking SortedKeys and then EachKey. It creates a sorted slice as intermediary product.

func (Wrapper[K, V]) ValLessKP

func (w Wrapper[K, V]) ValLessKP(less Less[V]) Less[KeyPair[K, V]]

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