-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmap.go
187 lines (158 loc) · 5.15 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
package maps
import (
"iter"
)
// Map is a go map that uses a standard set of functions shared with other Map-like types.
//
// The recommended way to create a Map is to first declare a concrete type alias, and then call
// new on it, like this:
//
// type MyMap = Map[string,int]
//
// m := new(MyMap)
//
// This will allow you to swap in a different kind of Map just by changing the type.
type Map[K comparable, V any] struct {
items StdMap[K, V]
}
// NewMap creates a new map that maps values of type K to values of type V.
// Pass in zero or more standard maps and the contents of those maps will be copied to the new Map.
func NewMap[K comparable, V any](sources ...map[K]V) *Map[K, V] {
m := new(Map[K, V])
for _, i := range sources {
m.Copy(Cast(i))
}
return m
}
// Clear resets the map to an empty map
func (m *Map[K, V]) Clear() {
m.items = nil
}
// Len returns the number of items in the map
func (m *Map[K, V]) Len() int {
return m.items.Len()
}
// Range calls the given function for each key,value pair in the map.
// This is the same interface as sync.Map.Range().
// While its safe to call methods of the map from within the Range function, its discouraged.
// If you ever switch to one of the SafeMap maps, it will cause a deadlock.
func (m *Map[K, V]) Range(f func(k K, v V) bool) {
m.items.Range(f)
}
// Load returns the value based on its key, and a boolean indicating whether it exists in the map.
// This is the same interface as sync.Map.Load()
func (m *Map[K, V]) Load(k K) (V, bool) {
return m.items.Load(k)
}
// Get returns the value for the given key. If the key does not exist, the zero value will be returned.
func (m *Map[K, V]) Get(k K) V {
return m.items.Get(k)
}
// Has returns true if the key exists.
func (m *Map[K, V]) Has(k K) bool {
return m.items.Has(k)
}
// Delete removes the key from the map. If the key does not exist, nothing happens.
func (m Map[K, V]) Delete(k K) V {
return m.items.Delete(k)
}
// Keys returns a new slice containing the keys of the map.
func (m *Map[K, V]) Keys() []K {
return m.items.Keys()
}
// Values returns a new slice containing the values of the map.
func (m *Map[K, V]) Values() []V {
return m.items.Values()
}
// Set sets the key to the given value.
func (m *Map[K, V]) Set(k K, v V) {
if m.items == nil {
m.items = map[K]V{k: v}
} else {
m.items.Set(k, v)
}
}
// Merge copies the items from in to the map, overwriting any conflicting keys.
// Deprecated: Call Copy instead.
func (m *Map[K, V]) Merge(in MapI[K, V]) {
m.Copy(in)
}
// Copy copies the items from in to the map, overwriting any conflicting keys.
func (m *Map[K, V]) Copy(in MapI[K, V]) {
if m.items == nil {
m.items = make(map[K]V, in.Len())
}
m.items.Copy(in)
}
// Equal returns true if all the keys and values are equal.
//
// If the values are not comparable, you should implement the Equaler interface on the values.
// Otherwise, you will get a runtime panic.
func (m *Map[K, V]) Equal(m2 MapI[K, V]) bool {
return m.items.Equal(m2)
}
// MarshalBinary implements the BinaryMarshaler interface to convert the map to a byte stream.
func (m *Map[K, V]) MarshalBinary() ([]byte, error) {
return m.items.MarshalBinary()
}
// UnmarshalBinary implements the BinaryUnmarshaler interface to convert a byte stream to a Map.
//
// Note that you may need to register the map at init time with gob like this:
//
// func init() {
// gob.Register(new(Map[keytype,valuetype]))
// }
func (m *Map[K, V]) UnmarshalBinary(data []byte) (err error) {
return m.items.UnmarshalBinary(data)
}
// MarshalJSON implements the json.Marshaler interface to convert the map into a JSON object.
func (m *Map[K, V]) MarshalJSON() (out []byte, err error) {
return m.items.MarshalJSON()
}
// UnmarshalJSON implements the json.Unmarshaler interface to convert a json object to a Map.
// The JSON must start with an object.
func (m *Map[K, V]) UnmarshalJSON(in []byte) (err error) {
return m.items.UnmarshalJSON(in)
}
// String returns the map as a string.
func (m *Map[K, V]) String() string {
return m.items.String()
}
// All returns an iterator over all the items in the map.
func (m *Map[K, V]) All() iter.Seq2[K, V] {
return m.items.All()
}
// KeysIter returns an iterator over all the keys in the map.
func (m *Map[K, V]) KeysIter() iter.Seq[K] {
return m.items.KeysIter()
}
// ValuesIter returns an iterator over all the values in the map.
func (m *Map[K, V]) ValuesIter() iter.Seq[V] {
return m.items.ValuesIter()
}
// Insert adds the values from seq to the map.
// Duplicate keys are overridden.
func (m *Map[K, V]) Insert(seq iter.Seq2[K, V]) {
if m.items == nil {
m.items = map[K]V{}
}
m.items.Insert(seq)
}
// CollectMap collects key-value pairs from seq into a new Map
// and returns it.
func CollectMap[K comparable, V any](seq iter.Seq2[K, V]) *Map[K, V] {
m := new(Map[K, V])
m.Insert(seq)
return m
}
// Clone returns a copy of the Map. This is a shallow clone:
// the new keys and values are set using ordinary assignment.
func (m *Map[K, V]) Clone() *Map[K, V] {
m1 := new(Map[K, V])
m1.items = m.items.Clone()
return m1
}
// DeleteFunc deletes any key/value pairs for which del returns true.
func (m *Map[K, V]) DeleteFunc(del func(K, V) bool) {
m.items.DeleteFunc(del)
}