maps

package module
v0.0.0-...-2fc83c6 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2026 License: MIT Imports: 8 Imported by: 3

Documentation

Overview

Package maps 提供原生 map 类型相关的一些工具

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clone

func Clone[K constraints.Ordered, V any](m map[K]V) map[K]V

Clone 浅复制一个 map

func Equal

func Equal[K comparable, V comparable](a, b map[K]V) bool

Equal 判断两个 map 是否完全一致

func GetOne

func GetOne[K constraints.Ordered, V any](m map[K]V) (key K, value V, exist bool)

GetOne 表示随便从 map 中取一个 KV 对

func GetOrDefault

func GetOrDefault[K constraints.Ordered, V any](m map[K]V, key K, defaultValue V) V

GetOrDefault 从 map 中获取数据, 如果不存在则返回 default value

func GetStringOrFormat

func GetStringOrFormat[K constraints.Ordered, V ~string](m map[K]V, key K, format string) V

GetStringOrFormat 从 value 为 string 的 map 中获取数据, 如果不存在则使用 fmt.Sprintf(format, key) 的测试返回 value

func Keys

func Keys[K constraints.Ordered, V any](m map[K]V) (keys slices.List[K])

Keys 返回所有的 key

func KeysEqual

func KeysEqual[K comparable, V1, V2 any](a map[K]V1, b map[K]V2) bool

KeysEqual 判断两个 map 是否拥有相同的 keys

func Values

func Values[K constraints.Ordered, V any](m map[K]V) (values []V)

Values 返回所有的 value

Types

type KVPair

type KVPair[K comparable, V any] struct {
	K K
	V V
}

KVPair 表示键值对, 便于调用方排序用。

func KeyValues

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

KeyValues 获取键值对列表

func KeyValuesAndSortByKeys

func KeyValuesAndSortByKeys[K constraints.Ordered, V any](m map[K]V, sortOrder SortOrder) []KVPair[K, V]

KeyValuesAndSortByKeys 获取键值对列表, 并且按照 key 排序

func KeyValuesAndSortByValues

func KeyValuesAndSortByValues[K comparable, V constraints.Ordered](m map[K]V, sortOrder SortOrder) []KVPair[K, V]

KeyValuesAndSortByValues 获取键值对列表, 并且按照 key 排序

type OrderedMap

type OrderedMap[K constraints.Ordered, V any] struct {
	// contains filtered or unexported fields
}

OrderedMap 表示一个有序的 map, 底层使用红黑树实现。 请注意: 非协程安全

func NewOrderedMap

func NewOrderedMap[K constraints.Ordered, V any]() *OrderedMap[K, V]

func (*OrderedMap[K, V]) All

func (m *OrderedMap[K, V]) All() iter.Seq2[K, V]

All 返回一个迭代器, 用于遍历所有的键值对, 按照 key 的升序排列 用法示例:

for k, v := range m.All() {
	// 处理 k 和 v
}

func (*OrderedMap[K, V]) Delete

func (m *OrderedMap[K, V]) Delete(k K)

Delete 删除某个 key

func (*OrderedMap[K, V]) Get

func (m *OrderedMap[K, V]) Get(k K) (v V, exist bool)

Get 获取某个 key 的值

func (*OrderedMap[K, V]) Keys

func (m *OrderedMap[K, V]) Keys() iter.Seq[K]

Keys 返回一个迭代器, 用于遍历所有的 key, 按照 key 的升序排列 用法示例:

for k := range m.Keys() {
	// 处理 k
}

func (*OrderedMap[K, V]) Len

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

Len 返回 map 的大小

func (*OrderedMap[K, V]) Range

func (m *OrderedMap[K, V]) Range(f func(k K, v V) bool)

Range 遍历 map, 如果 f 返回 false, 则停止遍历

func (*OrderedMap[K, V]) Set

func (m *OrderedMap[K, V]) Set(k K, v V)

Set 设置某个 key 的值

func (*OrderedMap[K, V]) SetOrSwap

func (m *OrderedMap[K, V]) SetOrSwap(k K, v V) (swapped bool)

SetOrSwap 设置某个 key 的值, 如果 key 不存在, 则设置, 如果 key 存在, 则交换

func (*OrderedMap[K, V]) Size

func (m *OrderedMap[K, V]) Size() int

Size 返回 map 的大小

type RWSafeMap

type RWSafeMap[K comparable, V any] interface {
	json.Marshaler
	json.Unmarshaler

	Store(k K, v V)
	Load(k K) (V, bool)
	Delete(k K)

	LoadAndDelete(k K) (value V, loaded bool)
	LoadOrStore(k K, v V) (actual V, loaded bool)
	LoadOrNew(k K, newFunc func() V) (actual V, loaded bool)

	Swap(k K, v V) (previous V, loaded bool)
	Range(f func(key K, value V) bool)

	Size() int
}

RWSafeMap 即 Read-Write-Locked Safe Map, 表示这是一个使用读写锁锁住的安全的 map 类型。 适用于读多写少的情况。

func NewRWSafeMap

func NewRWSafeMap[K comparable, V any](capacity ...int) RWSafeMap[K, V]

NewRWSafeMap 新建一个 RWSafeMap 实例。可选参数只有一个, 表示 capacity

type Set

type Set[K comparable] map[K]struct{}

Set 表示一个集合

func NewSet

func NewSet[K comparable](vals ...K) Set[K]

NewSet 返回一个集合类型

func NewSetFromSlice

func NewSetFromSlice[T constraints.Ordered](sli slices.List[T]) Set[T]

NewSetFromSlice 从一个切片转为 Set 类型

func NewSetWithCapacity

func NewSetWithCapacity[K comparable, I constraints.Integer](cap I) Set[K]

NewSetWithCapacity 返回一个集合类型并初始化容量

func (Set[K]) Add

func (s Set[K]) Add(key K)

Add 添加一个值

func (Set[K]) Clone

func (s Set[K]) Clone() Set[K]

Clone 浅复制

func (Set[K]) Del

func (s Set[K]) Del(key K) bool

Del 删除某个 key, 并且返回删除之前是否已存在

func (Set[K]) Difference

func (s Set[K]) Difference(toSub Set[K]) Set[K]

Difference 差集

func (Set[K]) Equal

func (s Set[K]) Equal(another Set[K]) bool

Equal 判断两个 set 是不是相等

func (Set[K]) Has

func (s Set[K]) Has(key K) bool

Has 是否包含某个 key

func (Set[K]) Intersection

func (s Set[K]) Intersection(another Set[K]) Set[K]

Intersection 交集

func (Set[K]) Len

func (s Set[K]) Len() int

Len 返回集合的大小

func (Set[K]) SymmetricDifference

func (s Set[K]) SymmetricDifference(another Set[K]) Set[K]

SymmetricDifference 对称差集, 表示两个集合中不相交的部份, 即 并集 减去 交集

func (Set[K]) Union

func (s Set[K]) Union(another Set[K]) Set[K]

Union 并集

type SortOrder

type SortOrder bool

SortOrder 排序方向, 用于 KVPair

const (
	// Ascend 升序
	Ascend SortOrder = false
	// Descend 降序
	Descend SortOrder = true
)

Directories

Path Synopsis
Package freshness 实现一个新鲜度 map, 新鲜度指的是存取时间的新鲜度, 如果一个值经常被存取, 可能永远不会过期
Package freshness 实现一个新鲜度 map, 新鲜度指的是存取时间的新鲜度, 如果一个值经常被存取, 可能永远不会过期

Jump to

Keyboard shortcuts

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