1*b3f79822SAndrew Rist /**************************************************************
2cdf0e10cSrcweir *
3*b3f79822SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one
4*b3f79822SAndrew Rist * or more contributor license agreements. See the NOTICE file
5*b3f79822SAndrew Rist * distributed with this work for additional information
6*b3f79822SAndrew Rist * regarding copyright ownership. The ASF licenses this file
7*b3f79822SAndrew Rist * to you under the Apache License, Version 2.0 (the
8*b3f79822SAndrew Rist * "License"); you may not use this file except in compliance
9*b3f79822SAndrew Rist * with the License. You may obtain a copy of the License at
10cdf0e10cSrcweir *
11*b3f79822SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir *
13*b3f79822SAndrew Rist * Unless required by applicable law or agreed to in writing,
14*b3f79822SAndrew Rist * software distributed under the License is distributed on an
15*b3f79822SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b3f79822SAndrew Rist * KIND, either express or implied. See the License for the
17*b3f79822SAndrew Rist * specific language governing permissions and limitations
18*b3f79822SAndrew Rist * under the License.
19cdf0e10cSrcweir *
20*b3f79822SAndrew Rist *************************************************************/
21*b3f79822SAndrew Rist
22*b3f79822SAndrew Rist
23cdf0e10cSrcweir
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_sc.hxx"
26cdf0e10cSrcweir
27cdf0e10cSrcweir #include "lookupcache.hxx"
28cdf0e10cSrcweir #include "document.hxx"
29cdf0e10cSrcweir
30cdf0e10cSrcweir #ifdef erDEBUG
31cdf0e10cSrcweir #include <cstdio>
32cdf0e10cSrcweir using ::std::fprintf;
33cdf0e10cSrcweir static long nCacheCount = 0;
34cdf0e10cSrcweir #endif
35cdf0e10cSrcweir
36cdf0e10cSrcweir
ScLookupCache(ScDocument * pDoc,const ScRange & rRange)37cdf0e10cSrcweir ScLookupCache::ScLookupCache( ScDocument * pDoc, const ScRange & rRange ) :
38cdf0e10cSrcweir maRange( rRange),
39cdf0e10cSrcweir mpDoc( pDoc)
40cdf0e10cSrcweir {
41cdf0e10cSrcweir #ifdef erDEBUG
42cdf0e10cSrcweir ++nCacheCount;
43cdf0e10cSrcweir fprintf( stderr, "\nctor ScLookupCache %ld: %d, %d, %d, %d, %d, %d; buckets: %lu, size: %lu\n",
44cdf0e10cSrcweir nCacheCount,
45cdf0e10cSrcweir (int)getRange().aStart.Col(), (int)getRange().aStart.Row(),
46cdf0e10cSrcweir (int)getRange().aStart.Tab(), (int)getRange().aEnd.Col(),
47cdf0e10cSrcweir (int)getRange().aEnd.Row(), (int)getRange().aEnd.Tab(),
48cdf0e10cSrcweir (unsigned long)maQueryMap.bucket_count(), (unsigned long)maQueryMap.size());
49cdf0e10cSrcweir #endif
50cdf0e10cSrcweir }
51cdf0e10cSrcweir
52cdf0e10cSrcweir
~ScLookupCache()53cdf0e10cSrcweir ScLookupCache::~ScLookupCache()
54cdf0e10cSrcweir {
55cdf0e10cSrcweir #ifdef erDEBUG
56cdf0e10cSrcweir fprintf( stderr, "\ndtor ScLookupCache %ld: %d, %d, %d, %d, %d, %d; buckets: %lu, size: %lu\n",
57cdf0e10cSrcweir nCacheCount,
58cdf0e10cSrcweir (int)getRange().aStart.Col(), (int)getRange().aStart.Row(),
59cdf0e10cSrcweir (int)getRange().aStart.Tab(), (int)getRange().aEnd.Col(),
60cdf0e10cSrcweir (int)getRange().aEnd.Row(), (int)getRange().aEnd.Tab(),
61cdf0e10cSrcweir (unsigned long)maQueryMap.bucket_count(), (unsigned long)maQueryMap.size());
62cdf0e10cSrcweir --nCacheCount;
63cdf0e10cSrcweir #endif
64cdf0e10cSrcweir }
65cdf0e10cSrcweir
66cdf0e10cSrcweir
lookup(ScAddress & o_rResultAddress,const QueryCriteria & rCriteria,const ScAddress & rQueryAddress) const67cdf0e10cSrcweir ScLookupCache::Result ScLookupCache::lookup( ScAddress & o_rResultAddress,
68cdf0e10cSrcweir const QueryCriteria & rCriteria, const ScAddress & rQueryAddress ) const
69cdf0e10cSrcweir {
70cdf0e10cSrcweir QueryMap::const_iterator it( maQueryMap.find( QueryKey( rQueryAddress,
71cdf0e10cSrcweir rCriteria.getQueryOp())));
72cdf0e10cSrcweir if (it == maQueryMap.end())
73cdf0e10cSrcweir return NOT_CACHED;
74cdf0e10cSrcweir const QueryCriteriaAndResult& rResult = (*it).second;
75cdf0e10cSrcweir if (!(rResult.maCriteria == rCriteria))
76cdf0e10cSrcweir return CRITERIA_DIFFERENT;
77cdf0e10cSrcweir if (rResult.maAddress.Row() < 0 )
78cdf0e10cSrcweir return NOT_AVAILABLE;
79cdf0e10cSrcweir o_rResultAddress = rResult.maAddress;
80cdf0e10cSrcweir return FOUND;
81cdf0e10cSrcweir }
82cdf0e10cSrcweir
83cdf0e10cSrcweir
insert(const ScAddress & rResultAddress,const QueryCriteria & rCriteria,const ScAddress & rQueryAddress,const bool bAvailable)84cdf0e10cSrcweir bool ScLookupCache::insert( const ScAddress & rResultAddress,
85cdf0e10cSrcweir const QueryCriteria & rCriteria, const ScAddress & rQueryAddress,
86cdf0e10cSrcweir const bool bAvailable )
87cdf0e10cSrcweir {
88cdf0e10cSrcweir #ifdef erDEBUG
89cdf0e10cSrcweir size_t nBuckets = maQueryMap.bucket_count();
90cdf0e10cSrcweir #endif
91cdf0e10cSrcweir QueryKey aKey( rQueryAddress, rCriteria.getQueryOp());
92cdf0e10cSrcweir QueryCriteriaAndResult aResult( rCriteria, rResultAddress);
93cdf0e10cSrcweir if (!bAvailable)
94cdf0e10cSrcweir aResult.maAddress.SetRow(-1);
95cdf0e10cSrcweir bool bInserted = maQueryMap.insert( ::std::pair< const QueryKey,
96cdf0e10cSrcweir QueryCriteriaAndResult>( aKey, aResult)).second;
97cdf0e10cSrcweir #ifdef erDEBUG
98cdf0e10cSrcweir if (nBuckets != maQueryMap.bucket_count())
99cdf0e10cSrcweir {
100cdf0e10cSrcweir fprintf( stderr, "\nbuck ScLookupCache: %d, %d, %d, %d, %d, %d; buckets: %lu, size: %lu\n",
101cdf0e10cSrcweir (int)getRange().aStart.Col(), (int)getRange().aStart.Row(),
102cdf0e10cSrcweir (int)getRange().aStart.Tab(), (int)getRange().aEnd.Col(),
103cdf0e10cSrcweir (int)getRange().aEnd.Row(), (int)getRange().aEnd.Tab(),
104cdf0e10cSrcweir (unsigned long)maQueryMap.bucket_count(), (unsigned long)maQueryMap.size());
105cdf0e10cSrcweir }
106cdf0e10cSrcweir #endif
107cdf0e10cSrcweir return bInserted;
108cdf0e10cSrcweir }
109cdf0e10cSrcweir
110cdf0e10cSrcweir
Notify(SvtBroadcaster &,const SfxHint & rHint)111cdf0e10cSrcweir void ScLookupCache::Notify( SvtBroadcaster & /* rBC */ , const SfxHint & rHint )
112cdf0e10cSrcweir {
113cdf0e10cSrcweir if (!mpDoc->IsInDtorClear())
114cdf0e10cSrcweir {
115cdf0e10cSrcweir const ScHint* p = PTR_CAST( ScHint, &rHint );
116cdf0e10cSrcweir if (p && (p->GetId() & (SC_HINT_DATACHANGED | SC_HINT_DYING)))
117cdf0e10cSrcweir {
118cdf0e10cSrcweir mpDoc->RemoveLookupCache( *this);
119cdf0e10cSrcweir delete this;
120cdf0e10cSrcweir }
121cdf0e10cSrcweir }
122cdf0e10cSrcweir }
123