set: introduce basic set operations #1

merged
opened by oppi.li targeting main from push-soqmukrvport

introduces New, Add, Remove and Size

Signed-off-by: oppiliappan me@oppi.li

Changed files
+20
+20
set.go
···
package hashset
type Set[T comparable] map[T]struct{}
···
package hashset
type Set[T comparable] map[T]struct{}
+
+
// New creates and returns a new empty Set.
+
func New[T comparable]() Set[T] {
+
return make(Set[T])
+
}
+
+
// Add inserts an element into the set.
+
func (s Set[T]) Add(value T) {
+
s[value] = struct{}{}
+
}
+
+
// Remove deletes an element from the set.
+
func (s Set[T]) Remove(value T) {
+
delete(s, value)
+
}
+
+
// Size returns the number of elements in the set.
+
func (s Set[T]) Size() int {
+
return len(s)
+
}