Skip to content

Commit

Permalink
add Get
Browse files Browse the repository at this point in the history
  • Loading branch information
saiskee committed Apr 8, 2024
1 parent eb0dcc9 commit 2edf415
Showing 1 changed file with 21 additions and 1 deletion.
22 changes: 21 additions & 1 deletion contrib/pkg/sets/v2/sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,12 @@ type ResourceSet[T client.Object] interface {
Difference(set ResourceSet[T]) ResourceSet[T]
// Return the intersection with the provided set
Intersection(set ResourceSet[T]) ResourceSet[T]
// Find the resource with the given ID
// Find the resource with the given ID.
// Returns a NotFoundErr error if the resource is not found.
Find(resource ezkube.ResourceId) (T, error)
// Find the resource with the given ID.
// Returns nil if the resource is not found.
Get(resource ezkube.ResourceId) T
// Get the length of the set
Len() int
Length() int
Expand Down Expand Up @@ -274,6 +278,22 @@ func (s *resourceSet[T]) Intersection(set ResourceSet[T]) ResourceSet[T] {
return result
}

func (s *resourceSet[T]) Get(resource ezkube.ResourceId) T {
s.lock.RLock()
defer s.lock.RUnlock()

insertIndex, found := slices.BinarySearchFunc(
s.set,
resource,
func(a T, b ezkube.ResourceId) int { return s.compareFunc(a, b) },
)
if found {
return s.set[insertIndex]
}
var r T
return r
}

func (s *resourceSet[T]) Find(resource ezkube.ResourceId) (T, error) {
s.lock.RLock()
defer s.lock.RUnlock()
Expand Down

0 comments on commit 2edf415

Please sign in to comment.