From 2fe8df158fa1ec57711c5c27e2b523699f7b43eb Mon Sep 17 00:00:00 2001 From: alexchao26 Date: Mon, 21 Dec 2020 18:35:12 -0500 Subject: [PATCH] added string and int set implementations . --- data-structures/set/set.go | 75 +++++++++++++++++++++++++++++++++ data-structures/set/set_test.go | 54 ++++++++++++++++++++++++ 2 files changed, 129 insertions(+) create mode 100644 data-structures/set/set.go create mode 100644 data-structures/set/set_test.go diff --git a/data-structures/set/set.go b/data-structures/set/set.go new file mode 100644 index 0000000..7134021 --- /dev/null +++ b/data-structures/set/set.go @@ -0,0 +1,75 @@ +package set + +// StringSet maintains a deduped list of strings added to it +type StringSet map[string]bool + +// NewStringSet initializes a set with the values form the input string slice +func NewStringSet(stringSlice []string) StringSet { + set := StringSet{} + for _, v := range stringSlice { + set[v] = true + } + return set +} + +// Has returns true if the value if found in the underlying set +func (s StringSet) Has(val string) bool { + _, ok := s[val] + return ok +} + +// Add a value to the set +func (s StringSet) Add(val string) { + s[val] = true +} + +// Remove a value from the set +func (s StringSet) Remove(val string) { + delete(s, val) +} + +// Keys returns a slice of all keys in the set +func (s StringSet) Keys() []string { + var keys []string + for k := range s { + keys = append(keys, k) + } + return keys +} + +// IntSet maintains a deduped list of strings added to it +type IntSet map[int]bool + +// NewIntSet initializes a set with the values form the input int slice +func NewIntSet(intSlice []int) IntSet { + set := IntSet{} + for _, v := range intSlice { + set[v] = true + } + return set +} + +// Has returns true if the value if found in the underlying set +func (s IntSet) Has(val int) bool { + _, ok := s[val] + return ok +} + +// Add a value to the set +func (s IntSet) Add(val int) { + s[val] = true +} + +// Remove a value from the set +func (s IntSet) Remove(val int) { + delete(s, val) +} + +// Keys returns a slice of all keys in the set +func (s IntSet) Keys() []int { + var keys []int + for k := range s { + keys = append(keys, k) + } + return keys +} diff --git a/data-structures/set/set_test.go b/data-structures/set/set_test.go new file mode 100644 index 0000000..6cd8c1b --- /dev/null +++ b/data-structures/set/set_test.go @@ -0,0 +1,54 @@ +package set_test + +import ( + "reflect" + "sort" + "testing" + + "github.com/alexchao26/advent-of-code-go/data-structures/set" +) + +func TestIntSet(t *testing.T) { + tests := []struct { + valToAdd int + wantHas []int + }{ + {5, []int{5}}, + {5, []int{5}}, + {10, []int{5, 10}}, + {20, []int{5, 10, 20}}, + {20, []int{5, 10, 20}}, + {2230, []int{5, 10, 20, 2230}}, + {123, []int{5, 10, 20, 123, 2230}}, + } + + intSet := set.NewIntSet(nil) + for _, tt := range tests { + intSet.Add(tt.valToAdd) + for _, want := range tt.wantHas { + if !intSet.Has(want) { + t.Errorf("want IntSet.Has(%d) = true, got false", want) + } + } + } + + got := intSet.Keys() + sort.Ints(got) + want := []int{5, 10, 20, 123, 2230} + if !reflect.DeepEqual(got, want) { + t.Errorf("want intSet.Keys() to be %v, got %v", want, got) + } + + valsToRemove := []int{5, 10, 20, 123, 2230} + for _, tt := range valsToRemove { + intSet.Remove(tt) + if intSet.Has(tt) { + t.Errorf("want IntSet.Has(%d) = false, got true", tt) + } + } + + got = intSet.Keys() + if len(got) != 0 { + t.Errorf("want zero-length slice after removing all keys, got %v", got) + } +}