-
Notifications
You must be signed in to change notification settings - Fork 662
/
linkedhashmap.go
197 lines (162 loc) · 4.37 KB
/
linkedhashmap.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
// Copyright (C) 2019-2022, Ava Labs, Inte. All rights reserved.
// See the file LICENSE for licensing terms.
package linkedhashmap
import (
"container/list"
"sync"
"github.com/ava-labs/avalanchego/ids"
)
// Hashmap provides an O(1) mapping from a comparable key to any value.
// Comparable is defined by https://golang.org/ref/spec#Comparison_operators.
type Hashmap interface {
Put(key, val interface{})
Get(key interface{}) (val interface{}, exists bool)
Delete(key interface{})
Len() int
}
// LinkedHashmap is a hashmap that keeps track of the oldest pairing an the
// newest pairing.
type LinkedHashmap interface {
Hashmap
Oldest() (key interface{}, val interface{}, exists bool)
Newest() (key interface{}, val interface{}, exists bool)
NewIterator() Iter
}
// Iterates over the keys and values in a LinkedHashmap
// from oldest to newest elements.
// Assumes the underlying LinkedHashmap is not modified while
// the iterator is in use, except to delete elements that
// have already been iterated over.
type Iter interface {
Next() bool
Key() interface{}
Value() interface{}
}
type keyValue struct {
key interface{}
value interface{}
}
type linkedHashmap struct {
lock sync.RWMutex
entryMap map[interface{}]*list.Element
entryList *list.List
}
func New() LinkedHashmap {
return &linkedHashmap{
entryMap: make(map[interface{}]*list.Element),
entryList: list.New(),
}
}
func (lh *linkedHashmap) Put(key, val interface{}) {
lh.lock.Lock()
defer lh.lock.Unlock()
lh.put(key, val)
}
func (lh *linkedHashmap) Get(key interface{}) (interface{}, bool) {
lh.lock.Lock()
defer lh.lock.Unlock()
return lh.get(key)
}
func (lh *linkedHashmap) Delete(key interface{}) {
lh.lock.Lock()
defer lh.lock.Unlock()
lh.delete(key)
}
func (lh *linkedHashmap) Len() int {
lh.lock.Lock()
defer lh.lock.Unlock()
return lh.len()
}
func (lh *linkedHashmap) Oldest() (interface{}, interface{}, bool) {
lh.lock.Lock()
defer lh.lock.Unlock()
return lh.oldest()
}
func (lh *linkedHashmap) Newest() (interface{}, interface{}, bool) {
lh.lock.Lock()
defer lh.lock.Unlock()
return lh.newest()
}
func (lh *linkedHashmap) put(key, value interface{}) {
if e, ok := lh.entryMap[key]; ok {
lh.entryList.MoveToBack(e)
e.Value = keyValue{
key: key,
value: value,
}
} else {
lh.entryMap[key] = lh.entryList.PushBack(keyValue{
key: key,
value: value,
})
}
}
func (lh *linkedHashmap) get(key interface{}) (interface{}, bool) {
if e, ok := lh.entryMap[key]; ok {
return e.Value.(keyValue).value, true
}
return nil, false
}
func (lh *linkedHashmap) delete(key interface{}) {
if e, ok := lh.entryMap[key]; ok {
lh.entryList.Remove(e)
delete(lh.entryMap, key)
}
}
func (lh *linkedHashmap) len() int { return len(lh.entryMap) }
func (lh *linkedHashmap) oldest() (interface{}, interface{}, bool) {
if val := lh.entryList.Front(); val != nil {
return val.Value.(keyValue).key, val.Value.(keyValue).value, true
}
return nil, nil, false
}
func (lh *linkedHashmap) newest() (interface{}, interface{}, bool) {
if val := lh.entryList.Back(); val != nil {
return val.Value.(keyValue).key, val.Value.(keyValue).value, true
}
return nil, nil, false
}
func (lh *linkedHashmap) NewIterator() Iter {
return &iterator{lh: lh}
}
type iterator struct {
lh *linkedHashmap
key interface{}
value interface{}
next *list.Element
initialized, exhausted bool
}
func (it *iterator) Next() bool {
// If the iterator has been exhausted, there is no next value.
if it.exhausted {
it.key = ids.Empty
it.value = nil
it.next = nil
return false
}
it.lh.lock.RLock()
defer it.lh.lock.RUnlock()
// 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 = ids.Empty
it.value = nil
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.(keyValue).key
it.value = it.next.Value.(keyValue).value
it.next = it.next.Next() // Next time, return next element
it.exhausted = it.next == nil
return true
}
func (it *iterator) Key() interface{} { return it.key }
func (it *iterator) Value() interface{} { return it.value }