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

Add Core::FIFOCache #79

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open

Add Core::FIFOCache #79

wants to merge 3 commits into from

Conversation

SimBe195
Copy link
Collaborator

Cache based on an unordered_map with a maximum size such that when the cache is full and
a new item is trying to be added, the oldest item in the cache gets removed.
Note: oldest is determined by order of insertion, not order of last access.

Example:

FIFOCache<int, std::string> cache(2);  // Cache has room for two items
cache.put(1, "one");  // Internal data: {1: "one"}
cache.put(2, "two");  // Internal data: {1: "one", 2: "two"}
cache.put(3, "three");  // Oldest element is deleted since max size was reached. Internal data: {2: "two", 3: "three"}

src/Core/FIFOCache.hh Outdated Show resolved Hide resolved

template<typename Key, typename Value, typename Hash, typename KeyEqual>
Value& FIFOCache<Key, Value, Hash, KeyEqual>::get(const Key& key) {
return cacheMap_.at(key);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This throws an exception if the key is not present. As nobody ever catches these it will probably terminate the application. I think it would be better to return std::optional<Value&> and for an interface the default-constructs the value add an operator[] function.

For the version returning std::optional<Value&> we could also have a const version of the function.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

C++ unfortunately doesn't support optional references so std::optional<Value&> doesn't work . I have implemented this now by using std::optional<std::reference_wrapper<Value>>. It could also be done by using Value* as return value instead and returning nullptr when the key is not found.

@curufinwe
Copy link
Contributor

PS: same comment about adding inline as in #78

@SimBe195 SimBe195 requested a review from curufinwe December 5, 2024 10:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants