| 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_INSERTION_RESULT_HPP |
| 23 |
#define LRU_INSERTION_RESULT_HPP |
| 24 |
|
| 25 |
#include <algorithm> |
| 26 |
#include <type_traits> |
| 27 |
#include <utility> |
| 28 |
|
| 29 |
namespace LRU { |
| 30 |
|
| 31 |
/// The result of an insertion into a cache. |
| 32 |
/// |
| 33 |
/// This is a semantically nicer alternative to a generic `std::pair`, as is |
| 34 |
/// returned by `std::unordered_map` or so. It still has the same static |
| 35 |
/// interface as the `std::pair` (with `first` and `second` members), but adds |
| 36 |
/// nicer `was_inserted()` and `iterator()` accessors. |
| 37 |
/// |
| 38 |
/// \tparam Iterator The class of the iterator contained in the result. |
| 39 |
template <typename Iterator> |
| 40 |
struct InsertionResult final { |
| 41 |
using IteratorType = Iterator; |
| 42 |
|
| 43 |
/// Constructor. |
| 44 |
/// |
| 45 |
/// \param result Whether the result was successful. |
| 46 |
/// \param iterator The iterator pointing to the inserted or updated key. |
| 47 |
InsertionResult(bool result, Iterator iterator) |
| 48 |
: first(result), second(iterator) { |
| 49 |
} |
| 50 |
|
| 51 |
/// \returns True if the key was newly inserted, false if it was only updated. |
| 52 |
bool was_inserted() const noexcept { |
| 53 |
return first; |
| 54 |
} |
| 55 |
|
| 56 |
/// \returns The iterator pointing to the inserted or updated key. |
| 57 |
Iterator iterator() const noexcept { |
| 58 |
return second; |
| 59 |
} |
| 60 |
|
| 61 |
/// \copydoc was_inserted |
| 62 |
explicit operator bool() const noexcept { |
| 63 |
return was_inserted(); |
| 64 |
} |
| 65 |
|
| 66 |
/// Whether the result was successful. |
| 67 |
bool first; |
| 68 |
|
| 69 |
/// The iterator pointing to the inserted or updated key. |
| 70 |
Iterator second; |
| 71 |
}; |
| 72 |
|
| 73 |
} // namespace LRU |
| 74 |
|
| 75 |
|
| 76 |
#endif // LRU_INSERTION_RESULT_HPP |