-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
141 additions
and
36 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package set | ||
|
||
type StringSet struct { | ||
entryMap map[string]struct{} | ||
} | ||
|
||
func (s *StringSet) Add(value string) { | ||
s.entryMap[value] = struct{}{} | ||
} | ||
|
||
func (s *StringSet) AddMany(values []string) { | ||
for _, value := range values { | ||
s.entryMap[value] = struct{}{} | ||
} | ||
} | ||
|
||
func (s *StringSet) ToList() []string { | ||
values := make([]string, 0, len(s.entryMap)) | ||
for k := range s.entryMap { | ||
values = append(values, k) | ||
} | ||
|
||
return values | ||
} | ||
|
||
func (s *StringSet) Remove(value string) { | ||
delete(s.entryMap, value) | ||
} | ||
|
||
func (s *StringSet) Contains(value string) bool { | ||
_, c := s.entryMap[value] | ||
|
||
return c | ||
} | ||
|
||
func NewStringSet() *StringSet { | ||
return &StringSet{ | ||
entryMap: make(map[string]struct{}), | ||
} | ||
} | ||
|
||
func NewStringSetWithValues(values []string) *StringSet { | ||
set := &StringSet{ | ||
entryMap: make(map[string]struct{}), | ||
} | ||
set.AddMany(values) | ||
|
||
return set | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package set | ||
|
||
import ( | ||
"reflect" | ||
"sort" | ||
"testing" | ||
) | ||
|
||
func TestStringSet(t *testing.T) { | ||
t.Run("add string to set", func(t *testing.T) { | ||
set := NewStringSet() | ||
|
||
set.Add("a") | ||
set.Add("b") | ||
set.Add("a") | ||
set.Add("c") | ||
|
||
values := set.ToList() | ||
sort.Strings(values) | ||
if !reflect.DeepEqual(values, []string{"a", "b", "c"}) { | ||
t.Errorf("set must contain only unique elements, got: %s", values) | ||
} | ||
}) | ||
t.Run("add many strings at once to set", func(t *testing.T) { | ||
set := NewStringSet() | ||
|
||
set.AddMany([]string{"a", "b", "c"}) | ||
set.Add("c") | ||
|
||
values := set.ToList() | ||
sort.Strings(values) | ||
if !reflect.DeepEqual(values, []string{"a", "b", "c"}) { | ||
t.Errorf("set must contain only unique elements, got: %s", values) | ||
} | ||
}) | ||
|
||
t.Run("remove string from set", func(t *testing.T) { | ||
set := NewStringSet() | ||
|
||
set.Add("a") | ||
set.Add("b") | ||
set.Add("c") | ||
set.Remove("c") | ||
|
||
values := set.ToList() | ||
sort.Strings(values) | ||
if !reflect.DeepEqual(values, []string{"a", "b"}) { | ||
t.Errorf("set contains element which must be deleted, got: %s", values) | ||
} | ||
}) | ||
|
||
t.Run("remove string from set", func(t *testing.T) { | ||
set := NewStringSet() | ||
|
||
set.Add("a") | ||
set.Add("b") | ||
set.Add("c") | ||
|
||
if !set.Contains("c") { | ||
t.Errorf("set must contain element which was added, got: %s", set.ToList()) | ||
} | ||
}) | ||
} |
This file was deleted.
Oops, something went wrong.