summaryrefslogtreecommitdiffstats
path: root/include/o3tl/lru_map.hxx
blob: 859617e5d98859f798f330c7d85dc4593045ca35 (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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
 * This file is part of the LibreOffice project.
 *
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 */

#ifndef INCLUDED_O3TL_LRU_MAP_HXX
#define INCLUDED_O3TL_LRU_MAP_HXX

#include <algorithm>
#include <cassert>
#include <list>
#include <unordered_map>
#include <cstddef>
#include <type_traits>

namespace o3tl
{
namespace detail
{
// Helper base class to keep total cost for lru_map with custom item size.
// Custom size is specified by the ValueSize functor, the default of each
// item counting as 1 is specified using the void type.
template <class ValueSize> class lru_map_base
{
public:
    // Returns total of ValueSize for all items.
    size_t total_size() const { return mCurrentSize; }

protected:
    size_t mCurrentSize = 0; // sum of ValueSize for all items
};

// By default cost of each item is 1, so it doesn't need to be tracked.
template <> class lru_map_base<void>
{
};
} // namespace

/** LRU map
 *
 * Similar to unordered_map (it actually uses it) with additionally functionality
 * which removes the entries that have been "least recently used" when the size
 * hits the specified capacity.
 *
 * It only implements the minimal methods needed and the implementation is NOT
 * thread safe.
 *
 * The implementation is as simple as possible but it still uses O(1) complexity
 * for most of the operations with a combination unordered map and linked list.
 *
 * It is optionally possible to specify a function for ValueSize template
 * argument (that can be called as 'size_t func(Value)') that will return
 * a size (cost) for an item instead of the default size of 1 for each item.
 * The size of an item must not change for an item (if needed, re-insert
 * the item). A newly inserted item is guaranteed to be in the container,
 * even if its size exceeds the maximum size.
 *
 **/
template <typename Key, typename Value, class KeyHash = std::hash<Key>,
          class KeyEqual = std::equal_to<Key>, class ValueSize = void>
class lru_map final : public detail::lru_map_base<ValueSize>
{
public:
    typedef typename std::pair<Key, Value> key_value_pair_t;

private:
    typedef std::list<key_value_pair_t> list_t;
    typedef typename list_t::iterator list_iterator_t;
    typedef typename list_t::const_iterator list_const_iterator_t;

    typedef std::unordered_map<Key, list_iterator_t, KeyHash, KeyEqual> map_t;
    typedef typename map_t::iterator map_iterator_t;
    typedef typename map_t::const_iterator map_const_iterator_t;

    list_t mLruList;
    map_t mLruMap;
    size_t mMaxSize;

    void addSize(const Value& value)
    {
        // by default total size is equal to number of items
        if constexpr (!std::is_void_v<ValueSize>)
            this->mCurrentSize += ValueSize()(value);
    }

    void removeSize(const Value& value)
    {
        // by default total size is equal to number of items
        if constexpr (!std::is_void_v<ValueSize>)
        {
            size_t itemSize = ValueSize()(value);
            assert(itemSize <= this->mCurrentSize);
            this->mCurrentSize -= itemSize;
        }
    }

    void removeOldestItem()
    {
        removeSize(mLruList.back().second);
        // remove from map
        mLruMap.erase(mLruList.back().first);
        // remove from list
        mLruList.pop_back();
    }

    void checkLRUItemInsert()
    {
        if constexpr (std::is_void_v<ValueSize>)
        { // One added, so it's enough to remove one, if needed.
            if (mLruMap.size() > mMaxSize)
                removeOldestItem();
        }
        else
        {
            // This must leave at least one item (it's called from insert).
            while (this->mCurrentSize > mMaxSize && mLruList.size() > 1)
                removeOldestItem();
        }
    }

    void checkLRUItemUpdate()
    {
        // Item update does not change total size by default.
        if constexpr (!std::is_void_v<ValueSize>)
        {
            // This must leave at least one item (it's called from insert).
            while (this->mCurrentSize > mMaxSize && mLruList.size() > 1)
                removeOldestItem();
        }
    }

    void checkLRUMaxSize()
    {
        if constexpr (std::is_void_v<ValueSize>)
        {
            while (mLruMap.size() > mMaxSize)
                removeOldestItem();
        }
        else
        {
            while (this->mCurrentSize > mMaxSize)
                removeOldestItem();
        }
    }

    void clearSize()
    {
        if constexpr (!std::is_void_v<ValueSize>)
        {
#ifdef DBG_UTIL
            for (const key_value_pair_t& item : mLruList)
                removeSize(item.second);
            assert(this->mCurrentSize == 0);
#else
            this->mCurrentSize = 0;
#endif
        }
    }

public:
    typedef list_iterator_t iterator;
    typedef list_const_iterator_t const_iterator;

    lru_map(size_t nMaxSize)
        : mMaxSize(nMaxSize)
    {
        assert(mMaxSize > 0);
    }
    ~lru_map()
    {
        clearSize();
        // Some code .e.g. SalBitmap likes to remove itself from a cache during it's destructor, which means we
        // get calls into lru_map while we are in destruction, so use the swap-and-clear idiom to avoid those problems.
        mLruMap.clear();
        list_t().swap(mLruList);
    }

    void setMaxSize(size_t nMaxSize)
    {
        mMaxSize = nMaxSize;
        assert(mMaxSize > 0);
        checkLRUMaxSize();
    }

    void insert(key_value_pair_t& rPair)
    {
        map_iterator_t i = mLruMap.find(rPair.first);

        if (i == mLruMap.end()) // doesn't exist -> add to queue and map
        {
            addSize(rPair.second);
            // add to front of the list
            mLruList.push_front(rPair);
            // add the list position (iterator) to the map
            auto it = mLruList.begin();
            mLruMap[it->first] = it;
            checkLRUItemInsert();
        }
        else // already exists -> replace value
        {
            // update total cost
            removeSize(i->second->second);
            addSize(rPair.second);
            // replace value
            i->second->second = rPair.second;
            // bring to front of the lru list
            mLruList.splice(mLruList.begin(), mLruList, i->second);
            checkLRUItemUpdate();
        }
    }

    void insert(key_value_pair_t&& rPair)
    {
        map_iterator_t i = mLruMap.find(rPair.first);

        if (i == mLruMap.end()) // doesn't exist -> add to list and map
        {
            addSize(rPair.second);
            // add to front of the list
            mLruList.push_front(std::move(rPair));
            // add the list position (iterator) to the map
            auto it = mLruList.begin();
            mLruMap[it->first] = it;
            checkLRUItemInsert();
        }
        else // already exists -> replace value
        {
            removeSize(i->second->second);
            addSize(rPair.second);
            // replace value
            i->second->second = std::move(rPair.second);
            // push to back of the lru list
            mLruList.splice(mLruList.begin(), mLruList, i->second);
            checkLRUItemUpdate();
        }
    }

    list_const_iterator_t find(const Key& key)
    {
        const map_iterator_t i = mLruMap.find(key);
        if (i == mLruMap.cend()) // can't find entry for the key
        {
            // return empty iterator
            return mLruList.cend();
        }
        else
        {
            // push to back of the lru list
            mLruList.splice(mLruList.begin(), mLruList, i->second);
            return i->second;
        }
    }

    // reverse-iterates the list removing all items matching the predicate
    template <class UnaryPredicate> void remove_if(UnaryPredicate pred)
    {
        auto it = mLruList.rbegin();
        while (it != mLruList.rend())
        {
            if (pred(*it))
            {
                removeSize(it->second);
                mLruMap.erase(it->first);
                it = decltype(it){ mLruList.erase(std::next(it).base()) };
            }
            else
                ++it;
        }
    }

    list_const_iterator_t begin() const { return mLruList.cbegin(); }

    list_const_iterator_t end() const { return mLruList.cend(); }

    size_t size() const
    {
        assert(mLruMap.size() == mLruList.size());
        return mLruMap.size();
    }

    // size_t total_size() const; - only if custom ValueSize

    void clear()
    {
        clearSize();
        mLruMap.clear();
        mLruList.clear();
    }
};
}

#endif /* INCLUDED_O3TL_LRU_MAP_HXX */

/* vim:set shiftwidth=4 softtabstop=4 expandtab: */