Skip to content

Commit 30392a3

Browse files
feat: add set implementation using generic (TheAlgorithms#656)
* feat: add set implementation using generic * some refact * use gofmt --------- Co-authored-by: Rak Laptudirm <[email protected]>
1 parent 58bb31e commit 30392a3

File tree

3 files changed

+55
-55
lines changed

3 files changed

+55
-55
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ Read our [Contribution Guidelines](CONTRIBUTING.md) before you contribute.
925925

926926
---
927927

928-
##### package set implements a Set using a golang map. This implies that only the types that are accepted as valid map keys can be used as set elements. For instance, do not try to Add a slice, or the program will panic.
928+
##### package set implements a Set using generics and a golang map with comparable interface key. This implies that only the types that are accepted as valid map keys can be used as set elements
929929

930930
---
931931
##### Functions:

structure/set/set.go

+39-39
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
package set
55

66
// New gives new set.
7-
func New(items ...any) Set {
8-
st := set{
9-
elements: make(map[any]bool),
7+
func New[T comparable](items ...T) Set[T] {
8+
st := set[T]{
9+
elements: make(map[T]bool),
1010
}
1111
for _, item := range items {
1212
st.Add(item)
@@ -15,73 +15,73 @@ func New(items ...any) Set {
1515
}
1616

1717
// Set is an interface of possible methods on 'set'.
18-
type Set interface {
18+
type Set[T comparable] interface {
1919
// Add: adds new element to the set
20-
Add(item any)
20+
Add(item T)
2121
// Delete: deletes the passed element from the set if present
22-
Delete(item any)
22+
Delete(item T)
2323
// Len: gives the length of the set (total no. of elements in set)
2424
Len() int
25-
// GetItems: gives the array( []any ) of elements of the set.
26-
GetItems() []any
25+
// GetItems: gives the array( []T ) of elements of the set.
26+
GetItems() []T
2727
// In: checks whether item is present in set or not.
28-
In(item any) bool
28+
In(item T) bool
2929
// IsSubsetOf: checks whether set is subset of set2 or not.
30-
IsSubsetOf(set2 Set) bool
30+
IsSubsetOf(set2 Set[T]) bool
3131
// IsProperSubsetOf: checks whether set is proper subset of set2 or not.
3232
// ex: [1,2,3] proper subset of [1,2,3,4] -> true
33-
IsProperSubsetOf(set2 Set) bool
33+
IsProperSubsetOf(set2 Set[T]) bool
3434
// IsSupersetOf: checks whether set is superset of set2 or not.
35-
IsSupersetOf(set2 Set) bool
35+
IsSupersetOf(set2 Set[T]) bool
3636
// IsProperSupersetOf: checks whether set is proper superset of set2 or not.
3737
// ex: [1,2,3,4] proper superset of [1,2,3] -> true
38-
IsProperSupersetOf(set2 Set) bool
38+
IsProperSupersetOf(set2 Set[T]) bool
3939
// Union: gives new union set of both sets.
4040
// ex: [1,2,3] union [3,4,5] -> [1,2,3,4,5]
41-
Union(set2 Set) Set
41+
Union(set2 Set[T]) Set[T]
4242
// Intersection: gives new intersection set of both sets.
4343
// ex: [1,2,3] Intersection [3,4,5] -> [3]
44-
Intersection(set2 Set) Set
44+
Intersection(set2 Set[T]) Set[T]
4545
// Difference: gives new difference set of both sets.
4646
// ex: [1,2,3] Difference [3,4,5] -> [1,2]
47-
Difference(set2 Set) Set
47+
Difference(set2 Set[T]) Set[T]
4848
// SymmetricDifference: gives new symmetric difference set of both sets.
4949
// ex: [1,2,3] SymmetricDifference [3,4,5] -> [1,2,4,5]
50-
SymmetricDifference(set2 Set) Set
50+
SymmetricDifference(set2 Set[T]) Set[T]
5151
}
5252

53-
type set struct {
54-
elements map[any]bool
53+
type set[T comparable] struct {
54+
elements map[T]bool
5555
}
5656

57-
func (st *set) Add(value any) {
57+
func (st *set[T]) Add(value T) {
5858
st.elements[value] = true
5959
}
6060

61-
func (st *set) Delete(value any) {
61+
func (st *set[T]) Delete(value T) {
6262
delete(st.elements, value)
6363
}
6464

65-
func (st *set) GetItems() []any {
66-
keys := make([]any, 0, len(st.elements))
65+
func (st *set[T]) GetItems() []T {
66+
keys := make([]T, 0, len(st.elements))
6767
for k := range st.elements {
6868
keys = append(keys, k)
6969
}
7070
return keys
7171
}
7272

73-
func (st *set) Len() int {
73+
func (st *set[T]) Len() int {
7474
return len(st.elements)
7575
}
7676

77-
func (st *set) In(value any) bool {
77+
func (st *set[T]) In(value T) bool {
7878
if _, in := st.elements[value]; in {
7979
return true
8080
}
8181
return false
8282
}
8383

84-
func (st *set) IsSubsetOf(superSet Set) bool {
84+
func (st *set[T]) IsSubsetOf(superSet Set[T]) bool {
8585
if st.Len() > superSet.Len() {
8686
return false
8787
}
@@ -94,26 +94,26 @@ func (st *set) IsSubsetOf(superSet Set) bool {
9494
return true
9595
}
9696

97-
func (st *set) IsProperSubsetOf(superSet Set) bool {
97+
func (st *set[T]) IsProperSubsetOf(superSet Set[T]) bool {
9898
if st.Len() == superSet.Len() {
9999
return false
100100
}
101101
return st.IsSubsetOf(superSet)
102102
}
103103

104-
func (st *set) IsSupersetOf(subSet Set) bool {
104+
func (st *set[T]) IsSupersetOf(subSet Set[T]) bool {
105105
return subSet.IsSubsetOf(st)
106106
}
107107

108-
func (st *set) IsProperSupersetOf(subSet Set) bool {
108+
func (st *set[T]) IsProperSupersetOf(subSet Set[T]) bool {
109109
if st.Len() == subSet.Len() {
110110
return false
111111
}
112112
return st.IsSupersetOf(subSet)
113113
}
114114

115-
func (st *set) Union(st2 Set) Set {
116-
unionSet := New()
115+
func (st *set[T]) Union(st2 Set[T]) Set[T] {
116+
unionSet := New[T]()
117117
for _, item := range st.GetItems() {
118118
unionSet.Add(item)
119119
}
@@ -123,9 +123,9 @@ func (st *set) Union(st2 Set) Set {
123123
return unionSet
124124
}
125125

126-
func (st *set) Intersection(st2 Set) Set {
127-
intersectionSet := New()
128-
var minSet, maxSet Set
126+
func (st *set[T]) Intersection(st2 Set[T]) Set[T] {
127+
intersectionSet := New[T]()
128+
var minSet, maxSet Set[T]
129129
if st.Len() > st2.Len() {
130130
minSet = st2
131131
maxSet = st
@@ -141,8 +141,8 @@ func (st *set) Intersection(st2 Set) Set {
141141
return intersectionSet
142142
}
143143

144-
func (st *set) Difference(st2 Set) Set {
145-
differenceSet := New()
144+
func (st *set[T]) Difference(st2 Set[T]) Set[T] {
145+
differenceSet := New[T]()
146146
for _, item := range st.GetItems() {
147147
if !st2.In(item) {
148148
differenceSet.Add(item)
@@ -151,9 +151,9 @@ func (st *set) Difference(st2 Set) Set {
151151
return differenceSet
152152
}
153153

154-
func (st *set) SymmetricDifference(st2 Set) Set {
155-
symmetricDifferenceSet := New()
156-
dropSet := New()
154+
func (st *set[T]) SymmetricDifference(st2 Set[T]) Set[T] {
155+
symmetricDifferenceSet := New[T]()
156+
dropSet := New[T]()
157157
for _, item := range st.GetItems() {
158158
if st2.In(item) {
159159
dropSet.Add(item)

structure/set/set_test.go

+15-15
Original file line numberDiff line numberDiff line change
@@ -118,9 +118,9 @@ func TestIsProperSupersetOf(t *testing.T) {
118118
func TestUnion(t *testing.T) {
119119
td := []struct {
120120
name string
121-
s1 Set
122-
s2 Set
123-
expSet Set
121+
s1 Set[int]
122+
s2 Set[int]
123+
expSet Set[int]
124124
}{
125125
{"union of different sets", New(1, 2, 3), New(4, 5, 6), New(1, 2, 3, 4, 5, 6)},
126126
{"union of sets with elements in common", New(1, 2, 3), New(1, 2, 4), New(1, 2, 3, 4)},
@@ -144,11 +144,11 @@ func TestUnion(t *testing.T) {
144144
func TestIntersection(t *testing.T) {
145145
td := []struct {
146146
name string
147-
s1 Set
148-
s2 Set
149-
expSet Set
147+
s1 Set[int]
148+
s2 Set[int]
149+
expSet Set[int]
150150
}{
151-
{"intersection of different sets", New(0, 1, 2, 3), New(4, 5, 6), New()},
151+
{"intersection of different sets", New(0, 1, 2, 3), New(4, 5, 6), New[int]()},
152152
{"intersection of sets with elements in common", New(1, 2, 3), New(1, 2, 4), New(1, 2)},
153153
{"intersection of same sets", New(1, 2, 3), New(1, 2, 3), New(1, 2, 3)},
154154
}
@@ -170,13 +170,13 @@ func TestIntersection(t *testing.T) {
170170
func TestDifference(t *testing.T) {
171171
td := []struct {
172172
name string
173-
s1 Set
174-
s2 Set
175-
expSet Set
173+
s1 Set[int]
174+
s2 Set[int]
175+
expSet Set[int]
176176
}{
177177
{"difference of different sets", New(1, 2, 3), New(4, 5, 6), New(1, 2, 3)},
178178
{"difference of sets with elements in common", New(1, 2, 3), New(1, 2, 4), New(3)},
179-
{"difference of same sets", New(1, 2, 3), New(1, 2, 3), New()},
179+
{"difference of same sets", New(1, 2, 3), New(1, 2, 3), New[int]()},
180180
}
181181
for _, tc := range td {
182182
t.Run(tc.name, func(t *testing.T) {
@@ -196,13 +196,13 @@ func TestDifference(t *testing.T) {
196196
func TestSymmetricDifference(t *testing.T) {
197197
td := []struct {
198198
name string
199-
s1 Set
200-
s2 Set
201-
expSet Set
199+
s1 Set[int]
200+
s2 Set[int]
201+
expSet Set[int]
202202
}{
203203
{"symmetric difference of different sets", New(1, 2, 3), New(4, 5, 6), New(1, 2, 3, 4, 5, 6)},
204204
{"symmetric difference of sets with elements in common", New(1, 2, 3), New(1, 2, 4), New(3, 4)},
205-
{"symmetric difference of same sets", New(1, 2, 3), New(1, 2, 3), New()},
205+
{"symmetric difference of same sets", New(1, 2, 3), New(1, 2, 3), New[int]()},
206206
}
207207
for _, tc := range td {
208208
t.Run(tc.name, func(t *testing.T) {

0 commit comments

Comments
 (0)