LCOV - code coverage report
Current view: top level - binaryurp/source - cache.hxx (source / functions) Hit Total Coverage
Test: commit 10e77ab3ff6f4314137acd6e2702a6e5c1ce1fae Lines: 22 24 91.7 %
Date: 2014-11-03 Functions: 16 16 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       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             : #ifndef INCLUDED_BINARYURP_SOURCE_CACHE_HXX
      21             : #define INCLUDED_BINARYURP_SOURCE_CACHE_HXX
      22             : 
      23             : #include "sal/config.h"
      24             : 
      25             : #include <cassert>
      26             : #include <cstddef>
      27             : #include <map>
      28             : #include <list>
      29             : 
      30             : #include "boost/noncopyable.hpp"
      31             : #include "sal/types.h"
      32             : 
      33             : namespace binaryurp {
      34             : 
      35             : namespace cache {
      36             : 
      37             : enum { size = 256, ignore = 0xFFFF };
      38             : 
      39             : }
      40             : 
      41       43996 : template< typename T > class Cache : private boost::noncopyable {
      42             : public:
      43             :     typedef sal_uInt16 IdxType;
      44             : 
      45       43996 :     explicit Cache(std::size_t size):
      46       43996 :         size_(size)
      47             :     {
      48             :         assert(size < cache::ignore);
      49       43996 :     }
      50             : 
      51      726916 :     IdxType add( const T& rContent, bool* pbFound) {
      52             :         assert( pbFound != NULL);
      53      726916 :         if( !size_) {
      54           0 :             *pbFound = false;
      55           0 :             return cache::ignore;
      56             :         }
      57             :         // try to insert into the map
      58      726916 :         list_.push_front( rContent); // create a temp entry
      59             :         typedef std::pair<typename LruList::iterator, IdxType> MappedType;
      60             :         typedef std::pair<typename LruItMap::iterator,bool> MapPair;
      61      726916 :         MapPair aMP = map_.insert( MappedType( list_.begin(), 0));
      62      726916 :         *pbFound = !aMP.second;
      63             : 
      64      726916 :         if( !aMP.second) { // insertion not needed => found the entry
      65      328300 :             list_.pop_front(); // remove the temp entry
      66      328300 :             list_.splice( list_.begin(), list_, aMP.first->first); // the found entry is moved to front
      67      328300 :             return aMP.first->second;
      68             :         }
      69             : 
      70             :         // test insertion successful => it was new so we keep it
      71      398616 :         IdxType n = static_cast<IdxType>( map_.size() - 1);
      72      398616 :         if( n >= size_) { // cache full => replace the LRU entry
      73             :             // find the least recently used element in the map
      74      209809 :             typename LruItMap::iterator it = map_.find( --list_.end());
      75      209809 :             n = it->second;
      76      209809 :             map_.erase( it); // remove it from the map
      77      209809 :             list_.pop_back(); // remove from the list
      78             :         }
      79      398616 :         aMP.first->second = n;
      80      398616 :         return n;
      81             :     }
      82             : 
      83             : private:
      84             :     typedef std::list<T> LruList; // last recently used list
      85             :     typedef typename LruList::iterator LruListIt;
      86     4736328 :     struct CmpT{ bool operator()( const LruListIt& rA, const LruListIt& rB) const { return (*rA<*rB);}};
      87             :     typedef ::std::map< LruListIt, IdxType, CmpT > LruItMap; // a map into a LruList
      88             : 
      89             :     std::size_t size_;
      90             :     LruItMap map_;
      91             :     LruList list_;
      92             : };
      93             : 
      94             : }
      95             : 
      96             : #endif
      97             : 
      98             : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */

Generated by: LCOV version 1.10