maps

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

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

Go to latest
Published: Mar 29, 2024 License: MIT Imports: 6 Imported by: 2

Documentation

Overview

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

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Equal

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

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

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 slice.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 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 slice.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]) Del

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

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

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

type SortOrder

type SortOrder bool

SortOrder 排序方向, 用于 KVPair

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

Jump to

Keyboard shortcuts

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