Skip to content

Commit

Permalink
Add helper constructors to Set data structure (#11)
Browse files Browse the repository at this point in the history
  • Loading branch information
mokiat authored Feb 2, 2023
1 parent e95b959 commit 716ffcb
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 0 deletions.
30 changes: 30 additions & 0 deletions ds/set.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//
Expand Down
96 changes: 96 additions & 0 deletions ds/set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
})
})
})
})

0 comments on commit 716ffcb

Please sign in to comment.