Version 6.1.4.1, tag libreoffice-6.1.4.1
[LibreOffice.git] / binaryurp / source / cache.hxx
blobd1c1df7e32bfeaa161f4dc2543ede77f5710e050
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_BINARYURP_SOURCE_CACHE_HXX
21 #define INCLUDED_BINARYURP_SOURCE_CACHE_HXX
23 #include <sal/config.h>
25 #include <cassert>
26 #include <cstddef>
27 #include <map>
28 #include <list>
30 #include <sal/types.h>
32 namespace binaryurp {
34 namespace cache {
36 enum { size = 256, ignore = 0xFFFF };
40 template< typename T > class Cache {
41 public:
42 typedef sal_uInt16 IdxType;
44 explicit Cache(std::size_t size):
45 size_(size)
47 assert(size < cache::ignore);
50 IdxType add( const T& rContent, bool* pbFound) {
51 assert( pbFound != nullptr);
52 if( !size_) {
53 *pbFound = false;
54 return cache::ignore;
56 // try to insert into the map
57 list_.push_front( rContent); // create a temp entry
58 typedef std::pair<typename LruItMap::iterator,bool> MapPair;
59 MapPair aMP = map_.emplace( list_.begin(), 0 );
60 *pbFound = !aMP.second;
62 if( !aMP.second) { // insertion not needed => found the entry
63 list_.pop_front(); // remove the temp entry
64 list_.splice( list_.begin(), list_, aMP.first->first); // the found entry is moved to front
65 return aMP.first->second;
68 // test insertion successful => it was new so we keep it
69 IdxType n = static_cast<IdxType>( map_.size() - 1);
70 if( n >= size_) { // cache full => replace the LRU entry
71 // find the least recently used element in the map
72 typename LruItMap::iterator it = map_.find( --list_.end());
73 n = it->second;
74 map_.erase( it); // remove it from the map
75 list_.pop_back(); // remove from the list
77 aMP.first->second = n;
78 return n;
81 private:
82 Cache(const Cache&) = delete;
83 Cache& operator=(const Cache&) = delete;
85 typedef std::list<T> LruList; // last recently used list
86 typedef typename LruList::iterator LruListIt;
87 struct CmpT{ bool operator()( const LruListIt& rA, const LruListIt& rB) const { return (*rA<*rB);}};
88 typedef std::map< LruListIt, IdxType, CmpT > LruItMap; // a map into a LruList
90 std::size_t size_;
91 LruItMap map_;
92 LruList list_;
97 #endif
99 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */