update dev300-m57
[ooovba.git] / sc / source / core / tool / lookupcache.cxx
blobe7b5a3ce4d66ad1058e07ca837eadf8e01ac3a5a
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: lookupcache.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_sc.hxx"
34 #include "lookupcache.hxx"
35 #include "document.hxx"
37 #ifdef erDEBUG
38 #include <cstdio>
39 using ::std::fprintf;
40 static long nCacheCount = 0;
41 #endif
44 ScLookupCache::ScLookupCache( ScDocument * pDoc, const ScRange & rRange ) :
45 maRange( rRange),
46 mpDoc( pDoc)
48 #ifdef erDEBUG
49 ++nCacheCount;
50 fprintf( stderr, "\nctor ScLookupCache %ld: %d, %d, %d, %d, %d, %d; buckets: %lu, size: %lu\n",
51 nCacheCount,
52 (int)getRange().aStart.Col(), (int)getRange().aStart.Row(),
53 (int)getRange().aStart.Tab(), (int)getRange().aEnd.Col(),
54 (int)getRange().aEnd.Row(), (int)getRange().aEnd.Tab(),
55 (unsigned long)maQueryMap.bucket_count(), (unsigned long)maQueryMap.size());
56 #endif
60 ScLookupCache::~ScLookupCache()
62 #ifdef erDEBUG
63 fprintf( stderr, "\ndtor ScLookupCache %ld: %d, %d, %d, %d, %d, %d; buckets: %lu, size: %lu\n",
64 nCacheCount,
65 (int)getRange().aStart.Col(), (int)getRange().aStart.Row(),
66 (int)getRange().aStart.Tab(), (int)getRange().aEnd.Col(),
67 (int)getRange().aEnd.Row(), (int)getRange().aEnd.Tab(),
68 (unsigned long)maQueryMap.bucket_count(), (unsigned long)maQueryMap.size());
69 --nCacheCount;
70 #endif
74 ScLookupCache::Result ScLookupCache::lookup( ScAddress & o_rResultAddress,
75 const QueryCriteria & rCriteria, const ScAddress & rQueryAddress ) const
77 QueryMap::const_iterator it( maQueryMap.find( QueryKey( rQueryAddress,
78 rCriteria.getQueryOp())));
79 if (it == maQueryMap.end())
80 return NOT_CACHED;
81 const QueryCriteriaAndResult& rResult = (*it).second;
82 if (!(rResult.maCriteria == rCriteria))
83 return CRITERIA_DIFFERENT;
84 if (rResult.maAddress.Row() < 0 )
85 return NOT_AVAILABLE;
86 o_rResultAddress = rResult.maAddress;
87 return FOUND;
91 bool ScLookupCache::insert( const ScAddress & rResultAddress,
92 const QueryCriteria & rCriteria, const ScAddress & rQueryAddress,
93 const bool bAvailable )
95 #ifdef erDEBUG
96 size_t nBuckets = maQueryMap.bucket_count();
97 #endif
98 QueryKey aKey( rQueryAddress, rCriteria.getQueryOp());
99 QueryCriteriaAndResult aResult( rCriteria, rResultAddress);
100 if (!bAvailable)
101 aResult.maAddress.SetRow(-1);
102 bool bInserted = maQueryMap.insert( ::std::pair< const QueryKey,
103 QueryCriteriaAndResult>( aKey, aResult)).second;
104 #ifdef erDEBUG
105 if (nBuckets != maQueryMap.bucket_count())
107 fprintf( stderr, "\nbuck ScLookupCache: %d, %d, %d, %d, %d, %d; buckets: %lu, size: %lu\n",
108 (int)getRange().aStart.Col(), (int)getRange().aStart.Row(),
109 (int)getRange().aStart.Tab(), (int)getRange().aEnd.Col(),
110 (int)getRange().aEnd.Row(), (int)getRange().aEnd.Tab(),
111 (unsigned long)maQueryMap.bucket_count(), (unsigned long)maQueryMap.size());
113 #endif
114 return bInserted;
118 void ScLookupCache::Notify( SvtBroadcaster & /* rBC */ , const SfxHint & rHint )
120 if (!mpDoc->IsInDtorClear())
122 const ScHint* p = PTR_CAST( ScHint, &rHint );
123 if (p && (p->GetId() & (SC_HINT_DATACHANGED | SC_HINT_DYING)))
125 mpDoc->RemoveLookupCache( *this);
126 delete this;