blob: 68db15382fe68c03dc91f07561e3f8c89440b078 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
//========================================================================
//
// PopplerCache.h
//
// This file is licensed under the GPLv2 or later
//
// Copyright (C) 2009 Koji Otani <sho@bbr.jp>
// Copyright (C) 2009, 2010, 2017, 2018, 2021 Albert Astals Cid <aacid@kde.org>
// Copyright (C) 2010 Carlos Garcia Campos <carlosgc@gnome.org>
// Copyright (C) 2018 Adam Reichold <adam.reichold@t-online.de>
//
//========================================================================
#ifndef POPPLER_CACHE_H
#define POPPLER_CACHE_H
#include <algorithm>
#include <memory>
#include <utility>
#include <vector>
template<typename Key, typename Item>
class PopplerCache
{
public:
PopplerCache(const PopplerCache &) = delete;
PopplerCache &operator=(const PopplerCache &other) = delete;
explicit PopplerCache(std::size_t cacheSizeA) { entries.reserve(cacheSizeA); }
/* The item returned is owned by the cache */
Item *lookup(const Key &key)
{
if (!entries.empty() && entries.front().first == key) {
return entries.front().second.get();
}
for (auto it = entries.begin(); it != entries.end(); ++it) {
if (it->first == key) {
auto *item = it->second.get();
std::rotate(entries.begin(), it, std::next(it));
return item;
}
}
return nullptr;
}
/* The key and item pointers ownership is taken by the cache */
void put(const Key &key, Item *item)
{
if (entries.size() == entries.capacity()) {
entries.pop_back();
}
entries.emplace(entries.begin(), key, std::unique_ptr<Item> { item });
}
private:
std::vector<std::pair<Key, std::unique_ptr<Item>>> entries;
};
#endif
|