-
Notifications
You must be signed in to change notification settings - Fork 11
/
orderedmap.go
101 lines (84 loc) · 2.55 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
package css
import (
"container/list"
)
type Pair struct {
Key string
Value string
element *list.Element
}
type OrderedMap struct {
pairs map[string]*Pair
list *list.List
}
// New creates a new OrderedMap.
func NewOrderedMap() *OrderedMap {
return &OrderedMap{
pairs: make(map[string]*Pair),
list: list.New(),
}
}
// Get looks for the given key, and returns the value associated with it,
// or nil if not found. The boolean it returns says whether the key is present in the map.
func (om *OrderedMap) Get(key string) (string, bool) {
if pair, present := om.pairs[key]; present {
return pair.Value, present
}
return "", false
}
// Set sets the key-value pair, and returns what `Get` would have returned
// on that key prior to the call to `Set`.
func (om *OrderedMap) Set(key string, value string) (string, bool) {
if pair, present := om.pairs[key]; present {
oldValue := pair.Value
pair.Value = value
return oldValue, true
}
pair := &Pair{
Key: key,
Value: value,
}
pair.element = om.list.PushBack(pair)
om.pairs[key] = pair
return "", false
}
// Delete removes the key-value pair, and returns what `Get` would have returned
// on that key prior to the call to `Delete`.
func (om *OrderedMap) Delete(key string) (string, bool) {
if pair, present := om.pairs[key]; present {
om.list.Remove(pair.element)
delete(om.pairs, key)
return pair.Value, true
}
return "", false
}
// Len returns the length of the ordered map.
func (om *OrderedMap) Len() int {
return len(om.pairs)
}
// Oldest returns a pointer to the oldest pair. It's meant to be used to iterate on the ordered map's
// pairs from the oldest to the newest, e.g.:
// for pair := orderedMap.Oldest(); pair != nil; pair = pair.Next() { fmt.Printf("%v => %v\n", pair.Key, pair.Value) }
func (om *OrderedMap) Oldest() *Pair {
return listElementToPair(om.list.Front())
}
// Newest returns a pointer to the newest pair. It's meant to be used to iterate on the ordered map's
// pairs from the newest to the oldest, e.g.:
// for pair := orderedMap.Oldest(); pair != nil; pair = pair.Next() { fmt.Printf("%v => %v\n", pair.Key, pair.Value) }
func (om *OrderedMap) Newest() *Pair {
return listElementToPair(om.list.Back())
}
// Next returns a pointer to the next pair.
func (p *Pair) Next() *Pair {
return listElementToPair(p.element.Next())
}
// Previous returns a pointer to the previous pair.
func (p *Pair) Prev() *Pair {
return listElementToPair(p.element.Prev())
}
func listElementToPair(element *list.Element) *Pair {
if element == nil {
return nil
}
return element.Value.(*Pair)
}