Skip to content

Commit

Permalink
remove issortedby
Browse files Browse the repository at this point in the history
  • Loading branch information
saiskee committed May 16, 2024
1 parent dbfae1c commit d9b74a3
Showing 1 changed file with 2 additions and 32 deletions.
34 changes: 2 additions & 32 deletions contrib/pkg/sets/v2/sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,6 @@ import (
"sigs.k8s.io/controller-runtime/pkg/client"
)

const (
ResourceId SortType = iota
)

type SortType int

// ResourceSet is a thread-safe container for a set of resources.
// It provides a set of operations for working with the set of resources, typically used for managing Kubernetes resources.
Expand Down Expand Up @@ -75,10 +70,6 @@ type ResourceSet[T client.Object] interface {
Clone() ResourceSet[T]
// ShallowCopy returns a shallow copy of the set
ShallowCopy() ResourceSet[T]

// Guarantees that when running Iter, Filter, or FilterOutAndCreateList, elements in the set will be processed in a
// sorted order by ResourceId. See ezkube.ResourceIdsCompare for the definition of ResourceId sorting.
IsSortedBy(SortType) bool
}

// ResourceDelta represents the set of changes between two ResourceSets.
Expand All @@ -101,14 +92,6 @@ type resourceSet[T client.Object] struct {
set []T
}

func (s *resourceSet[T]) IsSortedBy(r SortType) bool {
switch r {
case ResourceId:
return true
}
return false
}

func NewResourceSet[T client.Object](
resources ...T,
) ResourceSet[T] {
Expand Down Expand Up @@ -258,23 +241,10 @@ func (s *resourceSet[T]) Union(set ResourceSet[T]) ResourceSet[T] {
return s.ShallowCopy()
}

// if we can use the sets `Iter` method to iterate over the set in a sorted order,
// we can use that to iterate over the set and add the elements to the new set.
if set.IsSortedBy(ResourceId) {
return s.unionSortedSet(set)
}

// fallback to generic union, and sort after the fact (in NewResourceSet())
list := []T{}
for _, resource := range s.Generic().Union(set.Generic()).List() {
list = append(list, resource.(T))
}
return NewResourceSet[T](
list...,
)
return s.unionSortedSet(set)
}

// Assuming that the argument set is sorted by resource id (via the SortedByResourceId method),
// Assuming that the argument set is sorted by resource id,
// this method will efficiently union the two sets together and return the unioned set.
func (s *resourceSet[T]) unionSortedSet(set ResourceSet[T]) *resourceSet[T] {
merged := make([]T, 0, len(s.set)+set.Len())
Expand Down

0 comments on commit d9b74a3

Please sign in to comment.