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