File indexing completed on 2025-12-10 10:23:49
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014 #ifndef POPPLER_CACHE_H
0015 #define POPPLER_CACHE_H
0016
0017 #include <algorithm>
0018 #include <memory>
0019 #include <utility>
0020 #include <vector>
0021
0022 template<typename Key, typename Item>
0023 class PopplerCache
0024 {
0025 public:
0026 PopplerCache(const PopplerCache &) = delete;
0027 PopplerCache &operator=(const PopplerCache &other) = delete;
0028
0029 explicit PopplerCache(std::size_t cacheSizeA) { entries.reserve(cacheSizeA); }
0030
0031
0032 Item *lookup(const Key &key)
0033 {
0034 if (!entries.empty() && entries.front().first == key) {
0035 return entries.front().second.get();
0036 }
0037
0038 for (auto it = entries.begin(); it != entries.end(); ++it) {
0039 if (it->first == key) {
0040 auto *item = it->second.get();
0041
0042 std::rotate(entries.begin(), it, std::next(it));
0043
0044 return item;
0045 }
0046 }
0047
0048 return nullptr;
0049 }
0050
0051
0052 void put(const Key &key, Item *item)
0053 {
0054 if (entries.size() == entries.capacity()) {
0055 entries.pop_back();
0056 }
0057
0058 entries.emplace(entries.begin(), key, std::unique_ptr<Item> { item });
0059 }
0060
0061 private:
0062 std::vector<std::pair<Key, std::unique_ptr<Item>>> entries;
0063 };
0064
0065 #endif