-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from ecmwf/feature/lru-cache
Feature/lru cache
- Loading branch information
Showing
6 changed files
with
191 additions
and
42 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* (C) Copyright 2024- ECMWF. | ||
* | ||
* This software is licensed under the terms of the Apache Licence Version 2.0 | ||
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. | ||
* In applying this licence, ECMWF does not waive the privileges and immunities | ||
* granted to it by virtue of its status as an intergovernmental organisation nor | ||
* does it submit to any jurisdiction. | ||
*/ | ||
|
||
/// @author Christopher Bradley | ||
|
||
#pragma once | ||
|
||
#include <list> | ||
#include <unordered_map> | ||
#include "eckit/exception/Exceptions.h" | ||
|
||
namespace gribjump { | ||
|
||
// Note: not a thread safe container, use an external lock if needed | ||
template <typename K, typename V> | ||
class LRUCache { | ||
public: | ||
LRUCache(size_t capacity) : capacity_(capacity) {} | ||
|
||
void put(const K& key, const V& value) { | ||
if (map_.find(key) == map_.end()) { | ||
if (list_.size() == capacity_) { | ||
auto last = list_.back(); | ||
list_.pop_back(); | ||
map_.erase(last); | ||
} | ||
list_.push_front(key); | ||
} else { | ||
list_.remove(key); | ||
list_.push_front(key); | ||
} | ||
map_[key] = value; | ||
} | ||
|
||
|
||
V& get(const K& key) { | ||
if (map_.find(key) == map_.end()) { | ||
throw eckit::BadValue("Key does not exist"); | ||
} | ||
list_.remove(key); | ||
list_.push_front(key); | ||
return map_[key]; | ||
} | ||
|
||
bool exists(const K& key) { | ||
return map_.find(key) != map_.end(); | ||
} | ||
|
||
typename std::unordered_map<K, V>::const_iterator begin() const { | ||
return map_.begin(); | ||
} | ||
|
||
typename std::unordered_map<K, V>::const_iterator end() const { | ||
return map_.end(); | ||
} | ||
|
||
void clear() { | ||
list_.clear(); | ||
map_.clear(); | ||
} | ||
|
||
private: | ||
size_t capacity_; | ||
std::list<K> list_; | ||
std::unordered_map<K, V> map_; | ||
}; | ||
|
||
} // namespace gribjump |
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,68 @@ | ||
/* | ||
* (C) Copyright 2024- ECMWF. | ||
* | ||
* This software is licensed under the terms of the Apache Licence Version 2.0 | ||
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. | ||
* In applying this licence, ECMWF does not waive the privileges and immunities | ||
* granted to it by virtue of its status as an intergovernmental organisation | ||
* nor does it submit to any jurisdiction. | ||
*/ | ||
|
||
#include <cmath> | ||
#include <fstream> | ||
|
||
#include "eckit/testing/Test.h" | ||
#include "gribjump/info/LRUCache.h" | ||
|
||
|
||
#include "metkit/mars/MarsParser.h" | ||
#include "metkit/mars/MarsExpension.h" | ||
using namespace eckit::testing; | ||
|
||
namespace gribjump { | ||
namespace test { | ||
|
||
// Miscellanous unit tests | ||
//----------------------------------------------------------------------------- | ||
CASE( "test_lru" ){ | ||
|
||
LRUCache<std::string, int> cache(3); | ||
|
||
// Check basic functionality | ||
cache.put("a", 1); | ||
cache.put("b", 2); | ||
cache.put("c", 3); | ||
|
||
EXPECT(cache.get("a") == 1); | ||
EXPECT(cache.get("b") == 2); | ||
EXPECT(cache.get("c") == 3); | ||
|
||
cache.put("d", 4); | ||
|
||
EXPECT_THROWS_AS(cache.get("a"), eckit::BadValue); | ||
EXPECT(cache.get("d") == 4); | ||
|
||
// Check recency is updated with get | ||
cache.put("x", 1); | ||
cache.put("y", 2); | ||
cache.put("z", 3); | ||
|
||
EXPECT(cache.get("z") == 3); | ||
EXPECT(cache.get("y") == 2); | ||
EXPECT(cache.get("x") == 1); | ||
|
||
// z should now be the least recently used | ||
cache.put("w", 1); | ||
|
||
EXPECT_THROWS_AS(cache.get("z"), eckit::BadValue); | ||
} | ||
//----------------------------------------------------------------------------- | ||
|
||
} // namespace test | ||
} // namespace gribjump | ||
|
||
|
||
int main(int argc, char **argv) | ||
{ | ||
return run_tests ( argc, argv ); | ||
} |