-
Notifications
You must be signed in to change notification settings - Fork 453
/
namespace_results_map_gen.go
299 lines (264 loc) · 11.4 KB
/
namespace_results_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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
// Copyright (c) 2019 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 bootstrap
import (
"github.com/m3db/m3/src/x/ident"
)
// Copyright (c) 2019 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
// 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.
// NamespaceResultsMapHash is the hash for a given map entry, this is public to support
// iterating over the map using a native Go for loop.
type NamespaceResultsMapHash uint64
// NamespaceResultsMapHashFn is the hash function to execute when hashing a key.
type NamespaceResultsMapHashFn func(ident.ID) NamespaceResultsMapHash
// NamespaceResultsMapEqualsFn is the equals key function to execute when detecting equality of a key.
type NamespaceResultsMapEqualsFn func(ident.ID, ident.ID) bool
// NamespaceResultsMapCopyFn is the copy key function to execute when copying the key.
type NamespaceResultsMapCopyFn func(ident.ID) ident.ID
// NamespaceResultsMapFinalizeFn is the finalize key function to execute when finished with a key.
type NamespaceResultsMapFinalizeFn func(ident.ID)
// NamespaceResultsMap 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.
// NamespaceResultsMap 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 NamespaceResultsMap struct {
_NamespaceResultsMapOptions
// 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[NamespaceResultsMapHash]NamespaceResultsMapEntry
}
// _NamespaceResultsMapOptions 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 _NamespaceResultsMapOptions struct {
// hash is the hash function to execute when hashing a key.
hash NamespaceResultsMapHashFn
// equals is the equals key function to execute when detecting equality.
equals NamespaceResultsMapEqualsFn
// copy is the copy key function to execute when copying the key.
copy NamespaceResultsMapCopyFn
// finalize is the finalize key function to execute when finished with a
// key, this is optional to specify.
finalize NamespaceResultsMapFinalizeFn
// 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
}
// NamespaceResultsMapEntry is an entry in the map, this is public to support iterating
// over the map using a native Go for loop.
type NamespaceResultsMapEntry struct {
// key is used to check equality on lookups to resolve collisions
key _NamespaceResultsMapKey
// value type stored
value NamespaceResult
}
type _NamespaceResultsMapKey struct {
key ident.ID
finalize bool
}
// Key returns the map entry key.
func (e NamespaceResultsMapEntry) Key() ident.ID {
return e.key.key
}
// Value returns the map entry value.
func (e NamespaceResultsMapEntry) Value() NamespaceResult {
return e.value
}
// _NamespaceResultsMapAlloc 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 _NamespaceResultsMapAlloc(opts _NamespaceResultsMapOptions) *NamespaceResultsMap {
m := &NamespaceResultsMap{_NamespaceResultsMapOptions: opts}
m.Reallocate()
return m
}
func (m *NamespaceResultsMap) newMapKey(k ident.ID, opts _NamespaceResultsMapKeyOptions) _NamespaceResultsMapKey {
key := _NamespaceResultsMapKey{key: k, finalize: opts.finalizeKey}
if !opts.copyKey {
return key
}
key.key = m.copy(k)
return key
}
func (m *NamespaceResultsMap) removeMapKey(hash NamespaceResultsMapHash, key _NamespaceResultsMapKey) {
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 *NamespaceResultsMap) Get(k ident.ID) (NamespaceResult, 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 NamespaceResult
return empty, false
}
// Set will set the value for an identifier.
func (m *NamespaceResultsMap) Set(k ident.ID, v NamespaceResult) {
m.set(k, v, _NamespaceResultsMapKeyOptions{
copyKey: true,
finalizeKey: m.finalize != nil,
})
}
// NamespaceResultsMapSetUnsafeOptions is a set of options to use when setting a value with
// the SetUnsafe method.
type NamespaceResultsMapSetUnsafeOptions 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 *NamespaceResultsMap) SetUnsafe(k ident.ID, v NamespaceResult, opts NamespaceResultsMapSetUnsafeOptions) {
m.set(k, v, _NamespaceResultsMapKeyOptions{
copyKey: !opts.NoCopyKey,
finalizeKey: !opts.NoFinalizeKey,
})
}
type _NamespaceResultsMapKeyOptions struct {
copyKey bool
finalizeKey bool
}
func (m *NamespaceResultsMap) set(k ident.ID, v NamespaceResult, opts _NamespaceResultsMapKeyOptions) {
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] = NamespaceResultsMapEntry{
key: entry.key,
value: v,
}
return
}
// Linear probe to "next" to this entry (really a rehash)
hash++
}
m.lookup[hash] = NamespaceResultsMapEntry{
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 *NamespaceResultsMap) Iter() map[NamespaceResultsMapHash]NamespaceResultsMapEntry {
return m.lookup
}
// Len returns the number of map entries in the map.
func (m *NamespaceResultsMap) 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 *NamespaceResultsMap) Contains(k ident.ID) bool {
_, ok := m.Get(k)
return ok
}
// Delete will remove a value set in the map for the specified key.
func (m *NamespaceResultsMap) Delete(k ident.ID) {
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 *NamespaceResultsMap) 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 *NamespaceResultsMap) Reallocate() {
if m.initialSize > 0 {
m.lookup = make(map[NamespaceResultsMapHash]NamespaceResultsMapEntry, m.initialSize)
} else {
m.lookup = make(map[NamespaceResultsMapHash]NamespaceResultsMapEntry)
}
}