7 // Set is a set data structure.
9 m map[interface{}]interface{}
13 // Hashable is the interface used by set to get the hash code of a value.
14 // If this isn't given, then the value of the item being added to the set
15 // itself is used as the comparison value.
16 type Hashable interface {
17 Hashcode() interface{}
20 // hashcode returns the hashcode used for set elements.
21 func hashcode(v interface{}) interface{} {
22 if h, ok := v.(Hashable); ok {
29 // Add adds an item to the set
30 func (s *Set) Add(v interface{}) {
35 // Delete removes an item from the set.
36 func (s *Set) Delete(v interface{}) {
38 delete(s.m, hashcode(v))
41 // Include returns true/false of whether a value is in the set.
42 func (s *Set) Include(v interface{}) bool {
44 _, ok := s.m[hashcode(v)]
48 // Intersection computes the set intersection with other.
49 func (s *Set) Intersection(other *Set) *Set {
55 for _, v := range s.m {
65 // Difference returns a set with the elements that s has but
67 func (s *Set) Difference(other *Set) *Set {
70 for k, v := range s.m {
84 // Len is the number of items in the set.
85 func (s *Set) Len() int {
93 // List returns the list of set elements.
94 func (s *Set) List() []interface{} {
99 r := make([]interface{}, 0, len(s.m))
100 for _, v := range s.m {
107 func (s *Set) init() {
108 s.m = make(map[interface{}]interface{})