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 .
22 #include "address.hxx"
23 #include <svl/listener.hxx>
26 #include <unordered_map>
29 struct ScLookupCacheMap
;
32 /** Lookup cache for one range used with interpreter functions such as VLOOKUP
33 and MATCH. Caches query for a specific row and the resulting address looked
34 up, in case other lookups of the same query in the same row are to be
35 performed, which usually occur to obtain a different offset column of the
39 class ScLookupCache final
: public SvtListener
45 NOT_CACHED
, /// Query not found in cache.
46 CRITERIA_DIFFERENT
, /// Different criteria for same query position exists.
47 NOT_AVAILABLE
, /// Criteria not available in lookup range.
48 FOUND
/// Criteria found.
64 const OUString
*mpStr
;
72 if (mbAlloc
&& mbString
)
76 QueryCriteria
& operator=( const QueryCriteria
& r
) = delete;
80 explicit QueryCriteria( const ScQueryEntry
& rEntry
);
81 QueryCriteria( const QueryCriteria
& r
);
84 QueryOp
getQueryOp() const { return meOp
; }
86 void setDouble( double fVal
)
89 mbAlloc
= mbString
= false;
93 void setString( const OUString
& rStr
)
96 mbAlloc
= mbString
= true;
97 mpStr
= new OUString( rStr
);
100 bool operator==( const QueryCriteria
& r
) const
102 return meOp
== r
.meOp
&& mbString
== r
.mbString
&&
103 (mbString
? (*mpStr
== *r
.mpStr
) : (mfVal
== r
.mfVal
));
106 bool isEmptyStringQuery() const
108 return (getQueryOp() == QueryOp::EQUAL
) && mbString
&& mpStr
&& mpStr
->isEmpty();
112 /// MUST be new'd because Notify() deletes.
113 ScLookupCache( ScDocument
* pDoc
, const ScRange
& rRange
, ScLookupCacheMap
& cacheMap
)
114 : maRange( rRange
), mpDoc( pDoc
), mCacheMap(cacheMap
) {}
115 /// Remove from document structure and delete (!) cache on modify hint.
116 virtual void Notify( const SfxHint
& rHint
) override
;
118 /// @returns document address in o_rResultAddress if Result==FOUND
119 Result
lookup( ScAddress
& o_rResultAddress
,
120 const QueryCriteria
& rCriteria
,
121 const ScAddress
& rQueryAddress
) const;
123 SCROW
lookup( const QueryCriteria
& rCriteria
) const;
125 /** Insert query and result.
127 Pass sal_False if the search didn't deliver a result. A subsequent
128 lookup() then will return Result::NOT_AVAILABLE.
129 @returns successful insertion.
131 bool insert( const ScAddress
& rResultAddress
,
132 const QueryCriteria
& rCriteria
,
133 const ScAddress
& rQueryAddress
,
134 const bool bAvailable
);
136 const ScRange
& getRange() const { return maRange
; }
138 ScLookupCacheMap
& getCacheMap() const { return mCacheMap
; }
142 size_t operator()( const ScRange
& rRange
) const
144 // Lookups are performed on the first column.
145 return rRange
.hashStartColumn();
157 QueryKey( const ScAddress
& rAddress
, const QueryOp eOp
) :
158 mnRow( rAddress
.Row()),
159 mnTab( rAddress
.Tab()),
164 bool operator==( const QueryKey
& r
) const
166 return mnRow
== r
.mnRow
&& mnTab
== r
.mnTab
&& meOp
== r
.meOp
&& meOp
!= UNKNOWN
;
171 size_t operator()( const QueryKey
& r
) const
173 return (static_cast<size_t>(r
.mnTab
) << 24) ^
174 (static_cast<size_t>(r
.meOp
) << 22) ^
175 static_cast<size_t>(r
.mnRow
);
180 struct QueryCriteriaAndResult
182 QueryCriteria maCriteria
;
185 QueryCriteriaAndResult( const QueryCriteria
& rCriteria
, const ScAddress
& rAddress
) :
186 maCriteria( rCriteria
),
192 std::unordered_map
< QueryKey
, QueryCriteriaAndResult
, QueryKey::Hash
> maQueryMap
;
195 ScLookupCacheMap
& mCacheMap
;
197 ScLookupCache( const ScLookupCache
& ) = delete;
198 ScLookupCache
& operator=( const ScLookupCache
& ) = delete;
202 // Struct because including lookupcache.hxx in document.hxx isn't wanted.
203 struct ScLookupCacheMap
205 std::unordered_map
< ScRange
, std::unique_ptr
<ScLookupCache
>, ScLookupCache::Hash
> aCacheMap
;
209 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */