1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2011 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir #ifndef INCLUDED_BINARYURP_SOURCE_CACHE_HXX 29*cdf0e10cSrcweir #define INCLUDED_BINARYURP_SOURCE_CACHE_HXX 30*cdf0e10cSrcweir 31*cdf0e10cSrcweir #include "sal/config.h" 32*cdf0e10cSrcweir 33*cdf0e10cSrcweir #include <cstddef> 34*cdf0e10cSrcweir #include <map> 35*cdf0e10cSrcweir 36*cdf0e10cSrcweir #include "boost/noncopyable.hpp" 37*cdf0e10cSrcweir #include "osl/diagnose.h" 38*cdf0e10cSrcweir #include "sal/types.h" 39*cdf0e10cSrcweir 40*cdf0e10cSrcweir namespace binaryurp { 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir namespace cache { 43*cdf0e10cSrcweir 44*cdf0e10cSrcweir enum { size = 256, ignore = 0xFFFF }; 45*cdf0e10cSrcweir 46*cdf0e10cSrcweir } 47*cdf0e10cSrcweir 48*cdf0e10cSrcweir template< typename T > class Cache: private boost::noncopyable { 49*cdf0e10cSrcweir public: 50*cdf0e10cSrcweir explicit Cache(std::size_t size): 51*cdf0e10cSrcweir size_(size), first_(map_.end()), last_(map_.end()) 52*cdf0e10cSrcweir { 53*cdf0e10cSrcweir OSL_ASSERT(size < cache::ignore); 54*cdf0e10cSrcweir } 55*cdf0e10cSrcweir 56*cdf0e10cSrcweir sal_uInt16 add(T const & content, bool * found) { 57*cdf0e10cSrcweir OSL_ASSERT(found != 0); 58*cdf0e10cSrcweir typename Map::iterator i(map_.find(content)); 59*cdf0e10cSrcweir *found = i != map_.end(); 60*cdf0e10cSrcweir if (i == map_.end()) { 61*cdf0e10cSrcweir typename Map::size_type n = map_.size(); 62*cdf0e10cSrcweir if (n < size_) { 63*cdf0e10cSrcweir i = 64*cdf0e10cSrcweir (map_.insert( 65*cdf0e10cSrcweir typename Map::value_type( 66*cdf0e10cSrcweir content, 67*cdf0e10cSrcweir Entry( 68*cdf0e10cSrcweir static_cast< sal_uInt16 >(n), map_.end(), 69*cdf0e10cSrcweir first_)))). 70*cdf0e10cSrcweir first; 71*cdf0e10cSrcweir if (first_ == map_.end()) { 72*cdf0e10cSrcweir last_ = i; 73*cdf0e10cSrcweir } else { 74*cdf0e10cSrcweir first_->second.prev = i; 75*cdf0e10cSrcweir } 76*cdf0e10cSrcweir first_ = i; 77*cdf0e10cSrcweir } else if (last_ != map_.end()) { 78*cdf0e10cSrcweir i = 79*cdf0e10cSrcweir (map_.insert( 80*cdf0e10cSrcweir typename Map::value_type( 81*cdf0e10cSrcweir content, 82*cdf0e10cSrcweir Entry(last_->second.index, map_.end(), first_)))). 83*cdf0e10cSrcweir first; 84*cdf0e10cSrcweir first_->second.prev = i; 85*cdf0e10cSrcweir first_ = i; 86*cdf0e10cSrcweir typename Map::iterator j(last_); 87*cdf0e10cSrcweir last_ = last_->second.prev; 88*cdf0e10cSrcweir last_->second.next = map_.end(); 89*cdf0e10cSrcweir map_.erase(j); 90*cdf0e10cSrcweir } else { 91*cdf0e10cSrcweir // Reached iff size_ == 0: 92*cdf0e10cSrcweir return cache::ignore; 93*cdf0e10cSrcweir } 94*cdf0e10cSrcweir } else if (i != first_) { 95*cdf0e10cSrcweir // Move to front (reached only if size_ > 1): 96*cdf0e10cSrcweir i->second.prev->second.next = i->second.next; 97*cdf0e10cSrcweir if (i->second.next == map_.end()) { 98*cdf0e10cSrcweir last_ = i->second.prev; 99*cdf0e10cSrcweir } else { 100*cdf0e10cSrcweir i->second.next->second.prev = i->second.prev; 101*cdf0e10cSrcweir } 102*cdf0e10cSrcweir i->second.prev = map_.end(); 103*cdf0e10cSrcweir i->second.next = first_; 104*cdf0e10cSrcweir first_->second.prev = i; 105*cdf0e10cSrcweir first_ = i; 106*cdf0e10cSrcweir } 107*cdf0e10cSrcweir return i->second.index; 108*cdf0e10cSrcweir } 109*cdf0e10cSrcweir 110*cdf0e10cSrcweir private: 111*cdf0e10cSrcweir struct Entry; 112*cdf0e10cSrcweir 113*cdf0e10cSrcweir typedef std::map< T, Entry > Map; 114*cdf0e10cSrcweir 115*cdf0e10cSrcweir struct Entry { 116*cdf0e10cSrcweir sal_uInt16 index; 117*cdf0e10cSrcweir typename Map::iterator prev; 118*cdf0e10cSrcweir typename Map::iterator next; 119*cdf0e10cSrcweir 120*cdf0e10cSrcweir Entry( 121*cdf0e10cSrcweir sal_uInt16 theIndex, typename Map::iterator thePrev, 122*cdf0e10cSrcweir typename Map::iterator theNext): 123*cdf0e10cSrcweir index(theIndex), prev(thePrev), next(theNext) {} 124*cdf0e10cSrcweir }; 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir std::size_t size_; 127*cdf0e10cSrcweir Map map_; 128*cdf0e10cSrcweir typename Map::iterator first_; 129*cdf0e10cSrcweir typename Map::iterator last_; 130*cdf0e10cSrcweir }; 131*cdf0e10cSrcweir 132*cdf0e10cSrcweir } 133*cdf0e10cSrcweir 134*cdf0e10cSrcweir #endif 135