Documentation
¶
Overview ¶
Maps package provide a helper functions to work with maps.
Index ¶
- func Clone[Key comparable, Value any](src map[Key]Value) map[Key]Value
- func Copy[Key comparable, Value any](dest map[Key]Value, src map[Key]Value) bool
- func Equal[K comparable, V comparable](left, right map[K]V) bool
- func EqualFunc[K comparable, T any](left, right map[K]T, cmp func(K) bool) bool
- func KeyExists[Key comparable, Value any](container map[Key]Value, key Key) bool
- func Keys[K comparable, V any](m map[K]V) []K
- func Search[Key comparable, Value comparable](container map[Key]Value, value Value) (Key, bool)
- func SearchFunc[Key comparable, Value any](container map[Key]Value, cmp func(Key) bool) (Key, bool)
- func Set[Key comparable, Value any](container map[Key]Value, key Key, value Value) (old Value, ok bool)
- func Values[K comparable, V any](m map[K]V) []V
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Clone ¶ added in v0.4.2
func Clone[Key comparable, Value any](src map[Key]Value) map[Key]Value
Clone create a new map with the same size as a source map and assign values from the source map. This functions does not do DeepCopy, it just assigns values.
Example ¶
package main
import (
"fmt"
"github.com/aohorodnyk/stl/maps"
)
func main() {
src := map[string]int{"one": 1, "eleven": 11, "twenty": 20}
dest := maps.Clone(src)
fmt.Println(dest)
}
Output: map[eleven:11 one:1 twenty:20]
func Copy ¶ added in v0.4.2
func Copy[Key comparable, Value any](dest map[Key]Value, src map[Key]Value) bool
Copy copies all key and values from source map to destination. If destination already contains they key, it will be overwritten. If destination map contains some unique key, it will not be removed. Returns false one when destination is nil, otherwise returns true. This functions does not do DeepCopy, it just assigns values.
Example ¶
package main
import (
"fmt"
"github.com/aohorodnyk/stl/maps"
)
func main() {
dest := map[string]int{"three": 3, "one": 1000}
src := map[string]int{"one": 1, "eleven": 11, "twenty": 20}
copied := maps.Copy(dest, src)
fmt.Println(copied)
fmt.Println(dest)
}
Output: true map[eleven:11 one:1 three:3 twenty:20]
func Equal ¶
func Equal[K comparable, V comparable](left, right map[K]V) bool
Equal compares two maps with comparable values. It returns true if the maps are equal, false otherwise. This function does not use EqualFunc, because of performance reason. This is linear comparing function with O(n) time complexity and O(1) space complexity.
Example ¶
package main
import (
"fmt"
"github.com/aohorodnyk/stl/maps"
)
func main() {
mapStrLeft := map[string]int{
"a": 1,
"b": 2,
"c": 3,
}
mapStrRight := map[string]int{
"b": 2,
"c": 3,
"a": 1,
}
fmt.Println(maps.Equal(mapStrLeft, mapStrRight))
mapStrLeft = map[string]int{
"a": 1,
"b": 2,
"c": 3,
}
mapStrRight = map[string]int{
"b": 2,
"s": 3,
"a": 1,
}
fmt.Println(maps.Equal(mapStrLeft, mapStrRight))
}
Output: true false
func EqualFunc ¶
func EqualFunc[K comparable, T any](left, right map[K]T, cmp func(K) bool) bool
EqualFunc compares two maps with any values by a custom comparable function. It returns true if the maps are equal, false otherwise. This is linear comparing function with O(n) time complexity and O(1) space complexity.
Example ¶
package main
import (
"fmt"
"github.com/aohorodnyk/stl/maps"
)
func main() {
type customType struct {
a int
}
mapStrLeft := map[string]customType{
"b": {a: 2},
"a": {a: 1},
"c": {a: 3},
}
mapStrRight := map[string]customType{
"c": {a: 3},
"b": {a: 2},
"a": {a: 1},
}
cmp := func(i string) bool {
return mapStrLeft[i].a == mapStrRight[i].a
}
fmt.Println(maps.EqualFunc(mapStrLeft, mapStrRight, cmp))
}
Output: true
func KeyExists ¶ added in v0.4.2
func KeyExists[Key comparable, Value any](container map[Key]Value, key Key) bool
KeyExists checks is key exists in a map. This function does not compare values. To delete key from a map, `delete` function must be used.
Example ¶
package main
import (
"fmt"
"github.com/aohorodnyk/stl/maps"
)
func main() {
fmt.Println(maps.KeyExists(map[string]int{"a": 1}, "b"))
fmt.Println(maps.KeyExists(map[string]int{"b": 2}, "b"))
// First parameter can be nil.
fmt.Println(maps.KeyExists[string, int](nil, "b"))
}
Output: false true false
func Keys ¶
func Keys[K comparable, V any](m map[K]V) []K
Keys extracts keys from a map and returns it as a slice. The key order will be the same as we received by iteration by the map.
Example ¶
package main
import (
"fmt"
"github.com/aohorodnyk/stl/maps"
"github.com/aohorodnyk/stl/slices"
)
func main() {
mapStr := map[string]int{
"b": 2,
"a": 1,
"c": 3,
}
keysStr := maps.Keys(mapStr)
slices.Sort(keysStr)
fmt.Println(keysStr)
mapEmpty := map[string]int{}
fmt.Println(maps.Keys(mapEmpty))
mapEmpty = nil
fmt.Println(maps.Keys(mapEmpty))
}
Output: [a b c] [] []
func Search ¶ added in v0.4.2
func Search[Key comparable, Value comparable](container map[Key]Value, value Value) (Key, bool)
Search searches value in a provided map. Performance complexity is O(n), memory complaxity is O(1). It supports ONLY comparable value, otherwise we cannot compare values without reflect.
Example ¶
package main
import (
"fmt"
"github.com/aohorodnyk/stl/maps"
)
func main() {
container := map[int]string{1: "one", 10: "ten", 20: "twenty"}
fmt.Println(maps.Search(container, "ten"))
}
Output: 10 true
func SearchFunc ¶ added in v0.4.2
func SearchFunc[Key comparable, Value any](container map[Key]Value, cmp func(Key) bool) (Key, bool)
Search searches value in a provided map. Performance complexity is O(n), memory complaxity is O(1).
Example ¶
package main
import (
"fmt"
"github.com/aohorodnyk/stl/maps"
)
func main() {
container := map[string]int{"one": 1, "two": 2, "three": 3}
searchVal := 2
cmp := func(key string) bool {
return container[key] == searchVal
}
fmt.Println(maps.SearchFunc(container, cmp))
}
Output: two true
func Set ¶ added in v0.4.3
func Set[Key comparable, Value any](container map[Key]Value, key Key, value Value) (old Value, ok bool)
Set sets value to a provided key in a provided container. It returns old value and did container contain old value before set. If there is not needs to know about previous value, just do `container[key] = value`, no needs to use this function.
Example ¶
package main
import (
"fmt"
"github.com/aohorodnyk/stl/maps"
)
func main() {
fmt.Println("map[string]int")
containerStrInt := map[string]int{}
fmt.Println(maps.Set(containerStrInt, "test", 3))
fmt.Println(maps.Set(containerStrInt, "test", 12))
fmt.Println("map[string]string")
containerStrStr := map[string]string{}
fmt.Println(maps.Set(containerStrStr, "test", "string1"))
fmt.Println(maps.Set(containerStrStr, "test", "string2"))
}
Output: map[string]int 0 false 3 true map[string]string false string1 true
func Values ¶
func Values[K comparable, V any](m map[K]V) []V
Values extracts values from a map and returns it as a slice. The values order will be the same as we received by iteration by the map.
Example ¶
package main
import (
"fmt"
"github.com/aohorodnyk/stl/maps"
"github.com/aohorodnyk/stl/slices"
)
func main() {
mapStr := map[string]int{
"c": 3,
"a": 1,
"b": 2,
}
valuesStr := maps.Values(mapStr)
slices.Sort(valuesStr)
fmt.Println(valuesStr)
mapEmpty := map[string]int{}
fmt.Println(maps.Values(mapEmpty))
mapEmpty = nil
fmt.Println(maps.Values(mapEmpty))
}
Output: [1 2 3] [] []
Types ¶
This section is empty.