|
| 1 | +/* vim:set ts=2 sw=2 sts=2 et: */ |
| 2 | +/** |
| 3 | + * \author Marcus Holland-Moritz ([email protected]) |
| 4 | + * \copyright Copyright (c) Marcus Holland-Moritz |
| 5 | + * |
| 6 | + * This file is part of dwarfs. |
| 7 | + * |
| 8 | + * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | + * of this software and associated documentation files (the “Software”), to deal |
| 10 | + * in the Software without restriction, including without limitation the rights |
| 11 | + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | + * copies of the Software, and to permit persons to whom the Software is |
| 13 | + * furnished to do so, subject to the following conditions: |
| 14 | + * |
| 15 | + * The above copyright notice and this permission notice shall be included in |
| 16 | + * all copies or substantial portions of the Software. |
| 17 | + * |
| 18 | + * THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 19 | + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 20 | + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 21 | + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 22 | + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 23 | + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 24 | + * SOFTWARE. |
| 25 | + * |
| 26 | + * SPDX-License-Identifier: MIT |
| 27 | + */ |
| 28 | + |
| 29 | +#pragma once |
| 30 | + |
| 31 | +#include <functional> |
| 32 | +#include <iterator> |
| 33 | +#include <list> |
| 34 | + |
| 35 | +#include <parallel_hashmap/phmap.h> |
| 36 | + |
| 37 | +namespace dwarfs::reader::internal { |
| 38 | + |
| 39 | +template <typename KeyT, typename T> |
| 40 | +class lru_cache { |
| 41 | + public: |
| 42 | + using key_type = KeyT; |
| 43 | + using mapped_type = T; |
| 44 | + using value_type = std::pair<key_type const, mapped_type>; |
| 45 | + |
| 46 | + using iterator = typename std::list<value_type>::iterator; |
| 47 | + using const_iterator = typename std::list<value_type>::const_iterator; |
| 48 | + |
| 49 | + using prune_hook_type = std::function<void(key_type, mapped_type&&)>; |
| 50 | + |
| 51 | + lru_cache() = default; |
| 52 | + |
| 53 | + explicit lru_cache(size_t max_size) |
| 54 | + : max_size_{max_size} { |
| 55 | + index_.reserve(max_size_); |
| 56 | + } |
| 57 | + |
| 58 | + // Set the maximum cache size |
| 59 | + void set_max_size(size_t max_size) { |
| 60 | + max_size_ = max_size; |
| 61 | + while (cache_.size() > max_size_) { |
| 62 | + evict_lru(); |
| 63 | + } |
| 64 | + index_.reserve(max_size_); |
| 65 | + } |
| 66 | + |
| 67 | + // Set a custom prune hook |
| 68 | + void set_prune_hook(prune_hook_type hook) { prune_hook_ = std::move(hook); } |
| 69 | + |
| 70 | + // Insert or update an item in the cache, promoting it |
| 71 | + void set(key_type const& key, mapped_type value, |
| 72 | + prune_hook_type custom_prune_hook = {}) { |
| 73 | + auto it = index_.find(key); |
| 74 | + if (it != index_.end()) { |
| 75 | + it->second->second = std::move(value); |
| 76 | + move_to_front(it->second); |
| 77 | + } else { |
| 78 | + if (index_.size() >= max_size_) { |
| 79 | + evict_lru(std::move(custom_prune_hook)); |
| 80 | + } |
| 81 | + cache_.push_front(value_type(key, std::move(value))); |
| 82 | + index_[key] = cache_.begin(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + // Find an item, optionally promoting it |
| 87 | + iterator find(key_type const& key, bool promote = true) { |
| 88 | + auto it = index_.find(key); |
| 89 | + if (it == index_.end()) { |
| 90 | + return end(); |
| 91 | + } |
| 92 | + if (promote) { |
| 93 | + move_to_front(it->second); |
| 94 | + } |
| 95 | + return it->second; |
| 96 | + } |
| 97 | + |
| 98 | + iterator erase(iterator pos, prune_hook_type custom_prune_hook = {}) { |
| 99 | + auto& key = pos->first; |
| 100 | + auto& value = pos->second; |
| 101 | + if (custom_prune_hook) { |
| 102 | + custom_prune_hook(key, std::move(value)); |
| 103 | + } else if (prune_hook_) { |
| 104 | + prune_hook_(key, std::move(value)); |
| 105 | + } |
| 106 | + index_.erase(key); |
| 107 | + return cache_.erase(pos); |
| 108 | + } |
| 109 | + |
| 110 | + void clear() { |
| 111 | + index_.clear(); |
| 112 | + cache_.clear(); |
| 113 | + } |
| 114 | + |
| 115 | + bool empty() const { return cache_.empty(); } |
| 116 | + |
| 117 | + size_t size() const { return cache_.size(); } |
| 118 | + |
| 119 | + iterator begin() { return cache_.begin(); } |
| 120 | + iterator end() { return cache_.end(); } |
| 121 | + |
| 122 | + const_iterator begin() const { return cache_.begin(); } |
| 123 | + const_iterator end() const { return cache_.end(); } |
| 124 | + |
| 125 | + private: |
| 126 | + // Move the accessed item to the front of the cache (most recently used) |
| 127 | + void move_to_front(iterator it) { cache_.splice(cache_.begin(), cache_, it); } |
| 128 | + |
| 129 | + // Evict the least recently used item |
| 130 | + void evict_lru(prune_hook_type custom_prune_hook = {}) { |
| 131 | + if (auto it = cache_.end(); it != cache_.begin()) { |
| 132 | + erase(--it, std::move(custom_prune_hook)); |
| 133 | + } |
| 134 | + } |
| 135 | + |
| 136 | + size_t max_size_; |
| 137 | + phmap::flat_hash_map<key_type, iterator> index_; |
| 138 | + std::list<value_type> cache_; |
| 139 | + prune_hook_type prune_hook_; |
| 140 | +}; |
| 141 | + |
| 142 | +} // namespace dwarfs::reader::internal |
0 commit comments