nss: upgrade to release 3.73
[LibreOffice.git] / sc / inc / lookupcache.hxx
blob820fa3d6be4eab4d87e68b0ce665e9c44cbc0da5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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_INC_LOOKUPCACHE_HXX
21 #define INCLUDED_SC_INC_LOOKUPCACHE_HXX
23 #include "address.hxx"
24 #include <svl/listener.hxx>
26 #include <memory>
27 #include <unordered_map>
29 class ScDocument;
30 struct ScLookupCacheMap;
31 struct ScQueryEntry;
33 /** Lookup cache for one range used with interpreter functions such as VLOOKUP
34 and MATCH. Caches query for a specific row and the resulting address looked
35 up, in case other lookups of the same query in the same row are to be
36 performed, which usually occur to obtain a different offset column of the
37 same query.
40 class ScLookupCache final : public SvtListener
42 public:
44 enum Result
46 NOT_CACHED, /// Query not found in cache.
47 CRITERIA_DIFFERENT, /// Different criteria for same query position exists.
48 NOT_AVAILABLE, /// Criteria not available in lookup range.
49 FOUND /// Criteria found.
52 enum QueryOp
54 UNKNOWN,
55 EQUAL,
56 LESS_EQUAL,
57 GREATER_EQUAL
60 class QueryCriteria
62 union
64 double mfVal;
65 const OUString *mpStr;
67 bool mbAlloc;
68 bool mbString;
69 QueryOp meOp;
71 void deleteString()
73 if (mbAlloc && mbString)
74 delete mpStr;
77 QueryCriteria & operator=( const QueryCriteria & r ) = delete;
79 public:
81 explicit QueryCriteria( const ScQueryEntry & rEntry );
82 QueryCriteria( const QueryCriteria & r );
83 ~QueryCriteria();
85 QueryOp getQueryOp() const { return meOp; }
87 void setDouble( double fVal )
89 deleteString();
90 mbAlloc = mbString = false;
91 mfVal = fVal;
94 void setString( const OUString & rStr )
96 deleteString();
97 mbAlloc = mbString = true;
98 mpStr = new OUString( rStr);
101 bool operator==( const QueryCriteria & r ) const
103 return meOp == r.meOp && mbString == r.mbString &&
104 (mbString ? (*mpStr == *r.mpStr) : (mfVal == r.mfVal));
107 bool isEmptyStringQuery() const
109 return (getQueryOp() == QueryOp::EQUAL) && mbString && mpStr && mpStr->isEmpty();
113 /// MUST be new'd because Notify() deletes.
114 ScLookupCache( ScDocument * pDoc, const ScRange & rRange, ScLookupCacheMap & cacheMap );
115 virtual ~ScLookupCache() override;
116 /// Remove from document structure and delete (!) cache on modify hint.
117 virtual void Notify( const SfxHint& rHint ) override;
119 /// @returns document address in o_rResultAddress if Result==FOUND
120 Result lookup( ScAddress & o_rResultAddress,
121 const QueryCriteria & rCriteria,
122 const ScAddress & rQueryAddress ) const;
124 SCROW lookup( const QueryCriteria & rCriteria ) const;
126 /** Insert query and result.
127 @param bAvailable
128 Pass sal_False if the search didn't deliver a result. A subsequent
129 lookup() then will return Result::NOT_AVAILABLE.
130 @returns successful insertion.
132 bool insert( const ScAddress & rResultAddress,
133 const QueryCriteria & rCriteria,
134 const ScAddress & rQueryAddress,
135 const bool bAvailable );
137 const ScRange& getRange() const { return maRange; }
139 ScLookupCacheMap & getCacheMap() const { return mCacheMap; }
141 struct Hash
143 size_t operator()( const ScRange & rRange ) const
145 // Lookups are performed on the first column.
146 return rRange.hashStartColumn();
150 private:
152 struct QueryKey
154 SCROW mnRow;
155 SCTAB mnTab;
156 QueryOp meOp;
158 QueryKey( const ScAddress & rAddress, const QueryOp eOp ) :
159 mnRow( rAddress.Row()),
160 mnTab( rAddress.Tab()),
161 meOp( eOp)
165 bool operator==( const QueryKey & r ) const
167 return mnRow == r.mnRow && mnTab == r.mnTab && meOp == r.meOp && meOp != UNKNOWN;
170 struct Hash
172 size_t operator()( const QueryKey & r ) const
174 return (static_cast<size_t>(r.mnTab) << 24) ^
175 (static_cast<size_t>(r.meOp) << 22) ^
176 static_cast<size_t>(r.mnRow);
181 struct QueryCriteriaAndResult
183 QueryCriteria maCriteria;
184 ScAddress maAddress;
186 QueryCriteriaAndResult( const QueryCriteria & rCriteria, const ScAddress & rAddress ) :
187 maCriteria( rCriteria),
188 maAddress( rAddress)
193 std::unordered_map< QueryKey, QueryCriteriaAndResult, QueryKey::Hash > maQueryMap;
194 ScRange maRange;
195 ScDocument * mpDoc;
196 ScLookupCacheMap & mCacheMap;
198 ScLookupCache( const ScLookupCache & ) = delete;
199 ScLookupCache & operator=( const ScLookupCache & ) = delete;
203 // Struct because including lookupcache.hxx in document.hxx isn't wanted.
204 struct ScLookupCacheMap
206 std::unordered_map< ScRange, std::unique_ptr<ScLookupCache>, ScLookupCache::Hash > aCacheMap;
210 #endif
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */