idiomatic project structure

This commit is contained in:
2024-11-26 11:31:46 -06:00
parent 1200380341
commit 052f95702d
44 changed files with 717 additions and 481 deletions

63
internal/utils/hashset.go Normal file
View File

@@ -0,0 +1,63 @@
package utils
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
}
func (s *Set[T]) Intersect(otherSet Set[T]) Set[T] {
intersect := make(Set[T])
for val := range *s {
if otherSet.Contains(val) {
intersect.Add(val)
}
}
return intersect
}

View File

@@ -0,0 +1,35 @@
package utils
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)
}

View File

@@ -0,0 +1,11 @@
package utils
func All[T comparable](slice []T, condition func(T) bool) bool {
for _, v := range slice {
if !condition(v) {
return false
}
}
return true
}