LibreOffice Module sc (master) 1
lookupcache.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#pragma once
21
22#include "address.hxx"
23#include <svl/listener.hxx>
24
25#include <memory>
26#include <unordered_map>
27
28class ScDocument;
29struct ScLookupCacheMap;
30struct ScQueryEntry;
31
39class ScLookupCache final : public SvtListener
40{
41public:
42
43 enum Result
44 {
48 FOUND
49 };
50
52 {
57 };
58
60 {
61 union
62 {
63 double mfVal;
64 const OUString *mpStr;
65 };
66 bool mbAlloc;
69
71 {
72 if (mbAlloc && mbString)
73 delete mpStr;
74 }
75
76 QueryCriteria & operator=( const QueryCriteria & r ) = delete;
77
78 public:
79
80 explicit QueryCriteria( const ScQueryEntry & rEntry );
81 QueryCriteria( const QueryCriteria & r );
83
84 QueryOp getQueryOp() const { return meOp; }
85
86 void setDouble( double fVal )
87 {
89 mbAlloc = mbString = false;
90 mfVal = fVal;
91 }
92
93 void setString( const OUString & rStr )
94 {
96 mbAlloc = mbString = true;
97 mpStr = new OUString( rStr);
98 }
99
100 bool operator==( const QueryCriteria & r ) const
101 {
102 return meOp == r.meOp && mbString == r.mbString &&
103 (mbString ? (*mpStr == *r.mpStr) : (mfVal == r.mfVal));
104 }
105
107 {
108 return (getQueryOp() == QueryOp::EQUAL) && mbString && mpStr && mpStr->isEmpty();
109 }
110 };
111
113 ScLookupCache( ScDocument * pDoc, const ScRange & rRange, ScLookupCacheMap & cacheMap )
114 : maRange( rRange), mpDoc( pDoc), mCacheMap(cacheMap) {}
116 virtual void Notify( const SfxHint& rHint ) override;
117
119 Result lookup( ScAddress & o_rResultAddress,
120 const QueryCriteria & rCriteria,
121 const ScAddress & rQueryAddress ) const;
122
123 SCROW lookup( const QueryCriteria & rCriteria ) const;
124
131 bool insert( const ScAddress & rResultAddress,
132 const QueryCriteria & rCriteria,
133 const ScAddress & rQueryAddress,
134 const bool bAvailable );
135
136 const ScRange& getRange() const { return maRange; }
137
139
140 struct Hash
141 {
142 size_t operator()( const ScRange & rRange ) const
143 {
144 // Lookups are performed on the first column.
145 return rRange.hashStartColumn();
146 }
147 };
148
149private:
150
151 struct QueryKey
152 {
156
157 QueryKey( const ScAddress & rAddress, const QueryOp eOp ) :
158 mnRow( rAddress.Row()),
159 mnTab( rAddress.Tab()),
160 meOp( eOp)
161 {
162 }
163
164 bool operator==( const QueryKey & r ) const
165 {
166 return mnRow == r.mnRow && mnTab == r.mnTab && meOp == r.meOp && meOp != UNKNOWN;
167 }
168
169 struct Hash
170 {
171 size_t operator()( const QueryKey & r ) const
172 {
173 return (static_cast<size_t>(r.mnTab) << 24) ^
174 (static_cast<size_t>(r.meOp) << 22) ^
175 static_cast<size_t>(r.mnRow);
176 }
177 };
178 };
179
181 {
184
185 QueryCriteriaAndResult( const QueryCriteria & rCriteria, const ScAddress & rAddress ) :
186 maCriteria( rCriteria),
187 maAddress( rAddress)
188 {
189 }
190 };
191
192 std::unordered_map< QueryKey, QueryCriteriaAndResult, QueryKey::Hash > maQueryMap;
196
197 ScLookupCache( const ScLookupCache & ) = delete;
198 ScLookupCache & operator=( const ScLookupCache & ) = delete;
199
200};
201
202// Struct because including lookupcache.hxx in document.hxx isn't wanted.
204{
205 std::unordered_map< ScRange, std::unique_ptr<ScLookupCache>, ScLookupCache::Hash > aCacheMap;
206};
207
208
209/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
QueryCriteria & operator=(const QueryCriteria &r)=delete
bool operator==(const QueryCriteria &r) const
void setDouble(double fVal)
Definition: lookupcache.hxx:86
void setString(const OUString &rStr)
Definition: lookupcache.hxx:93
QueryCriteria(const ScQueryEntry &rEntry)
Definition: lookupcache.cxx:27
Lookup cache for one range used with interpreter functions such as VLOOKUP and MATCH.
Definition: lookupcache.hxx:40
ScLookupCache & operator=(const ScLookupCache &)=delete
std::unordered_map< QueryKey, QueryCriteriaAndResult, QueryKey::Hash > maQueryMap
virtual void Notify(const SfxHint &rHint) override
Remove from document structure and delete (!) cache on modify hint.
bool insert(const ScAddress &rResultAddress, const QueryCriteria &rCriteria, const ScAddress &rQueryAddress, const bool bAvailable)
Insert query and result.
@ FOUND
Criteria not available in lookup range.
Definition: lookupcache.hxx:48
@ CRITERIA_DIFFERENT
Query not found in cache.
Definition: lookupcache.hxx:46
@ NOT_AVAILABLE
Different criteria for same query position exists.
Definition: lookupcache.hxx:47
ScLookupCache(ScDocument *pDoc, const ScRange &rRange, ScLookupCacheMap &cacheMap)
MUST be new'd because Notify() deletes.
Result lookup(ScAddress &o_rResultAddress, const QueryCriteria &rCriteria, const ScAddress &rQueryAddress) const
Definition: lookupcache.cxx:71
ScDocument * mpDoc
const ScRange & getRange() const
ScLookupCache(const ScLookupCache &)=delete
ScLookupCacheMap & mCacheMap
ScLookupCacheMap & getCacheMap() const
size_t hashStartColumn() const
Hash start column and start and end rows.
Definition: address.hxx:767
std::unordered_map< ScRange, std::unique_ptr< ScLookupCache >, ScLookupCache::Hash > aCacheMap
size_t operator()(const ScRange &rRange) const
QueryCriteriaAndResult(const QueryCriteria &rCriteria, const ScAddress &rAddress)
size_t operator()(const QueryKey &r) const
bool operator==(const QueryKey &r) const
QueryKey(const ScAddress &rAddress, const QueryOp eOp)
Each instance of this struct represents a single filtering criteria.
Definition: queryentry.hxx:34
sal_Int16 SCTAB
Definition: types.hxx:22
sal_Int32 SCROW
Definition: types.hxx:17