Documentation
¶
Overview ¶
Package xmap 字典(Sync)相关常用数据结构和算法
Index ¶
- func Create(pairs ...any) map[any]any
- func CreateErr(pairs ...any) (map[any]any, error)
- func CreateStrErr(pairs ...any) (map[string]any, error)
- func Filter[Map ~map[K]V, K comparable, V any](m Map, filter func(k K, v V, ok int) bool) Map
- func FilterByKeys[Map ~map[K]V, K comparable, V any](m Map, keys ...K) Map
- func FilterByValues[Map ~map[K]V, K comparable, V comparable](m Map, values ...V) Map
- func FilterKeys[K comparable, V any](m map[K]V, filter func(k K, v V, ok int) bool) []K
- func FilterValues[K comparable, V any](m map[K]V, filter func(k K, v V, ok int) bool) []V
- func Get[K comparable, V any](m map[K]V, key K) (v V, found bool)
- func GetDf[K comparable, V any](m map[K]V, key K, def V) V
- func HasAnyKey[K comparable, V any](m map[K]V, keys ...K) bool
- func HasAnyKeyValue[K comparable, V comparable](m map[K]V, search map[K]V) bool
- func HasKey[K comparable, V any](m map[K]V, key K) bool
- func HasKeyValue[K comparable, V comparable](m map[K]V, key K, value V) bool
- func Join[K string, V any](m map[K]V, sep string) string
- func Keys[K comparable, V any](m map[K]V) []K
- func Values[K comparable, V any](m map[K]V) []V
- type Ordered
- func (s *Ordered[K, V]) Clear()
- func (s *Ordered[K, V]) Clone() *Ordered[K, V]
- func (s *Ordered[K, V]) Delete(keys ...K)
- func (s *Ordered[K, V]) Get(key K) (V, bool)
- func (s *Ordered[K, V]) GetDf(key K, def V) V
- func (s *Ordered[K, V]) Has(key K) bool
- func (s *Ordered[K, V]) HasAny(keys ...K) bool
- func (s *Ordered[K, V]) Iter() iter.Seq2[K, V]
- func (s *Ordered[K, V]) KVs(clone bool) ([]K, map[K]V)
- func (s *Ordered[K, V]) Keys() []K
- func (s *Ordered[K, V]) Len() int
- func (s *Ordered[K, V]) Map(clone bool) map[K]V
- func (s *Ordered[K, V]) MustGet(key K) V
- func (s *Ordered[K, V]) Range(fn func(key K, value V) bool)
- func (s *Ordered[K, V]) Set(key K, value V)
- func (s *Ordered[K, V]) Values() []V
- type OrderedSync
- func (s *OrderedSync[K, V]) Clear()
- func (s *OrderedSync[K, V]) Clone() *OrderedSync[K, V]
- func (s *OrderedSync[K, V]) Delete(keys ...K)
- func (s *OrderedSync[K, V]) Get(key K) (V, bool)
- func (s *OrderedSync[K, V]) GetDf(key K, def V) V
- func (s *OrderedSync[K, V]) Has(key K) bool
- func (s *OrderedSync[K, V]) HasAny(keys ...K) bool
- func (s *OrderedSync[K, V]) Iter() iter.Seq2[K, V]
- func (s *OrderedSync[K, V]) KVs(clone bool) ([]K, map[K]V)
- func (s *OrderedSync[K, V]) Keys() []K
- func (s *OrderedSync[K, V]) Len() int
- func (s *OrderedSync[K, V]) Map(clone bool) map[K]V
- func (s *OrderedSync[K, V]) MustGet(key K) V
- func (s *OrderedSync[K, V]) Range(fn func(key K, value V) bool)
- func (s *OrderedSync[K, V]) Set(key K, value V)
- func (s *OrderedSync[K, V]) Values() []V
- type SliceValue
- func (s *SliceValue[K, V]) Add(key K, values ...V)
- func (s *SliceValue[K, V]) Delete(keys ...K)
- func (s *SliceValue[K, V]) DeleteValue(key K, values ...V)
- func (s *SliceValue[K, V]) Get(key K) []V
- func (s *SliceValue[K, V]) GetFirst(key K) (v V)
- func (s *SliceValue[K, V]) Has(key K) bool
- func (s *SliceValue[K, V]) HasValue(key K, values ...V) bool
- func (s *SliceValue[K, V]) Iter() iter.Seq2[K, []V]
- func (s *SliceValue[K, V]) Keys() []K
- func (s *SliceValue[K, V]) Map(clone bool) map[K][]V
- func (s *SliceValue[K, V]) Set(key K, values ...V)
- type SliceValueSync
- func (s *SliceValueSync[K, V]) Add(key K, values ...V)
- func (s *SliceValueSync[K, V]) Delete(keys ...K)
- func (s *SliceValueSync[K, V]) DeleteValue(key K, values ...V)
- func (s *SliceValueSync[K, V]) Get(key K) []V
- func (s *SliceValueSync[K, V]) GetFirst(key K) (v V)
- func (s *SliceValueSync[K, V]) Has(key K) bool
- func (s *SliceValueSync[K, V]) HasValue(key K, values ...V) bool
- func (s *SliceValueSync[K, V]) Iter() iter.Seq2[K, []V]
- func (s *SliceValueSync[K, V]) Keys() []K
- func (s *SliceValueSync[K, V]) Map(clone bool) (result map[K][]V)
- func (s *SliceValueSync[K, V]) Set(key K, values ...V)
- type Sync
- func (m *Sync[K, V]) Clear()
- func (m *Sync[K, V]) CompareAndDelete(key K, old V) (deleted bool)
- func (m *Sync[K, V]) CompareAndSwap(key K, old V, new V) bool
- func (m *Sync[K, V]) Count() int
- func (m *Sync[K, V]) Delete(key K)
- func (m *Sync[K, V]) DeleteFunc(fn func(key K, value V) bool)
- func (m *Sync[K, V]) Iter() iter.Seq2[K, V]
- func (m *Sync[K, V]) Load(key K) (value V, ok bool)
- func (m *Sync[K, V]) LoadAndDelete(key K) (value V, loaded bool)
- func (m *Sync[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)
- func (m *Sync[K, V]) Range(fn func(key K, value V) bool)
- func (m *Sync[K, V]) Store(key K, value V)
- func (m *Sync[K, V]) Swap(key K, value V) (previous V, loaded bool)
- func (m *Sync[K, V]) ToMap() map[K]V
- type Tags
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Filter ¶
func Filter[Map ~map[K]V, K comparable, V any](m Map, filter func(k K, v V, ok int) bool) Map
Filter 从 map 中 过滤出满足条件的项
filter: 过滤函数,参数依次为 k、v 分别是 map 的 key 和 value、ok-已过滤满足条件的个数
Example ¶
package main import ( "fmt" "github.com/xanygo/anygo/xmap" ) func main() { m := map[int]int{ 0: 0, 1: 1, 2: 2, 3: 3, } result := xmap.Filter(m, func(k int, v int, ok int) bool { return v%2 == 0 }) fmt.Println(result) }
Output: map[0:0 2:2]
func FilterByKeys ¶
func FilterByKeys[Map ~map[K]V, K comparable, V any](m Map, keys ...K) Map
FilterByKeys 从 map 中筛选出指定的 key 的项
func FilterByValues ¶
func FilterByValues[Map ~map[K]V, K comparable, V comparable](m Map, values ...V) Map
FilterByValues 从 map 中筛选出指定的 key 的项
func FilterKeys ¶
func FilterKeys[K comparable, V any](m map[K]V, filter func(k K, v V, ok int) bool) []K
FilterKeys 从 map 中 过滤出满足条件的 key 列表
filter: 过滤函数,参数依次为 k、v 分别是 map 的 key 和 value、ok-已过滤满足条件的个数
Example ¶
package main import ( "fmt" "sort" "github.com/xanygo/anygo/xmap" ) func main() { m := map[int]int{ 0: 10, 1: 11, 2: 22, 3: 33, } result := xmap.FilterKeys(m, func(k int, v int, ok int) bool { return v%2 == 0 }) sort.Ints(result) fmt.Println(result) }
Output: [0 2]
func FilterValues ¶
func FilterValues[K comparable, V any](m map[K]V, filter func(k K, v V, ok int) bool) []V
FilterValues 从 map 中 过滤出满足条件的 value 列表
filter: 过滤函数,参数依次为 k、v 分别是 map 的 key 和 value、ok-已过滤满足条件的个数
Example ¶
package main import ( "fmt" "sort" "github.com/xanygo/anygo/xmap" ) func main() { m := map[int]int{ 0: 10, 1: 11, 2: 22, 3: 33, } result := xmap.FilterValues(m, func(k int, v int, ok int) bool { return v%2 == 0 }) sort.Ints(result) fmt.Println(result) }
Output: [10 22]
func Get ¶
func Get[K comparable, V any](m map[K]V, key K) (v V, found bool)
Get 从 map 中读取指定 key 的值。支持 map 为 nil。
Example ¶
package main import ( "fmt" "github.com/xanygo/anygo/xmap" ) func main() { var m1 map[string]int // Get from nil map got1, ok1 := xmap.Get(m1, "k1") fmt.Println("k1=", got1, ok1) // k1= 0 false m1 = map[string]int{"k1": 1} got2, ok2 := xmap.Get(m1, "k1") fmt.Println("k1=", got2, ok2) // k1= 1 true }
Output: k1= 0 false k1= 1 true
func GetDf ¶
func GetDf[K comparable, V any](m map[K]V, key K, def V) V
GetDf 从 map 中读取指定 key 的值,若 key 不存在则返回默认值。支持 map 为 nil。
Example ¶
package main import ( "fmt" "github.com/xanygo/anygo/xmap" ) func main() { var m1 map[string]int // Get from nil map fmt.Println("k1=", xmap.GetDf(m1, "k1", 0)) // k1= 0 fmt.Println("k1=", xmap.GetDf(m1, "k1", 1)) // k1= 1 m1 = map[string]int{"k1": 1} fmt.Println("k1=", xmap.GetDf(m1, "k1", 0)) // k1= 1 fmt.Println("k2=", xmap.GetDf(m1, "k2", 0)) // k2= 0 fmt.Println("k2=", xmap.GetDf(m1, "k2", 1)) // k2= 1 }
Output: k1= 0 k1= 1 k1= 1 k2= 0 k2= 1
func HasAnyKey ¶
func HasAnyKey[K comparable, V any](m map[K]V, keys ...K) bool
HasAnyKey 判断 map 中是否存在任意 key。支持 map 为 nil。 若 map 或者 keys 为空,均会返回 false
func HasAnyKeyValue ¶
func HasAnyKeyValue[K comparable, V comparable](m map[K]V, search map[K]V) bool
HasAnyKeyValue 查找 map 中是否有 search 中的任意一项 key-value 。 若 m 或 search 为空,均会返回 false
func HasKey ¶
func HasKey[K comparable, V any](m map[K]V, key K) bool
HasKey 判断 map 中是否存在特定 key。支持 map 为 nil。
func HasKeyValue ¶
func HasKeyValue[K comparable, V comparable](m map[K]V, key K, value V) bool
HasKeyValue 判断 map 中是否有指定的 key 和 value。支持 map 为 nil。
Types ¶
type Ordered ¶
type Ordered[K comparable, V any] struct { // Caption 初始化 map 时,默认的容量,可选,默认值为 4 Caption int // contains filtered or unexported fields }
Ordered 按照写入顺序排序的 Sync, 非并发安全的
func (*Ordered[K, V]) Keys ¶
func (s *Ordered[K, V]) Keys() []K
Example ¶
package main import ( "fmt" "github.com/xanygo/anygo/xmap" ) func main() { mp := &xmap.OrderedSync[string, int]{} mp.Set("k0", 0) mp.Set("k1", 1) mp.Set("k2", 2) fmt.Println("keys:", mp.Keys()) }
Output: keys: [k0 k1 k2]
func (*Ordered[K, V]) Values ¶
func (s *Ordered[K, V]) Values() []V
Example ¶
package main import ( "fmt" "github.com/xanygo/anygo/xmap" ) func main() { mp := &xmap.OrderedSync[string, int]{} mp.Set("k0", 0) mp.Set("k1", 1) mp.Set("k2", 2) fmt.Println("values:", mp.Values()) }
Output: values: [0 1 2]
type OrderedSync ¶
type OrderedSync[K comparable, V any] struct { // Caption 初始化 map 时,默认的容量,可选,默认值为 4 Caption int // contains filtered or unexported fields }
OrderedSync 按照写入顺序排序的 Sync, 并发安全的
func (*OrderedSync[K, V]) Clear ¶
func (s *OrderedSync[K, V]) Clear()
func (*OrderedSync[K, V]) Clone ¶
func (s *OrderedSync[K, V]) Clone() *OrderedSync[K, V]
func (*OrderedSync[K, V]) Delete ¶
func (s *OrderedSync[K, V]) Delete(keys ...K)
func (*OrderedSync[K, V]) Get ¶
func (s *OrderedSync[K, V]) Get(key K) (V, bool)
func (*OrderedSync[K, V]) GetDf ¶
func (s *OrderedSync[K, V]) GetDf(key K, def V) V
func (*OrderedSync[K, V]) Has ¶
func (s *OrderedSync[K, V]) Has(key K) bool
func (*OrderedSync[K, V]) HasAny ¶
func (s *OrderedSync[K, V]) HasAny(keys ...K) bool
func (*OrderedSync[K, V]) Iter ¶
func (s *OrderedSync[K, V]) Iter() iter.Seq2[K, V]
func (*OrderedSync[K, V]) KVs ¶
func (s *OrderedSync[K, V]) KVs(clone bool) ([]K, map[K]V)
func (*OrderedSync[K, V]) Keys ¶
func (s *OrderedSync[K, V]) Keys() []K
func (*OrderedSync[K, V]) Len ¶
func (s *OrderedSync[K, V]) Len() int
func (*OrderedSync[K, V]) Map ¶
func (s *OrderedSync[K, V]) Map(clone bool) map[K]V
func (*OrderedSync[K, V]) MustGet ¶
func (s *OrderedSync[K, V]) MustGet(key K) V
func (*OrderedSync[K, V]) Range ¶
func (s *OrderedSync[K, V]) Range(fn func(key K, value V) bool)
func (*OrderedSync[K, V]) Set ¶
func (s *OrderedSync[K, V]) Set(key K, value V)
func (*OrderedSync[K, V]) Values ¶
func (s *OrderedSync[K, V]) Values() []V
type SliceValue ¶
type SliceValue[K, V comparable] struct { // contains filtered or unexported fields }
SliceValue 值为 slice 的 并发安全的 map ( map[K][]V )
func (*SliceValue[K, V]) Add ¶
func (s *SliceValue[K, V]) Add(key K, values ...V)
func (*SliceValue[K, V]) Delete ¶
func (s *SliceValue[K, V]) Delete(keys ...K)
func (*SliceValue[K, V]) DeleteValue ¶
func (s *SliceValue[K, V]) DeleteValue(key K, values ...V)
func (*SliceValue[K, V]) Get ¶
func (s *SliceValue[K, V]) Get(key K) []V
func (*SliceValue[K, V]) GetFirst ¶
func (s *SliceValue[K, V]) GetFirst(key K) (v V)
func (*SliceValue[K, V]) Has ¶
func (s *SliceValue[K, V]) Has(key K) bool
func (*SliceValue[K, V]) HasValue ¶
func (s *SliceValue[K, V]) HasValue(key K, values ...V) bool
func (*SliceValue[K, V]) Iter ¶
func (s *SliceValue[K, V]) Iter() iter.Seq2[K, []V]
func (*SliceValue[K, V]) Keys ¶
func (s *SliceValue[K, V]) Keys() []K
func (*SliceValue[K, V]) Map ¶
func (s *SliceValue[K, V]) Map(clone bool) map[K][]V
func (*SliceValue[K, V]) Set ¶
func (s *SliceValue[K, V]) Set(key K, values ...V)
type SliceValueSync ¶
type SliceValueSync[K, V comparable] struct { // contains filtered or unexported fields }
SliceValueSync 值为 slice 的 并发安全的 map ( map[K][]V )
func (*SliceValueSync[K, V]) Add ¶
func (s *SliceValueSync[K, V]) Add(key K, values ...V)
func (*SliceValueSync[K, V]) Delete ¶
func (s *SliceValueSync[K, V]) Delete(keys ...K)
func (*SliceValueSync[K, V]) DeleteValue ¶
func (s *SliceValueSync[K, V]) DeleteValue(key K, values ...V)
func (*SliceValueSync[K, V]) Get ¶
func (s *SliceValueSync[K, V]) Get(key K) []V
func (*SliceValueSync[K, V]) GetFirst ¶
func (s *SliceValueSync[K, V]) GetFirst(key K) (v V)
func (*SliceValueSync[K, V]) Has ¶
func (s *SliceValueSync[K, V]) Has(key K) bool
func (*SliceValueSync[K, V]) HasValue ¶
func (s *SliceValueSync[K, V]) HasValue(key K, values ...V) bool
func (*SliceValueSync[K, V]) Iter ¶
func (s *SliceValueSync[K, V]) Iter() iter.Seq2[K, []V]
func (*SliceValueSync[K, V]) Keys ¶
func (s *SliceValueSync[K, V]) Keys() []K
func (*SliceValueSync[K, V]) Map ¶
func (s *SliceValueSync[K, V]) Map(clone bool) (result map[K][]V)
func (*SliceValueSync[K, V]) Set ¶
func (s *SliceValueSync[K, V]) Set(key K, values ...V)
type Sync ¶
type Sync[K comparable, V any] struct { // contains filtered or unexported fields }
Sync 并发安全的 Map,基于 sync.Map 简单封装以支持泛型
func (*Sync[K, V]) CompareAndDelete ¶
func (*Sync[K, V]) CompareAndSwap ¶
func (*Sync[K, V]) DeleteFunc ¶
func (*Sync[K, V]) LoadAndDelete ¶
func (*Sync[K, V]) LoadOrStore ¶
type Tags ¶
type Tags[K comparable, V any, T comparable] struct { // contains filtered or unexported fields }
Tags 以 map 格式存储 < key: value & tags > 数据,并且支持使用 tags 查找数据。 非并发安全的