Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sai/efficient union #561

Merged
merged 15 commits into from
May 16, 2024
Next Next commit
efficient union
saiskee committed May 7, 2024
commit d4d86b3b3f1b6c8d573d98b667251724fbbb80bc
30 changes: 24 additions & 6 deletions contrib/pkg/sets/v2/sets.go
Original file line number Diff line number Diff line change
@@ -229,13 +229,31 @@ func (s *resourceSet[T]) Delete(resource ezkube.ResourceId) {
}

func (s *resourceSet[T]) Union(set ResourceSet[T]) ResourceSet[T] {
list := []T{}
for _, resource := range s.Generic().Union(set.Generic()).List() {
list = append(list, resource.(T))
merged := make([]T, 0, len(s.set)+set.Len())
idx := 0

// Iterate through the second set
set.Iter(func(_ int, resource T) bool {
// Ensure all elements from s.set are added in sorted order
for idx < len(s.set) && ezkube.ResourceIdsCompare(s.set[idx], resource) < 0 {
merged = append(merged, s.set[idx])
idx++
}
// If elements are equal, skip the element from s.set and use the element from the argument set
if idx < len(s.set) && ezkube.ResourceIdsCompare(s.set[idx], resource) == 0 {
idx++ // Increment to skip the element in s.set since it's equal and we use the one from set
}
// Add the current element from the second set
merged = append(merged, resource)
return true
})

// Append any remaining elements from s.set that were not added
if idx < len(s.set) {
merged = append(merged, s.set[idx:]...)
}
return NewResourceSet[T](
list...,
)

return &resourceSet[T]{set: merged, lock: sync.RWMutex{}}
}

func (s *resourceSet[T]) Difference(set ResourceSet[T]) ResourceSet[T] {