-
Notifications
You must be signed in to change notification settings - Fork 658
/
linkedhashmap.go
122 lines (96 loc) · 2.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
// (c) 2019-2021, 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 an [ids.ID] to any value.
type Hashmap interface {
Put(key ids.ID, val interface{})
Get(key ids.ID) (val interface{}, exists bool)
Delete(key ids.ID)
Len() int
}
// LinkedHashmap is a hashmap that keeps track of the oldest pairing an the
// newest pairing.
type LinkedHashmap interface {
Hashmap
Oldest() (val interface{}, exists bool)
Newest() (val interface{}, exists bool)
}
type linkedHashmap struct {
lock sync.Mutex
entryMap map[ids.ID]*list.Element
entryList *list.List
}
func New() LinkedHashmap {
return &linkedHashmap{
entryMap: make(map[ids.ID]*list.Element),
entryList: list.New(),
}
}
func (lh *linkedHashmap) Put(key ids.ID, val interface{}) {
lh.lock.Lock()
defer lh.lock.Unlock()
lh.put(key, val)
}
func (lh *linkedHashmap) Get(key ids.ID) (interface{}, bool) {
lh.lock.Lock()
defer lh.lock.Unlock()
return lh.get(key)
}
func (lh *linkedHashmap) Delete(key ids.ID) {
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{}, bool) {
lh.lock.Lock()
defer lh.lock.Unlock()
return lh.oldest()
}
func (lh *linkedHashmap) Newest() (interface{}, bool) {
lh.lock.Lock()
defer lh.lock.Unlock()
return lh.newest()
}
func (lh *linkedHashmap) put(key ids.ID, value interface{}) {
if e, ok := lh.entryMap[key]; ok {
lh.entryList.MoveToBack(e)
e.Value = value
} else {
lh.entryMap[key] = lh.entryList.PushBack(value)
}
}
func (lh *linkedHashmap) get(key ids.ID) (interface{}, bool) {
if e, ok := lh.entryMap[key]; ok {
return e.Value, true
}
return nil, false
}
func (lh *linkedHashmap) delete(key ids.ID) {
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{}, bool) {
if val := lh.entryList.Front(); val != nil {
return val.Value, true
}
return nil, false
}
func (lh *linkedHashmap) newest() (interface{}, bool) {
if val := lh.entryList.Back(); val != nil {
return val.Value, true
}
return nil, false
}