Welcome to mirror list, hosted at ThFree Co, Russian Federation.

mru_cache.hpp « base - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 2d94d4981adfbf035fd287f37790310fd3bc4c8c (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
#pragma once

#include "../std/unordered_map.hpp"
#include "../std/list.hpp"
#include "assert.hpp"
#include "logging.hpp"

namespace my
{
  template <typename TValue>
  struct MRUCacheValueTraits
  {
    static void Evict(TValue &){}
  };

  template <typename KeyT, typename ValueT, typename ValueTraitsT = MRUCacheValueTraits<ValueT> >
  class MRUCache
  {
  public:

    typedef MRUCache<KeyT, ValueT> this_type;

  private:

    MRUCache(this_type const & c);
    this_type & operator= (this_type const &);

    typedef list<KeyT> list_t;

    struct MapEntry
    {
      ValueT m_value;
      size_t m_weight;
      size_t m_lockCount;
      typename list_t::iterator m_it;
    };

    typedef map<KeyT, MapEntry> map_t;
    typedef set<KeyT> key_set_t;

    key_set_t m_keys;
    map_t m_map;
    list_t m_list;
    int m_curWeight;
    int m_maxWeight;

  public:

    MRUCache()
      : m_curWeight(0), m_maxWeight(0)
    {}

    explicit MRUCache(int maxWeight)
      : m_curWeight(0), m_maxWeight(maxWeight)
    {}

    set<KeyT> const & Keys() const
    {
      return m_keys;
    }

    bool HasRoom(int weight)
    {
      return m_curWeight + weight <= m_maxWeight;
    }

    /// how many elements in unlocked state do we have in cache
    int UnlockedWeight() const
    {
      int unlockedWeight = 0;

      for (typename map_t::const_iterator it = m_map.begin(); it != m_map.end(); ++it)
      {
        if (it->second.m_lockCount == 0)
          unlockedWeight += it->second.m_weight;
      }

      return unlockedWeight;
    }

    /// how many elements in locked state do we have in cache
    int LockedWeight() const
    {
      int lockedWeight = 0;

      for (typename map_t::const_iterator it = m_map.begin(); it != m_map.end(); ++it)
      {
        if (it->second.m_lockCount != 0)
          lockedWeight += it->second.m_weight;
      }

      return lockedWeight;
    }

    /// how much elements we can fit in this cache, considering unlocked
    /// elements, that could be popped out on request
    int CanFit() const
    {
      return m_maxWeight - LockedWeight();
    }

    int MaxWeight() const
    {
      return m_maxWeight;
    }

    void Resize(int maxWeight)
    {
      m_maxWeight = maxWeight;
      // in case of making cache smaller this
      // function pops out some unlocked elements
      FreeRoom(0);
    }

    void FreeRoom(int weight)
    {
      if (HasRoom(weight))
        return;

      if (!m_list.empty())
      {
        typename list<KeyT>::iterator it = (++m_list.rbegin()).base();

        while (m_curWeight + weight > m_maxWeight)
        {
          KeyT k = *it;

          MapEntry & e = m_map[k];

          /// erasing only unlocked elements
          if (e.m_lockCount == 0)
          {
            m_curWeight -= e.m_weight;
            ValueTraitsT::Evict(e.m_value);
            m_map.erase(k);
            m_keys.erase(k);

            typename list<KeyT>::iterator nextIt = it;
            if (nextIt != m_list.begin())
            {
              --nextIt;
              m_list.erase(it);
              it = nextIt;
            }
            else
            {
              m_list.erase(it);
              break;
            }
          }
          else
          {
            if (it == m_list.begin())
              break;

            --it;
          }
        }
      }

      ASSERT(m_curWeight + weight <= m_maxWeight, ());
    }

    bool HasElem(KeyT const & key)
    {
      return m_keys.find(key) != m_keys.end();
    }

    void LockElem(KeyT const & key)
    {
      ASSERT(HasElem(key), ());
      m_map[key].m_lockCount += 1;
    }

    size_t LockCount(KeyT const & key)
    {
      ASSERT(HasElem(key), ());
      return m_map[key].m_lockCount;
    }

    void UnlockElem(KeyT const & key)
    {
      ASSERT(HasElem(key), ());
      ASSERT(m_map[key].m_lockCount > 0, (m_map[key].m_lockCount));
      m_map[key].m_lockCount -= 1;
    }

    ValueT const & Find(KeyT const & key, bool DoTouch = true)
    {
      typename map_t::iterator it = m_map.find(key);

      ASSERT(it != m_map.end(), ());

      if (DoTouch)
        Touch(key);

      return it->second.m_value;
    }

    void Touch(KeyT const & key)
    {
      if (!HasElem(key))
        return;

      typename map_t::iterator it = m_map.find(key);

      ASSERT(it != m_map.end(), ());

      typename list_t::iterator listIt = it->second.m_it;
      KeyT k = *listIt;
      m_list.erase(listIt);
      m_list.push_front(k);
      it->second.m_it = m_list.begin();
    }

    void Remove(KeyT const & key)
    {
      typename map_t::iterator it = m_map.find(key);

      ASSERT(it->second.m_lockCount == 0, ("removing locked element"));

      if (it != m_map.end() && it->second.m_lockCount == 0)
      {
        m_curWeight -= it->second.m_weight;
        m_list.erase(it->second.m_it);
        ValueTraitsT::Evict(it->second.m_value);
        m_map.erase(it);
        m_keys.erase(key);
      }
    }

    void Add(KeyT const & key, ValueT const & val, size_t weight)
    {
      if (HasElem(key))
        Remove(key);

      FreeRoom(weight);

      m_list.push_front(key);
      m_map[key].m_weight = weight;
      m_map[key].m_value = val;
      m_map[key].m_lockCount = 0;
      m_map[key].m_it = m_list.begin();
      m_keys.insert(key);

      m_curWeight += weight;
    }

    void Clear()
    {
      for (typename map_t::iterator it = m_map.begin(); it != m_map.end(); ++it)
        ValueTraitsT::Evict(it->second);

      m_map.clear();
      m_keys.clear();
      m_list.clear();
      m_curWeight = 0;
    }
  };
}