aboutsummaryrefslogtreecommitdiff
path: root/src-qt5/desktop-utils/lumina-pdf/lrucache.h
blob: 0ed241c21a2d7401b39fed6e03db00b7e2cf7dd8 (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
#ifndef LUMINA_PDF_LRUCACHE_H
#define LUMINA_PDF_LRUCACHE_H

#include <list>
#include <optional>
#include <tuple>
#include <unordered_map>

namespace LuminaPDF {
template <class Value, class Key = int> class LRUCache {
public:
  LRUCache() = default;
  ~LRUCache() = default;

  void setCacheSize(size_t _max) {
    max = _max;
    hashmap.reserve(max);
    return;
  }

  std::optional<Value> get(const Key k) {
    auto itr = hashmap.find(k);

    if (itr == hashmap.end()) {
      return std::nullopt;
    }

    Value v = std::get<1>(*std::get<1>(*itr));

    values.erase(std::get<1>(*itr));
    values.push_front(std::make_tuple(k, v));
    hashmap[k] = values.begin();
    return v;
  }

  void push(const Key k, const Value v) {
    while (hashmap.size() >= max) {
      Key _k = std::get<0>(values.back());
      values.pop_back();
      hashmap.erase(_k);
    }

    values.push_front(std::make_tuple(k, v));
    hashmap[k] = values.begin();
  }

  bool contains(const Key k){
    auto itr = hashmap.find(k);
    return ( itr!=hashmap.end() );
  }

private:
  size_t max;
  std::list<std::tuple<Key, Value>> values;
  std::unordered_map<Key, typename std::list<std::tuple<Key, Value>>::iterator>
      hashmap;
};
} // namespace LuminaPDF

#endif // LUIMINA_PDF_LRUCACHE_H
bgstack15