maps

package module
v0.0.0-...-fec0ade Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2023 License: MIT Imports: 2 Imported by: 0

README

Go maps

Go Tests

It's modern package that based on generic maps. The package provides many helpers to operate on thread-safe (only) maps. You don't have to worry about the concurrency moment of this map. Under the hood, realization is based on sync.Map.

Installation

go get github.com/0x9ef/go-maps

Usage

import "github.com/0x9ef/go-maps"

DefaultMap

We can start from the native map realisation.

Set

We can set map key with a value.

m := maps.NewDefaultMap[string, int]()
m.Set("one", 1) // sets for key "one" value "1" 
SetIf

We can set map key with a value if predicate function is true.

m := maps.NewDefaultMap[string, int]()
m.Set("ten", 10)
m.SetIf("one", 1, func(m Map[int, int]) bool {
	return m.Get("ten") == 10 // sets only if "ten" key is equals to 10
})
Get

We can get a map value.

m := maps.NewDefaultMap[string, int]()
m.Set("one", 1)
val := m.Get("one")
if val == 1 { 
    fmt.Println("found")
}
GetOk

We can get a value and identifier if a record was found.

m := maps.NewDefaultMap[string, int]()
val, ok := m.GetOk("one")
if !ok {
    fmt.Println("key was not found")
} 
GetOrSet

We can get a value or if value was not found we have to store it.

m := maps.NewDefaultMap[string, int]()
val, loaded := m.GetOrSet("one", 1)
if !loaded {
    fmt.Println("key was not found, but we store it")
}
fmt.Println(val)
// 1
GetAndDelete

We can get a value and after this delete this value from the map.

m := maps.NewDefaultMap[string, int]()
m.Set("one", 1)
val, loaded := m.GetAndDelete("one")
if !loaded {
    fmt.Println("key was not found, we didn't delete it")
}
fmt.Println(val)
// 1 
Delete

We can delete a key from the map.

m := maps.NewDefaultMap[string, int]()
m.Set("one", 1)
m.Delete("one")
DeleteIf

We can delete a key from the map if predicate function is true.

m := maps.NewDefaultMap[string, int]()
m.Set("one", 1)
m.DeleteIf("one", func(m Map[int, int]) bool {
	return !m.Exists("ten") // deletes value only if key "ten" doesn't exists
})
Clear

We can clear full map, so this means that we can delete all keys from the map.

m := maps.NewDefaultMap[string, int]()
m.Set("one", 1)
m.Set("two", 2)
m.Set("three", 3)
m.Clear()
Keys

We can get all keys from the map.

m := maps.NewDefaultMap[string, int]()
m.Set("one", 1)
m.Set("two", 2)
m.Set("three", 3)
fmt.Println(m.Keys())
// ["one", "two", "three"]
Values

We can get all values from the map.

m := maps.NewDefaultMap[string, int]()
m.Set("one", 1)
m.Set("two", 2)
m.Set("three", 3)
fmt.Println(m.Values())
// [1, 2, 3]
Filter

We can filter map content and return only keys and values that was matched by our rules.

m := maps.NewDefaultMap[string, int]()
m.Set("one", 1)
m.Set("two", 2)
m.Set("three", 3)
m.Set("four", 3)
keys, values := m.Filter(func(key string,  value int) bool {
	return key == "three" && value == 3 // we match third element in the map
})
fmt.Println(keys, values)
// ["three"], [1]
Iterate

We can iterate over all keys in the map

m := maps.NewDefaultMap[string, int]()
m.Set("one", 1)
m.Set("two", 2)
m.Set("three", 3)
m.Iterate(func(key string, value int) bool{
    fmt.Println(key, value)
})
// "one" 1
// "two" 2
// "three" 3

License

MIT

Documentation

Overview

Copyright (c) 2022 0x9ef

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DefaultMap

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

DefaultMap is a thread-safe map that have additional helpers functions that give more flexibility to a user. Under the hood this map use sync.Map for rw-locks.

func NewDefaultMap

func NewDefaultMap[K comparable, V any]() DefaultMap[K, V]

NewDefaultMap returns initialized map without capacity.

func (DefaultMap[K, V]) Clear

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

Clear clears all keys from the map.

func (DefaultMap[K, V]) Delete

func (m DefaultMap[K, V]) Delete(key K)

Delete deletes a key from the map.

func (DefaultMap[K, V]) DeleteIf

func (m DefaultMap[K, V]) DeleteIf(key K, f func(m Map[K, V]) bool) bool

DeleteIf deletes a key from the map if the predicate function f is true.

func (DefaultMap[K, V]) Exists

func (m DefaultMap[K, V]) Exists(key K) bool

Exists returns true if a key exists in the map.

func (DefaultMap[K, V]) Filter

func (m DefaultMap[K, V]) Filter(f func(key K, value V) bool) ([]K, []V)

Filter returns filtered pairs of keys and values from the map, if predicate f is false filterting will be stopped.

func (DefaultMap[K, V]) Get

func (m DefaultMap[K, V]) Get(key K) V

Get returns value underlined by a key from the map.

func (DefaultMap[K, V]) GetAndDelete

func (m DefaultMap[K, V]) GetAndDelete(key K) (V, bool)

GetAndDelete deletes the value for a key, returning the previous value.

func (DefaultMap[K, V]) GetOk

func (m DefaultMap[K, V]) GetOk(key K) (V, bool)

GetOk returns value and bool flag if a key in the map was founded.

func (DefaultMap[K, V]) GetOrSet

func (m DefaultMap[K, V]) GetOrSet(key K, value V) (V, bool)

GetOrSet returns the existing value for the key if present. Otherwise, it stores and returns the given value.

func (DefaultMap[K, V]) Iterate

func (m DefaultMap[K, V]) Iterate(f func(key K, value V) bool)

Iterate iterates over each map key and value with iterator f function.

func (DefaultMap[K, V]) Keys

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

Keys returns all existed keys as slice in the map.

func (DefaultMap[K, V]) Len

func (m DefaultMap[K, V]) Len() int32

Len returns length of map.

func (DefaultMap[K, V]) Set

func (m DefaultMap[K, V]) Set(key K, value V)

Set sets value for a key.

func (DefaultMap[K, V]) SetIf

func (m DefaultMap[K, V]) SetIf(key K, value V, f func(m Map[K, V]) bool) bool

SetIf sets value if the predicate function f is true.

func (DefaultMap[K, V]) Values

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

Values returns all existed values as slice in the map.

type Map

type Map[K comparable, V any] interface {
	// Len returns length of map.
	Len() int32
	// Exists returns true if a key exists in the map.
	Exists(key K) bool
	// Set sets value for a key.
	Set(key K, value V)
	// SetIf sets value if the predicate function f is true.
	SetIf(key K, value V, f func(m Map[K, V]) bool) bool
	// Get returns value underlined by a key from the map.
	Get(key K) V
	// GetOk returns value and bool flag if a key in the map was found.
	GetOk(key K) (V, bool)
	// GetOrSet returns the existing value for the key if present.
	// Otherwise, it stores and returns the given value.
	GetOrSet(key K, value V) (V, bool)
	// GetAndDelete deletes the value for a key, returning the previous value.
	GetAndDelete(key K) (V, bool)
	// Delete deletes a key from the map.
	Delete(key K)
	// DeleteIf deletes a key from the map if the predicate function f is true.
	DeleteIf(key K, f func(m Map[K, V]) bool) bool
	// Clear clears an all keys from the map.
	Clear()
	// Keys returns all existed keys as slice in the map.
	Keys() []K
	// Values returns all existed values as slice in the map.
	Values() []V
	// Filter returns filtered pairs of keys and values from the map, if
	// predicate f is false filterting will be stopped.
	Filter(f func(key K, value V) bool) ([]K, []V)
	// Iterate iterates over each map key and value, if predicate f is false
	// iterations will be stopped.
	// Iterate may be O(N) with the number of elements in the map even if f
	// returns false after a constant number of calls.
	Iterate(f func(key K, value V) bool)
}

func Merge

func Merge[K comparable, V any](collection ...Map[K, V]) Map[K, V]

Merge merges all maps to the one combined map.

func MergeUnique

func MergeUnique[K comparable, V any](collection ...Map[K, V]) Map[K, V]

MergeUnique merges only unique elements to the one combined map.

type Merger

type Merger[K comparable, V any] interface {
	// Merge merges all maps to the one combined map.
	Merge(collection ...Map[K, V]) Map[K, V]
	// MergeUnique merges only unique elements to the map.
	MergeUnique(collection ...Map[K, V]) Map[K, V]
}

type UniqueMap

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

UniqueMap is a thread-safe map that operates of zero-value keys and have an additional helper functions that give more flexibility to a user.

func NewUniqueMap

func NewUniqueMap[K comparable]() UniqueMap[K]

NewUniqueMap returns initialized with make map.

func (UniqueMap[K]) Clear

func (m UniqueMap[K]) Clear()

Clear clears all keys from the map.

func (UniqueMap[K]) Delete

func (m UniqueMap[K]) Delete(key K)

Delete deletes a key from the map.

func (UniqueMap[K]) Exists

func (m UniqueMap[K]) Exists(key K) bool

Exists returns true if a key exists in the map.

func (UniqueMap[K]) Keys

func (m UniqueMap[K]) Keys() []K

Keys returns all existed keys as slice in the map.

func (UniqueMap[K]) Len

func (m UniqueMap[K]) Len() int32

Len returns length of map.

func (UniqueMap[K]) Set

func (m UniqueMap[K]) Set(key K)

Set sets value for a key.

func (UniqueMap[K]) SetIf

func (m UniqueMap[K]) SetIf(key K, f func(m UniqueMap[K]) bool) bool

SetIf sets value if the predicate function f is true.

Jump to

Keyboard shortcuts

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