xref: /aoo41x/main/sc/source/core/tool/lookupcache.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_sc.hxx"
30 
31 #include "lookupcache.hxx"
32 #include "document.hxx"
33 
34 #ifdef erDEBUG
35 #include <cstdio>
36 using ::std::fprintf;
37 static long nCacheCount = 0;
38 #endif
39 
40 
41 ScLookupCache::ScLookupCache( ScDocument * pDoc, const ScRange & rRange ) :
42     maRange( rRange),
43     mpDoc( pDoc)
44 {
45 #ifdef erDEBUG
46     ++nCacheCount;
47     fprintf( stderr, "\nctor ScLookupCache %ld: %d, %d, %d, %d, %d, %d; buckets: %lu, size: %lu\n",
48             nCacheCount,
49             (int)getRange().aStart.Col(), (int)getRange().aStart.Row(),
50             (int)getRange().aStart.Tab(), (int)getRange().aEnd.Col(),
51             (int)getRange().aEnd.Row(), (int)getRange().aEnd.Tab(),
52             (unsigned long)maQueryMap.bucket_count(), (unsigned long)maQueryMap.size());
53 #endif
54 }
55 
56 
57 ScLookupCache::~ScLookupCache()
58 {
59 #ifdef erDEBUG
60     fprintf( stderr, "\ndtor ScLookupCache %ld: %d, %d, %d, %d, %d, %d; buckets: %lu, size: %lu\n",
61             nCacheCount,
62             (int)getRange().aStart.Col(), (int)getRange().aStart.Row(),
63             (int)getRange().aStart.Tab(), (int)getRange().aEnd.Col(),
64             (int)getRange().aEnd.Row(), (int)getRange().aEnd.Tab(),
65             (unsigned long)maQueryMap.bucket_count(), (unsigned long)maQueryMap.size());
66     --nCacheCount;
67 #endif
68 }
69 
70 
71 ScLookupCache::Result ScLookupCache::lookup( ScAddress & o_rResultAddress,
72         const QueryCriteria & rCriteria, const ScAddress & rQueryAddress ) const
73 {
74     QueryMap::const_iterator 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 
87 
88 bool ScLookupCache::insert( const ScAddress & rResultAddress,
89         const QueryCriteria & rCriteria, const ScAddress & rQueryAddress,
90         const bool bAvailable )
91 {
92 #ifdef erDEBUG
93     size_t nBuckets = maQueryMap.bucket_count();
94 #endif
95     QueryKey aKey( rQueryAddress, rCriteria.getQueryOp());
96     QueryCriteriaAndResult aResult( rCriteria, rResultAddress);
97     if (!bAvailable)
98         aResult.maAddress.SetRow(-1);
99     bool bInserted = maQueryMap.insert( ::std::pair< const QueryKey,
100             QueryCriteriaAndResult>( aKey, aResult)).second;
101 #ifdef erDEBUG
102     if (nBuckets != maQueryMap.bucket_count())
103     {
104         fprintf( stderr, "\nbuck ScLookupCache: %d, %d, %d, %d, %d, %d; buckets: %lu, size: %lu\n",
105                 (int)getRange().aStart.Col(), (int)getRange().aStart.Row(),
106                 (int)getRange().aStart.Tab(), (int)getRange().aEnd.Col(),
107                 (int)getRange().aEnd.Row(), (int)getRange().aEnd.Tab(),
108                 (unsigned long)maQueryMap.bucket_count(), (unsigned long)maQueryMap.size());
109     }
110 #endif
111     return bInserted;
112 }
113 
114 
115 void ScLookupCache::Notify( SvtBroadcaster & /* rBC */ , const SfxHint &  rHint )
116 {
117 	if (!mpDoc->IsInDtorClear())
118 	{
119 		const ScHint* p = PTR_CAST( ScHint, &rHint );
120 		if (p && (p->GetId() & (SC_HINT_DATACHANGED | SC_HINT_DYING)))
121 		{
122             mpDoc->RemoveLookupCache( *this);
123             delete this;
124 		}
125 	}
126 }
127