1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_SC_LOOKUPCACHE_HXX
21 #define INCLUDED_SC_LOOKUPCACHE_HXX
23 #include "address.hxx"
25 #include "formula/token.hxx"
26 #include <svl/listener.hxx>
27 #include <tools/string.hxx>
29 #include <boost/unordered_map.hpp>
34 /** Lookup cache for one range used with interpreter functions such as VLOOKUP
35 and MATCH. Caches query for a specific row and the resulting address looked
36 up, in case other lookups of the same query in the same row are to be
37 performed, which usually occur to obtain a different offset column of the
41 class ScLookupCache
: public SvtListener
47 NOT_CACHED
, /// Query not found in cache.
48 CRITERIA_DIFFERENT
, /// Different criteria for same query position exists.
49 NOT_AVAILABLE
, /// Criteria not available in lookup range.
50 FOUND
/// Criteria found.
74 if (mbAlloc
&& mbString
)
80 QueryCriteria
& operator=( const QueryCriteria
& r
);
84 explicit QueryCriteria( const ScQueryEntry
& rEntry
);
85 QueryCriteria( const QueryCriteria
& r
);
88 QueryOp
getQueryOp() const { return meOp
; }
90 void setDouble( double fVal
)
93 mbAlloc
= mbString
= false;
97 void setString( const String
* pStr
)
105 void setString( const String
& rStr
)
108 mbAlloc
= mbString
= true;
109 mpStr
= new String( rStr
);
112 bool operator==( const QueryCriteria
& r
) const
114 return meOp
== r
.meOp
&& mbString
== r
.mbString
&&
115 (mbString
? (*mpStr
== *r
.mpStr
) : (mfVal
== r
.mfVal
));
120 /// MUST be new'd because Notify() deletes.
121 ScLookupCache( ScDocument
* pDoc
, const ScRange
& rRange
);
122 virtual ~ScLookupCache();
123 /// Remove from document structure and delete (!) cache on modify hint.
124 virtual void Notify( SvtBroadcaster
& rBC
, const SfxHint
& rHint
);
126 /// @returns document address in o_rAddress if Result==FOUND
127 Result
lookup( ScAddress
& o_rResultAddress
,
128 const QueryCriteria
& rCriteria
,
129 const ScAddress
& rQueryAddress
) const;
131 /** Insert query and result.
133 Pass sal_False if the search didn't deliver a result. A subsequent
134 lookup() then will return Result::NOT_AVAILABLE.
135 @returns successful insertion.
137 bool insert( const ScAddress
& rResultAddress
,
138 const QueryCriteria
& rCriteria
,
139 const ScAddress
& rQueryAddress
,
140 const bool bAvailable
);
142 inline const ScRange
& getRange() const { return maRange
; }
146 size_t operator()( const ScRange
& rRange
) const
148 // Lookups are performed on the first column.
149 return rRange
.hashStartColumn();
161 QueryKey( const ScAddress
& rAddress
, const QueryOp eOp
) :
162 mnRow( rAddress
.Row()),
163 mnTab( rAddress
.Tab()),
168 bool operator==( const QueryKey
& r
) const
170 return mnRow
== r
.mnRow
&& mnTab
== r
.mnTab
&& meOp
== r
.meOp
&& meOp
!= UNKNOWN
;
175 size_t operator()( const QueryKey
& r
) const
177 return (static_cast<size_t>(r
.mnTab
) << 24) ^
178 (static_cast<size_t>(r
.meOp
) << 22) ^
179 static_cast<size_t>(r
.mnRow
);
184 struct QueryCriteriaAndResult
186 QueryCriteria maCriteria
;
189 QueryCriteriaAndResult( const QueryCriteria
& rCriteria
, const ScAddress
& rAddress
) :
190 maCriteria( rCriteria
),
194 ~QueryCriteriaAndResult()
199 typedef ::boost::unordered_map
< QueryKey
, QueryCriteriaAndResult
, QueryKey::Hash
, ::std::equal_to
< QueryKey
> > QueryMap
;
205 ScLookupCache( const ScLookupCache
& );
206 ScLookupCache
& operator=( const ScLookupCache
& );
211 typedef ::boost::unordered_map
< ScRange
, ScLookupCache
*, ScLookupCache::Hash
, ::std::equal_to
< ScRange
> > ScLookupCacheMap
;
215 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */