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
, sal_Int8 nSearchMode
) :
28 mfVal(0.0), mbAlloc(false), mbString(false), meSearchMode(static_cast<SearchMode
>(nSearchMode
))
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
) :
58 meSearchMode( r
.meSearchMode
)
60 if (r
.mbString
&& r
.mpStr
)
62 mpStr
= new OUString( *r
.mpStr
);
63 mbAlloc
= mbString
= true;
67 ScLookupCache::QueryCriteria::~QueryCriteria()
72 ScLookupCache::Result
ScLookupCache::lookup( ScAddress
& o_rResultAddress
,
73 const QueryCriteria
& rCriteria
, const ScAddress
& rQueryAddress
) const
75 auto it( maQueryMap
.find( QueryKey( rQueryAddress
,
76 rCriteria
.getQueryOp(), rCriteria
.getSearchMode())));
77 if (it
== maQueryMap
.end())
79 const QueryCriteriaAndResult
& rResult
= (*it
).second
;
80 if (!(rResult
.maCriteria
== rCriteria
))
81 return CRITERIA_DIFFERENT
;
82 if (rResult
.maAddress
.Row() < 0 )
84 o_rResultAddress
= rResult
.maAddress
;
88 SCROW
ScLookupCache::lookup( const QueryCriteria
& rCriteria
) const
90 // try to find the row index for which we have already performed lookup
91 auto it
= std::find_if(maQueryMap
.begin(), maQueryMap
.end(),
92 [&rCriteria
](const std::pair
<QueryKey
, QueryCriteriaAndResult
>& rEntry
) {
93 return rEntry
.second
.maCriteria
== rCriteria
;
95 if (it
!= maQueryMap
.end())
96 return it
->first
.mnRow
;
102 bool ScLookupCache::insert( const ScAddress
& rResultAddress
,
103 const QueryCriteria
& rCriteria
, const ScAddress
& rQueryAddress
,
104 const bool bAvailable
)
106 QueryKey
aKey( rQueryAddress
, rCriteria
.getQueryOp(), rCriteria
.getSearchMode() );
107 QueryCriteriaAndResult
aResult( rCriteria
, rResultAddress
);
109 aResult
.maAddress
.SetRow(-1);
110 bool bInserted
= maQueryMap
.insert( ::std::pair
< const QueryKey
,
111 QueryCriteriaAndResult
>( aKey
, aResult
)).second
;
116 void ScLookupCache::Notify( const SfxHint
& rHint
)
118 if (!mpDoc
->IsInDtorClear())
120 if (rHint
.GetId() == SfxHintId::ScDataChanged
|| rHint
.GetId() == SfxHintId::ScAreaChanged
)
122 mpDoc
->RemoveLookupCache( *this);
123 // this ScLookupCache is deleted by RemoveLookupCache
128 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */