Created
April 5, 2013 05:13
-
-
Save jimfleming/5316799 to your computer and use it in GitHub Desktop.
A simple set data structure in go.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package set | |
type Set map[interface{}]struct{} | |
func (this *Set) Contains(m *interface{}) (exists bool) { | |
s := *this | |
_, exists = s[m] | |
return | |
} | |
func (this *Set) Remove(mems ...interface{}) (cnt int) { | |
s := *this | |
for m := range mems { | |
if _, exists := s[m]; exists { | |
delete(s, m) | |
cnt++ | |
} | |
} | |
return | |
} | |
func (this *Set) Add(mems ...interface{}) (cnt int) { | |
s := *this | |
for m := range mems { | |
if _, exists := s[m]; exists { | |
s[m] = struct{}{} | |
cnt++ | |
} | |
} | |
return | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment