initial commit
This commit is contained in:
commit
4382c63449
84
set.go
Normal file
84
set.go
Normal file
@ -0,0 +1,84 @@
|
||||
package main
|
||||
|
||||
type StringSet struct {
|
||||
set map[string]bool
|
||||
}
|
||||
|
||||
func NewStringSet() *StringSet {
|
||||
return &StringSet{
|
||||
set: make(map[string]bool, 0),
|
||||
}
|
||||
}
|
||||
|
||||
func NewStringSetWith(strings []string) *StringSet {
|
||||
s := NewStringSet()
|
||||
for _, str := range strings {
|
||||
s.Add(str)
|
||||
}
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *StringSet) Add(str string) {
|
||||
s.set[str] = true
|
||||
}
|
||||
|
||||
func (s *StringSet) Remove(str string) {
|
||||
delete(s.set, str)
|
||||
}
|
||||
|
||||
func (s *StringSet) All() []string {
|
||||
all := make([]string, 0)
|
||||
for i := range s.set {
|
||||
all = append(all, i)
|
||||
}
|
||||
return all
|
||||
}
|
||||
|
||||
func (s *StringSet) Contains(str string) bool {
|
||||
_, ok := s.set[str]
|
||||
return ok
|
||||
}
|
||||
|
||||
func (s *StringSet) Count() int {
|
||||
return len(s.set)
|
||||
}
|
||||
|
||||
func (s *StringSet) Equals(s2 *StringSet) bool {
|
||||
if s.Count() != s2.Count() {
|
||||
return false
|
||||
}
|
||||
for _, entry := range s2.All() {
|
||||
_, ok := s.set[entry]
|
||||
if !ok {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
// Diff returns a new StringSet that contains elements from s that were not present in s2
|
||||
func (s *StringSet) Diff(s2 *StringSet) *StringSet {
|
||||
temp := NewStringSet()
|
||||
for _, item := range s.All() {
|
||||
if !s2.Contains(item) {
|
||||
temp.Add(item)
|
||||
}
|
||||
}
|
||||
return temp
|
||||
}
|
||||
|
||||
// Intersect returns a new StringSet that contains elements common to both s and s2
|
||||
func (s *StringSet) Intersect(s2 *StringSet) *StringSet {
|
||||
temp := NewStringSet()
|
||||
for _, item := range s.All() {
|
||||
if s2.Contains(item) {
|
||||
temp.Add(item)
|
||||
}
|
||||
}
|
||||
for _, item := range s2.All() {
|
||||
if s.Contains(item) {
|
||||
temp.Add(item)
|
||||
}
|
||||
}
|
||||
return temp
|
||||
}
|
86
set_test.go
Normal file
86
set_test.go
Normal file
@ -0,0 +1,86 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewStringSetWith(t *testing.T) {
|
||||
s := NewStringSet()
|
||||
|
||||
if len(s.All()) != 0 {
|
||||
t.Errorf("expected empty at initiaization")
|
||||
}
|
||||
|
||||
s.Add("foo")
|
||||
s.Add("bar")
|
||||
s.Add("bar")
|
||||
// s = foo,bar
|
||||
|
||||
if len(s.All()) != 2 {
|
||||
t.Errorf("expected 2 entries")
|
||||
}
|
||||
|
||||
if !s.Contains("foo") {
|
||||
t.Errorf("set missing expected entry")
|
||||
}
|
||||
|
||||
s.Remove("bar")
|
||||
// s = foo
|
||||
|
||||
if len(s.All()) != 1 {
|
||||
t.Errorf("expected 1 entry")
|
||||
}
|
||||
|
||||
if s.Contains("bar") {
|
||||
t.Errorf("removed entry still present")
|
||||
}
|
||||
|
||||
if !s.Contains("foo") {
|
||||
t.Errorf("entry that was not removed is missing")
|
||||
}
|
||||
|
||||
s.Add("qux")
|
||||
// s = foo, qux
|
||||
|
||||
s2 := NewStringSetWith([]string{"foo", "qux"})
|
||||
|
||||
if !s.Equals(s2) {
|
||||
t.Errorf("sets should match")
|
||||
}
|
||||
|
||||
s.Add("lol")
|
||||
s2.Add("noo")
|
||||
// s = foo, qux, lol
|
||||
// s2 = foo, qux, noo
|
||||
|
||||
if s.Equals(s2) {
|
||||
t.Errorf("sets shouldn't match")
|
||||
}
|
||||
|
||||
if !s.Equals(NewStringSetWith([]string{"foo", "qux", "lol"})) {
|
||||
t.Errorf("unexpected result from All()")
|
||||
}
|
||||
|
||||
if !s.Intersect(s2).Equals(NewStringSetWith([]string{"foo", "qux"})) {
|
||||
t.Errorf("unexpected result from Intersect()")
|
||||
}
|
||||
|
||||
s.Add("noo")
|
||||
// s = foo, qux, noo, lol
|
||||
// s2 = foo, qux, noo
|
||||
if !s.Diff(s2).Equals(NewStringSetWith([]string{"lol"})) {
|
||||
t.Errorf("unexpected result from Diff()")
|
||||
}
|
||||
}
|
||||
|
||||
func stringSlicesMatch(s1, s2 []string) bool {
|
||||
if len(s1) != len(s2) {
|
||||
return false
|
||||
}
|
||||
for i, s := range s1 {
|
||||
if s != s2[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
Loading…
Reference in New Issue
Block a user