Merge pull request #289 from thockin/cleanups

Add a NewStringSet() function
This commit is contained in:
brendandburns 2014-06-28 19:51:34 -07:00
commit fe4ce67aeb
2 changed files with 23 additions and 0 deletions

View File

@ -21,6 +21,13 @@ type empty struct{}
// A set of strings, implemented via map[string]struct{} for minimal memory consumption.
type StringSet map[string]empty
// NewStringSet creates a StringSet from a list of values.
func NewStringSet(items ...string) StringSet {
ss := StringSet{}
ss.Insert(items...)
return ss
}
// Insert adds items to the set.
func (s StringSet) Insert(items ...string) {
for _, item := range items {

View File

@ -22,7 +22,13 @@ import (
func TestStringSet(t *testing.T) {
s := StringSet{}
if len(s) != 0 {
t.Errorf("Expected len=0: %d", len(s))
}
s.Insert("a", "b")
if len(s) != 2 {
t.Errorf("Expected len=2: %d", len(s))
}
s.Insert("c")
if s.Has("d") {
t.Errorf("Unexpected contents: %#v", s)
@ -35,3 +41,13 @@ func TestStringSet(t *testing.T) {
t.Errorf("Unexpected contents: %#v", s)
}
}
func TestNewStringSet(t *testing.T) {
s := NewStringSet("a", "b", "c")
if len(s) != 3 {
t.Errorf("Expected len=3: %d", len(s))
}
if !s.Has("a") || !s.Has("b") || !s.Has("c") {
t.Errorf("Unexpected contents: %#v", s)
}
}