Skip to content

Commit

Permalink
fixed get_freq_items()
Browse files Browse the repository at this point in the history
  • Loading branch information
myui committed Jan 27, 2025
1 parent 4897b95 commit 53b743c
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
7 changes: 4 additions & 3 deletions rtrec/utils/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ def __repr__(self) -> str:
"""
return f"LRUFreqCache(capacity={self.capacity}, size={len(self.data)})"

def get_freq_items(self, n: Optional[int] = None) -> Iterator[Tuple[Any, int]]:
def get_freq_items(self, n: Optional[int] = None) -> Iterator[Tuple[Any, Any]]:
"""
Retrieve the top `n` most frequently used items in the cache. If `n` is None,
return all items sorted by frequency.
Expand All @@ -169,5 +169,6 @@ def get_freq_items(self, n: Optional[int] = None) -> Iterator[Tuple[Any, int]]:
Iterator[Tuple[Any, int]]: An iterator of (key, frequency) pairs, sorted by frequency in descending order.
"""
# If n is not specified, return all items sorted by frequency
items = sorted(self.data.items(), key=lambda item: item[1][1], reverse=True)
return iter(items if n is None else items[:n])
sorted_items = sorted(self.data.items(), key=lambda item: item[1][1], reverse=True)
for key, (value, _) in (sorted_items if n is None else sorted_items[:n]):
yield key, value
20 changes: 14 additions & 6 deletions tests/utils/test_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@ def test_update_frequency(cache):
"""Test that frequency is updated when an item is accessed."""
cache["a"] = "apple"
cache["b"] = "banana"
cache["c"] = "cherry"

# Access "a" to update its frequency
# Update the frequency of "a" by accessing it multiple times
cache["a"]

# Add a third item to force eviction
cache["c"] = "cherry"
# "b" should be evicted because "a" has been accessed more frequently
cache["d"] = "date"

# "b" should be evicted because "a" has been accessed more frequently
assert "b" not in cache
Expand All @@ -53,20 +54,27 @@ def test_get_freq_items(cache):
cache["a"] = "apple"
cache["b"] = "banana"
cache["c"] = "cherry"
cache["d"] = "date"

# Access some items to update their frequencies
cache["a"]
cache["a"]

# "b" should be evicted because "a" has been accessed more frequently
cache["d"] = "date"

# Access "c" to update its frequency
cache["c"]

# Get top 2 most frequently used items
freq_items = list(cache.get_freq_items(2))
assert freq_items == [("a", 3), ("c", 1)]
assert freq_items == [("a", "apple"), ("c", "cherry")]

# Get all items sorted by frequency
all_items = list(cache.get_freq_items())
assert all_items == [("a", 3), ("c", 1), ("b", 1), ("d", 1)]
assert all_items == [("a", "apple"), ("c", "cherry"), ("d", "date")]

all_items = list(cache.get_freq_items(n=4))
assert all_items == [("a", "apple"), ("c", "cherry"), ("d", "date")]

def test_get_with_default(cache):
"""Test the get method with a default value."""
Expand Down

0 comments on commit 53b743c

Please sign in to comment.