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 :
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)
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
72 const QueryCriteria & rCriteria, const ScAddress & rQueryAddress ) const
73{
74 auto it( maQueryMap.find( QueryKey( rQueryAddress,
75 rCriteria.getQueryOp())));
76 if (it == maQueryMap.end())
77 return NOT_CACHED;
78 const QueryCriteriaAndResult& rResult = (*it).second;
79 if (!(rResult.maCriteria == rCriteria))
80 return CRITERIA_DIFFERENT;
81 if (rResult.maAddress.Row() < 0 )
82 return NOT_AVAILABLE;
83 o_rResultAddress = rResult.maAddress;
84 return FOUND;
85}
86
87SCROW ScLookupCache::lookup( const QueryCriteria & rCriteria ) const
88{
89 // try to find the row index for which we have already performed lookup
90 auto it = std::find_if(maQueryMap.begin(), maQueryMap.end(),
91 [&rCriteria](const std::pair<QueryKey, QueryCriteriaAndResult>& rEntry) {
92 return rEntry.second.maCriteria == rCriteria;
93 });
94 if (it != maQueryMap.end())
95 return it->first.mnRow;
96
97 // not found
98 return -1;
99}
100
101bool ScLookupCache::insert( const ScAddress & rResultAddress,
102 const QueryCriteria & rCriteria, const ScAddress & rQueryAddress,
103 const bool bAvailable )
104{
105 QueryKey aKey( rQueryAddress, rCriteria.getQueryOp());
106 QueryCriteriaAndResult aResult( rCriteria, rResultAddress);
107 if (!bAvailable)
108 aResult.maAddress.SetRow(-1);
109 bool bInserted = maQueryMap.insert( ::std::pair< const QueryKey,
110 QueryCriteriaAndResult>( aKey, aResult)).second;
111
112 return bInserted;
113}
114
115void ScLookupCache::Notify( const SfxHint& rHint )
116{
117 if (!mpDoc->IsInDtorClear())
118 {
119 if (rHint.GetId() == SfxHintId::ScDataChanged || rHint.GetId() == SfxHintId::ScAreaChanged)
120 {
121 mpDoc->RemoveLookupCache( *this);
122 // this ScLookupCache is deleted by RemoveLookupCache
123 }
124 }
125}
126
127/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SCROW Row() const
Definition: address.hxx:274
void SetRow(SCROW nRowP)
Definition: address.hxx:287
void RemoveLookupCache(ScLookupCache &rCache)
Only ScLookupCache dtor uses RemoveLookupCache(), do not use elsewhere!
Definition: documen2.cxx:1271
bool IsInDtorClear() const
Definition: document.hxx:2455
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
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
Result lookup(ScAddress &o_rResultAddress, const QueryCriteria &rCriteria, const ScAddress &rQueryAddress) const
Definition: lookupcache.cxx:71
ScDocument * mpDoc
SfxHintId GetId() const
const OUString & getString() const
@ SC_LESS_EQUAL
Definition: global.hxx:838
@ SC_GREATER_EQUAL
Definition: global.hxx:839
@ SC_EQUAL
Definition: global.hxx:835
#define SAL_WARN(area, stream)
double mfVal
Definition: scmatrix.cxx:3482
svl::SharedString maString
Definition: queryentry.hxx:49
Each instance of this struct represents a single filtering criteria.
Definition: queryentry.hxx:34
const Item & GetQueryItem() const
Definition: queryentry.hxx:85
ScQueryOp eOp
Definition: queryentry.hxx:62
sal_Int32 SCROW
Definition: types.hxx:17