| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | #ifndef LRU_CACHE_HPP |
| | #define LRU_CACHE_HPP |
| |
|
| | #include <cassert> |
| | #include <chrono> |
| | #include <cstddef> |
| | #include <functional> |
| | #include <iterator> |
| | #include <list> |
| | #include <stdexcept> |
| | #include <unordered_map> |
| |
|
| | #include <lru/cache-tags.hpp> |
| | #include <lru/error.hpp> |
| | #include <lru/internal/base-cache.hpp> |
| | #include <lru/internal/information.hpp> |
| | #include <lru/internal/last-accessed.hpp> |
| |
|
| | namespace LRU { |
| | namespace Internal { |
| | template <typename Key, |
| | typename Value, |
| | typename HashFunction, |
| | typename KeyEqual> |
| | using UntimedCacheBase = Internal::BaseCache<Key, |
| | Value, |
| | Internal::Information, |
| | HashFunction, |
| | KeyEqual, |
| | Tag::BasicCache>; |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | template <typename Key, |
| | typename Value, |
| | typename HashFunction = std::hash<Key>, |
| | typename KeyEqual = std::equal_to<Key>> |
| | class Cache |
| | : public Internal::UntimedCacheBase<Key, Value, HashFunction, KeyEqual> { |
| | private: |
| | using super = Internal::UntimedCacheBase<Key, Value, HashFunction, KeyEqual>; |
| | using PRIVATE_BASE_CACHE_MEMBERS; |
| |
|
| | public: |
| | using PUBLIC_BASE_CACHE_MEMBERS; |
| | using typename super::size_t; |
| |
|
| | |
| | |
| | explicit Cache(size_t capacity = Internal::DEFAULT_CAPACITY, |
| | const HashFunction& hash = HashFunction(), |
| | const KeyEqual& equal = KeyEqual()) |
| | : super(capacity, hash, equal) { |
| | } |
| |
|
| | |
| | |
| | template <typename Iterator> |
| | Cache(size_t capacity, |
| | Iterator begin, |
| | Iterator end, |
| | const HashFunction& hash = HashFunction(), |
| | const KeyEqual& equal = KeyEqual()) |
| | : super(capacity, begin, end, hash, equal) { |
| | } |
| |
|
| | |
| | |
| | template <typename Iterator> |
| | Cache(Iterator begin, |
| | Iterator end, |
| | const HashFunction& hash = HashFunction(), |
| | const KeyEqual& equal = KeyEqual()) |
| | : super(begin, end, hash, equal) { |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | template <typename Range, typename = Internal::enable_if_range<Range>> |
| | Cache(size_t capacity, |
| | Range&& range, |
| | const HashFunction& hash = HashFunction(), |
| | const KeyEqual& equal = KeyEqual()) |
| | : super(capacity, std::forward<Range>(range), hash, equal) { |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | template <typename Range, typename = Internal::enable_if_range<Range>> |
| | explicit Cache(Range&& range, |
| | const HashFunction& hash = HashFunction(), |
| | const KeyEqual& equal = KeyEqual()) |
| | : super(std::forward<Range>(range), hash, equal) { |
| | } |
| |
|
| | |
| | |
| | Cache(InitializerList list, |
| | const HashFunction& hash = HashFunction(), |
| | const KeyEqual& equal = KeyEqual()) |
| | : super(list, hash, equal) { |
| | } |
| |
|
| | |
| | |
| | Cache(size_t capacity, |
| | InitializerList list, |
| | const HashFunction& hash = HashFunction(), |
| | const KeyEqual& equal = KeyEqual()) |
| | : super(capacity, list, hash, equal) { |
| | } |
| |
|
| | |
| | UnorderedIterator find(const Key& key) override { |
| | auto iterator = _map.find(key); |
| | if (iterator != _map.end()) { |
| | _register_hit(key, iterator->second.value); |
| | _move_to_front(iterator->second.order); |
| | _last_accessed = iterator; |
| | } else { |
| | _register_miss(key); |
| | } |
| |
|
| | return {*this, iterator}; |
| | } |
| |
|
| | |
| | UnorderedConstIterator find(const Key& key) const override { |
| | auto iterator = _map.find(key); |
| | if (iterator != _map.end()) { |
| | _register_hit(key, iterator->second.value); |
| | _move_to_front(iterator->second.order); |
| | _last_accessed = iterator; |
| | } else { |
| | _register_miss(key); |
| | } |
| |
|
| | return {*this, iterator}; |
| | } |
| |
|
| | |
| | const Key& front() const noexcept { |
| | if (is_empty()) { |
| | throw LRU::Error::EmptyCache("front"); |
| | } else { |
| | |
| | return _order.back(); |
| | } |
| | } |
| |
|
| | |
| | const Key& back() const noexcept { |
| | if (is_empty()) { |
| | throw LRU::Error::EmptyCache("back"); |
| | } else { |
| | |
| | return _order.front(); |
| | } |
| | } |
| | }; |
| |
|
| | namespace Lowercase { |
| | template <typename... Ts> |
| | using cache = Cache<Ts...>; |
| | } |
| |
|
| | } |
| |
|
| | #endif |
| |
|