-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathlinkedhashmap.go
More file actions
160 lines (138 loc) · 2.86 KB
/
linkedhashmap.go
File metadata and controls
160 lines (138 loc) · 2.86 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
package set
import (
"fmt"
"hash/fnv"
"log"
)
type entry struct {
key interface{}
value interface{}
before *entry
after *entry
}
// linkedHashMap stores data in key-value pairs while maintaining insertion order
//
// - Uses a doubly linked list to maintain insertion order
type linkedHashMap struct {
table map[uint64]*entry
header *entry
last *entry
currentLength int
}
// Length returns the length of the linked hash map
func (l *linkedHashMap) Length() int {
return l.currentLength
}
// Put puts a new entry into the linked hash map
func (l *linkedHashMap) Put(key, value interface{}) {
if key == nil {
return
}
hash := l.hash(key)
newEntry := &entry{
key: key,
value: value,
}
if _, ok := l.table[hash]; ok {
return
}
l.table[hash] = newEntry
l.currentLength++
// put to first position
if l.header == nil {
l.header = newEntry
l.last = newEntry
return
}
// put to last position
l.last.after = newEntry
newEntry.before = l.last
l.last = newEntry
}
// Get gets an entry from the linked hash map
func (l *linkedHashMap) Get(key interface{}) (value interface{}, found bool) {
hash := l.hash(key)
if _, ok := l.table[hash]; !ok {
return nil, false
}
tmp := l.table[hash]
for tmp != nil {
if tmp.key == key {
return tmp.value, true
}
tmp = tmp.after
}
return nil, false
}
// Remove removes an entry from the linked hash map
func (l *linkedHashMap) Remove(key interface{}) bool {
hash := l.hash(key)
if _, ok := l.table[hash]; !ok {
return false
}
current := l.table[hash]
switch current.key {
// first entry
case l.header.key:
if l.header == l.last {
l.header = nil
l.last = nil
} else {
l.header = l.header.after
l.header.before = nil
}
// last entry
case l.last.key:
if l.header == l.last {
l.header = nil
l.last = nil
} else {
l.last = l.last.before
l.last.after = nil
}
// arbitrary entry
default:
var shouldNotUpdate bool
innerCurrent := l.header
for innerCurrent.key != current.key {
if innerCurrent.after == nil {
shouldNotUpdate = true
break
}
innerCurrent = innerCurrent.after
}
if !shouldNotUpdate {
innerCurrent.before.after = innerCurrent.after
innerCurrent.after.before = innerCurrent.before
}
}
delete(l.table, hash)
l.currentLength--
return true
}
// Iter iterates over each entry of the linked hash map
func (l *linkedHashMap) Iter() <-chan *entry {
ch := make(chan *entry, l.currentLength)
go func() {
current := l.header
for current != nil {
ch <- current
current = current.after
}
close(ch)
}()
return ch
}
func (l *linkedHashMap) hash(key interface{}) uint64 {
h := fnv.New64()
_, err := h.Write([]byte(fmt.Sprintf("%#v", key)))
if err != nil {
log.Print(err)
}
return h.Sum64()
}
func newLinkedHashMap() *linkedHashMap {
return &linkedHashMap{
table: make(map[uint64]*entry),
}
}