-
-
Notifications
You must be signed in to change notification settings - Fork 95
/
Copy pathordered_map.go
191 lines (159 loc) · 4.14 KB
/
ordered_map.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
// Copyright 2018-20 PJ Engineering and Business Solutions Pty. Ltd. All rights reserved.
package dataframe
import (
"sort"
)
// OrderedMapIntFloat64 is an ordered map[int]float64.
type OrderedMapIntFloat64 struct {
store map[int]float64
keys []int // if not initialized implies that it's not ordered
}
// NewOrderedMapIntFloat64 will create a new OrderedMapIntFloat64.
// By default it will be ordered, but setting notOrdered to true will make
// it operate as a builtin map.
func NewOrderedMapIntFloat64(notOrdered ...bool) *OrderedMapIntFloat64 {
if len(notOrdered) == 0 || !notOrdered[0] {
// ordered
return &OrderedMapIntFloat64{
store: map[int]float64{},
keys: []int{},
}
}
return &OrderedMapIntFloat64{store: map[int]float64{}}
}
// Get will return the value of the key. If it doesn't exist, it will
// return false for the second return value.
func (o *OrderedMapIntFloat64) Get(key int) (float64, bool) {
val, exists := o.store[key]
return val, exists
}
// Set will set a key and value pair. It will overwrite an
// existing pair if it exists already.
func (o *OrderedMapIntFloat64) Set(key int, val float64) {
if o.keys != nil {
if _, exists := o.store[key]; !exists {
o.keys = append(o.keys, key)
}
}
o.store[key] = val
}
// Delete will remove the key from the OrderedMapIntFloat64.
// For performance reasons, ensure the key exists beforehand when in ordered mode.
func (o *OrderedMapIntFloat64) Delete(key int) {
if o.keys == nil {
// unordered
delete(o.store, key)
return
}
// ordered
delete(o.store, key)
// Find key
var idx *int
for i, val := range o.keys {
if val == key {
idx = &[]int{i}[0]
break
}
}
if idx != nil {
o.keys = append(o.keys[:*idx], o.keys[*idx+1:]...)
}
}
// ValuesIterator is used to iterate through the values of OrderedMapIntFloat64.
func (o *OrderedMapIntFloat64) ValuesIterator() func() (*int, float64) {
var keys []int
if o.keys == nil {
for key := range o.store {
keys = append(keys, key)
}
sort.Ints(keys)
} else {
keys = o.keys
}
j := 0
return func() (*int, float64) {
if j > len(keys)-1 {
return nil, 0
}
row := keys[j]
j++
return &row, o.store[row]
}
}
// OrderedMapIntMixed is an ordered map[int]interface{}.
type OrderedMapIntMixed struct {
store map[int]interface{}
keys []int
}
// NewOrderedMapIntMixed will create a new OrderedMapIntMixed.
// By default it will be ordered, but setting notOrdered to true will make
// it operate as a builtin map.
func NewOrderedMapIntMixed(notOrdered ...bool) *OrderedMapIntMixed {
if len(notOrdered) == 0 || !notOrdered[0] {
// ordered
return &OrderedMapIntMixed{
store: map[int]interface{}{},
keys: []int{},
}
}
return &OrderedMapIntMixed{store: map[int]interface{}{}}
}
// Get will return the value of the key. If it doesn't exist, it will
// return false for the second return value.
func (o *OrderedMapIntMixed) Get(key int) (interface{}, bool) {
val, exists := o.store[key]
return val, exists
}
// Set will set a key and value pair. It will overwrite an
// existing pair if it exists already.
func (o *OrderedMapIntMixed) Set(key int, val interface{}) {
if o.keys != nil {
if _, exists := o.store[key]; !exists {
o.keys = append(o.keys, key)
}
}
o.store[key] = val
}
// Delete will remove the key from the OrderedMapIntMixed.
// For performance reasons, ensure the key exists beforehand when in ordered mode.
func (o *OrderedMapIntMixed) Delete(key int) {
if o.keys == nil {
// unordered
delete(o.store, key)
return
}
// ordered
delete(o.store, key)
// Find key
var idx *int
for i, val := range o.keys {
if val == key {
idx = &[]int{i}[0]
break
}
}
if idx != nil {
o.keys = append(o.keys[:*idx], o.keys[*idx+1:]...)
}
}
// ValuesIterator is used to iterate through the values of OrderedMapIntMixed.
func (o *OrderedMapIntMixed) ValuesIterator() func() (*int, interface{}) {
var keys []int
if o.keys == nil {
for key := range o.store {
keys = append(keys, key)
}
sort.Ints(keys)
} else {
keys = o.keys
}
j := 0
return func() (*int, interface{}) {
if j > len(keys)-1 {
return nil, 0
}
row := keys[j]
j++
return &row, o.store[row]
}
}