maps

package
v0.0.62 Latest Latest
Warning

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

Go to latest
Published: Jun 5, 2024 License: Apache-2.0, MIT Imports: 8 Imported by: 0

README

Go Reference Build Status Go Report Card codecov

maps

maps is a library using Go generics that offers a standard interface for manipulating different kinds of maps.

Using the same interface, you can create and use a standard Go map, a map that is safe for concurrency and/or a map that lets you order the keys in the map.

Example

package main

import . "github.com/goradd/maps"
import "fmt"

type myMap = Map[string,int] // the equal sign here is critical!
type myStdMap = StdMap[string, int]

func main() {
	m := new(Map[string, int])
	
	m.Merge(myStdMap{"b":2, "c":3})
	m.Set("a",1)

	sum := 0
	m.Range(func(k string, v int) bool {
		sum += v
		return true
    })
	fmt.Print(sum)
}

By simply changing myMap to a SafeMap, you can make the map safe for concurrent use. Or, you can change myMap to a SliceMap, or a SafeSliceMap to also be able to iterate the map in the order it was created, similar to a PHP map.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Equaler

type Equaler interface {
	Equal(a any) bool
}

type Getter

type Getter[K comparable, V any] interface {
	Get(k K) (v V)
}

type Loader

type Loader[K comparable, V any] interface {
	Load(k K) (v V)
}

type Map

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

func (*Map[K, V]) Clear

func (m *Map[K, V]) Clear()

func (Map[K, V]) Delete

func (m Map[K, V]) Delete(k K)

func (Map[K, V]) Equal

func (m Map[K, V]) Equal(m2 MapI[K, V]) bool

func (Map[K, V]) Get

func (m Map[K, V]) Get(k K) V

func (Map[K, V]) Has

func (m Map[K, V]) Has(k K) bool

func (Map[K, V]) Keys

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

func (Map[K, V]) Len

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

func (Map[K, V]) Load

func (m Map[K, V]) Load(k K) V

func (Map[K, V]) MarshalBinary

func (m Map[K, V]) MarshalBinary() ([]byte, error)

func (Map[K, V]) MarshalJSON

func (m Map[K, V]) MarshalJSON() (out []byte, err error)

func (*Map[K, V]) Merge

func (m *Map[K, V]) Merge(in MapI[K, V])

func (Map[K, V]) Range

func (m Map[K, V]) Range(f func(k K, v V) bool)

func (*Map[K, V]) Set

func (m *Map[K, V]) Set(k K, v V)

func (Map[K, V]) String

func (m Map[K, V]) String() string
Example
m := new(Map[string, int])
m.Set("a", 1)
m.Set("b", 2)
fmt.Print(m)
Output:

func (*Map[K, V]) UnmarshalBinary

func (m *Map[K, V]) UnmarshalBinary(data []byte) (err error)

func (*Map[K, V]) UnmarshalJSON

func (m *Map[K, V]) UnmarshalJSON(in []byte) (err error)

func (Map[K, V]) Values

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

type MapI

type MapI[K comparable, V any] interface {
	Setter[K, V]
	Getter[K, V]
	Loader[K, V]
	Clear()
	Len() int
	Range(func(k K, v V) bool)
	Has(k K) bool
	Keys() []K
	Values() []V
	Merge(MapI[K, V])
	Equal(MapI[K, V]) bool
	Delete(k K)
}

type SafeMap

type SafeMap[K comparable, V any] struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func (*SafeMap[K, V]) Clear

func (m *SafeMap[K, V]) Clear()

func (*SafeMap[K, V]) Delete

func (m *SafeMap[K, V]) Delete(k K)

func (*SafeMap[K, V]) Equal

func (m *SafeMap[K, V]) Equal(m2 MapI[K, V]) bool

func (*SafeMap[K, V]) Get

func (m *SafeMap[K, V]) Get(k K) (v V)

func (*SafeMap[K, V]) Has

func (m *SafeMap[K, V]) Has(k K) (exists bool)

func (*SafeMap[K, V]) Keys

func (m *SafeMap[K, V]) Keys() (keys []K)

func (*SafeMap[K, V]) Len

func (m *SafeMap[K, V]) Len() (l int)

func (*SafeMap[K, V]) Load

func (m *SafeMap[K, V]) Load(k K) (v V)

func (*SafeMap[K, V]) MarshalBinary

func (m *SafeMap[K, V]) MarshalBinary() ([]byte, error)

func (*SafeMap[K, V]) MarshalJSON

func (m *SafeMap[K, V]) MarshalJSON() (out []byte, err error)

func (*SafeMap[K, V]) Merge

func (m *SafeMap[K, V]) Merge(in MapI[K, V])

func (*SafeMap[K, V]) Range

func (m *SafeMap[K, V]) Range(f func(k K, v V) bool)

func (*SafeMap[K, V]) Set

func (m *SafeMap[K, V]) Set(k K, v V)

func (*SafeMap[K, V]) String

func (m *SafeMap[K, V]) String() string
Example
m := new(SafeMap[string, int])
m.Set("a", 1)
m.Set("b", 2)
fmt.Print(m)
Output:

func (*SafeMap[K, V]) UnmarshalBinary

func (m *SafeMap[K, V]) UnmarshalBinary(data []byte) (err error)

func (*SafeMap[K, V]) UnmarshalJSON

func (m *SafeMap[K, V]) UnmarshalJSON(in []byte) (err error)

func (*SafeMap[K, V]) Values

func (m *SafeMap[K, V]) Values() (v []V)

type SafeSliceMap

type SafeSliceMap[K comparable, V any] struct {
	sync.RWMutex
	// contains filtered or unexported fields
}

func (*SafeSliceMap[K, V]) Clear

func (m *SafeSliceMap[K, V]) Clear()

func (*SafeSliceMap[K, V]) Delete

func (m *SafeSliceMap[K, V]) Delete(key K)

func (*SafeSliceMap[K, V]) Equal

func (m *SafeSliceMap[K, V]) Equal(m2 MapI[K, V]) bool

func (*SafeSliceMap[K, V]) Get

func (m *SafeSliceMap[K, V]) Get(key K) (val V)

func (*SafeSliceMap[K, V]) GetAt

func (m *SafeSliceMap[K, V]) GetAt(position int) (val V)
Example
m := new(SafeSliceMap[string, int])
m.Set("b", 2)
m.Set("c", 3)
m.Set("a", 1)
v := m.GetAt(1)
fmt.Print(v)
Output:

func (*SafeSliceMap[K, V]) GetKeyAt

func (m *SafeSliceMap[K, V]) GetKeyAt(position int) (key K)
Example
m := new(SafeSliceMap[string, int])
m.Set("b", 2)
m.Set("c", 3)
m.Set("a", 1)
v := m.GetKeyAt(1)
fmt.Print(v)
Output:

func (*SafeSliceMap[K, V]) Has

func (m *SafeSliceMap[K, V]) Has(key K) (ok bool)

func (*SafeSliceMap[K, V]) Keys

func (m *SafeSliceMap[K, V]) Keys() (keys []K)

func (*SafeSliceMap[K, V]) Len

func (m *SafeSliceMap[K, V]) Len() int

func (*SafeSliceMap[K, V]) Load

func (m *SafeSliceMap[K, V]) Load(key K) (val V)

func (*SafeSliceMap[K, V]) MarshalBinary

func (m *SafeSliceMap[K, V]) MarshalBinary() (data []byte, err error)

func (*SafeSliceMap[K, V]) MarshalJSON

func (m *SafeSliceMap[K, V]) MarshalJSON() (data []byte, err error)

func (*SafeSliceMap[K, V]) Merge

func (m *SafeSliceMap[K, V]) Merge(in MapI[K, V])

func (*SafeSliceMap[K, V]) Range

func (m *SafeSliceMap[K, V]) Range(f func(key K, value V) bool)

func (*SafeSliceMap[K, V]) Set

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

func (*SafeSliceMap[K, V]) SetAt

func (m *SafeSliceMap[K, V]) SetAt(index int, key K, val V)
Example
m := new(SafeSliceMap[string, int])
m.Set("b", 2)
m.Set("a", 1)
m.SetAt(1, "c", 3)
fmt.Println(m)
Output:

func (*SafeSliceMap[K, V]) SetSortFunc

func (m *SafeSliceMap[K, V]) SetSortFunc(f func(key1, key2 K, val1, val2 V) bool)
Example
m := new(SafeSliceMap[string, int])

m.Set("b", 2)
m.Set("a", 1)

fmt.Println(m)

m.SetSortFunc(func(k1, k2 string, v1, v2 int) bool {
	return k1 < k2
})
fmt.Println(m)
Output:

func (*SafeSliceMap[K, V]) String

func (m *SafeSliceMap[K, V]) String() string

func (*SafeSliceMap[K, V]) UnmarshalBinary

func (m *SafeSliceMap[K, V]) UnmarshalBinary(data []byte) (err error)

func (*SafeSliceMap[K, V]) UnmarshalJSON

func (m *SafeSliceMap[K, V]) UnmarshalJSON(data []byte) (err error)

func (*SafeSliceMap[K, V]) Values

func (m *SafeSliceMap[K, V]) Values() (vals []V)

type Set

type Set[K comparable] struct {
	// contains filtered or unexported fields
}

func (*Set[K]) Add

func (m *Set[K]) Add(k ...K) SetI[K]

func (*Set[K]) Clear

func (m *Set[K]) Clear()

func (*Set[K]) Delete

func (m *Set[K]) Delete(k K)

func (*Set[K]) Equal

func (m *Set[K]) Equal(m2 SetI[K]) bool

func (*Set[K]) Has

func (m *Set[K]) Has(k K) bool

func (*Set[K]) Len

func (m *Set[K]) Len() int

func (*Set[K]) MarshalBinary

func (m *Set[K]) MarshalBinary() ([]byte, error)

func (*Set[K]) MarshalJSON

func (m *Set[K]) MarshalJSON() (out []byte, err error)

func (*Set[K]) Merge

func (m *Set[K]) Merge(in SetI[K])

func (*Set[K]) Range

func (m *Set[K]) Range(f func(k K) bool)

func (*Set[K]) String

func (m *Set[K]) String() string
Example
m := new(Set[string])
m.Add("a")
m.Add("b")
m.Add("a")
v := m.Values()
sort.Strings(v)
fmt.Print(v)
Output:

func (*Set[K]) UnmarshalBinary

func (m *Set[K]) UnmarshalBinary(data []byte) (err error)

func (*Set[K]) UnmarshalJSON

func (m *Set[K]) UnmarshalJSON(in []byte) (err error)

func (*Set[K]) Values

func (m *Set[K]) Values() []K

type SetI

type SetI[K comparable] interface {
	Add(k ...K) SetI[K]
	Clear()
	Len() int
	Range(func(k K) bool)
	Has(k K) bool
	Values() []K
	Merge(SetI[K])
	Equal(SetI[K]) bool
	Delete(k K)
}

type Setter

type Setter[K comparable, V any] interface {
	Set(K, V)
}

type SliceMap

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

func (*SliceMap[K, V]) Clear

func (m *SliceMap[K, V]) Clear()

func (*SliceMap[K, V]) Delete

func (m *SliceMap[K, V]) Delete(key K)

func (*SliceMap[K, V]) Equal

func (m *SliceMap[K, V]) Equal(m2 MapI[K, V]) bool

func (*SliceMap[K, V]) Get

func (m *SliceMap[K, V]) Get(key K) (val V)

func (*SliceMap[K, V]) GetAt

func (m *SliceMap[K, V]) GetAt(position int) (val V)
Example
m := new(SliceMap[string, int])
m.Set("b", 2)
m.Set("c", 3)
m.Set("a", 1)
v := m.GetAt(1)
fmt.Print(v)
Output:

func (*SliceMap[K, V]) GetKeyAt

func (m *SliceMap[K, V]) GetKeyAt(position int) (key K)
Example
m := new(SliceMap[string, int])
m.Set("b", 2)
m.Set("c", 3)
m.Set("a", 1)
v := m.GetKeyAt(1)
fmt.Print(v)
Output:

func (*SliceMap[K, V]) Has

func (m *SliceMap[K, V]) Has(key K) (ok bool)

func (*SliceMap[K, V]) Keys

func (m *SliceMap[K, V]) Keys() (keys []K)

func (*SliceMap[K, V]) Len

func (m *SliceMap[K, V]) Len() int

func (*SliceMap[K, V]) Load

func (m *SliceMap[K, V]) Load(key K) (val V)

func (*SliceMap[K, V]) MarshalBinary

func (m *SliceMap[K, V]) MarshalBinary() (data []byte, err error)

func (*SliceMap[K, V]) MarshalJSON

func (m *SliceMap[K, V]) MarshalJSON() (data []byte, err error)

func (*SliceMap[K, V]) Merge

func (m *SliceMap[K, V]) Merge(in MapI[K, V])

func (*SliceMap[K, V]) Range

func (m *SliceMap[K, V]) Range(f func(key K, value V) bool)

func (*SliceMap[K, V]) Set

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

func (*SliceMap[K, V]) SetAt

func (m *SliceMap[K, V]) SetAt(index int, key K, val V)
Example
m := new(SliceMap[string, int])
m.Set("b", 2)
m.Set("a", 1)
m.SetAt(1, "c", 3)
fmt.Println(m)
Output:

func (*SliceMap[K, V]) SetSortFunc

func (m *SliceMap[K, V]) SetSortFunc(f func(key1, key2 K, val1, val2 V) bool)
Example
m := new(SliceMap[string, int])

m.Set("b", 2)
m.Set("a", 1)

fmt.Println(m)

m.SetSortFunc(func(k1, k2 string, v1, v2 int) bool {
	return k1 < k2
})
fmt.Println(m)
Output:

func (*SliceMap[K, V]) String

func (m *SliceMap[K, V]) String() string

func (*SliceMap[K, V]) UnmarshalBinary

func (m *SliceMap[K, V]) UnmarshalBinary(data []byte) (err error)

func (*SliceMap[K, V]) UnmarshalJSON

func (m *SliceMap[K, V]) UnmarshalJSON(data []byte) (err error)

func (*SliceMap[K, V]) Values

func (m *SliceMap[K, V]) Values() (vals []V)

type StdMap

type StdMap[K comparable, V any] map[K]V

func Cast

func Cast[M ~map[K]V, K comparable, V any](m M) StdMap[K, V]
Example
m := map[string]int{"a": 1}
b := Cast(m)
fmt.Print(b.Len())
Output:

func NewStdMap

func NewStdMap[K comparable, V any](sources ...map[K]V) StdMap[K, V]

func (StdMap[K, V]) Clear

func (m StdMap[K, V]) Clear()

func (StdMap[K, V]) Delete

func (m StdMap[K, V]) Delete(k K)

func (StdMap[K, V]) Equal

func (m StdMap[K, V]) Equal(m2 MapI[K, V]) bool

func (StdMap[K, V]) Get

func (m StdMap[K, V]) Get(k K) (v V)

func (StdMap[K, V]) Has

func (m StdMap[K, V]) Has(k K) (exists bool)

func (StdMap[K, V]) Keys

func (m StdMap[K, V]) Keys() (keys []K)

func (StdMap[K, V]) Len

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

func (StdMap[K, V]) Load

func (m StdMap[K, V]) Load(k K) (v V)

func (StdMap[K, V]) MarshalBinary

func (m StdMap[K, V]) MarshalBinary() ([]byte, error)

func (StdMap[K, V]) MarshalJSON

func (m StdMap[K, V]) MarshalJSON() (out []byte, err error)

func (StdMap[K, V]) Merge

func (m StdMap[K, V]) Merge(in MapI[K, V])

func (StdMap[K, V]) Range

func (m StdMap[K, V]) Range(f func(k K, v V) bool)

func (StdMap[K, V]) Set

func (m StdMap[K, V]) Set(k K, v V)

func (StdMap[K, V]) String

func (m StdMap[K, V]) String() string

func (*StdMap[K, V]) UnmarshalBinary

func (m *StdMap[K, V]) UnmarshalBinary(data []byte) (err error)

func (*StdMap[K, V]) UnmarshalJSON

func (m *StdMap[K, V]) UnmarshalJSON(in []byte) (err error)

func (StdMap[K, V]) Values

func (m StdMap[K, V]) Values() (values []V)

Jump to

Keyboard shortcuts

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