-
Notifications
You must be signed in to change notification settings - Fork 453
/
received_blocks_map_gen.go
271 lines (239 loc) · 10 KB
/
received_blocks_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
// 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 client
// 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.
// receivedBlocksMapHash is the hash for a given map entry, this is public to support
// iterating over the map using a native Go for loop.
type receivedBlocksMapHash uint64
// receivedBlocksMapHashFn is the hash function to execute when hashing a key.
type receivedBlocksMapHashFn func(idAndBlockStart) receivedBlocksMapHash
// receivedBlocksMapEqualsFn is the equals key function to execute when detecting equality of a key.
type receivedBlocksMapEqualsFn func(idAndBlockStart, idAndBlockStart) bool
// receivedBlocksMapCopyFn is the copy key function to execute when copying the key.
type receivedBlocksMapCopyFn func(idAndBlockStart) idAndBlockStart
// receivedBlocksMapFinalizeFn is the finalize key function to execute when finished with a key.
type receivedBlocksMapFinalizeFn func(idAndBlockStart)
// receivedBlocksMap 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.
// receivedBlocksMap 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 receivedBlocksMap struct {
_receivedBlocksMapOptions
// 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[receivedBlocksMapHash]receivedBlocksMapEntry
}
// _receivedBlocksMapOptions 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 _receivedBlocksMapOptions struct {
// hash is the hash function to execute when hashing a key.
hash receivedBlocksMapHashFn
// equals is the equals key function to execute when detecting equality.
equals receivedBlocksMapEqualsFn
// copy is the copy key function to execute when copying the key.
copy receivedBlocksMapCopyFn
// finalize is the finalize key function to execute when finished with a
// key, this is optional to specify.
finalize receivedBlocksMapFinalizeFn
// 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
}
// receivedBlocksMapEntry is an entry in the map, this is public to support iterating
// over the map using a native Go for loop.
type receivedBlocksMapEntry struct {
// key is used to check equality on lookups to resolve collisions
key _receivedBlocksMapKey
// value type stored
value receivedBlocks
}
type _receivedBlocksMapKey struct {
key idAndBlockStart
finalize bool
}
// Key returns the map entry key.
func (e receivedBlocksMapEntry) Key() idAndBlockStart {
return e.key.key
}
// Value returns the map entry value.
func (e receivedBlocksMapEntry) Value() receivedBlocks {
return e.value
}
// _receivedBlocksMapAlloc 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 _receivedBlocksMapAlloc(opts _receivedBlocksMapOptions) *receivedBlocksMap {
m := &receivedBlocksMap{_receivedBlocksMapOptions: opts}
m.Reallocate()
return m
}
func (m *receivedBlocksMap) newMapKey(k idAndBlockStart, opts _receivedBlocksMapKeyOptions) _receivedBlocksMapKey {
key := _receivedBlocksMapKey{key: k, finalize: opts.finalizeKey}
if !opts.copyKey {
return key
}
key.key = m.copy(k)
return key
}
func (m *receivedBlocksMap) removeMapKey(hash receivedBlocksMapHash, key _receivedBlocksMapKey) {
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 *receivedBlocksMap) Get(k idAndBlockStart) (receivedBlocks, 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 receivedBlocks
return empty, false
}
// Set will set the value for an identifier.
func (m *receivedBlocksMap) Set(k idAndBlockStart, v receivedBlocks) {
m.set(k, v, _receivedBlocksMapKeyOptions{
copyKey: true,
finalizeKey: m.finalize != nil,
})
}
// receivedBlocksMapSetUnsafeOptions is a set of options to use when setting a value with
// the SetUnsafe method.
type receivedBlocksMapSetUnsafeOptions 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 *receivedBlocksMap) SetUnsafe(k idAndBlockStart, v receivedBlocks, opts receivedBlocksMapSetUnsafeOptions) {
m.set(k, v, _receivedBlocksMapKeyOptions{
copyKey: !opts.NoCopyKey,
finalizeKey: !opts.NoFinalizeKey,
})
}
type _receivedBlocksMapKeyOptions struct {
copyKey bool
finalizeKey bool
}
func (m *receivedBlocksMap) set(k idAndBlockStart, v receivedBlocks, opts _receivedBlocksMapKeyOptions) {
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] = receivedBlocksMapEntry{
key: entry.key,
value: v,
}
return
}
// Linear probe to "next" to this entry (really a rehash)
hash++
}
m.lookup[hash] = receivedBlocksMapEntry{
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 *receivedBlocksMap) Iter() map[receivedBlocksMapHash]receivedBlocksMapEntry {
return m.lookup
}
// Len returns the number of map entries in the map.
func (m *receivedBlocksMap) 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 *receivedBlocksMap) Contains(k idAndBlockStart) bool {
_, ok := m.Get(k)
return ok
}
// Delete will remove a value set in the map for the specified key.
func (m *receivedBlocksMap) Delete(k idAndBlockStart) {
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 *receivedBlocksMap) 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 *receivedBlocksMap) Reallocate() {
if m.initialSize > 0 {
m.lookup = make(map[receivedBlocksMapHash]receivedBlocksMapEntry, m.initialSize)
} else {
m.lookup = make(map[receivedBlocksMapHash]receivedBlocksMapEntry)
}
}