-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Cleanup: strip our unused functions and clean up namespaces
- Loading branch information
Showing
23 changed files
with
791 additions
and
1,202 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 |
---|---|---|
|
@@ -10,6 +10,8 @@ | |
|
||
namespace ROCKY_NAMESPACE | ||
{ | ||
using namespace ROCKY_NAMESPACE::util; | ||
|
||
class IOOptions; | ||
|
||
/** | ||
|
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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,74 @@ | ||
/** | ||
* rocky c++ | ||
* Copyright 2023 Pelican Mapping | ||
* MIT License | ||
*/ | ||
#pragma once | ||
#include <rocky/Common.h> | ||
#include <mutex> | ||
#include <unordered_map> | ||
#include <list> | ||
#include <algorithm> | ||
|
||
namespace ROCKY_NAMESPACE | ||
{ | ||
namespace util | ||
{ | ||
// Adapted from https://www.geeksforgeeks.org/lru-cache-implementation | ||
template<class K, class V> | ||
class LRUCache | ||
{ | ||
private: | ||
mutable std::mutex mutex; | ||
int capacity; | ||
using E = typename std::pair<K, V>; | ||
typename std::list<E> cache; | ||
std::unordered_map<K, typename std::list<E>::iterator> map; | ||
|
||
public: | ||
int hits = 0; | ||
int gets = 0; | ||
|
||
LRUCache(int capacity_ = 32) : capacity(capacity_) { } | ||
|
||
inline void setCapacity(int value) | ||
{ | ||
std::scoped_lock L(mutex); | ||
cache.clear(); | ||
map.clear(); | ||
hits = 0; | ||
gets = 0; | ||
capacity = std::max(0, value); | ||
} | ||
|
||
inline V get(const K& key) | ||
{ | ||
if (capacity == 0) | ||
return V(); | ||
std::scoped_lock L(mutex); | ||
++gets; | ||
auto it = map.find(key); | ||
if (it == map.end()) | ||
return V(); | ||
cache.splice(cache.end(), cache, it->second); | ||
++hits; | ||
return it->second->second; | ||
} | ||
|
||
inline void put(const K& key, const V& value) | ||
{ | ||
if (capacity == 0) | ||
return; | ||
std::scoped_lock L(mutex); | ||
if (cache.size() == capacity) | ||
{ | ||
auto first_key = cache.front().first; | ||
cache.pop_front(); | ||
map.erase(first_key); | ||
} | ||
cache.emplace_back(key, value); | ||
map[key] = --cache.end(); | ||
} | ||
}; | ||
} | ||
} |
Oops, something went wrong.