From 716ffcb0999f9446332036db98d46366f79bf838 Mon Sep 17 00:00:00 2001 From: Momchil Atanasov Date: Thu, 2 Feb 2023 22:10:47 +0200 Subject: [PATCH] Add helper constructors to Set data structure (#11) --- ds/set.go | 30 ++++++++++++++++ ds/set_test.go | 96 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) diff --git a/ds/set.go b/ds/set.go index b8f9d0e..f83418a 100644 --- a/ds/set.go +++ b/ds/set.go @@ -10,6 +10,36 @@ func NewSet[T comparable](initialCapacity int) *Set[T] { } } +// SetFromSlice creates a new Set instance based on the elements contained +// in the provided slice. +func SetFromSlice[T comparable](slice []T) *Set[T] { + result := NewSet[T](len(slice)) + for _, item := range slice { + result.items[item] = struct{}{} + } + return result +} + +// SetFromMapKeys creates a new Set instance based on the keys of the +// provided map. +func SetFromMapKeys[T comparable, V any](m map[T]V) *Set[T] { + result := NewSet[T](len(m)) + for key := range m { + result.items[key] = struct{}{} + } + return result +} + +// SetFromMapValues creates a new Set instance based on the values of the +// provided map. +func SetFromMapValues[K comparable, V comparable](m map[K]V) *Set[V] { + result := NewSet[V](len(m)) + for _, value := range m { + result.items[value] = struct{}{} + } + return result +} + // Set represents a set data structure, where only one instance // of a given item is stored. // diff --git a/ds/set_test.go b/ds/set_test.go index 2f8e085..4414ca6 100644 --- a/ds/set_test.go +++ b/ds/set_test.go @@ -103,4 +103,100 @@ var _ = Describe("Set", func() { }) }) }) + + When("constructed from a slice", func() { + BeforeEach(func() { + set = ds.SetFromSlice([]string{"a", "c", "c", "b", "a", "d"}) + }) + + It("has the correct size", func() { + Expect(set.Size()).To(Equal(4)) + }) + + It("contains the elements of the slice", func() { + Expect(set.Contains("a")).To(BeTrue()) + Expect(set.Contains("b")).To(BeTrue()) + Expect(set.Contains("c")).To(BeTrue()) + Expect(set.Contains("d")).To(BeTrue()) + }) + + When("the slice is nil", func() { + BeforeEach(func() { + var slice []string + set = ds.SetFromSlice(slice) + }) + + It("is empty", func() { + Expect(set.IsEmpty()).To(BeTrue()) + }) + }) + }) + + When("constructed from a map's keys", func() { + BeforeEach(func() { + set = ds.SetFromMapKeys(map[string]int{ + "a": 1, + "c": 5, + "b": 13, + "d": 31, + }) + }) + + It("has the correct size", func() { + Expect(set.Size()).To(Equal(4)) + }) + + It("contains the elements of the slice", func() { + Expect(set.Contains("a")).To(BeTrue()) + Expect(set.Contains("b")).To(BeTrue()) + Expect(set.Contains("c")).To(BeTrue()) + Expect(set.Contains("d")).To(BeTrue()) + }) + + When("the map is nil", func() { + BeforeEach(func() { + var m map[string]int + set = ds.SetFromMapKeys(m) + }) + + It("is empty", func() { + Expect(set.IsEmpty()).To(BeTrue()) + }) + }) + }) + + When("constructed from a map's values", func() { + BeforeEach(func() { + set = ds.SetFromMapValues(map[int]string{ + 1: "a", + 2: "c", + 3: "c", + 4: "b", + 5: "a", + 6: "d", + }) + }) + + It("has the correct size", func() { + Expect(set.Size()).To(Equal(4)) + }) + + It("contains the elements of the slice", func() { + Expect(set.Contains("a")).To(BeTrue()) + Expect(set.Contains("b")).To(BeTrue()) + Expect(set.Contains("c")).To(BeTrue()) + Expect(set.Contains("d")).To(BeTrue()) + }) + + When("the map is nil", func() { + BeforeEach(func() { + var m map[int]string + set = ds.SetFromMapValues(m) + }) + + It("is empty", func() { + Expect(set.IsEmpty()).To(BeTrue()) + }) + }) + }) })