nikkiing 2 years ago
parent 075757669c
commit 810360258d
  1. 40
      pkg/set/set.go
  2. 2
      pkg/tree/tree.go

@ -0,0 +1,40 @@
package pkg
type Set[T comparable] map[T]struct{}
func (s Set[T]) Add(items ...T) {
for _, v := range items {
s[v] = struct{}{}
}
}
func (s Set[T]) AddWithOutEmpty(items ...T) {
var empty T
for _, v := range items {
if v == empty {
continue
}
s[v] = struct{}{}
}
}
func (s Set[T]) Has(data T) bool {
_, ok := s[data]
return ok
}
func (s Set[T]) Delete(data T) {
delete(s, data)
}
func (s Set[T]) Count() int32 {
return int32(len(s))
}
func (s Set[T]) ToSlice() []T {
res := make([]T, 0, len(s))
for k := range s {
res = append(res, k)
}
return res
}

@ -1,4 +1,4 @@
package pkg package tree
type Node[T any] struct { type Node[T any] struct {
Data T `json:"data"` Data T `json:"data"`
Loading…
Cancel
Save