-
Notifications
You must be signed in to change notification settings - Fork 2
/
Map.go
45 lines (40 loc) · 1.51 KB
/
Map.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package Maps
import (
"math"
)
const (
MaxArrayLen uint = math.MaxInt
)
// Map is designed for compatibility with sync.Map. All the below functions have the exact same usage/behavior as documented in sync.Map.
type Map[K any, V any] interface {
Delete(K)
Load(K) (V, bool)
LoadAndDelete(K) (V, bool)
LoadOrStore(K, V) (V, bool)
Range(func(K, V) bool)
Store(K, V)
}
// ExtendedMap is the additional operation that my implementation support. Note that these operations aren't explicit implemented, meaning that they're merely taking advantage of the implementation. For example, values are internally stored as pointers in all implementations, so why not just provide a method to access the pointers directly?
type ExtendedMap[K any, V any] interface {
Map[K, V]
//HasKey is a convenient alias for `_,x:=M.Load(K)`
HasKey(K) bool
Size() uint
//Take an arbitrary key value pair from the Map.
Take() (K, V)
//Set is equivalent to Store(K,V) on a existing key, it won't do anything on a key that's not in the Map. In the prior case, it should be designed to be faster than Store.
Set(K, V) *V
}
type PtrMap[K any, V any] interface {
Map[K, V]
//TakePtr is the pointer variant of Take.
TakePtr(K, *V)
//LoadPtr is the pointer variant of Map.Load.
LoadPtr(K) *V
//LoadPtrAndDelete is the pointer variant of Map.LoadAndDelete.
LoadPtrAndDelete(K) (*V, bool)
//LoadPtrOrStore is the pointer variant of Map.LoadOrStore.
LoadPtrOrStore(K, V) (*V, bool)
//RangePtr is the pointer variant of Map.Range.
RangePtr(func(K, *V) bool)
}