Skip to content

Commit

Permalink
add KeysIter and ValuesIter methods to simplelru
Browse files Browse the repository at this point in the history
  • Loading branch information
paulcacheux committed Mar 4, 2025
1 parent 78f647c commit be15808
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions pkg/security/utils/lru/simplelru/lru.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package simplelru

import (
"errors"
"iter"

"github.com/DataDog/datadog-agent/pkg/security/utils/lru/internal"
)
Expand Down Expand Up @@ -131,6 +132,17 @@ func (c *LRU[K, V]) Keys() []K {
return keys
}

// KeysIter returns an iterator of the keys in the cache, from oldest to newest.
func (c *LRU[K, V]) KeysIter() iter.Seq[V] {
return func(yield func(K) bool) {
for ent := c.evictList.Back(); ent != nil; ent = ent.PrevEntry() {
if !yield(ent.Key) {
return
}
}
}
}

// Values returns a slice of the values in the cache, from oldest to newest.
func (c *LRU[K, V]) Values() []V {
values := make([]V, len(c.items))
Expand All @@ -142,6 +154,17 @@ func (c *LRU[K, V]) Values() []V {
return values
}

// ValuesIter returns an iterator of the values in the cache, from oldest to newest.
func (c *LRU[K, V]) ValuesIter() iter.Seq[V] {
return func(yield func(V) bool) {
for ent := c.evictList.Back(); ent != nil; ent = ent.PrevEntry() {
if !yield(ent.Value) {
return
}
}
}
}

// Len returns the number of items in the cache.
func (c *LRU[K, V]) Len() int {
return c.evictList.Length()
Expand Down

0 comments on commit be15808

Please sign in to comment.