-
Notifications
You must be signed in to change notification settings - Fork 0
/
set.go
47 lines (38 loc) · 838 Bytes
/
set.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
package set
import (
"reflect"
)
type Set struct {
hash map[interface{}]bool
}
func Make() *Set {
return &Set{hash: make(map[interface{}]bool)}
}
func (set *Set)Add(e interface{}) {
set.hash[e] = true
}
func (set *Set)Remove(e interface{}) {
delete(set.hash, e)
}
func (set *Set)Len() int {
return len(set.hash)
}
func (set *Set)Has(e interface{}) bool {
_, has := set.hash[e]
return has
}
func (set *Set)ToArray(out interface{}) { // out should be a pointer of slice
slice := reflect.ValueOf(out).Elem()
if slice.Kind() != reflect.Slice {
panic("out is not slice")
}
if slice.Cap() < set.Len() {
panic("out is too small")
}
slice.SetLen(set.Len())
i := 0
for k := range set.hash {
slice.Index(i).Set(reflect.ValueOf(k))
i++
}
}