-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathordered_map.go
More file actions
308 lines (282 loc) · 7.59 KB
/
ordered_map.go
File metadata and controls
308 lines (282 loc) · 7.59 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
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
package ordered
import (
"bytes"
"container/list"
"encoding"
"encoding/gob"
"encoding/json"
"errors"
"fmt"
"strings"
"github.com/buger/jsonparser"
)
type valuePair[V any] struct {
elem *list.Element
value V
}
// KeyValue represents a map elements as a key-value pair.
type KeyValue[K comparable, V any] struct {
Key K
Value V
}
// Map represents an ordered map which is an extension of hashmap.
// Unlike hashmap, the ordered map maintains the insertion order
// i.e. the order in which the keys and their mapped values are
// inserted in the map. The insertion order is not changed if a key
// which already exists in the map is re-inserted.
type Map[K comparable, V any] struct {
mp map[K]*valuePair[V]
items *list.List
}
// NewMap initializes an ordered map.
func NewMap[K comparable, V any]() *Map[K, V] {
return &Map[K, V]{
mp: make(map[K]*valuePair[V]),
items: list.New(),
}
}
// NewMapWithCapacity initializes an ordered map with the given
// initial capacity.
func NewMapWithCapacity[K comparable, V any](capacity int) *Map[K, V] {
return &Map[K, V]{
mp: make(map[K]*valuePair[V], capacity),
items: list.New(),
}
}
// NewMapWithKVs initializes an ordered map and inserts the given key-value pair
// in the map.
func NewMapWithKVs[K comparable, V any](kvs ...KeyValue[K, V]) *Map[K, V] {
om := NewMapWithCapacity[K, V](len(kvs))
for _, kv := range kvs {
om.Put(kv.Key, kv.Value)
}
return om
}
// Put inserts a key and its mapped value in the map. If the key already exists, the
// mapped value is replaced by the new value.
func (o *Map[K, V]) Put(key K, value V) {
if _, ok := o.mp[key]; !ok {
e := o.items.PushBack(key)
o.mp[key] = &valuePair[V]{elem: e, value: value}
} else {
o.mp[key].value = value
}
}
// Get returns the mapped value for the given key and a bool indicating
// whether the key exists or not.
func (o *Map[K, V]) Get(key K) (V, bool) {
if val, ok := o.mp[key]; ok {
return val.value, true
}
var dummy V
return dummy, false
}
// GetOrDefault returns the mapped value for the given key if it exists.
// Otherwise, it returns the default value.
func (o *Map[K, V]) GetOrDefault(key K, defaultValue V) V {
if val, ok := o.mp[key]; ok {
return val.value
}
return defaultValue
}
// ContainsKey checks if the map contains a mapping for the given key.
func (o *Map[K, V]) ContainsKey(key K) bool {
_, ok := o.mp[key]
return ok
}
// Remove removes the key with its mapped value from the map and returns
// the value if the key exists.
func (o *Map[K, V]) Remove(key K) V {
if vp, ok := o.mp[key]; ok {
value := vp.value
o.items.Remove(vp.elem)
delete(o.mp, key)
return value
}
var dummy V
return dummy
}
// Len returns the number of elements in the map.
func (o *Map[K, V]) Len() int {
return o.items.Len()
}
// Keys returns all the keys from the map according to their insertion order.
// The first element of the slice is the oldest key in the map.
func (o *Map[K, V]) Keys() []K {
keys := make([]K, o.items.Len())
idx := 0
for e := o.items.Front(); e != nil; e = e.Next() {
keys[idx] = e.Value.(K)
idx++
}
return keys
}
// Values returns all the values from the map according to their insertion order.
// The first element of the slice is the oldest value in the map.
func (o *Map[K, V]) Values() []V {
values := make([]V, o.items.Len())
idx := 0
for e := o.items.Front(); e != nil; e = e.Next() {
key := e.Value.(K)
if vp, ok := o.mp[key]; ok {
values[idx] = vp.value
idx++
}
}
return values
}
// KeyValues returns all the keys and values from the map according to their
// insertion order. The first element of the slice is the oldest key and value
// in the map.
func (o *Map[K, V]) KeyValues() []KeyValue[K, V] {
kvs := make([]KeyValue[K, V], o.items.Len())
idx := 0
for e := o.items.Front(); e != nil; e = e.Next() {
key := e.Value.(K)
if vp, ok := o.mp[key]; ok {
kvs[idx] = KeyValue[K, V]{Key: key, Value: vp.value}
idx++
}
}
return kvs
}
// ForEach invokes the given function f for each element of the map.
func (o *Map[K, V]) ForEach(f func(K, V)) {
for _, kv := range o.KeyValues() {
f(kv.Key, kv.Value)
}
}
// IsEmpty checks whether the map is empty or not.
func (o *Map[K, V]) IsEmpty() bool {
return len(o.mp) == 0
}
// Clear removes all the keys and their mapped values from the map.
func (o *Map[K, V]) Clear() {
for k := range o.mp {
delete(o.mp, k)
}
var next *list.Element
for e := o.items.Front(); e != nil; e = next {
next = e.Next()
o.items.Remove(e)
}
}
// String returns the string representation of the map.
func (o *Map[K, V]) String() string {
var sb strings.Builder
sb.WriteString("map{")
for idx, kv := range o.KeyValues() {
if idx > 0 {
sb.WriteByte(' ')
}
sb.WriteString(fmt.Sprint(kv.Key))
sb.WriteByte(':')
sb.WriteString(fmt.Sprint(kv.Value))
}
sb.WriteByte('}')
return sb.String()
}
// MarshalJSON implements json.Marshaler interface.
func (o Map[K, V]) MarshalJSON() ([]byte, error) {
var buf bytes.Buffer
buf.WriteByte('{')
for idx, kv := range o.KeyValues() {
if idx > 0 {
buf.WriteByte(',')
}
// key type must either be a string, an integer type, or implement encoding.TextMarshaler
switch any(kv.Key).(type) {
case string, encoding.TextMarshaler:
keyBytes, err := json.Marshal(kv.Key)
if err != nil {
return nil, err
}
buf.Write(keyBytes)
case int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64:
var keyBytes bytes.Buffer
b, _ := json.Marshal(kv.Key) // marshalling int/uint does not generate error
keyBytes.WriteByte('"')
keyBytes.Write(b)
keyBytes.WriteByte('"')
buf.Write(keyBytes.Bytes())
default:
return nil, errors.New("invalid key type")
}
buf.WriteByte(':')
valBytes, err := json.Marshal(kv.Value)
if err != nil {
return nil, err
}
buf.Write(valBytes)
}
buf.WriteByte('}')
return buf.Bytes(), nil
}
// UnmarshalJSON implements json.Unmarshaler interface.
func (o *Map[K, V]) UnmarshalJSON(b []byte) error {
if o.items == nil || o.mp == nil {
o.mp = make(map[K]*valuePair[V])
o.items = list.New()
}
return jsonparser.ObjectEach(b, func(key []byte, value []byte, dataType jsonparser.ValueType, offset int) error {
var k K
// key type must either be a string, an integer type, or implement encoding.TextMarshaler
switch any(k).(type) {
case string, int, int8, int16, int32, int64, uint, uint8, uint16, uint32, uint64, encoding.TextMarshaler:
if err := json.Unmarshal([]byte(fmt.Sprintf("\"%s\"", string(key))), &k); err != nil {
return err
}
default:
return errors.New("invalid key type")
}
var v V
var valBytes []byte
if dataType == jsonparser.String {
valBytes = []byte(fmt.Sprintf("\"%s\"", string(value)))
} else {
valBytes = value
}
if err := json.Unmarshal(valBytes, &v); err != nil {
return err
}
o.Put(k, v)
return nil
})
}
// GobEncode implements gob.GobEncoder interface.
func (o Map[K, V]) GobEncode() ([]byte, error) {
var buf bytes.Buffer
enc := gob.NewEncoder(&buf)
enc.Encode(o.Len())
for _, kv := range o.KeyValues() {
if err := enc.Encode(kv.Key); err != nil {
return nil, err
}
if err := enc.Encode(kv.Value); err != nil {
return nil, err
}
}
return buf.Bytes(), nil
}
// GobDecode implements gob.GobDecoder interface.
func (o *Map[K, V]) GobDecode(b []byte) error {
if o.items == nil || o.mp == nil {
o.mp = make(map[K]*valuePair[V])
o.items = list.New()
}
dec := gob.NewDecoder(bytes.NewBuffer(b))
len := 0
dec.Decode(&len)
for i := 0; i < len; i++ {
var k K
var v V
if err := dec.Decode(&k); err != nil {
return err
}
if err := dec.Decode(&v); err != nil {
return err
}
o.Put(k, v)
}
return nil
}