Documentation
¶
Overview ¶
Package maps 提供原生 map 类型相关的一些工具
Index ¶
- func Clone[K constraints.Ordered, V any](m map[K]V) map[K]V
- func Equal[K comparable, V comparable](a, b map[K]V) bool
- func GetOne[K constraints.Ordered, V any](m map[K]V) (key K, value V, exist bool)
- func GetOrDefault[K constraints.Ordered, V any](m map[K]V, key K, defaultValue V) V
- func GetStringOrFormat[K constraints.Ordered, V ~string](m map[K]V, key K, format string) V
- func Keys[K constraints.Ordered, V any](m map[K]V) (keys slices.List[K])
- func KeysEqual[K comparable, V1, V2 any](a map[K]V1, b map[K]V2) bool
- func Values[K constraints.Ordered, V any](m map[K]V) (values []V)
- type KVPair
- type OrderedMap
- func (m *OrderedMap[K, V]) All() iter.Seq2[K, V]
- func (m *OrderedMap[K, V]) Delete(k K)
- func (m *OrderedMap[K, V]) Get(k K) (v V, exist bool)
- func (m *OrderedMap[K, V]) Keys() iter.Seq[K]
- func (m *OrderedMap[K, V]) Len() int
- func (m *OrderedMap[K, V]) Range(f func(k K, v V) bool)
- func (m *OrderedMap[K, V]) Set(k K, v V)
- func (m *OrderedMap[K, V]) SetOrSwap(k K, v V) (swapped bool)
- func (m *OrderedMap[K, V]) Size() int
- type RWSafeMap
- type Set
- func (s Set[K]) Add(key K)
- func (s Set[K]) Clone() Set[K]
- func (s Set[K]) Del(key K) bool
- func (s Set[K]) Difference(toSub Set[K]) Set[K]
- func (s Set[K]) Equal(another Set[K]) bool
- func (s Set[K]) Has(key K) bool
- func (s Set[K]) Intersection(another Set[K]) Set[K]
- func (s Set[K]) Len() int
- func (s Set[K]) SymmetricDifference(another Set[K]) Set[K]
- func (s Set[K]) Union(another Set[K]) Set[K]
- type SortOrder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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 KeysEqual ¶
func KeysEqual[K comparable, V1, V2 any](a map[K]V1, b map[K]V2) bool
KeysEqual 判断两个 map 是否拥有相同的 keys
Types ¶
type KVPair ¶
type KVPair[K comparable, V any] struct { K K V V }
KVPair 表示键值对, 便于调用方排序用。
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]) 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]) Range ¶
func (m *OrderedMap[K, V]) Range(f func(k K, v V) bool)
Range 遍历 map, 如果 f 返回 false, 则停止遍历
func (*OrderedMap[K, V]) SetOrSwap ¶
func (m *OrderedMap[K, V]) SetOrSwap(k K, v V) (swapped bool)
SetOrSwap 设置某个 key 的值, 如果 key 不存在, 则设置, 如果 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 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]) SymmetricDifference ¶
SymmetricDifference 对称差集, 表示两个集合中不相交的部份, 即 并集 减去 交集
Directories
¶
| Path | Synopsis |
|---|---|
|
Package freshness 实现一个新鲜度 map, 新鲜度指的是存取时间的新鲜度, 如果一个值经常被存取, 可能永远不会过期
|
Package freshness 实现一个新鲜度 map, 新鲜度指的是存取时间的新鲜度, 如果一个值经常被存取, 可能永远不会过期 |