util/set: add iterator support to Set[T] (#20159)

This patch adds:

- Set.All which returns an iter.Seq to complement Set.Slice.

- Set.AddSeq which adds an iter.Seq.

- Set.DeleteSeq which deletes an iter.Seq to complement Set.AddSeq
  and provide the missing method for deleting multiple elements.

- Set.DeleteSlice and Set.DeleteSet to complement AddSlice and AddSet.

Updates #cleanup

Signed-off-by: Simon Law <sfllaw@tailscale.com>
This commit is contained in:
Simon Law
2026-06-18 00:12:56 -07:00
committed by GitHub
parent be2f554dd3
commit e3b16135b2
2 changed files with 70 additions and 0 deletions
+34
View File
@@ -50,6 +50,40 @@ func TestSet(t *testing.T) {
t.Errorf("slice missing %d (%#v)", e, es)
}
}
s.Delete(1)
if s.Contains(1) {
t.Error("shouldn't have 1")
}
if !s.Contains(2) {
t.Error("missing 2")
}
if !s.Contains(3) {
t.Error("missing 3")
}
if !s.Contains(4) {
t.Error("missing 4")
}
if s.Len() != 3 {
t.Errorf("wrong len %d; want 3", s.Len())
}
s.DeleteSeq(slices.Values([]int{2, 3}))
if s.Contains(1) {
t.Error("shouldn't have 1")
}
if s.Contains(2) {
t.Error("shouldn't have 2")
}
if s.Contains(3) {
t.Error("shouldn't have 3")
}
if !s.Contains(4) {
t.Error("missing 4")
}
if s.Len() != 1 {
t.Errorf("wrong len %d; want 1", s.Len())
}
}
func TestSetOf(t *testing.T) {