1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: urp_cache.hxx,v $
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 ************************************************************************/
33 #include <rtl/ustring.hxx>
35 #include "urp_threadid.hxx"
36 #include "urp_cache.h"
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
),
50 template < class t
, class tequals
>
51 inline Cache
< t
, tequals
>::~Cache( )
57 template < class t
, class tequals
>
58 inline sal_uInt16 Cache
< t
, tequals
>::put( const t
& value
)
64 sal_uInt16 nEntry
= 0xffff;
65 if( m_nEntries
< m_nMaxEntries
)
67 // cache has still empty places
68 m_pCache
[m_nEntries
] = value
;
72 // add it to the cache
73 m_lstLeastRecentlyUsed
.push_front( nEntry
);
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
;
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() ;
94 if( value
== m_pCache
[*ii
] )
96 sal_uInt16 nEntry
= *ii
;
97 m_lstLeastRecentlyUsed
.erase( ii
);
98 m_lstLeastRecentlyUsed
.push_front( nEntry
);
105 // helper predicate for element removal
107 struct PredicateOverMax
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();
129 t
*pNew
= new t
[nNewMaxEntries
];
130 sal_Int32 nMin
= nNewMaxEntries
< m_nMaxEntries
? nNewMaxEntries
: m_nMaxEntries
;
133 for( sal_Int32 i
= 0; i
< nMin
; i
++ )
135 pNew
[i
] = m_pCache
[i
];
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
++ )
160 m_lstLeastRecentlyUsed
.clear();