LibreOffice Module binaryurp (master) 1
cache.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18 */
19
20#pragma once
21
22#include <sal/config.h>
23
24#include <cassert>
25#include <cstddef>
26#include <map>
27#include <list>
28
29#include <sal/types.h>
30
31namespace binaryurp {
32
33namespace cache {
34
35enum { size = 256, ignore = 0xFFFF };
36
37}
38
39template< typename T > class Cache {
40public:
41 typedef sal_uInt16 IdxType;
42
43 explicit Cache(std::size_t size):
44 size_(size)
45 {
46 assert(size < cache::ignore);
47 }
48
49 IdxType add( const T& rContent, bool* pbFound) {
50 assert( pbFound != nullptr);
51 if( !size_) {
52 *pbFound = false;
53 return cache::ignore;
54 }
55 // try to insert into the map
56 list_.push_front( rContent); // create a temp entry
57 auto const [it, inserted] = map_.emplace( list_.begin(), 0 );
58 *pbFound = !inserted;
59
60 if( !inserted) { // insertion not needed => found the entry
61 list_.pop_front(); // remove the temp entry
62 list_.splice( list_.begin(), list_, it->first); // the found entry is moved to front
63 return it->second;
64 }
65
66 // test insertion successful => it was new so we keep it
67 IdxType n = static_cast<IdxType>( map_.size() - 1);
68 if( n >= size_) { // cache full => replace the LRU entry
69 // find the least recently used element in the map
70 typename LruItMap::iterator lru = map_.find( --list_.end());
71 n = lru->second;
72 map_.erase( lru); // remove it from the map
73 list_.pop_back(); // remove from the list
74 }
75 it->second = n;
76 return n;
77 }
78
79private:
80 Cache(const Cache&) = delete;
81 Cache& operator=(const Cache&) = delete;
82
83 typedef std::list<T> LruList; // last recently used list
84 typedef typename LruList::iterator LruListIt;
85 struct CmpT{ bool operator()( const LruListIt& rA, const LruListIt& rB) const { return (*rA<*rB);}};
86 typedef std::map< LruListIt, IdxType, CmpT > LruItMap; // a map into a LruList
87
88 std::size_t size_;
91};
92
93}
94
95/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Cache(std::size_t size)
Definition: cache.hxx:43
Cache & operator=(const Cache &)=delete
LruItMap map_
Definition: cache.hxx:89
LruList::iterator LruListIt
Definition: cache.hxx:84
LruList list_
Definition: cache.hxx:90
std::map< LruListIt, IdxType, CmpT > LruItMap
Definition: cache.hxx:86
std::list< T > LruList
Definition: cache.hxx:83
Cache(const Cache &)=delete
IdxType add(const T &rContent, bool *pbFound)
Definition: cache.hxx:49
std::size_t size_
Definition: cache.hxx:88
sal_uInt16 IdxType
Definition: cache.hxx:41
sal_Int64 n
DECL_LISTENERMULTIPLEXER_END void SAL_CALL inserted(::sal_Int32 ID) override
bool operator()(const LruListIt &rA, const LruListIt &rB) const
Definition: cache.hxx:85