/
omaplinkedhash.go
330 lines (302 loc) · 7.44 KB
/
omaplinkedhash.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package omap
import (
"fmt"
"hash/maphash"
)
//// OMapLinkedHash ////
// Objects that want to create a custom hashing for the key used by OMapLinkedHash must implement
// this interface, giving a HashSum32 func that returns the hash of the object as an uint32.
type Hasher interface {
HashSum32() uint32
}
// Hash func to be used by a given key
type hasherFunc[K comparable] func(key *K) uint32
// Implement an ordered map using a linked list but saving the key as an uint32 hash instead of
// copying the key into the map. This implementation should only be used when you have a very
// large object struct as K key, and preferable this object should implement Hasher
// interface to provide a performant hashing algorithm for the type.
type OMapLinkedHash[K comparable, V any] struct {
//hasher maphash.Hash
m map[uint32][]*mapEntry[*K, V]
head *mapEntry[*K, V]
tail *mapEntry[*K, V]
hasher hasherFunc[K]
length int
}
// Implement OMapIterator for OMapLinkedHash
type OMapLinkedHashIterator[K comparable, V any] struct {
cursor *mapEntry[*K, V]
bof bool
m *OMapLinkedHash[K, V]
}
// Return a new OMap based on OMapLinkedHash implementation, see OMapLinkedHash type for more
// details of the implementation.
func NewOMapLinkedHash[K comparable, V any]() OMap[K, V] {
var m OMapLinkedHash[K, V]
m.init()
return &m
}
func (m *OMapLinkedHash[K, V]) init() {
m.m = make(map[uint32][]*mapEntry[*K, V])
m.head = nil
m.tail = nil
m.setupHasher()
}
func (m *OMapLinkedHash[K, V]) setupHasher() {
var key K
switch any(key).(type) {
case Hasher:
m.hasher = func(key *K) uint32 {
h, _ := any(*key).(Hasher)
return h.HashSum32()
}
case string:
var mh maphash.Hash
m.hasher = func(key *K) uint32 {
s, _ := any(*key).(string)
_, _ = mh.WriteString(s)
ret := mh.Sum64()
mh.Reset()
return uint32(ret)
}
case uint32:
m.hasher = func(key *K) uint32 {
i, _ := any(*key).(uint32)
return i
}
case int32:
m.hasher = func(key *K) uint32 {
i, _ := any(*key).(int32)
return uint32(i)
}
case uint64:
m.hasher = func(key *K) uint32 {
i, _ := any(*key).(uint64)
return uint32(i)
}
case int64:
m.hasher = func(key *K) uint32 {
i, _ := any(*key).(int64)
return uint32(i)
}
case float32:
m.hasher = func(key *K) uint32 {
i, _ := any(*key).(float32)
return uint32(i)
}
case float64:
m.hasher = func(key *K) uint32 {
i, _ := any(*key).(float64)
return uint32(i)
}
default:
// always work, but it's slow
var mh maphash.Hash
m.hasher = func(key *K) uint32 {
s := fmt.Sprint(*key)
_, _ = mh.WriteString(s)
ret := mh.Sum64()
mh.Reset()
return uint32(ret)
}
}
}
func (m *OMapLinkedHash[K, V]) Put(key K, value V) {
elems, pos, hashedKey := m.getEntry(&key)
if pos >= 0 {
elems[pos].value = value
} else {
m.length++
entry := &mapEntry[*K, V]{
key: &key,
value: value,
next: nil,
prev: m.tail,
}
if elems != nil {
m.m[hashedKey] = append(elems, entry)
} else {
m.m[hashedKey] = []*mapEntry[*K, V]{entry}
}
if m.head == nil {
m.head = entry
m.tail = entry
} else {
m.tail.next = entry
m.tail = entry
}
}
}
func (m *OMapLinkedHash[K, V]) PutAfter(interfaceIt OMapIterator[K, V], key K, value V) error {
if it, ok := interfaceIt.(*OMapLinkedHashIterator[K, V]); !ok {
return fmt.Errorf("%w - expected OMapLinkedHash found %T", ErrInvalidIteratorType, interfaceIt)
} else if it.m != m {
return ErrInvalidIteratorMap
} else if !it.bof && it.cursor == nil {
return ErrInvalidIteratorPos
} else {
if !it.bof {
// validate if the iterator is still at a valid entry
itKey := it.Key()
elems, pos, _ := m.getEntry(&itKey)
if pos < 0 || elems[pos] != it.cursor {
return fmt.Errorf("%w - iterator positioned at invalid entry", ErrInvalidIteratorPos)
}
// simple case, just overwrite
if it.Key() == key {
it.cursor.value = value
return nil
}
}
m.Delete(key)
m.length++
entry := &mapEntry[*K, V]{
key: &key,
value: value,
}
hashedKey := m.hasher(&key)
if elems, ok := m.m[hashedKey]; ok {
m.m[hashedKey] = append(elems, entry)
} else {
m.m[hashedKey] = []*mapEntry[*K, V]{entry}
}
if !it.bof {
entry.prev = it.cursor
entry.next = entry.prev.next
if entry.prev.next != nil {
entry.prev.next.prev = entry
}
entry.prev.next = entry
} else {
entry.prev = nil
entry.next = m.head
}
// update map head and tail
if m.head == nil {
m.head = entry
m.tail = entry
} else if it.bof {
m.head.prev = entry
m.head = entry
} else if m.tail == entry.prev {
m.tail = entry
}
return nil
}
}
func (m *OMapLinkedHash[K, V]) getEntry(key *K) ([]*mapEntry[*K, V], int, uint32) {
var elems []*mapEntry[*K, V]
var ok bool
hashedKey := m.hasher(key)
if elems, ok = m.m[hashedKey]; ok {
for i, entry := range elems {
if *entry.key == *key {
return elems, i, hashedKey
}
}
}
// not found
return elems, -1, hashedKey
}
func (m *OMapLinkedHash[K, V]) Get(key K) (V, bool) {
elems, pos, _ := m.getEntry(&key)
if pos >= 0 {
return elems[pos].value, true
} else {
var val V
return val, false
}
}
func (m *OMapLinkedHash[K, V]) GetIteratorAt(key K) OMapIterator[K, V] {
elems, pos, _ := m.getEntry(&key)
if pos >= 0 {
return &OMapLinkedHashIterator[K, V]{m: m, cursor: elems[pos], bof: false}
} else {
return &OMapLinkedHashIterator[K, V]{m: m, cursor: nil, bof: false}
}
}
func (m *OMapLinkedHash[K, V]) Delete(key K) {
elems, pos, hashedKey := m.getEntry(&key)
if pos >= 0 {
entry := elems[pos]
if entry != nil {
if m.head == entry {
m.head = entry.next
}
if m.tail == entry {
m.tail = entry.prev
}
if entry.prev != nil {
entry.prev.next = entry.next
}
if entry.next != nil {
entry.next.prev = entry.prev
}
m.m[hashedKey] = append(elems[0:pos], elems[pos+1:]...)
m.length--
}
}
}
func (m *OMapLinkedHash[K, V]) Iterator() OMapIterator[K, V] {
return &OMapLinkedHashIterator[K, V]{m: m, cursor: m.head, bof: true}
}
func (m *OMapLinkedHash[K, V]) Len() int {
return m.length
}
// Implement fmt.Stringer
func (m *OMapLinkedHash[K, V]) String() string {
return IteratorToString[K, V]("omap.OMapLinkedHash", m.Iterator())
}
// Implement json.Marshaler interface.
func (m OMapLinkedHash[K, V]) MarshalJSON() ([]byte, error) {
buffer, err := MarshalJSON(m.Iterator())
return buffer, err
}
// Implement json.Unmarshaler interface.
func (m *OMapLinkedHash[K, V]) UnmarshalJSON(b []byte) error {
m.init()
return UnmarshalJSON[K, V](m.Put, b)
}
func (it *OMapLinkedHashIterator[K, V]) Next() bool {
if !it.bof {
it.cursor = it.cursor.next
} else {
it.bof = false
}
return it.cursor != nil
}
func (it *OMapLinkedHashIterator[K, V]) EOF() bool {
return !it.bof && it.cursor == nil
}
func (it *OMapLinkedHashIterator[K, V]) Key() K {
return *it.cursor.key
}
func (it *OMapLinkedHashIterator[K, V]) Value() V {
return it.cursor.value
}
func (it *OMapLinkedHashIterator[K, V]) IsValid() bool {
return !it.bof && it.cursor != nil
}
func (it *OMapLinkedHashIterator[K, V]) MoveFront() OMapIterator[K, V] {
it.bof = true
it.cursor = it.m.head
return it
}
func (it *OMapLinkedHashIterator[K, V]) MoveBack() OMapIterator[K, V] {
it.bof = false
it.cursor = nil
return it
}
func (it *OMapLinkedHashIterator[K, V]) Prev() bool {
if it.bof {
return false
} else if it.cursor == nil {
it.cursor = it.m.tail
} else {
it.cursor = it.cursor.prev
}
if it.cursor == nil {
it.bof = true
}
return it.IsValid()
}