xmap

package
v0.0.0-...-b147793 Latest Latest
Warning

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

Go to latest
Published: Jan 7, 2025 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

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

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Create

func Create(pairs ...any) map[any]any

Create 使用 pairs 对,创建一个 map,若 key 或者 value 为nil,则会跳过此条数据

func CreateErr

func CreateErr(pairs ...any) (map[any]any, error)

func CreateStrErr

func CreateStrErr(pairs ...any) (map[string]any, error)

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。

func Join

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

func Keys

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

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

func Values

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

Values 返回 map 所有的 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]) 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]) 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/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]) 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]) 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]) 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]) Count

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

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

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

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)

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