Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a Clear() function to generic sets
This is useful when a given set is shared through pointers, and therefore can't be replaced with a new empty set. This replaces set.Delete(set.UnsortedList()...), and allows the compiler to optimize the function to a call to runtime.mapclear() when the set content type is reflexive for ==. That optimization *is* currently accessible using s := Set[...]{} ... for i := range s { delete(s, i) } but this circumvents the published API for sets; calling s.Delete(i) instead can't be optimized in this fashion. Alternatives considered but discarded include: * turning sets into a pointer type (this isn't possible because pointer types can't be receivers) * using a pointer receiver for the Clear() function, i.e. func (s *Set[T]) Clear() { *s = New[T]() } but this doesn't update shared references. Signed-off-by: Stephen Kitt <skitt@redhat.com> Kubernetes-commit: 57b0f7aa385b631c0be4702693a460cb93421702
- Loading branch information