initial commit
This commit is contained in:
53
hashset/hashset.go
Normal file
53
hashset/hashset.go
Normal 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
|
||||
}
|
||||
Reference in New Issue
Block a user