1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <lookupcache.hxx>
21 #include <document.hxx>
22 #include <queryentry.hxx>
25 #include <sal/log.hxx>
27 ScLookupCache::QueryCriteria::QueryCriteria( const ScQueryEntry
& rEntry
) :
28 mfVal(0.0), mbAlloc(false), mbString(false)
38 case SC_GREATER_EQUAL
:
43 SAL_WARN( "sc.core", "ScLookupCache::QueryCriteria not prepared for this ScQueryOp");
46 const ScQueryEntry::Item
& rItem
= rEntry
.GetQueryItem();
47 if (rItem
.meType
== ScQueryEntry::ByString
)
48 setString(rItem
.maString
.getString());
50 setDouble(rItem
.mfVal
);
53 ScLookupCache::QueryCriteria::QueryCriteria( const ScLookupCache::QueryCriteria
& r
) :
59 if (r
.mbString
&& r
.mpStr
)
61 mpStr
= new OUString( *r
.mpStr
);
62 mbAlloc
= mbString
= true;
66 ScLookupCache::QueryCriteria::~QueryCriteria()
71 ScLookupCache::Result
ScLookupCache::lookup( ScAddress
& o_rResultAddress
,
72 const QueryCriteria
& rCriteria
, const ScAddress
& rQueryAddress
) const
74 auto it( maQueryMap
.find( QueryKey( rQueryAddress
,
75 rCriteria
.getQueryOp())));
76 if (it
== maQueryMap
.end())
78 const QueryCriteriaAndResult
& rResult
= (*it
).second
;
79 if (!(rResult
.maCriteria
== rCriteria
))
80 return CRITERIA_DIFFERENT
;
81 if (rResult
.maAddress
.Row() < 0 )
83 o_rResultAddress
= rResult
.maAddress
;
87 SCROW
ScLookupCache::lookup( const QueryCriteria
& rCriteria
) const
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
;
94 if (it
!= maQueryMap
.end())
95 return it
->first
.mnRow
;
101 bool ScLookupCache::insert( const ScAddress
& rResultAddress
,
102 const QueryCriteria
& rCriteria
, const ScAddress
& rQueryAddress
,
103 const bool bAvailable
)
105 QueryKey
aKey( rQueryAddress
, rCriteria
.getQueryOp());
106 QueryCriteriaAndResult
aResult( rCriteria
, rResultAddress
);
108 aResult
.maAddress
.SetRow(-1);
109 bool bInserted
= maQueryMap
.insert( ::std::pair
< const QueryKey
,
110 QueryCriteriaAndResult
>( aKey
, aResult
)).second
;
115 void ScLookupCache::Notify( const SfxHint
& rHint
)
117 if (!mpDoc
->IsInDtorClear())
119 if (rHint
.GetId() == SfxHintId::ScDataChanged
|| rHint
.GetId() == SfxHintId::ScAreaChanged
)
121 mpDoc
->RemoveLookupCache( *this);
122 // this ScLookupCache is deleted by RemoveLookupCache
127 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */