-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.go
81 lines (67 loc) · 2.57 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
/*
© 2022–present Harald Rudell <harald.rudell@gmail.com> (https://haraldrudell.github.io/haraldrudell/)
ISC License
*/
// Package pmaps2 contains resusable map types for other parl packages.
package pmaps2
import "golang.org/x/exp/maps"
// Map is a reusable promotable Go map
// - 5 native Go Map functions: Get Put Delete Length Range
// - convenience functions:
// - — Clear using fast, scavenging re-create
// - — Clone using range, optionally appending to provided instance
// - — these methods require access to the underlying Go map
// - not thread-safe:
// - — zero-value delete can be implemented by consumer
// - — range-Clear with zero-value write can be implemented by consumer
// - — order methods List and Keys can be implemented by consumer
type Map[K comparable, V any] struct{ goMap map[K]V }
// NewMap returns a reusable Go Map object
func NewMap[K comparable, V any]() (mapping *Map[K, V]) { return &Map[K, V]{goMap: make(map[K]V)} }
// Get returns the value mapped by key or the V zero-value otherwise
// - ok: true if a mapping was found
// - O(1)
func (m *Map[K, V]) Get(key K) (value V, ok bool) {
value, ok = m.goMap[key]
return
}
// Put create or replaces a mapping
func (m *Map[K, V]) Put(key K, value V) { m.goMap[key] = value }
// Delete removes mapping for key
// - if key is not mapped, the map is unchanged.
// - O(log n)
func (m *Map[K, V]) Delete(key K) { delete(m.goMap, key) }
// Length returns the number of mappings
func (m *Map[K, V]) Length() (length int) { return len(m.goMap) }
// Range traverses map bindings
// - iterates over map until rangeFunc returns false
// - order is undefined
// - similar to [sync.Map.Range] func (*sync.Map).Range(f func(key any, value any) bool)
func (m *Map[K, V]) Range(rangeFunc func(key K, value V) (keepGoing bool)) (rangedAll bool) {
for key, value := range m.goMap {
if !rangeFunc(key, value) {
return
}
}
return true
}
// Clear empties the map
// - clears by re-initializing the map
// - when instead ranging and deleting all keys,
// the unused size of the map is retained
func (m *Map[K, V]) Clear() { m.goMap = make(map[K]V) }
// Clone returns a shallow clone of the map
// - mp is an optional pointer to an already allocated map instance
// to be used and appended to
// - delegates to [maps.Clone] ranging all keys
func (m *Map[K, V]) Clone(mp ...*Map[K, V]) (clone *Map[K, V]) {
// clone should point to a destination instance
if len(mp) > 0 {
clone = mp[0]
}
if clone == nil {
clone = &Map[K, V]{}
}
clone.goMap = maps.Clone(m.goMap)
return
}