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 #ifndef INCLUDED_SC_INC_LOOKUPCACHE_HXX
21 #define INCLUDED_SC_INC_LOOKUPCACHE_HXX
22 
23 #include "address.hxx"
24 #include <svl/listener.hxx>
25 
26 #include <memory>
27 #include <unordered_map>
28 
29 class ScDocument;
30 struct ScLookupCacheMap;
31 struct ScQueryEntry;
32 
40 class ScLookupCache final : public SvtListener
41 {
42 public:
43 
44  enum Result
45  {
50  };
51 
52  enum QueryOp
53  {
58  };
59 
61  {
62  union
63  {
64  double mfVal;
65  const OUString *mpStr;
66  };
67  bool mbAlloc;
68  bool mbString;
70 
71  void deleteString()
72  {
73  if (mbAlloc && mbString)
74  delete mpStr;
75  }
76 
77  QueryCriteria & operator=( const QueryCriteria & r ) = delete;
78 
79  public:
80 
81  explicit QueryCriteria( const ScQueryEntry & rEntry );
82  QueryCriteria( const QueryCriteria & r );
84 
85  QueryOp getQueryOp() const { return meOp; }
86 
87  void setDouble( double fVal )
88  {
89  deleteString();
90  mbAlloc = mbString = false;
91  mfVal = fVal;
92  }
93 
94  void setString( const OUString & rStr )
95  {
96  deleteString();
97  mbAlloc = mbString = true;
98  mpStr = new OUString( rStr);
99  }
100 
101  bool operator==( const QueryCriteria & r ) const
102  {
103  return meOp == r.meOp && mbString == r.mbString &&
104  (mbString ? (*mpStr == *r.mpStr) : (mfVal == r.mfVal));
105  }
106 
107  bool isEmptyStringQuery() const
108  {
109  return (getQueryOp() == QueryOp::EQUAL) && mbString && mpStr && mpStr->isEmpty();
110  }
111  };
112 
114  ScLookupCache( ScDocument * pDoc, const ScRange & rRange, ScLookupCacheMap & cacheMap );
115  virtual ~ScLookupCache() override;
117  virtual void Notify( const SfxHint& rHint ) override;
118 
120  Result lookup( ScAddress & o_rResultAddress,
121  const QueryCriteria & rCriteria,
122  const ScAddress & rQueryAddress ) const;
123 
124  SCROW lookup( const QueryCriteria & rCriteria ) const;
125 
132  bool insert( const ScAddress & rResultAddress,
133  const QueryCriteria & rCriteria,
134  const ScAddress & rQueryAddress,
135  const bool bAvailable );
136 
137  const ScRange& getRange() const { return maRange; }
138 
139  ScLookupCacheMap & getCacheMap() const { return mCacheMap; }
140 
141  struct Hash
142  {
143  size_t operator()( const ScRange & rRange ) const
144  {
145  // Lookups are performed on the first column.
146  return rRange.hashStartColumn();
147  }
148  };
149 
150 private:
151 
152  struct QueryKey
153  {
157 
158  QueryKey( const ScAddress & rAddress, const QueryOp eOp ) :
159  mnRow( rAddress.Row()),
160  mnTab( rAddress.Tab()),
161  meOp( eOp)
162  {
163  }
164 
165  bool operator==( const QueryKey & r ) const
166  {
167  return mnRow == r.mnRow && mnTab == r.mnTab && meOp == r.meOp && meOp != UNKNOWN;
168  }
169 
170  struct Hash
171  {
172  size_t operator()( const QueryKey & r ) const
173  {
174  return (static_cast<size_t>(r.mnTab) << 24) ^
175  (static_cast<size_t>(r.meOp) << 22) ^
176  static_cast<size_t>(r.mnRow);
177  }
178  };
179  };
180 
182  {
185 
186  QueryCriteriaAndResult( const QueryCriteria & rCriteria, const ScAddress & rAddress ) :
187  maCriteria( rCriteria),
188  maAddress( rAddress)
189  {
190  }
191  };
192 
193  std::unordered_map< QueryKey, QueryCriteriaAndResult, QueryKey::Hash > maQueryMap;
197 
198  ScLookupCache( const ScLookupCache & ) = delete;
199  ScLookupCache & operator=( const ScLookupCache & ) = delete;
200 
201 };
202 
203 // Struct because including lookupcache.hxx in document.hxx isn't wanted.
205 {
206  std::unordered_map< ScRange, std::unique_ptr<ScLookupCache>, ScLookupCache::Hash > aCacheMap;
207 };
208 
209 
210 #endif
211 
212 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ScLookupCacheMap & mCacheMap
Result
virtual ~ScLookupCache() override
Definition: lookupcache.cxx:78
QueryKey(const ScAddress &rAddress, const QueryOp eOp)
size_t hashStartColumn() const
Hash start column and start and end rows.
Definition: address.hxx:774
ScDocument * mpDoc
Query not found in cache.
Definition: lookupcache.hxx:47
Different criteria for same query position exists.
Definition: lookupcache.hxx:48
size_t operator()(const QueryKey &r) const
Lookup cache for one range used with interpreter functions such as VLOOKUP and MATCH.
Definition: lookupcache.hxx:40
virtual void Notify(const SfxHint &rHint) override
Remove from document structure and delete (!) cache on modify hint.
QueryOp getQueryOp() const
Definition: lookupcache.hxx:85
QueryCriteria(const ScQueryEntry &rEntry)
Definition: lookupcache.cxx:27
QueryCriteriaAndResult(const QueryCriteria &rCriteria, const ScAddress &rAddress)
const ScRange & getRange() const
QueryCriteria & operator=(const QueryCriteria &r)=delete
bool insert(const ScAddress &rResultAddress, const QueryCriteria &rCriteria, const ScAddress &rQueryAddress, const bool bAvailable)
Insert query and result.
void setString(const OUString &rStr)
Definition: lookupcache.hxx:94
ScLookupCache(ScDocument *pDoc, const ScRange &rRange, ScLookupCacheMap &cacheMap)
MUST be new'd because Notify() deletes.
Definition: lookupcache.cxx:71
bool operator==(const QueryCriteria &r) const
ScLookupCacheMap & getCacheMap() const
std::unordered_map< ScRange, std::unique_ptr< ScLookupCache >, ScLookupCache::Hash > aCacheMap
sal_Int32 SCROW
Definition: types.hxx:18
size_t operator()(const ScRange &rRange) const
Criteria not available in lookup range.
Definition: lookupcache.hxx:49
ScLookupCache & operator=(const ScLookupCache &)=delete
Result lookup(ScAddress &o_rResultAddress, const QueryCriteria &rCriteria, const ScAddress &rQueryAddress) const
Definition: lookupcache.cxx:82
void setDouble(double fVal)
Definition: lookupcache.hxx:87
bool operator==(const QueryKey &r) const
Each instance of this struct represents a single filtering criteria.
Definition: queryentry.hxx:33
sal_Int16 SCTAB
Definition: types.hxx:23
std::unordered_map< QueryKey, QueryCriteriaAndResult, QueryKey::Hash > maQueryMap