merge the formfield patch from ooo-build
[ooovba.git] / bridges / source / remote / urp / urp_cache.hxx
blob627c2a8618a6bbb5876974135f8f4b31dacb5d20
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: urp_cache.hxx,v $
10 * $Revision: 1.7 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
30 #include <stdio.h>
31 #include <list>
32 #include <algorithm>
33 #include <rtl/ustring.hxx>
35 #include "urp_threadid.hxx"
36 #include "urp_cache.h"
38 namespace bridges_urp
41 template < class t , class tequals >
42 inline Cache< t , tequals >::Cache( sal_uInt16 nMaxEntries ) :
43 m_pCache( new t[nMaxEntries] ),
44 m_nMaxEntries( nMaxEntries ),
45 m_nEntries( 0 )
50 template < class t , class tequals >
51 inline Cache< t , tequals >::~Cache( )
53 delete [] m_pCache;
57 template < class t , class tequals >
58 inline sal_uInt16 Cache< t , tequals >::put( const t & value )
60 if( ! m_nMaxEntries )
62 return 0xffff;
64 sal_uInt16 nEntry = 0xffff;
65 if( m_nEntries < m_nMaxEntries )
67 // cache has still empty places
68 m_pCache[m_nEntries] = value;
69 nEntry = m_nEntries;
70 m_nEntries ++;
72 // add it to the cache
73 m_lstLeastRecentlyUsed.push_front( nEntry );
75 else
77 // cache is full, remove an element and insert the new one
78 nEntry = m_lstLeastRecentlyUsed.back();
79 m_lstLeastRecentlyUsed.pop_back();
80 m_lstLeastRecentlyUsed.push_front( nEntry );
82 m_pCache[nEntry] = value;
84 return nEntry;
87 template < class t , class tequals >
88 inline sal_uInt16 Cache< t , tequals >::seek( const t & value )
90 for( ::std::list< sal_uInt16 >::iterator ii = m_lstLeastRecentlyUsed.begin() ;
91 ii != m_lstLeastRecentlyUsed.end() ;
92 ++ ii )
94 if( value == m_pCache[*ii] )
96 sal_uInt16 nEntry = *ii;
97 m_lstLeastRecentlyUsed.erase( ii );
98 m_lstLeastRecentlyUsed.push_front( nEntry );
99 return nEntry;
102 return 0xffff;
105 // helper predicate for element removal
106 template < class t >
107 struct PredicateOverMax
109 t m_;
110 inline PredicateOverMax( const t &value ) : m_(value)
112 sal_Int32 operator () ( const t &value ) const
113 { return value >= m_; }
116 template < class t, class tequals >
117 inline void Cache < t , tequals >::resize( sal_uInt16 nNewMaxEntries )
119 if( 0 == nNewMaxEntries )
121 m_lstLeastRecentlyUsed.clear();
122 delete [] m_pCache;
123 m_pCache = 0;
124 m_nMaxEntries = 0;
126 else
128 // allocate
129 t *pNew = new t[nNewMaxEntries];
130 sal_Int32 nMin = nNewMaxEntries < m_nMaxEntries ? nNewMaxEntries : m_nMaxEntries;
132 // copy
133 for( sal_Int32 i = 0; i < nMin ; i ++ )
135 pNew[i] = m_pCache[i];
137 // delete
138 delete [] m_pCache;
140 // assign
141 m_pCache = pNew;
143 // remove overlapping lru cache entries
144 ::std::remove_if(m_lstLeastRecentlyUsed.begin(),
145 m_lstLeastRecentlyUsed.end(),
146 PredicateOverMax< sal_Int32 > ( nMin ) );
148 m_nMaxEntries = nNewMaxEntries;
149 m_nEntries = m_nEntries < m_nMaxEntries ?
150 m_nEntries : m_nMaxEntries;
153 template < class t, class tequals >
154 inline void Cache < t, tequals >:: clear()
156 for( sal_Int32 i = 0; i < m_nMaxEntries ; i ++ )
158 m_pCache[i] = t();
160 m_lstLeastRecentlyUsed.clear();
161 m_nEntries = 0;