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 
28 class ScDocument;
29 struct ScLookupCacheMap;
30 struct ScQueryEntry;
31 
39 class ScLookupCache final : public SvtListener
40 {
41 public:
42 
43  enum Result
44  {
49  };
50 
51  enum QueryOp
52  {
57  };
58 
60  {
61  union
62  {
63  double mfVal;
64  const OUString *mpStr;
65  };
66  bool mbAlloc;
67  bool mbString;
69 
70  void deleteString()
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  {
88  deleteString();
89  mbAlloc = mbString = false;
90  mfVal = fVal;
91  }
92 
93  void setString( const OUString & rStr )
94  {
95  deleteString();
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 
106  bool isEmptyStringQuery() const
107  {
108  return (getQueryOp() == QueryOp::EQUAL) && mbString && mpStr && mpStr->isEmpty();
109  }
110  };
111 
113  ScLookupCache( ScDocument * pDoc, const ScRange & rRange, ScLookupCacheMap & cacheMap );
114  virtual ~ScLookupCache() override;
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 
138  ScLookupCacheMap & getCacheMap() const { return mCacheMap; }
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 
149 private:
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: */
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:773
ScDocument * mpDoc
Query not found in cache.
Definition: lookupcache.hxx:46
Different criteria for same query position exists.
Definition: lookupcache.hxx:47
size_t operator()(const QueryKey &r) const
Lookup cache for one range used with interpreter functions such as VLOOKUP and MATCH.
Definition: lookupcache.hxx:39
virtual void Notify(const SfxHint &rHint) override
Remove from document structure and delete (!) cache on modify hint.
QueryOp getQueryOp() const
Definition: lookupcache.hxx:84
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:93
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:17
size_t operator()(const ScRange &rRange) const
Criteria not available in lookup range.
Definition: lookupcache.hxx:48
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:86
bool operator==(const QueryKey &r) const
Each instance of this struct represents a single filtering criteria.
Definition: queryentry.hxx:32
sal_Int16 SCTAB
Definition: types.hxx:22
std::unordered_map< QueryKey, QueryCriteriaAndResult, QueryKey::Hash > maQueryMap