chore: cleanup and add tools

This commit is contained in:
xuu
2024-10-30 13:32:44 -06:00
parent 04bbac8559
commit e046a6c06d
11 changed files with 325 additions and 53 deletions

12
set.go
View File

@@ -2,22 +2,22 @@ package aoc
import "golang.org/x/exp/maps"
type set[T comparable] map[T]struct{}
type Set[T comparable] map[T]struct{}
func Set[T comparable](arr ...T) set[T] {
m := make(set[T], len(arr))
func NewSet[T comparable](arr ...T) Set[T] {
m := make(Set[T], len(arr))
for _, a := range arr {
m[a] = struct{}{}
}
return m
}
func (m *set[T]) Add(a T) {
func (m *Set[T]) Add(a T) {
(*m)[a] = struct{}{}
}
func (m *set[T]) Items() []T {
func (m *Set[T]) Items() []T {
return maps.Keys(*m)
}
func (m *set[T]) Has(a T) bool {
func (m *Set[T]) Has(a T) bool {
var ok bool
_, ok = (*m)[a]
return ok