-
Notifications
You must be signed in to change notification settings - Fork 1
/
map.go
183 lines (152 loc) · 5.05 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
package gg
/*
Non-idempotent version of `MapInit`. If the target pointer is nil, does nothing
and returns nil. If the target pointer is non-nil, allocates the map via
`make`, stores it at the target pointer, and returns the resulting non-nil
map.
*/
func MapMake[Map ~map[Key]Val, Key comparable, Val any](ptr *Map) Map {
if ptr == nil {
return nil
}
val := make(map[Key]Val)
*ptr = val
return val
}
/*
Shortcut for converting an arbitrary map to `Dict`. Workaround for the
limitations of type inference in Go generics. This is a free cast with no
reallocation.
*/
func ToDict[Src ~map[Key]Val, Key comparable, Val any](val Src) Dict[Key, Val] {
return Dict[Key, Val](val)
}
/*
Typedef of an arbitrary map with various methods that duplicate global map
functions. Useful as a shortcut for creating bound methods that can be passed
to higher-order functions.
*/
type Dict[Key comparable, Val any] map[Key]Val
// Same as `len(self)`.
func (self Dict[_, _]) Len() int { return len(self) }
// Same as `len(self) <= 0`. Inverse of `.IsNotEmpty`.
func (self Dict[_, _]) IsEmpty() bool { return len(self) <= 0 }
// Same as `len(self) > 0`. Inverse of `.IsEmpty`.
func (self Dict[_, _]) IsNotEmpty() bool { return len(self) > 0 }
/*
Idempotent map initialization. If the target pointer is nil, does nothing and
returns nil. If the map at the target pointer is non-nil, does nothing and
returns that map. Otherwise allocates the map via `make`, stores it at the
target pointer, and returns the resulting non-nil map.
*/
func MapInit[Map ~map[Key]Val, Key comparable, Val any](ptr *Map) Map {
if ptr == nil {
return nil
}
val := *ptr
if val == nil {
val = make(map[Key]Val)
*ptr = val
}
return val
}
// Self as global `MapInit`.
func (self *Dict[Key, Val]) Init() Dict[Key, Val] { return MapInit(self) }
/*
Copies the given map. If the input is nil, the output is nil. Otherwise the
output is a shallow copy.
*/
func MapClone[Map ~map[Key]Val, Key comparable, Val any](src Map) Map {
if src == nil {
return nil
}
out := make(Map, len(src))
for key, val := range src {
out[key] = val
}
return out
}
// Self as global `MapClone`.
func (self Dict[Key, Val]) Clone() Dict[Key, Val] { return MapClone(self) }
// Returns the maps's keys as a slice. Order is random.
func MapKeys[Key comparable, Val any](src map[Key]Val) []Key {
if src == nil {
return nil
}
out := make([]Key, 0, len(src))
for key := range src {
out = append(out, key)
}
return out
}
// Self as global `MapKeys`.
func (self Dict[Key, _]) Keys() []Key { return MapKeys(self) }
// Returns the maps's values as a slice. Order is random.
func MapVals[Key comparable, Val any](src map[Key]Val) []Val {
if src == nil {
return nil
}
out := make([]Val, 0, len(src))
for _, val := range src {
out = append(out, val)
}
return out
}
// Self as global `MapVals`.
func (self Dict[_, Val]) Vals() []Val { return MapVals(self) }
// Same as `_, ok := tar[key]`, expressed as a generic function.
func MapHas[Map ~map[Key]Val, Key comparable, Val any](tar Map, key Key) bool {
_, ok := tar[key]
return ok
}
// Self as global `MapHas`.
func (self Dict[Key, _]) Has(key Key) bool { return MapHas(self, key) }
// Same as `val, ok := tar[key]`, expressed as a generic function.
func MapGot[Map ~map[Key]Val, Key comparable, Val any](tar Map, key Key) (Val, bool) {
val, ok := tar[key]
return val, ok
}
// Self as global `MapGot`.
func (self Dict[Key, Val]) Got(key Key) (Val, bool) { return MapGot(self, key) }
// Same as `tar[key]`, expressed as a generic function.
func MapGet[Map ~map[Key]Val, Key comparable, Val any](tar Map, key Key) Val {
return tar[key]
}
// Self as global `MapGet`.
func (self Dict[Key, Val]) Get(key Key) Val { return MapGet(self, key) }
// Same as `tar[key] = val`, expressed as a generic function.
func MapSet[Map ~map[Key]Val, Key comparable, Val any](tar Map, key Key, val Val) {
tar[key] = val
}
// Self as global `MapSet`.
func (self Dict[Key, Val]) Set(key Key, val Val) { MapSet(self, key, val) }
/*
Same as `MapSet`, but key and value should be be non-zero.
If either is zero, this ignores the inputs and does nothing.
*/
func MapSetOpt[Map ~map[Key]Val, Key comparable, Val any](tar Map, key Key, val Val) {
if IsNotZero(key) && IsNotZero(val) {
MapSet(tar, key, val)
}
}
// Self as global `MapSetOpt`.
func (self Dict[Key, Val]) SetOpt(key Key, val Val) { MapSetOpt(self, key, val) }
// Same as `delete(tar, key)`, expressed as a generic function.
func MapDel[Map ~map[Key]Val, Key comparable, Val any](tar Map, key Key) {
delete(tar, key)
}
// Self as global `MapDel`.
func (self Dict[Key, _]) Del(key Key) { delete(self, key) }
/*
Deletes all entries, returning the resulting map. Passing nil is safe.
Note that this involves iterating the map, which is inefficient in Go.
In many cases, it's more efficient to make a new map. Also note that
Go 1.21 and higher have an equivalent built-in function `clear`.
*/
func MapClear[Map ~map[Key]Val, Key comparable, Val any](tar Map) {
for key := range tar {
delete(tar, key)
}
}
// Self as global `MapClear`.
func (self Dict[_, _]) Clear() { MapClear(self) }