Skip to content

Commit

Permalink
Update LruCache for consistency
Browse files Browse the repository at this point in the history
  • Loading branch information
jbaldwin committed Apr 8, 2019
1 parent 29b4ec7 commit 5803db4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 35 deletions.
20 changes: 8 additions & 12 deletions inc/cappuccino/LruCache.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,8 @@ class LruCache {
const KeyType& key,
ValueType value) -> void;

template <template <class...> typename RangeType>
auto InsertRange(
RangeType<KeyValue> key_value_range) -> void;
template <template <class...> typename RangeType, template <class, class> typename PairType>
auto InsertRange(
RangeType<PairType<KeyType, ValueType>> key_value_range) -> void;
template <typename RangeType>
auto InsertRange(RangeType&& key_value_range) -> void;

auto Delete(
const KeyType& key) -> bool;
Expand All @@ -53,12 +49,12 @@ class LruCache {
auto Find(
const KeyType& key) -> std::optional<ValueType>;

template <template <class...> typename RangeType>
auto FindRange(
RangeType<KeyType, std::optional<ValueType>>& key_optional_value_range) -> void;
template <template <class...> typename RangeType, template <class, class> typename PairType>
auto FindRange(
RangeType<PairType<KeyType, std::optional<ValueType>>>& key_optional_value_range) -> void;
template <typename RangeType>
auto FindRange(const RangeType& keys) -> std::unordered_map<KeyType, std::optional<ValueType>>;

template <typename RangeType>
auto FindRangeFill(
RangeType&& key_optional_value_range) -> void;

auto GetUsedSize() const -> size_t;

Expand Down
44 changes: 21 additions & 23 deletions inc/cappuccino/LruCache.tcc
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,18 @@ LruCache<KeyType, ValueType, SyncType>::LruCache(
}

template <typename KeyType, typename ValueType, SyncImplEnum SyncType>
auto LruCache<KeyType, ValueType, SyncType>::Insert(const KeyType& key, ValueType value) -> void
auto LruCache<KeyType, ValueType, SyncType>::Insert(
const KeyType& key,
ValueType value) -> void
{
LockScopeGuard<SyncType> guard { m_lock };
doInsertUpdate(key, std::move(value));
};

template <typename KeyType, typename ValueType, SyncImplEnum SyncType>
template <template <class...> typename RangeType>
auto LruCache<KeyType, ValueType, SyncType>::InsertRange(
RangeType<KeyValue> key_value_range) -> void
{
LockScopeGuard<SyncType> guard { m_lock };
for (auto& [key, value] : key_value_range) {
doInsertUpdate(key, std::move(value));
}
};

template <typename KeyType, typename ValueType, SyncImplEnum SyncType>
template <template <class...> typename RangeType, template <class, class> typename PairType>
template <typename RangeType>
auto LruCache<KeyType, ValueType, SyncType>::InsertRange(
RangeType<PairType<KeyType, ValueType>> key_value_range) -> void
RangeType&& key_value_range) -> void
{
LockScopeGuard<SyncType> guard { m_lock };
for (auto& [key, value] : key_value_range) {
Expand Down Expand Up @@ -83,27 +74,34 @@ auto LruCache<KeyType, ValueType, SyncType>::DeleteRange(
}

template <typename KeyType, typename ValueType, SyncImplEnum SyncType>
auto LruCache<KeyType, ValueType, SyncType>::Find(const KeyType& key) -> std::optional<ValueType>
auto LruCache<KeyType, ValueType, SyncType>::Find(
const KeyType& key) -> std::optional<ValueType>
{
LockScopeGuard<SyncType> guard { m_lock };
return doFind(key);
}

template <typename KeyType, typename ValueType, SyncImplEnum SyncType>
template <template <class...> typename RangeType>
template <typename RangeType>
auto LruCache<KeyType, ValueType, SyncType>::FindRange(
RangeType<KeyType, std::optional<ValueType>>& key_optional_value_range) -> void
const RangeType& keys) -> std::unordered_map<KeyType, std::optional<ValueType>>
{
LockScopeGuard<SyncType> guard { m_lock };
for (auto& [key, optional_value] : key_optional_value_range) {
optional_value = doFind(key);
std::unordered_map<KeyType, std::optional<ValueType>> output;
output.reserve(std::size(keys));

for (auto& key : keys) {
output.emplace(key, std::nullopt);
}

FindRangeFill(output);

return output;
}

template <typename KeyType, typename ValueType, SyncImplEnum SyncType>
template <template <class...> typename RangeType, template <class, class> typename PairType>
auto LruCache<KeyType, ValueType, SyncType>::FindRange(
RangeType<PairType<KeyType, std::optional<ValueType>>>& key_optional_value_range) -> void
template <typename RangeType>
auto LruCache<KeyType, ValueType, SyncType>::FindRangeFill(
RangeType&& key_optional_value_range) -> void
{
LockScopeGuard<SyncType> guard { m_lock };
for (auto& [key, optional_value] : key_optional_value_range) {
Expand Down

0 comments on commit 5803db4

Please sign in to comment.