-
Notifications
You must be signed in to change notification settings - Fork 3
/
ordered_maps.go
153 lines (134 loc) · 4.94 KB
/
ordered_maps.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
package brigodier
import (
"encoding/json"
"github.com/emirpasic/gods/maps/linkedhashmap"
)
type (
// Container is base interface that container structures implement.
Container interface {
// Empty returns true if map does not contain any elements
Empty() bool
// Size returns number of elements in the map.
Size() int
// Clear removes all elements from the map.
Clear()
}
// CommandNodeStringMap holds the elements in a regular hash table,
// and uses doubly-linked list to store key ordering.
CommandNodeStringMap interface {
// Put inserts key-value pair into the map.
Put(key CommandNode, value string)
// Get searches the element in the map by key and returns its value or nil if key is not found in tree.
// Second return parameter is true if key was found, otherwise false.
Get(key CommandNode) (value string, found bool)
// Remove removes the element from the map by key.
Remove(key CommandNode)
// Keys returns all keys in-order
Keys() []CommandNode
// Values returns all values in-order based on the key.
Values() []string
// Range calls the given function once for each element
// until f returns false, passing that element's key and value.
Range(f func(key CommandNode, value string) bool)
Container
}
// StringCommandNodeMap holds the elements in a regular hash table,
// and uses doubly-linked list to store key ordering.
StringCommandNodeMap interface {
// Put inserts key-value pair into the map.
Put(key string, value CommandNode)
// Get searches the element in the map by key and returns its value or nil if key is not found in tree.
// Second return parameter is true if key was found, otherwise false.
Get(key string) (value CommandNode, found bool)
// Remove removes the element from the map by key.
Remove(key string)
// Keys returns all keys in-order
Keys() []string
// Values returns all values in-order based on the key.
Values() []CommandNode
// Range calls the given function once for each element
// until f returns false, passing that element's key and value.
Range(f func(key string, value CommandNode) bool)
Container
}
)
// NewCommandNodeStringMap returns a new CommandNodeStringMap.
func NewCommandNodeStringMap() CommandNodeStringMap {
return &commandNodeStringMap{linkedhashmap.New()}
}
// NewStringCommandNodeMap returns a new StringCommandNodeMap.
func NewStringCommandNodeMap() StringCommandNodeMap {
return &stringCommandNodeMap{linkedhashmap.New()}
}
type commandNodeStringMap struct{ *linkedhashmap.Map }
func (m *commandNodeStringMap) MarshalJSON() ([]byte, error) { return m.Map.ToJSON() }
func (m *commandNodeStringMap) UnmarshalJSON(data []byte) error { return m.Map.FromJSON(data) }
var _ CommandNodeStringMap = (*commandNodeStringMap)(nil)
var _ json.Marshaler = (*commandNodeStringMap)(nil)
var _ json.Unmarshaler = (*commandNodeStringMap)(nil)
func (m *commandNodeStringMap) Range(f func(key CommandNode, value string) bool) {
m.Map.All(func(key interface{}, value interface{}) bool {
return f(key.(CommandNode), value.(string))
})
}
func (m *commandNodeStringMap) Put(key CommandNode, value string) { m.Map.Put(key, value) }
func (m *commandNodeStringMap) Get(key CommandNode) (string, bool) {
v, found := m.Map.Get(key)
if found {
return v.(string), true
}
return "", false
}
func (m *commandNodeStringMap) Remove(key CommandNode) { m.Map.Remove(key) }
func (m *commandNodeStringMap) Keys() []CommandNode {
keys := m.Map.Keys()
a := make([]CommandNode, len(keys))
for i, k := range keys {
a[i] = k.(CommandNode)
}
return a
}
func (m *commandNodeStringMap) Values() []string {
values := m.Map.Values()
a := make([]string, len(values))
for i, v := range values {
a[i] = v.(string)
}
return a
}
type stringCommandNodeMap struct{ *linkedhashmap.Map }
func (m *stringCommandNodeMap) MarshalJSON() ([]byte, error) { return m.Map.ToJSON() }
func (m *stringCommandNodeMap) UnmarshalJSON(data []byte) error { return m.Map.FromJSON(data) }
var _ StringCommandNodeMap = (*stringCommandNodeMap)(nil)
var _ json.Marshaler = (*stringCommandNodeMap)(nil)
var _ json.Unmarshaler = (*stringCommandNodeMap)(nil)
func (m *stringCommandNodeMap) Range(f func(key string, value CommandNode) bool) {
m.Map.All(func(key interface{}, value interface{}) bool {
return f(key.(string), value.(CommandNode))
})
}
func (m *stringCommandNodeMap) Put(key string, value CommandNode) { m.Map.Put(key, value) }
func (m *stringCommandNodeMap) Get(key string) (CommandNode, bool) {
v, found := m.Map.Get(key)
if found {
return v.(CommandNode), true
}
return nil, false
}
func (m *stringCommandNodeMap) Remove(key string) { m.Map.Remove(key) }
func (m *stringCommandNodeMap) Keys() []string {
keys := m.Map.Keys()
a := make([]string, len(keys))
for i, k := range keys {
a[i] = k.(string)
}
return a
}
func (m *stringCommandNodeMap) Values() []CommandNode {
values := m.Map.Values()
a := make([]CommandNode, len(values))
for i, v := range values {
a[i] = v.(CommandNode)
}
return a
}