forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 4
/
hashmap.go
166 lines (143 loc) · 3.94 KB
/
hashmap.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
// Copyright (C) 2019-2024, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package linked
import "github.com/MetalBlockchain/metalgo/utils"
type keyValue[K, V any] struct {
key K
value V
}
// Hashmap provides an ordered O(1) mapping from keys to values.
//
// Entries are tracked by insertion order.
type Hashmap[K comparable, V any] struct {
entryMap map[K]*ListElement[keyValue[K, V]]
entryList *List[keyValue[K, V]]
freeList []*ListElement[keyValue[K, V]]
}
func NewHashmap[K comparable, V any]() *Hashmap[K, V] {
return NewHashmapWithSize[K, V](0)
}
func NewHashmapWithSize[K comparable, V any](initialSize int) *Hashmap[K, V] {
lh := &Hashmap[K, V]{
entryMap: make(map[K]*ListElement[keyValue[K, V]], initialSize),
entryList: NewList[keyValue[K, V]](),
freeList: make([]*ListElement[keyValue[K, V]], initialSize),
}
for i := range lh.freeList {
lh.freeList[i] = &ListElement[keyValue[K, V]]{}
}
return lh
}
func (lh *Hashmap[K, V]) Put(key K, value V) {
if e, ok := lh.entryMap[key]; ok {
lh.entryList.MoveToBack(e)
e.Value = keyValue[K, V]{
key: key,
value: value,
}
return
}
var e *ListElement[keyValue[K, V]]
if numFree := len(lh.freeList); numFree > 0 {
numFree--
e = lh.freeList[numFree]
lh.freeList = lh.freeList[:numFree]
} else {
e = &ListElement[keyValue[K, V]]{}
}
e.Value = keyValue[K, V]{
key: key,
value: value,
}
lh.entryMap[key] = e
lh.entryList.PushBack(e)
}
func (lh *Hashmap[K, V]) Get(key K) (V, bool) {
if e, ok := lh.entryMap[key]; ok {
return e.Value.value, true
}
return utils.Zero[V](), false
}
func (lh *Hashmap[K, V]) Delete(key K) bool {
e, ok := lh.entryMap[key]
if ok {
lh.remove(e)
}
return ok
}
func (lh *Hashmap[K, V]) Clear() {
for _, e := range lh.entryMap {
lh.remove(e)
}
}
// remove assumes that [e] is currently in the Hashmap.
func (lh *Hashmap[K, V]) remove(e *ListElement[keyValue[K, V]]) {
delete(lh.entryMap, e.Value.key)
lh.entryList.Remove(e)
e.Value = keyValue[K, V]{} // Free the key value pair
lh.freeList = append(lh.freeList, e)
}
func (lh *Hashmap[K, V]) Len() int {
return len(lh.entryMap)
}
func (lh *Hashmap[K, V]) Oldest() (K, V, bool) {
if e := lh.entryList.Front(); e != nil {
return e.Value.key, e.Value.value, true
}
return utils.Zero[K](), utils.Zero[V](), false
}
func (lh *Hashmap[K, V]) Newest() (K, V, bool) {
if e := lh.entryList.Back(); e != nil {
return e.Value.key, e.Value.value, true
}
return utils.Zero[K](), utils.Zero[V](), false
}
func (lh *Hashmap[K, V]) NewIterator() *Iterator[K, V] {
return &Iterator[K, V]{lh: lh}
}
// Iterates over the keys and values in a LinkedHashmap from oldest to newest.
// Assumes the underlying LinkedHashmap is not modified while the iterator is in
// use, except to delete elements that have already been iterated over.
type Iterator[K comparable, V any] struct {
lh *Hashmap[K, V]
key K
value V
next *ListElement[keyValue[K, V]]
initialized, exhausted bool
}
func (it *Iterator[K, V]) Next() bool {
// If the iterator has been exhausted, there is no next value.
if it.exhausted {
it.key = utils.Zero[K]()
it.value = utils.Zero[V]()
it.next = nil
return false
}
// If the iterator was not yet initialized, do it now.
if !it.initialized {
it.initialized = true
oldest := it.lh.entryList.Front()
if oldest == nil {
it.exhausted = true
it.key = utils.Zero[K]()
it.value = utils.Zero[V]()
it.next = nil
return false
}
it.next = oldest
}
// It's important to ensure that [it.next] is not nil
// by not deleting elements that have not yet been iterated
// over from [it.lh]
it.key = it.next.Value.key
it.value = it.next.Value.value
it.next = it.next.Next() // Next time, return next element
it.exhausted = it.next == nil
return true
}
func (it *Iterator[K, V]) Key() K {
return it.key
}
func (it *Iterator[K, V]) Value() V {
return it.value
}