forked from COVESA/dlt-viewer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'COVESA:master' into master
- Loading branch information
Showing
14 changed files
with
195 additions
and
203 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#ifndef QDLTLRUCACHE_HPP | ||
#define QDLTLRUCACHE_HPP | ||
|
||
#include <unordered_map> | ||
#include <list> | ||
#include <stdexcept> | ||
|
||
template<typename Key, typename Value> | ||
class QDltLruCache { | ||
|
||
struct CacheEntry { | ||
Key key; | ||
Value value; | ||
}; | ||
|
||
using CacheListIterator = typename std::list<CacheEntry>::iterator; | ||
public: | ||
|
||
QDltLruCache(size_t capacity) : | ||
m_capacity(capacity) { | ||
} | ||
|
||
void put(const Key& key, const Value& value) { | ||
auto it = m_keyIteratorsMap.find(key); | ||
m_cacheItems.push_front(CacheEntry{key, value}); | ||
if (it != m_keyIteratorsMap.end()) { | ||
m_cacheItems.erase(it->second); | ||
m_keyIteratorsMap.erase(it); | ||
} | ||
m_keyIteratorsMap[key] = m_cacheItems.begin(); | ||
|
||
if (m_keyIteratorsMap.size() > m_capacity) { | ||
auto last = std::prev(m_cacheItems.end()); | ||
m_keyIteratorsMap.erase(last->key); | ||
m_cacheItems.pop_back(); | ||
} | ||
} | ||
|
||
const Value& get(const Key& key) { | ||
auto it = m_keyIteratorsMap.find(key); | ||
if (it == m_keyIteratorsMap.end()) { | ||
throw std::range_error("no such key in cache found"); | ||
} | ||
|
||
m_cacheItems.splice(m_cacheItems.begin(), m_cacheItems, it->second); | ||
return it->second->value; | ||
} | ||
|
||
bool exists(const Key& key) const { | ||
return m_keyIteratorsMap.find(key) != m_keyIteratorsMap.end(); | ||
} | ||
|
||
private: | ||
std::list<CacheEntry> m_cacheItems; | ||
std::unordered_map<Key, CacheListIterator> m_keyIteratorsMap; | ||
const size_t m_capacity; | ||
}; | ||
|
||
#endif // QDLTLRUCACHE_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.