initial commit

This commit is contained in:
2024-08-17 10:26:02 -05:00
commit 7711fc14ed
17 changed files with 975 additions and 0 deletions

53
hashset/hashset.go Normal file
View File

@@ -0,0 +1,53 @@
package hashset
type Set[T comparable] map[T]struct{}
func (s *Set[T]) Add(element T) {
(*s)[element] = struct{}{}
}
func (s *Set[T]) Remove(element T) {
delete(*s, element)
}
func (s *Set[T]) Contains(element T) bool {
_, exists := (*s)[element]
return exists
}
func (s *Set[T]) Size() int {
return len(*s)
}
func (s *Set[T]) ToSlice() []T {
list := make([]T, 0, len(*s))
for key := range *s {
list = append(list, key)
}
return list
}
func NewSetFromSlice[T comparable](slice []T) Set[T] {
set := make(Set[T])
for _, val := range slice {
set.Add(val)
}
return set
}
func (s *Set[T]) Copy() Set[T] {
newSet := make(Set[T])
for key, val := range *s {
newSet[key] = val
}
return newSet
}
func (s *Set[T]) IsDisjoint(otherSet Set[T]) bool {
for attr, _ := range *s {
if otherSet.Contains(attr) {
return false
}
}
return true
}

35
hashset/hashset_test.go Normal file
View File

@@ -0,0 +1,35 @@
package hashset
import (
"github.com/stretchr/testify/assert"
"testing"
)
func TestSet(t *testing.T) {
intSet := make(Set[int])
intSet.Add(1)
intSet.Add(2)
assert.EqualValues(t, intSet.Size(), 2)
intSet.Add(3)
intSet.Add(3)
assert.EqualValues(t, intSet.Size(), 3)
intSet.Remove(2)
assert.EqualValues(t, intSet.Size(), 2)
assert.False(t, intSet.Contains(2))
assert.True(t, intSet.Contains(1))
list := intSet.ToSlice()
assert.Contains(t, list, 1)
assert.Contains(t, list, 3)
}
func TestSet_Copy(t *testing.T) {
intSet := NewSetFromSlice[int]([]int{1, 2, 3})
copySet := intSet.Copy()
intSet.Remove(1)
assert.Equal(t, intSet.Size(), 2)
assert.Equal(t, copySet.Size(), 3)
}