| 1 |
/// The MIT License (MIT) |
| 2 |
/// Copyright (c) 2016 Peter Goldsborough |
| 3 |
/// |
| 4 |
/// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
/// of this software and associated documentation files (the "Software"), to |
| 6 |
/// deal in the Software without restriction, including without limitation the |
| 7 |
/// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 8 |
/// sell copies of the Software, and to permit persons to whom the Software is |
| 9 |
/// furnished to do so, subject to the following conditions: |
| 10 |
/// |
| 11 |
/// The above copyright notice and this permission notice shall be included in |
| 12 |
/// all copies or substantial portions of the Software. |
| 13 |
/// |
| 14 |
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 |
/// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 |
/// IN THE SOFTWARE. |
| 21 |
|
| 22 |
#ifndef LRU_INTERNAL_INFORMATION_HPP |
| 23 |
#define LRU_INTERNAL_INFORMATION_HPP |
| 24 |
|
| 25 |
#include <cstddef> |
| 26 |
#include <tuple> |
| 27 |
#include <utility> |
| 28 |
|
| 29 |
#include <lru/internal/definitions.hpp> |
| 30 |
#include <lru/internal/utility.hpp> |
| 31 |
|
| 32 |
namespace LRU { |
| 33 |
namespace Internal { |
| 34 |
|
| 35 |
/// The value type of internal maps, used to store a value and iterator. |
| 36 |
/// |
| 37 |
/// This information object is the basis of an LRU cache, which must associated |
| 38 |
/// a value and such an order iterator with a key, such that the iterator may be |
| 39 |
/// moved to the front of the order when the key is updated with a new value. |
| 40 |
/// |
| 41 |
/// \tparam Key The key type of the information. |
| 42 |
/// \tparam Value The value type of the information. |
| 43 |
template <typename Key, typename Value> |
| 44 |
struct Information { |
| 45 |
using KeyType = Key; |
| 46 |
using ValueType = Value; |
| 47 |
using QueueIterator = typename Internal::Queue<const Key>::const_iterator; |
| 48 |
|
| 49 |
/// Constructor. |
| 50 |
/// |
| 51 |
/// \param value_ The value for the information. |
| 52 |
/// \param order_ The order iterator for the information. |
| 53 |
explicit Information(const Value& value_, |
| 54 |
QueueIterator order_ = QueueIterator()) |
| 55 |
: value(value_), order(order_) { |
| 56 |
} |
| 57 |
|
| 58 |
/// Constructor. |
| 59 |
/// |
| 60 |
/// \param order_ The order iterator for the information. |
| 61 |
/// \param value_arguments Any number of arguments to perfectly forward to the |
| 62 |
/// value type's constructor. |
| 63 |
// template <typename... ValueArguments> |
| 64 |
// Information(QueueIterator order_, ValueArguments&&... value_arguments) |
| 65 |
// : value(std::forward<ValueArguments>(value_arguments)...), order(order_) { |
| 66 |
// } |
| 67 |
|
| 68 |
/// Constructor. |
| 69 |
/// |
| 70 |
/// \param order_ The order iterator for the information. |
| 71 |
/// \param value_arguments A tuple of arguments to perfectly forward to the |
| 72 |
/// value type's constructor. |
| 73 |
/// |
| 74 |
template <typename... ValueArguments> |
| 75 |
explicit Information(const std::tuple<ValueArguments...>& value_arguments, |
| 76 |
QueueIterator order_ = QueueIterator()) |
| 77 |
: Information( |
| 78 |
order_, value_arguments, Internal::tuple_indices(value_arguments)) { |
| 79 |
} |
| 80 |
|
| 81 |
/// Copy constructor. |
| 82 |
Information(const Information& other) = default; |
| 83 |
|
| 84 |
/// Move constructor. |
| 85 |
Information(Information&& other) = default; |
| 86 |
|
| 87 |
/// Copy assignment operator. |
| 88 |
Information& operator=(const Information& other) = default; |
| 89 |
|
| 90 |
/// Move assignment operator. |
| 91 |
Information& operator=(Information&& other) = default; |
| 92 |
|
| 93 |
/// Destructor. |
| 94 |
virtual ~Information() = default; |
| 95 |
|
| 96 |
/// Compares the information for equality with another information object. |
| 97 |
/// |
| 98 |
/// \param other The other information object to compare to. |
| 99 |
/// \returns True if key and value (not the iterator itself) of the two |
| 100 |
/// information objects are equal, else false. |
| 101 |
virtual bool operator==(const Information& other) const noexcept { |
| 102 |
if (this == &other) return true; |
| 103 |
if (this->value != other.value) return false; |
| 104 |
// We do not compare the iterator (because otherwise two containers |
| 105 |
// holding information would never be equal). We also do not compare |
| 106 |
// the key stored in the iterator, because keys will always have been |
| 107 |
// compared before this operator is called. |
| 108 |
return true; |
| 109 |
} |
| 110 |
|
| 111 |
/// Compares the information for inequality with another information object. |
| 112 |
/// |
| 113 |
/// \param other The other information object to compare for. |
| 114 |
/// \returns True if key and value (not the iterator itself) of the two |
| 115 |
/// information objects are unequal, else false. |
| 116 |
virtual bool operator!=(const Information& other) const noexcept { |
| 117 |
return !(*this == other); |
| 118 |
} |
| 119 |
|
| 120 |
/// The value of the information. |
| 121 |
Value value; |
| 122 |
|
| 123 |
/// The order iterator of the information. |
| 124 |
QueueIterator order; |
| 125 |
|
| 126 |
private: |
| 127 |
/// Implementation for the constructor taking a tuple of arguments for the |
| 128 |
/// value. |
| 129 |
/// |
| 130 |
/// \param order_ The order iterator for the information. |
| 131 |
/// \param value_argument The tuple of arguments to perfectly forward to the |
| 132 |
/// value type's constructor. |
| 133 |
/// \param _ An index sequence to access the elements of the tuple |
| 134 |
template <typename... ValueArguments, std::size_t... Indices> |
| 135 |
Information(const QueueIterator& order_, |
| 136 |
const std::tuple<ValueArguments...>& value_argument, |
| 137 |
std::index_sequence<Indices...> _) |
| 138 |
: value(std::forward<ValueArguments>(std::get<Indices>(value_argument))...) |
| 139 |
, order(order_) { |
| 140 |
} |
| 141 |
}; |
| 142 |
} // namespace Internal |
| 143 |
} // namespace LRU |
| 144 |
|
| 145 |
#endif // LRU_INTERNAL_INFORMATION_HPP |