Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-12-10 10:23:49

0001 //========================================================================
0002 //
0003 // PopplerCache.h
0004 //
0005 // This file is licensed under the GPLv2 or later
0006 //
0007 // Copyright (C) 2009 Koji Otani <sho@bbr.jp>
0008 // Copyright (C) 2009, 2010, 2017, 2018, 2021 Albert Astals Cid <aacid@kde.org>
0009 // Copyright (C) 2010 Carlos Garcia Campos <carlosgc@gnome.org>
0010 // Copyright (C) 2018 Adam Reichold <adam.reichold@t-online.de>
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     /* The item returned is owned by the cache */
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     /* The key and item pointers ownership is taken by the cache */
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