| 1 |
package types |
| 2 |
|
| 3 |
type Index struct { |
| 4 |
index int |
| 5 |
} |
| 6 |
|
| 7 |
|
| 8 |
type OrderedSet struct { |
| 9 |
filter map[string]*Index |
| 10 |
cur int |
| 11 |
Values []string |
| 12 |
} |
| 13 |
|
| 14 |
|
| 15 |
func (o *OrderedSet) Init(items ...string) { |
| 16 |
o.filter = make(map[string]*Index) |
| 17 |
o.cur = 0 |
| 18 |
for _, item := range items { |
| 19 |
o.Add(item) |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
func (o *OrderedSet) Add(item string) { |
| 25 |
if o.filter[item] == nil { |
| 26 |
o.filter[item] = &Index{ |
| 27 |
index: o.cur, |
| 28 |
} |
| 29 |
o.cur++ |
| 30 |
o.Values = append(o.Values, item) |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
func (o *OrderedSet) Remove(item string) { |
| 35 |
if o.filter[item] != nil { |
| 36 |
idx := o.filter[item].index |
| 37 |
o.Values = append(o.Values[:idx], o.Values[idx+1:]...) |
| 38 |
|
| 39 |
renew := &OrderedSet{} |
| 40 |
renew.Init(o.Values...) |
| 41 |
|
| 42 |
o.cur = renew.cur |
| 43 |
o.filter = renew.filter |
| 44 |
o.Values = renew.Values |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
func (o *OrderedSet) Size() int { |
| 49 |
return o.cur |
| 50 |
} |
| 51 |
|
| 52 |
func (o *OrderedSet) Contains(item string) bool { |
| 53 |
return o.filter[item] != nil |
| 54 |
} |
| 55 |
|
| 56 |
func (o *OrderedSet) Clear() { |
| 57 |
o.filter = make(map[string]*Index) |
| 58 |
o.cur = 0 |
| 59 |
o.Values = []string{} |
| 60 |
} |