-
Notifications
You must be signed in to change notification settings - Fork 455
/
ids_map_gen.go
275 lines (242 loc) · 9.29 KB
/
ids_map_gen.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// This file was automatically generated by genny.
// Any changes will be lost if this file is regenerated.
// see https://github.com/mauricelam/genny
package builder
// This file was automatically generated by genny.
// Any changes will be lost if this file is regenerated.
// see https://github.com/mauricelam/genny
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
// IDsMapHash is the hash for a given map entry, this is public to support
// iterating over the map using a native Go for loop.
type IDsMapHash uint64
// IDsMapHashFn is the hash function to execute when hashing a key.
type IDsMapHashFn func([]byte) IDsMapHash
// IDsMapEqualsFn is the equals key function to execute when detecting equality of a key.
type IDsMapEqualsFn func([]byte, []byte) bool
// IDsMapCopyFn is the copy key function to execute when copying the key.
type IDsMapCopyFn func([]byte) []byte
// IDsMapFinalizeFn is the finalize key function to execute when finished with a key.
type IDsMapFinalizeFn func([]byte)
// IDsMap uses the genny package to provide a generic hash map that can be specialized
// by running the following command from this root of the repository:
// ```
// make hashmap-gen pkg=outpkg key_type=Type value_type=Type out_dir=/tmp
// ```
// Or if you would like to use bytes or ident.ID as keys you can use the
// partially specialized maps to generate your own maps as well:
// ```
// make byteshashmap-gen pkg=outpkg value_type=Type out_dir=/tmp
// make idhashmap-gen pkg=outpkg value_type=Type out_dir=/tmp
// ```
// This will output to stdout the generated source file to use for your map.
// It uses linear probing by incrementing the number of the hash created when
// hashing the identifier if there is a collision.
// IDsMap is a value type and not an interface to allow for less painful
// upgrades when adding/removing methods, it is not likely to need mocking so
// an interface would not be super useful either.
type IDsMap struct {
_IDsMapOptions
// lookup uses hash of the identifier for the key and the MapEntry value
// wraps the value type and the key (used to ensure lookup is correct
// when dealing with collisions), we use uint64 for the hash partially
// because lookups of maps with uint64 keys has a fast path for Go.
lookup map[IDsMapHash]IDsMapEntry
}
// _IDsMapOptions is a set of options used when creating an identifier map, it is kept
// private so that implementers of the generated map can specify their own options
// that partially fulfill these options.
type _IDsMapOptions struct {
// hash is the hash function to execute when hashing a key.
hash IDsMapHashFn
// equals is the equals key function to execute when detecting equality.
equals IDsMapEqualsFn
// copy is the copy key function to execute when copying the key.
copy IDsMapCopyFn
// finalize is the finalize key function to execute when finished with a
// key, this is optional to specify.
finalize IDsMapFinalizeFn
// initialSize is the initial size for the map, use zero to use Go's std map
// initial size and consequently is optional to specify.
initialSize int
}
// IDsMapEntry is an entry in the map, this is public to support iterating
// over the map using a native Go for loop.
type IDsMapEntry struct {
// key is used to check equality on lookups to resolve collisions
key _IDsMapKey
// value type stored
value struct{}
}
type _IDsMapKey struct {
key []byte
finalize bool
}
// Key returns the map entry key.
func (e IDsMapEntry) Key() []byte {
return e.key.key
}
// Value returns the map entry value.
func (e IDsMapEntry) Value() struct{} {
return e.value
}
// _IDsMapAlloc is a non-exported function so that when generating the source code
// for the map you can supply a public constructor that sets the correct
// hash, equals, copy, finalize options without users of the map needing to
// implement them themselves.
func _IDsMapAlloc(opts _IDsMapOptions) *IDsMap {
m := &IDsMap{_IDsMapOptions: opts}
m.Reallocate()
return m
}
func (m *IDsMap) newMapKey(k []byte, opts _IDsMapKeyOptions) _IDsMapKey {
key := _IDsMapKey{key: k, finalize: opts.finalizeKey}
if !opts.copyKey {
return key
}
key.key = m.copy(k)
return key
}
func (m *IDsMap) removeMapKey(hash IDsMapHash, key _IDsMapKey) {
delete(m.lookup, hash)
if key.finalize {
m.finalize(key.key)
}
}
// Get returns a value in the map for an identifier if found.
func (m *IDsMap) Get(k []byte) (struct{}, bool) {
hash := m.hash(k)
for entry, ok := m.lookup[hash]; ok; entry, ok = m.lookup[hash] {
if m.equals(entry.key.key, k) {
return entry.value, true
}
// Linear probe to "next" to this entry (really a rehash)
hash++
}
var empty struct{}
return empty, false
}
// Set will set the value for an identifier.
func (m *IDsMap) Set(k []byte, v struct{}) {
m.set(k, v, _IDsMapKeyOptions{
copyKey: true,
finalizeKey: m.finalize != nil,
})
}
// IDsMapSetUnsafeOptions is a set of options to use when setting a value with
// the SetUnsafe method.
type IDsMapSetUnsafeOptions struct {
NoCopyKey bool
NoFinalizeKey bool
}
// SetUnsafe will set the value for an identifier with unsafe options for how
// the map treats the key.
func (m *IDsMap) SetUnsafe(k []byte, v struct{}, opts IDsMapSetUnsafeOptions) {
m.set(k, v, _IDsMapKeyOptions{
copyKey: !opts.NoCopyKey,
finalizeKey: !opts.NoFinalizeKey,
})
}
type _IDsMapKeyOptions struct {
copyKey bool
finalizeKey bool
}
func (m *IDsMap) set(k []byte, v struct{}, opts _IDsMapKeyOptions) {
hash := m.hash(k)
for entry, ok := m.lookup[hash]; ok; entry, ok = m.lookup[hash] {
if m.equals(entry.key.key, k) {
m.lookup[hash] = IDsMapEntry{
key: entry.key,
value: v,
}
return
}
// Linear probe to "next" to this entry (really a rehash)
hash++
}
m.lookup[hash] = IDsMapEntry{
key: m.newMapKey(k, opts),
value: v,
}
}
// Iter provides the underlying map to allow for using a native Go for loop
// to iterate the map, however callers should only ever read and not write
// the map.
func (m *IDsMap) Iter() map[IDsMapHash]IDsMapEntry {
return m.lookup
}
// Len returns the number of map entries in the map.
func (m *IDsMap) Len() int {
return len(m.lookup)
}
// Contains returns true if value exists for key, false otherwise, it is
// shorthand for a call to Get that doesn't return the value.
func (m *IDsMap) Contains(k []byte) bool {
_, ok := m.Get(k)
return ok
}
// Delete will remove a value set in the map for the specified key.
func (m *IDsMap) Delete(k []byte) {
hash := m.hash(k)
for entry, ok := m.lookup[hash]; ok; entry, ok = m.lookup[hash] {
if m.equals(entry.key.key, k) {
m.removeMapKey(hash, entry.key)
return
}
// Linear probe to "next" to this entry (really a rehash)
hash++
}
}
// Reset will reset the map by simply deleting all keys to avoid
// allocating a new map.
func (m *IDsMap) Reset() {
for hash, entry := range m.lookup {
m.removeMapKey(hash, entry.key)
}
}
// Reallocate will avoid deleting all keys and reallocate a new
// map, this is useful if you believe you have a large map and
// will not need to grow back to a similar size.
func (m *IDsMap) Reallocate() {
if m.initialSize > 0 {
m.lookup = make(map[IDsMapHash]IDsMapEntry, m.initialSize)
} else {
m.lookup = make(map[IDsMapHash]IDsMapEntry)
}
}