-
Notifications
You must be signed in to change notification settings - Fork 38
/
set.go
72 lines (57 loc) · 1.17 KB
/
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
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
package external
import (
"hash/crc32"
"strconv"
)
func hashcode(s string) int {
v := int(crc32.ChecksumIEEE([]byte(s)))
if v >= 0 {
return v
}
return -v
}
func hashString(v interface{}) int {
return hashcode(v.(string))
}
func hashInt(v interface{}) int {
return hashcode(strconv.Itoa(v.(int)))
}
// setIDFunc is the function to identify the unique id for the item of set
type setIDFunc func(interface{}) int
// set is a structure to distinct instance
type set struct {
idFunc setIDFunc
idMap map[int]interface{}
}
// newSet will expected a list, reserving only one item with same id and return a set-collection
func newSet(idFunc setIDFunc, vL []interface{}) *set {
s := &set{
idMap: make(map[int]interface{}, len(vL)),
idFunc: idFunc,
}
for _, v := range vL {
s.Add(v)
}
return s
}
func (s *set) Add(v interface{}) {
id := s.idFunc(v)
if _, ok := s.idMap[id]; !ok {
s.idMap[id] = v
}
}
func (s *set) Remove(v interface{}) {
delete(s.idMap, s.idFunc(v))
}
func (s *set) List() []interface{} {
vL := make([]interface{}, s.Len())
var i int
for _, v := range s.idMap {
vL[i] = v
i++
}
return vL
}
func (s *set) Len() int {
return len(s.idMap)
}