LibreOffice Module sc (master)  1
lookupcache.cxx
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 #include <lookupcache.hxx>
21 #include <document.hxx>
22 #include <queryentry.hxx>
23 #include <brdcst.hxx>
24 
25 #include <sal/log.hxx>
26 
28  mfVal(0.0), mbAlloc(false), mbString(false)
29 {
30  switch (rEntry.eOp)
31  {
32  case SC_EQUAL :
33  meOp = EQUAL;
34  break;
35  case SC_LESS_EQUAL :
36  meOp = LESS_EQUAL;
37  break;
38  case SC_GREATER_EQUAL :
40  break;
41  default:
42  meOp = UNKNOWN;
43  SAL_WARN( "sc.core", "ScLookupCache::QueryCriteria not prepared for this ScQueryOp");
44  }
45 
46  const ScQueryEntry::Item& rItem = rEntry.GetQueryItem();
47  if (rItem.meType == ScQueryEntry::ByString)
48  setString(rItem.maString.getString());
49  else
50  setDouble(rItem.mfVal);
51 }
52 
54  mfVal( r.mfVal),
55  mbAlloc( false),
56  mbString( false),
57  meOp( r.meOp)
58 {
59  if (r.mbString && r.mpStr)
60  {
61  mpStr = new OUString( *r.mpStr);
62  mbAlloc = mbString = true;
63  }
64 }
65 
67 {
68  deleteString();
69 }
70 
71 ScLookupCache::ScLookupCache( ScDocument * pDoc, const ScRange & rRange, ScLookupCacheMap & cacheMap ) :
72  maRange( rRange),
73  mpDoc( pDoc),
74  mCacheMap(cacheMap)
75 {
76 }
77 
79 {
80 }
81 
83  const QueryCriteria & rCriteria, const ScAddress & rQueryAddress ) const
84 {
85  auto it( maQueryMap.find( QueryKey( rQueryAddress,
86  rCriteria.getQueryOp())));
87  if (it == maQueryMap.end())
88  return NOT_CACHED;
89  const QueryCriteriaAndResult& rResult = (*it).second;
90  if (!(rResult.maCriteria == rCriteria))
91  return CRITERIA_DIFFERENT;
92  if (rResult.maAddress.Row() < 0 )
93  return NOT_AVAILABLE;
94  o_rResultAddress = rResult.maAddress;
95  return FOUND;
96 }
97 
98 SCROW ScLookupCache::lookup( const QueryCriteria & rCriteria ) const
99 {
100  // try to find the row index for which we have already performed lookup
101  auto it = std::find_if(maQueryMap.begin(), maQueryMap.end(),
102  [&rCriteria](const std::pair<QueryKey, QueryCriteriaAndResult>& rEntry) {
103  return rEntry.second.maCriteria == rCriteria;
104  });
105  if (it != maQueryMap.end())
106  return it->first.mnRow;
107 
108  // not found
109  return -1;
110 }
111 
112 bool ScLookupCache::insert( const ScAddress & rResultAddress,
113  const QueryCriteria & rCriteria, const ScAddress & rQueryAddress,
114  const bool bAvailable )
115 {
116  QueryKey aKey( rQueryAddress, rCriteria.getQueryOp());
117  QueryCriteriaAndResult aResult( rCriteria, rResultAddress);
118  if (!bAvailable)
119  aResult.maAddress.SetRow(-1);
120  bool bInserted = maQueryMap.insert( ::std::pair< const QueryKey,
121  QueryCriteriaAndResult>( aKey, aResult)).second;
122 
123  return bInserted;
124 }
125 
126 void ScLookupCache::Notify( const SfxHint& rHint )
127 {
128  if (!mpDoc->IsInDtorClear())
129  {
130  const ScHint* p = dynamic_cast<const ScHint*>(&rHint);
131  if ((p && (p->GetId() == SfxHintId::ScDataChanged)) || dynamic_cast<const ScAreaChangedHint*>(&rHint))
132  {
133  mpDoc->RemoveLookupCache( *this);
134  delete this;
135  }
136  }
137 }
138 
139 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ScLookupCacheMap & mCacheMap
OUString getString() const
SCROW Row() const
Definition: address.hxx:262
virtual ~ScLookupCache() override
Definition: lookupcache.cxx:78
ScDocument * mpDoc
Query not found in cache.
Definition: lookupcache.hxx:47
Different criteria for same query position exists.
Definition: lookupcache.hxx:48
virtual void Notify(const SfxHint &rHint) override
Remove from document structure and delete (!) cache on modify hint.
QueryOp getQueryOp() const
Definition: lookupcache.hxx:85
SfxHintId GetId() const
const Item & GetQueryItem() const
Definition: queryentry.cxx:118
QueryCriteria(const ScQueryEntry &rEntry)
Definition: lookupcache.cxx:27
double mfVal
Definition: scmatrix.cxx:3355
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
svl::SharedString maString
Definition: queryentry.hxx:41
bool IsInDtorClear() const
Definition: document.hxx:2353
sal_Int32 SCROW
Definition: types.hxx:18
Criteria not available in lookup range.
Definition: lookupcache.hxx:49
void * p
Result lookup(ScAddress &o_rResultAddress, const QueryCriteria &rCriteria, const ScAddress &rQueryAddress) const
Definition: lookupcache.cxx:82
#define SAL_WARN(area, stream)
void setDouble(double fVal)
Definition: lookupcache.hxx:87
ScQueryOp eOp
Definition: queryentry.hxx:52
Each instance of this struct represents a single filtering criteria.
Definition: queryentry.hxx:33
void RemoveLookupCache(ScLookupCache &rCache)
Only ScLookupCache dtor uses RemoveLookupCache(), do not use elsewhere!
Definition: documen2.cxx:1174
std::unordered_map< QueryKey, QueryCriteriaAndResult, QueryKey::Hash > maQueryMap