1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2011 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #ifndef INCLUDED_BINARYURP_SOURCE_CACHE_HXX
30 #define INCLUDED_BINARYURP_SOURCE_CACHE_HXX
32 #include "sal/config.h"
37 #include "boost/noncopyable.hpp"
38 #include "osl/diagnose.h"
39 #include "sal/types.h"
45 enum { size
= 256, ignore
= 0xFFFF };
49 template< typename T
> class Cache
: private boost::noncopyable
{
51 explicit Cache(std::size_t size
):
52 size_(size
), first_(map_
.end()), last_(map_
.end())
54 OSL_ASSERT(size
< cache::ignore
);
57 sal_uInt16
add(T
const & content
, bool * found
) {
58 OSL_ASSERT(found
!= 0);
59 typename
Map::iterator
i(map_
.find(content
));
60 *found
= i
!= map_
.end();
61 if (i
== map_
.end()) {
62 typename
Map::size_type n
= map_
.size();
66 typename
Map::value_type(
69 static_cast< sal_uInt16
>(n
), map_
.end(),
72 if (first_
== map_
.end()) {
75 first_
->second
.prev
= i
;
78 } else if (last_
!= map_
.end()) {
81 typename
Map::value_type(
83 Entry(last_
->second
.index
, map_
.end(), first_
)))).
85 first_
->second
.prev
= i
;
87 typename
Map::iterator
j(last_
);
88 last_
= last_
->second
.prev
;
89 last_
->second
.next
= map_
.end();
92 // Reached iff size_ == 0:
95 } else if (i
!= first_
) {
96 // Move to front (reached only if size_ > 1):
97 i
->second
.prev
->second
.next
= i
->second
.next
;
98 if (i
->second
.next
== map_
.end()) {
99 last_
= i
->second
.prev
;
101 i
->second
.next
->second
.prev
= i
->second
.prev
;
103 i
->second
.prev
= map_
.end();
104 i
->second
.next
= first_
;
105 first_
->second
.prev
= i
;
108 return i
->second
.index
;
114 typedef std::map
< T
, Entry
> Map
;
118 typename
Map::iterator prev
;
119 typename
Map::iterator next
;
122 sal_uInt16 theIndex
, typename
Map::iterator thePrev
,
123 typename
Map::iterator theNext
):
124 index(theIndex
), prev(thePrev
), next(theNext
) {}
129 typename
Map::iterator first_
;
130 typename
Map::iterator last_
;
137 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */