-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathorderedmap.go
226 lines (202 loc) · 6.12 KB
/
orderedmap.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
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
package container
import "fmt"
// OrderedMap is a hash map with the preservation of insertion order
type OrderedMap[K comparable, V any] struct {
mp map[K]*Element[Pair[K,V]]
list *List[Pair[K,V]]
}
// NewOrderedMap returns a new OrderedMap object
func NewOrderedMap[K comparable, V any]() *OrderedMap[K,V] {
return &OrderedMap[K,V]{
mp: make(map[K]*Element[Pair[K,V]]),
list: NewList[Pair[K,V]](),
}
}
// MustGet returns the value of the given key if the key exists,
// otherwise it returns the zero-value of V
func (m *OrderedMap[K,V]) MustGet(k K) (rVal V) {
if v, ok := m.Get(k); ok {
rVal = v
}
return
}
// Get returns the value of given key and if the key exists
// if the keys doesn't exists, returned value is the zero-value of V
func (m *OrderedMap[K,V]) Get(k K) (V, bool) {
var v V
if e, ok := m.mp[k]; ok {
v = e.Value.Value
return v, true
}
return v, false
}
// Has the given key in the OrderedMap
func (m *OrderedMap[K,V]) Has(k K) bool {
_, ok := m.mp[k]
return ok
}
// Insert a key-value pair into the OrderedMap
func (m *OrderedMap[K,V]) Insert(k K, v V) {
if m.Has(k) {
m.mp[k].Value = Pair[K,V]{Key: k, Value: v}
} else {
pair := Pair[K,V]{Key: k, Value: v}
e := m.list.PushBack(pair)
m.mp[k] = e
}
}
// Remove a key-value pair with given key, returns the value and if the key exists.
// If the key doesn't exist, returned value is the zero-value of V
func (m *OrderedMap[K,V]) Remove(k K) (v V, ok bool) {
if !m.Has(k) {
return
}
e := m.mp[k]
m.list.Remove(e)
delete(m.mp, k)
v, ok = e.Value.Value, true
return
}
// Clear all elements in the OrderedMap
func (m *OrderedMap[K,V]) Clear() {
m.list.Clear()
for k := range m.mp {
delete(m.mp, k)
}
}
// Move to an element with given key to the tail of the OrderedMap
// the given key must exist in the OrderedMap
func (m *OrderedMap[K,V]) MoveToBack(k K) {
if !m.Has(k) {
panic(fmt.Sprintf("Key %v does not in the map", k))
}
e := m.mp[k]
m.list.MoveToBack(e)
}
// Move to an element with given key to the head of the OrderedMap
// the given key must exist in the OrderedMap
func (m *OrderedMap[K,V]) MoveToFront(k K) {
if !m.Has(k) {
panic(fmt.Sprintf("Key %v does not in the map", k))
}
e := m.mp[k]
m.list.MoveToFront(e)
}
// Removes the tail element of the OrderedMap and returns the key-value pair
// and if the tail element exists.
// The tail element doesn't exist if and only if the OrderedMap is empty.
func (m *OrderedMap[K,V]) PopBack() (k K, v V, ok bool) {
e := m.list.Back()
if e == nil {
return
}
k, v, ok = e.Value.Key, e.Value.Value, true
delete(m.mp, k)
m.list.Remove(e)
return
}
// Removes the head element of the OrderedMap and returns the key-value pair
// and if the head element exists.
// The head element doesn't exist if and only if the OrderedMap is empty.
func (m *OrderedMap[K,V]) PopFront() (k K, v V, ok bool) {
e := m.list.Front()
if e == nil {
return
}
k, v, ok = e.Value.Key, e.Value.Value, true
delete(m.mp, k)
m.list.Remove(e)
return
}
// Len returns the size of the OrderedMap
func (m *OrderedMap[K,V]) Len() int {
return len(m.mp)
}
/////////////////////////////
///////// Testing ///////////
/////////////////////////////
func checkOMapSize(m *OrderedMap[string, int], expect int) {
if len(m.mp) != m.list.Len() {
panic(fmt.Sprintf("The length of map and list do not match, got map len %d, list len %d", len(m.mp), m.list.Len()))
}
if m.Len() != expect {
panic(fmt.Sprintf("Len check failed, got %d, expect, %d", m.Len(), expect))
}
}
func checkOMapOrder(m *OrderedMap[string, int], expKeys []string, expVals []int) {
checkOMapSize(m, len(expKeys))
for i, e1 := 0, m.list.Front(); e1 != nil; i, e1 = i+1, e1.Next() {
k, v := expKeys[i], expVals[i]
if e2, ok := m.mp[k]; !ok || e1 != e2 {
if !ok {
panic(fmt.Sprintf("Key %v should be in map but missed", k))
}
if e1 != e2 {
panic(fmt.Sprintf("map and list point to different elements for key %v", k))
}
}
if k != e1.Value.Key || v != e1.Value.Value {
panic(fmt.Sprintf("element entry failed, got (k,v): (%v,%v), expected (%v,%v)", e1.Value.Key, e1.Value.Value, k, v))
}
}
}
func checkOMapKV(gotK string, gotV int, gotOk bool, expK string, expV int, expOk bool) {
if gotK != expK || gotV != expV || gotOk != expOk {
panic(fmt.Sprintf("k,v,ok does not match where key:(%v,%v), val:(%v,%v), ok:(%v,%v)",
gotK, expK, gotV, expV, gotOk, expOk))
}
}
func testOrderedMap() {
var k string
var v int
var ok bool
m := NewOrderedMap[string, int]()
checkOMapOrder(m, []string{}, []int{})
// single element
m.Insert("apple", 1)
checkOMapOrder(m, []string{"apple"}, []int{1})
ok = m.Has("apple")
checkOMapKV(k, v, ok, k, v, true)
v, ok = m.Get("apple")
checkOMapKV(k, v, ok, k, 1, true)
m.Insert("apple", 2)
checkOMapOrder(m, []string{"apple"}, []int{2})
v, ok = m.Get("apple")
checkOMapKV(k, v, ok, k, 2, true)
v, ok = m.Get("banana")
checkOMapKV(k, v, ok, k, 0, false)
m.MoveToFront("apple")
checkOMapOrder(m, []string{"apple"}, []int{2})
m.MoveToBack("apple")
checkOMapOrder(m, []string{"apple"}, []int{2})
v, ok = m.Remove("apple")
checkOMapKV(k, v, ok, k, 2, true)
checkOMapOrder(m, []string{}, []int{})
v, ok = m.Get("apple")
checkOMapKV(k, v, ok, k, 0, false)
ok = m.Has("banana")
checkOMapKV(k, v, ok, k, v, false)
// multiple elements
m.Insert("apple", 1)
m.Insert("banana", 2)
m.Insert("cherry", 3)
checkOMapOrder(m, []string{"apple", "banana", "cherry"}, []int{1,2,3})
m.MoveToBack("banana")
checkOMapOrder(m, []string{"apple", "cherry", "banana"}, []int{1,3,2})
m.MoveToFront("cherry")
checkOMapOrder(m, []string{"cherry", "apple", "banana"}, []int{3,1,2})
m.Insert("cherry", -1)
checkOMapOrder(m, []string{"cherry", "apple", "banana"}, []int{-1,1,2})
k, v, ok = m.PopFront()
checkOMapKV(k, v, ok, "cherry", -1, true)
checkOMapOrder(m, []string{"apple", "banana"}, []int{1,2})
m.Insert("cherry", 3)
checkOMapOrder(m, []string{"apple", "banana", "cherry"}, []int{1,2,3})
k, v, ok = m.PopBack()
checkOMapKV(k, v, ok, "cherry", 3, true)
checkOMapOrder(m, []string{"apple", "banana"}, []int{1,2})
v, ok = m.Remove("banana")
checkOMapKV(k, v, ok, k, 2, true)
m.Clear()
checkOMapOrder(m, []string{}, []int{})
}