1
0
mirror of https://github.com/containers/image.git synced 2025-04-18 19:44:05 +03:00
image/internal/set/set.go
Miloslav Trmač bc6b35d0c9 Replace Set.Values with a non-allocating Set.All
Signed-off-by: Miloslav Trmač <mitr@redhat.com>
2025-03-12 20:16:03 +01:00

63 lines
922 B
Go

package set
import (
"iter"
"golang.org/x/exp/maps"
)
// FIXME:
// - Docstrings
// - This should be in a public library somewhere
type Set[E comparable] struct {
m map[E]struct{}
}
func New[E comparable]() *Set[E] {
return &Set[E]{
m: map[E]struct{}{},
}
}
func NewWithValues[E comparable](values ...E) *Set[E] {
s := New[E]()
for _, v := range values {
s.Add(v)
}
return s
}
func (s *Set[E]) Add(v E) {
s.m[v] = struct{}{} // Possibly writing the same struct{}{} presence marker again.
}
func (s *Set[E]) AddSlice(slice []E) {
for _, v := range slice {
s.Add(v)
}
}
func (s *Set[E]) Delete(v E) {
delete(s.m, v)
}
func (s *Set[E]) Contains(v E) bool {
_, ok := s.m[v]
return ok
}
func (s *Set[E]) Empty() bool {
return len(s.m) == 0
}
func (s *Set[E]) All() iter.Seq[E] {
return func(yield func(E) bool) {
for _, v := range maps.Keys(s.m) {
if !yield(v) {
return
}
}
}
}