This repository was archived by the owner on Nov 22, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathintlinkedhashmap.go
More file actions
340 lines (308 loc) · 8.4 KB
/
intlinkedhashmap.go
File metadata and controls
340 lines (308 loc) · 8.4 KB
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
331
332
333
334
335
336
337
338
339
340
// Portions of this file are based on Apache Commons Collections,
// licensed under the Apache License, Version 2.0.
package intlinkedhashmap
import (
"iter"
"math"
"github.com/bibenga/orderedmap"
"github.com/bibenga/orderedmap/internal/hash"
"github.com/bibenga/orderedmap/internal/utils"
)
type intLinkedHashMapEntry[K orderedmap.Integer, V any] struct {
hashCode uint32
key K
value V
next *intLinkedHashMapEntry[K, V]
before *intLinkedHashMapEntry[K, V]
after *intLinkedHashMapEntry[K, V]
}
type IntLinkedHashMap[K orderedmap.Integer, V any] struct {
modCount uint64
data []*intLinkedHashMapEntry[K, V]
header *intLinkedHashMapEntry[K, V]
size int
loadFactor float64
threshold int
}
var _ orderedmap.Map[int, int] = &IntLinkedHashMap[int, int]{}
// New creates a new IntLinkedHashMap with the specified
// initial capacity and load factor. It supports generic integer keys (K)
// and values of any type (V). The map preserves insertion order.
//
// Parameters:
// - capacity: initial size of the map; use DefaultCapacity if unsure.
// - loadFactor: determines when the map should resize; typically 0.75.
//
// Returns:
// - a new Map[K,V] instance.
func New[K orderedmap.Integer, V any](capacity int, loadFactor float64) *IntLinkedHashMap[K, V] {
if capacity < 0 {
panic("initial capacity must be a non negative number")
}
if loadFactor <= 0.0 || math.IsNaN(loadFactor) {
panic("load factor must be greater than 0")
}
m := IntLinkedHashMap[K, V]{
loadFactor: loadFactor,
}
initialCapacity := m.calculateNewCapacity(capacity)
m.threshold = m.calculateThreshold(initialCapacity, m.loadFactor)
m.data = make([]*intLinkedHashMapEntry[K, V], initialCapacity)
var emptyKey K
var emptyValue V
m.header = m.createEntry(nil, 0, emptyKey, emptyValue)
m.header.before = m.header
m.header.after = m.header
return &m
}
// NewDefault creates a new IntLinkedHashMap with default
// capacity and load factor. It supports generic integer keys (K) and values
// of any type (V). Useful for quick initialization when defaults are sufficient.
//
// Returns:
// - a new Map[K,V] instance.
func NewDefault[K orderedmap.Integer, V any]() orderedmap.Map[K, V] {
return New[K, V](orderedmap.DefaultCapacity, orderedmap.DefaultLoadFactor)
}
func (m *IntLinkedHashMap[K, V]) calculateNewCapacity(proposedCapacity int) int {
newCapacity := 1
if proposedCapacity > orderedmap.MaxCapacity {
newCapacity = orderedmap.MaxCapacity
} else {
for newCapacity < proposedCapacity {
newCapacity <<= 1 // multiply by two
}
if newCapacity > orderedmap.MaxCapacity {
newCapacity = orderedmap.MaxCapacity
}
}
return newCapacity
}
func (m *IntLinkedHashMap[K, V]) calculateThreshold(newCapacity int, factor float64) int {
return int(float64(newCapacity) * factor)
}
func (m *IntLinkedHashMap[K, V]) Size() int {
return m.size
}
func (m *IntLinkedHashMap[K, V]) IsEmpty() bool {
return m.size == 0
}
func (m *IntLinkedHashMap[K, V]) Clear() {
// m.modCount++
m.modCount++
entry := m.header.after
for entry != m.header {
toDestory := entry
entry = entry.after
m.destroyEntry(toDestory)
}
clear(m.data)
m.size = 0
m.header.before = m.header
m.header.after = m.header
}
func (m *IntLinkedHashMap[K, V]) Clone() orderedmap.Map[K, V] {
cm := New[K, V](m.size, m.loadFactor)
for k, v := range m.Items() {
cm.Put(k, v)
}
return cm
}
func (m *IntLinkedHashMap[K, V]) Put(key K, value V) (V, bool) {
hashCode := hash.MapHash32(hash.IntHash32(key))
hashIndex := hash.MapHashIndex(hashCode, len(m.data))
entry := m.data[hashIndex]
for entry != nil {
// if hashCode == entry.hashCode && && key == entry.key {
if key == entry.key {
oldValue := entry.value
entry.value = value
return oldValue, true
}
entry = entry.next
}
m.addMapping(hashIndex, hashCode, key, value)
var empty V
return empty, false
}
func (m *IntLinkedHashMap[K, V]) addMapping(hashIndex int, hashCode uint32, key K, value V) {
m.modCount++
entry := m.createEntry(m.data[hashIndex], hashCode, key, value)
m.addEntry(entry, hashIndex)
m.size++
m.checkCapacity()
}
func (m *IntLinkedHashMap[K, V]) addEntry(entry *intLinkedHashMapEntry[K, V], hashIndex int) {
entry.after = m.header
entry.before = m.header.before
m.header.before.after = entry
m.header.before = entry
m.data[hashIndex] = entry
}
func (m *IntLinkedHashMap[K, V]) createEntry(
next *intLinkedHashMapEntry[K, V],
hashCode uint32,
key K,
value V,
) *intLinkedHashMapEntry[K, V] {
return &intLinkedHashMapEntry[K, V]{
key: key,
value: value,
hashCode: hashCode,
next: next,
}
}
func (m *IntLinkedHashMap[K, V]) checkCapacity() {
if m.size >= m.threshold {
newCapacity := len(m.data) * 2
if newCapacity <= orderedmap.MaxCapacity {
m.ensureCapacity(newCapacity)
}
}
}
func (m *IntLinkedHashMap[K, V]) ensureCapacity(newCapacity int) {
oldCapacity := len(m.data)
if newCapacity <= oldCapacity {
return
}
if m.size == 0 {
m.threshold = m.calculateThreshold(newCapacity, m.loadFactor)
m.data = make([]*intLinkedHashMapEntry[K, V], newCapacity)
} else {
oldEntries := m.data
newEntries := make([]*intLinkedHashMapEntry[K, V], newCapacity)
m.modCount++
for i := oldCapacity - 1; i >= 0; i-- {
entry := oldEntries[i]
if entry != nil {
oldEntries[i] = nil // gc
for entry != nil {
next := entry.next
hashIndex := hash.MapHashIndex(entry.hashCode, newCapacity)
entry.next = newEntries[hashIndex]
newEntries[hashIndex] = entry
entry = next
}
}
}
m.threshold = m.calculateThreshold(newCapacity, m.loadFactor)
m.data = newEntries
}
}
func (m *IntLinkedHashMap[K, V]) ContainsKey(key K) bool {
_, found := m.Get(key)
return found
}
func (m *IntLinkedHashMap[K, V]) Get(key K) (V, bool) {
hashCode := hash.MapHash32(hash.IntHash32(key))
hashIndex := hash.MapHashIndex(hashCode, len(m.data))
entry := m.data[hashIndex]
for entry != nil {
// if hashCode == entry.hashCode && key == entry.key {
if key == entry.key {
return entry.value, true
}
entry = entry.next
}
var empty V
return empty, false
}
func (m *IntLinkedHashMap[K, V]) Delete(key K) (V, bool) {
hashCode := hash.MapHash32(hash.IntHash32(key))
hashIndex := hash.MapHashIndex(hashCode, len(m.data))
entry := m.data[hashIndex]
var previous *intLinkedHashMapEntry[K, V]
for entry != nil {
// if hashCode == entry.hashCode && && key == entry.key {
if key == entry.key {
oldValue := entry.value
m.removeMapping(entry, hashIndex, previous)
return oldValue, true
}
previous = entry
entry = entry.next
}
var empty V
return empty, false
}
func (m *IntLinkedHashMap[K, V]) removeMapping(
entry *intLinkedHashMapEntry[K, V],
hashIndex int,
previous *intLinkedHashMapEntry[K, V],
) {
m.modCount++
m.removeEntry(entry, hashIndex, previous)
m.size--
m.destroyEntry(entry)
}
func (m *IntLinkedHashMap[K, V]) removeEntry(
entry *intLinkedHashMapEntry[K, V],
hashIndex int,
previous *intLinkedHashMapEntry[K, V],
) {
var emptyKey K
var emptyValue V
entry.key = emptyKey
entry.value = emptyValue
entry.before.after = entry.after
entry.after.before = entry.before
entry.after = nil
entry.before = nil
if previous == nil {
m.data[hashIndex] = entry.next
} else {
previous.next = entry.next
}
entry.next = nil
}
func (m *IntLinkedHashMap[K, V]) destroyEntry(entry *intLinkedHashMapEntry[K, V]) {
entry.next = nil
}
func (m *IntLinkedHashMap[K, V]) Keys() iter.Seq[K] {
return func(yield func(K) bool) {
expectedModCount := m.modCount
entry := m.header.after
for entry != m.header {
if m.modCount != expectedModCount {
panic("concurrent modification")
}
if !yield(entry.key) {
return
}
entry = entry.after
}
}
}
func (m *IntLinkedHashMap[K, V]) Values() iter.Seq[V] {
return func(yield func(V) bool) {
expectedModCount := m.modCount
entry := m.header.after
for entry != m.header {
if m.modCount != expectedModCount {
panic("concurrent modification")
}
if !yield(entry.value) {
return
}
entry = entry.after
}
}
}
func (m *IntLinkedHashMap[K, V]) Items() iter.Seq2[K, V] {
return func(yield func(K, V) bool) {
expectedModCount := m.modCount
entry := m.header.after
for entry != m.header {
if m.modCount != expectedModCount {
panic("concurrent modification")
}
if !yield(entry.key, entry.value) {
return
}
entry = entry.after
}
}
}
func (m *IntLinkedHashMap[K, V]) String() string {
return utils.MapToString("IntLinkedHashMap", m)
}