Some checks failed
Docker. / Ubuntu (push) Has been cancelled
User-agent updater. / User-agent (push) Failing after 15s
Lock Threads / lock (push) Failing after 10s
Waiting for answer. / waiting-for-answer (push) Failing after 22s
Close stale issues and PRs / stale (push) Successful in 13s
Needs user action. / needs-user-action (push) Failing after 8s
Can't reproduce. / cant-reproduce (push) Failing after 8s
69 lines
1.4 KiB
C++
69 lines
1.4 KiB
C++
// This file is part of Desktop App Toolkit,
|
|
// a set of libraries for developing nice desktop applications.
|
|
//
|
|
// For license and copyright information please follow this link:
|
|
// https://github.com/desktop-app/legal/blob/master/LEGAL
|
|
//
|
|
#pragma once
|
|
|
|
#include <list>
|
|
#include <unordered_map>
|
|
|
|
namespace base {
|
|
|
|
template <typename Entry>
|
|
class last_used_cache {
|
|
public:
|
|
void up(Entry entry);
|
|
void remove(Entry entry);
|
|
void clear();
|
|
|
|
Entry take_lowest();
|
|
|
|
private:
|
|
std::list<Entry> _queue;
|
|
std::unordered_map<Entry, typename std::list<Entry>::iterator> _map;
|
|
|
|
};
|
|
|
|
template <typename Entry>
|
|
void last_used_cache<Entry>::up(Entry entry) {
|
|
if (!_queue.empty() && _queue.back() == entry) {
|
|
return;
|
|
}
|
|
const auto i = _map.find(entry);
|
|
if (i != end(_map)) {
|
|
_queue.splice(end(_queue), _queue, i->second);
|
|
} else {
|
|
_map.emplace(entry, _queue.insert(end(_queue), entry));
|
|
}
|
|
}
|
|
|
|
template <typename Entry>
|
|
void last_used_cache<Entry>::remove(Entry entry) {
|
|
const auto i = _map.find(entry);
|
|
if (i != end(_map)) {
|
|
_queue.erase(i->second);
|
|
_map.erase(i);
|
|
}
|
|
}
|
|
|
|
template <typename Entry>
|
|
void last_used_cache<Entry>::clear() {
|
|
_queue.clear();
|
|
_map.clear();
|
|
}
|
|
|
|
template <typename Entry>
|
|
Entry last_used_cache<Entry>::take_lowest() {
|
|
if (_queue.empty()) {
|
|
return Entry();
|
|
}
|
|
auto result = std::move(_queue.front());
|
|
_queue.erase(begin(_queue));
|
|
_map.erase(result);
|
|
return result;
|
|
}
|
|
|
|
} // namespace base
|