xmap

package
v0.0.0-...-c302f72 Latest Latest
Warning

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

Go to latest
Published: Jan 16, 2026 License: BSD-3-Clause Imports: 11 Imported by: 0

Documentation

Overview

Package xmap 字典(Sync)相关常用数据结构和算法

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckLenAtLeast

func CheckLenAtLeast[S ~map[K]V, K comparable, V any](s S, err error, min int) error

func CheckLenAtMost

func CheckLenAtMost[S ~map[K]V, K comparable, V any](s S, err error, max int) error

func CheckLenBetween

func CheckLenBetween[S ~map[K]V, K comparable, V any](s S, err error, min, max int) error

func CheckLenIn

func CheckLenIn[S ~map[K]V, K comparable, V any](s S, err error, lengths ...int) error

func Create

func Create[K comparable, V any](pairs ...any) (map[K]V, error)

Create 传入 k-v 对,创建 map,若 传入 value == nil,则会当做零值

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/ds/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/ds/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/ds/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/ds/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/ds/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 GetInt

func GetInt[K comparable, V any](m map[K]V, key K) (int, bool)

func GetInt64

func GetInt64[K comparable, V any](m map[K]V, key K) (int64, bool)

func GetString

func GetString[K comparable, V any](m map[K]V, key K) (string, bool)

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。

func Join

func Join[K string, V any](m map[K]V, sep string) string

func KeyValues

func KeyValues[K comparable, V any](m map[K]V) ([]K, []V)

func Keys

func Keys[K comparable, V any](m map[K]V) []K

Keys 返回 map 所有的 key。支持 map 为 nil。

func KeysMiss

func KeysMiss[K comparable, V any](mp map[K]V, keys []K) []K

KeysMiss 找出 map 中缺少的 keys

Example
package main

import (
	"fmt"

	"github.com/xanygo/anygo/ds/xmap"
)

func main() {
	mp := map[string]int{"a": 1, "b": 2}
	keys := []string{"a", "b", "c"}
	miss := xmap.KeysMiss(mp, keys)

	fmt.Println("miss=", miss)
}
Output:

miss= [c]

func MustCreate

func MustCreate[K comparable, V any](pairs ...any) map[K]V

MustCreate 使用 pairs 对,创建一个 map,若失败则 panic

func Range

func Range[K comparable, V any](m any, fn func(key K, val V) bool) int

Range 遍历任意类型的 map,返回 key、value 满足条件而且被 fn 接收的个数

  • 只有 map 数据的 key 和 value 的实际类型和 传入的类型完全匹配,才会触发回调
  • 使用 rv,ok := value.(Type) 方式断言 key 和 value
  • 不确定的类型,可以使用 any 代替,如只关注 key 的类型是 string,可以使用:Range[string,any](m,func(key string,value any)bool)
  • 传入的数据可以是任意类型;使用了反射
Example
package main

import (
	"fmt"

	"github.com/xanygo/anygo/ds/xmap"
)

func main() {
	mp := map[string]any{"a": 1, "b": int64(2)}

	var count1 int

	// 遍历出所有类型为 int 类型的 k-v 项
	matched := xmap.Range[string, int](mp, func(key string, val int) bool {
		count1 += val
		return true
	})
	fmt.Println("matched=", matched, ",count:", count1) // matched= 1 ,count: 1

	count1 = 0
	// value 使用 any,可以匹配所有类型
	matched = xmap.Range[string, any](mp, func(key string, val any) bool {
		switch rv := val.(type) {
		case int:
			count1 += rv
		case int64:
			count1 += int(rv)
		}
		return true
	})
	fmt.Println("matched=", matched, ",count:", count1) // matched= 2 ,count: 3

}
Output:

matched= 1 ,count: 1
matched= 2 ,count: 3

func Rekey

func Rekey[Map ~map[K]V, K comparable, V any](m Map, fn func(key K) K) Map

Rekey 使用回调函数处理 map 的 key,并返回新的只有新 key 的 map

func Values

func Values[K comparable, V any](m map[K]V) []V

Values 返回 map 所有的 value。支持 map 为 nil。

func ValuesByKeys

func ValuesByKeys[K comparable, V any](m map[K]V, keys []K) []V

ValuesByKeys 返回的结果严格按照传入的 keys 的顺序

Types

type Cached

type Cached[K comparable, v any] struct {
	New func(key K) v
	// contains filtered or unexported fields
}

func (*Cached[K, V]) Clear

func (c *Cached[K, V]) Clear()

func (*Cached[K, V]) Count

func (c *Cached[K, V]) Count() int

func (*Cached[K, V]) Delete

func (c *Cached[K, V]) Delete(keys ...K)

func (*Cached[K, V]) Get

func (c *Cached[K, V]) Get(key K) V

type LRU

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

LRU 最近最少使用( Least Recently Used ) 全内存缓存组件

func NewLRU

func NewLRU[K comparable, V any](capacity int) *LRU[K, V]

func (*LRU[K, V]) Clear

func (lru *LRU[K, V]) Clear()

Clear 重置、清空所有缓存

func (*LRU[K, V]) Delete

func (lru *LRU[K, V]) Delete(keys ...K)

func (*LRU[K, V]) Get

func (lru *LRU[K, V]) Get(key K) (v V, ok bool)

func (*LRU[K, V]) Has

func (lru *LRU[K, V]) Has(key K) bool

func (*LRU[K, V]) Keys

func (lru *LRU[K, V]) Keys() []K

Keys 返回所有的 key,已按照使用时间排序,最近使用的排在前面

func (*LRU[K, V]) Len

func (lru *LRU[K, V]) Len() int

func (*LRU[K, V]) Map

func (lru *LRU[K, V]) Map() map[K]V

func (*LRU[K, V]) Set

func (lru *LRU[K, V]) Set(key K, value V)

type LRUReader

type LRUReader[K string, V any] struct {
	New   func(key K) V
	Store *LRU[K, V]
	// contains filtered or unexported fields
}

func (*LRUReader[K, V]) Delete

func (lr *LRUReader[K, V]) Delete(keys ...K)

func (*LRUReader[K, V]) Get

func (lr *LRUReader[K, V]) Get(key K) V

type Ordered

type Ordered[K comparable, V any] struct {
	// Capacity 初始化 map 时,默认的容量,可选,默认值为 4
	Capacity int
	// contains filtered or unexported fields
}

Ordered 按照写入顺序排序的 Sync, 非并发安全的

func (*Ordered[K, V]) Clear

func (s *Ordered[K, V]) Clear()

func (*Ordered[K, V]) Clone

func (s *Ordered[K, V]) Clone() *Ordered[K, V]

func (*Ordered[K, V]) Delete

func (s *Ordered[K, V]) Delete(keys ...K)

func (*Ordered[K, V]) Get

func (s *Ordered[K, V]) Get(key K) (V, bool)

func (*Ordered[K, V]) GetDf

func (s *Ordered[K, V]) GetDf(key K, def V) V

func (*Ordered[K, V]) Has

func (s *Ordered[K, V]) Has(key K) bool

func (*Ordered[K, V]) HasAny

func (s *Ordered[K, V]) HasAny(keys ...K) bool

func (*Ordered[K, V]) Head

func (s *Ordered[K, V]) Head() (key K, value V, ok bool)

Head 返回头部第一的 key 和 value

func (*Ordered[K, V]) Iter

func (s *Ordered[K, V]) Iter() iter.Seq2[K, V]

func (*Ordered[K, V]) KVs

func (s *Ordered[K, V]) KVs(clone bool) ([]K, map[K]V)

func (*Ordered[K, V]) Keys

func (s *Ordered[K, V]) Keys() []K
Example
package main

import (
	"fmt"

	"github.com/xanygo/anygo/ds/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]) Len

func (s *Ordered[K, V]) Len() int

func (*Ordered[K, V]) LoadOrStore

func (s *Ordered[K, V]) LoadOrStore(key K, v V) (actual any, loaded bool)

func (*Ordered[K, V]) Map

func (s *Ordered[K, V]) Map(clone bool) map[K]V

func (*Ordered[K, V]) MustGet

func (s *Ordered[K, V]) MustGet(key K) V

func (*Ordered[K, V]) Range

func (s *Ordered[K, V]) Range(fn func(key K, value V) bool)

func (*Ordered[K, V]) Set

func (s *Ordered[K, V]) Set(key K, value V)

func (*Ordered[K, V]) Tail

func (s *Ordered[K, V]) Tail() (key K, value V, ok bool)

func (*Ordered[K, V]) Values

func (s *Ordered[K, V]) Values() []V
Example
package main

import (
	"fmt"

	"github.com/xanygo/anygo/ds/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 {
	// Capacity 初始化 map 时,默认的容量,可选,默认值为 4
	Capacity 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]) Head

func (s *OrderedSync[K, V]) Head() (key K, value V, ok 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]) LoadOrStore

func (s *OrderedSync[K, V]) LoadOrStore(key K, v V) (actual any, loaded bool)

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

func (s *OrderedSync[K, V]) Tail() (key K, value V, ok bool)

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

func (s *SliceValue[K, V]) AddUnique(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]) Len

func (s *SliceValue[K, V]) Len() int

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

func (s *SliceValueSync[K, V]) AddUnique(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]

Iter 用于遍历,在遍历期间会锁住整个对象

func (*SliceValueSync[K, V]) Keys

func (s *SliceValueSync[K, V]) Keys() []K

func (*SliceValueSync[K, V]) Len

func (s *SliceValueSync[K, V]) Len() int

func (*SliceValueSync[K, V]) Map

func (s *SliceValueSync[K, V]) Map(clone bool) (result map[K][]V)

func (*SliceValueSync[K, V]) Rand

func (s *SliceValueSync[K, V]) Rand() (key K, value []V, ok bool)

Rand 随机返回一个

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

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

func (*Sync[K, V]) CompareAndDelete

func (m *Sync[K, V]) CompareAndDelete(key K, old V) (deleted bool)

func (*Sync[K, V]) CompareAndSwap

func (m *Sync[K, V]) CompareAndSwap(key K, old V, new V) bool

func (*Sync[K, V]) Delete

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

func (*Sync[K, V]) DeleteFunc

func (m *Sync[K, V]) DeleteFunc(fn func(key K, value V) bool)

func (*Sync[K, V]) Exists

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

func (*Sync[K, V]) Iter

func (m *Sync[K, V]) Iter() iter.Seq2[K, V]

func (*Sync[K, V]) Len

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

func (*Sync[K, V]) Load

func (m *Sync[K, V]) Load(key K) (value V, ok bool)

func (*Sync[K, V]) LoadAndDelete

func (m *Sync[K, V]) LoadAndDelete(key K) (value V, loaded bool)

func (*Sync[K, V]) LoadOrStore

func (m *Sync[K, V]) LoadOrStore(key K, value V) (actual V, loaded bool)

func (*Sync[K, V]) Range

func (m *Sync[K, V]) Range(fn func(key K, value V) bool)

func (*Sync[K, V]) Store

func (m *Sync[K, V]) Store(key K, value V)

func (*Sync[K, V]) Swap

func (m *Sync[K, V]) Swap(key K, value V) (previous V, loaded bool)

func (*Sync[K, V]) ToMap

func (m *Sync[K, V]) ToMap() map[K]V

type Tags

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

Tags 以 map 格式存储 < key: value & tags > 数据,并且支持使用 tags 查找数据。 非并发安全的

func (*Tags[K, V, T]) Any

func (t *Tags[K, V, T]) Any(defaultKey K, tags ...T) (value V)

Any 先依次使用 tags 查找,如无则尝试查找 defaultKey,若也不存在,则返回任意一个

func (*Tags[K, V, T]) Set

func (t *Tags[K, V, T]) Set(key K, value V, tags ...T)

Jump to

Keyboard shortcuts

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