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/test slice set impl #534

Closed
wants to merge 12 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
slice map backing
  • Loading branch information
saiskee committed Mar 1, 2024
commit 4520e0f3b286e795edcd1d1c880afdc820c85158
12 changes: 6 additions & 6 deletions contrib/pkg/sets/sets.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func Key(id ezkube.ResourceId) string {
return ezkube.KeyWithSeparator(id, defaultSeparator)
}

func Hash(id ezkube.ResourceId) uint64 {
return ezkube.HashedKeyWithSeparator(id, defaultSeparator)
}

// typed keys are helpful for logging; currently unused in the Set implementation but placed here for convenience
func TypedKey(id ezkube.ResourceId) string {
return fmt.Sprintf("%s.%T", Key(id), id)
Expand Down Expand Up @@ -110,12 +114,8 @@ func (t *threadSafeResourceSet) Has(resource ezkube.ResourceId) bool {

// Has returns true if and only if item is contained in the set.
func (s Resources) Has(item ezkube.ResourceId) bool {
for _, e := range s {
if e == item {
return true
}
}
return false
_, found := s.m[Hash(item)]
return found
}

func (t *threadSafeResourceSet) IsSuperset(
Expand Down
49 changes: 27 additions & 22 deletions contrib/pkg/sets/sets_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,33 @@ import (
)

// sets.Resources is a set of strings, implemented via map[string]struct{} for minimal memory consumption.
type Resources []ezkube.ResourceId
type Resources struct {
l []ezkube.ResourceId
// map of hash to index
m map[uint64]uint64
}

func newResources(resources ...ezkube.ResourceId) *Resources {
m := make(Resources, len(resources))
m := &Resources{
l: make([]ezkube.ResourceId, 0, len(resources)),
m: make(map[uint64]uint64, len(resources)),
}
for idx, resource := range resources {
if resource == nil {
continue
}
m[idx] = resource
m.l[idx] = resource
m.m[Hash(resource)] = uint64(idx)
}
return &m
return m
}

func (r *Resources) Find(resourceType, id ezkube.ResourceId) (ezkube.ResourceId, error) {
key := Key(id)
for _, i := range *r {
if i == nil {
continue
}
if key == Key(i) {
return i, nil
}
key := Hash(id)
if idx, ok := r.m[key]; ok {
return r.l[idx], nil
}
return nil, NotFoundErr(resourceType, id)

}

func (r *Resources) Length() int {
Expand Down Expand Up @@ -90,7 +92,7 @@ func (r *Resources) Clone() ResourceSet {

func (r *Resources) Keys() sets.String {
keys := sets.NewString()
for _, e := range *r {
for _, e := range r.l {
key := Key(e)
keys.Insert(key)
}
Expand All @@ -100,7 +102,7 @@ func (r *Resources) Keys() sets.String {
func (r *Resources) List(filterResource ...func(ezkube.ResourceId) bool) []ezkube.ResourceId {
// sort.Sort(res)
var resources []ezkube.ResourceId
for _, e := range *r {
for _, e := range r.l {
var filtered bool
for _, filter := range filterResource {
if filter(e) {
Expand All @@ -117,7 +119,7 @@ func (r *Resources) List(filterResource ...func(ezkube.ResourceId) bool) []ezkub

func (r *Resources) UnsortedList(filterResource ...func(ezkube.ResourceId) bool) []ezkube.ResourceId {
var resources []ezkube.ResourceId
for _, e := range *r {
for _, e := range r.l {
var filtered bool
for _, filter := range filterResource {
if filter(e) {
Expand All @@ -134,7 +136,7 @@ func (r *Resources) UnsortedList(filterResource ...func(ezkube.ResourceId) bool)

func (r *Resources) Map() map[string]ezkube.ResourceId {
res := make(map[string]ezkube.ResourceId)
for _, resource := range *r {
for _, resource := range r.l {
if resource == nil {
continue
}
Expand All @@ -152,16 +154,19 @@ func (r *Resources) Insert(resources ...ezkube.ResourceId) {
if resource == nil {
continue
}
*r = append(*r, resource)
r.l = append(r.l, resource)
hash := Hash(resource)
r.m[hash] = uint64(len(r.l) - 1)
}
}

// Delete removes all items from the set.
func (s *Resources) Delete(items ...ezkube.ResourceId) *Resources {
for _, item := range items {
for idx, e := range *s {
for idx, e := range s.l {
if Key(e) == Key(item) {
*s = append((*s)[:idx], (*s)[idx+1:]...)
s.l = append((s.l)[:idx], (s.l)[idx+1:]...)
delete(s.m, Hash(item))
}
}
}
Expand Down Expand Up @@ -246,7 +251,7 @@ func (s1 *Resources) Intersection(s2 *Resources) *Resources {

// IsSuperset returns true if and only if s1 is a superset of s2.
func (s1 *Resources) IsSuperset(s2 *Resources) bool {
for _, item := range *s1 {
for _, item := range s1.l {
if !s1.Has(item) {
return false
}
Expand Down Expand Up @@ -278,7 +283,7 @@ func (s *Resources) PopAny() (ezkube.ResourceId, bool) {

// Len returns the size of the set.
func (s *Resources) Len() int {
return len(*s)
return len(s.l)
}

func lessString(lhs, rhs string) bool {
Expand Down
Loading