Skip to content

Commit 85934f6

Browse files
- bug fixes in ordered maps
1 parent 01aeb69 commit 85934f6

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

ordered_map.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,15 @@ func (o *OrderedMapIntFloat64) Get(key int) (float64, bool) {
3434
return val, exists
3535
}
3636

37-
// Set will set a key and value pair. It will over-write an
37+
// Set will set a key and value pair. It will overwrite an
3838
// existing pair if it exists already.
3939
func (o *OrderedMapIntFloat64) Set(key int, val float64) {
40-
o.store[key] = val
4140
if o.keys != nil {
42-
o.keys = append(o.keys, key)
41+
if _, exists := o.store[key]; !exists {
42+
o.keys = append(o.keys, key)
43+
}
4344
}
45+
o.store[key] = val
4446
}
4547

4648
// Delete will remove the key from the OrderedMapIntFloat64.
@@ -63,7 +65,9 @@ func (o *OrderedMapIntFloat64) Delete(key int) {
6365
break
6466
}
6567
}
66-
o.keys = append(o.keys[:*idx], o.keys[*idx+1:]...)
68+
if idx != nil {
69+
o.keys = append(o.keys[:*idx], o.keys[*idx+1:]...)
70+
}
6771
}
6872

6973
// ValuesIterator is used to iterate through the values of OrderedMapIntFloat64.
@@ -121,13 +125,15 @@ func (o *OrderedMapIntMixed) Get(key int) (interface{}, bool) {
121125
return val, exists
122126
}
123127

124-
// Set will set a key and value pair. It will over-write an
128+
// Set will set a key and value pair. It will overwrite an
125129
// existing pair if it exists already.
126130
func (o *OrderedMapIntMixed) Set(key int, val interface{}) {
127-
o.store[key] = val
128131
if o.keys != nil {
129-
o.keys = append(o.keys, key)
132+
if _, exists := o.store[key]; !exists {
133+
o.keys = append(o.keys, key)
134+
}
130135
}
136+
o.store[key] = val
131137
}
132138

133139
// Delete will remove the key from the OrderedMapIntMixed.
@@ -150,7 +156,9 @@ func (o *OrderedMapIntMixed) Delete(key int) {
150156
break
151157
}
152158
}
153-
o.keys = append(o.keys[:*idx], o.keys[*idx+1:]...)
159+
if idx != nil {
160+
o.keys = append(o.keys[:*idx], o.keys[*idx+1:]...)
161+
}
154162
}
155163

156164
// ValuesIterator is used to iterate through the values of OrderedMapIntMixed.

0 commit comments

Comments
 (0)