Impress Remote 1.0.5, tag sdremote-1.0.5
[LibreOffice.git] / sc / inc / lookupcache.hxx
blobaa8f4f7e698b74e731c5f184789e681c74df952f
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 #ifndef INCLUDED_SC_LOOKUPCACHE_HXX
21 #define INCLUDED_SC_LOOKUPCACHE_HXX
23 #include "address.hxx"
24 #include "global.hxx"
25 #include "formula/token.hxx"
26 #include <svl/listener.hxx>
27 #include <tools/string.hxx>
29 #include <boost/unordered_map.hpp>
31 class ScDocument;
32 struct ScQueryEntry;
34 /** Lookup cache for one range used with interpreter functions such as VLOOKUP
35 and MATCH. Caches query for a specific row and the resulting address looked
36 up, in case other lookups of the same query in the same row are to be
37 performed, which usually occur to obtain a different offset column of the
38 same query.
41 class ScLookupCache : public SvtListener
43 public:
45 enum Result
47 NOT_CACHED, /// Query not found in cache.
48 CRITERIA_DIFFERENT, /// Different criteria for same query position exists.
49 NOT_AVAILABLE, /// Criteria not available in lookup range.
50 FOUND /// Criteria found.
53 enum QueryOp
55 UNKNOWN,
56 EQUAL,
57 LESS_EQUAL,
58 GREATER_EQUAL
61 class QueryCriteria
63 union
65 double mfVal;
66 const String * mpStr;
68 bool mbAlloc : 1;
69 bool mbString : 1;
70 QueryOp meOp : 2;
72 void deleteString()
74 if (mbAlloc && mbString)
75 delete mpStr;
78 // prevent usage
79 QueryCriteria();
80 QueryCriteria & operator=( const QueryCriteria & r );
82 public:
84 explicit QueryCriteria( const ScQueryEntry & rEntry );
85 QueryCriteria( const QueryCriteria & r );
86 ~QueryCriteria();
88 QueryOp getQueryOp() const { return meOp; }
90 void setDouble( double fVal )
92 deleteString();
93 mbAlloc = mbString = false;
94 mfVal = fVal;
97 void setString( const String * pStr )
99 deleteString();
100 mbAlloc = false;
101 mbString = true;
102 mpStr = pStr;
105 void setString( const String & rStr )
107 deleteString();
108 mbAlloc = mbString = true;
109 mpStr = new String( rStr);
112 bool operator==( const QueryCriteria & r ) const
114 return meOp == r.meOp && mbString == r.mbString &&
115 (mbString ? (*mpStr == *r.mpStr) : (mfVal == r.mfVal));
120 /// MUST be new'd because Notify() deletes.
121 ScLookupCache( ScDocument * pDoc, const ScRange & rRange );
122 virtual ~ScLookupCache();
123 /// Remove from document structure and delete (!) cache on modify hint.
124 virtual void Notify( SvtBroadcaster & rBC, const SfxHint & rHint );
126 /// @returns document address in o_rAddress if Result==FOUND
127 Result lookup( ScAddress & o_rResultAddress,
128 const QueryCriteria & rCriteria,
129 const ScAddress & rQueryAddress ) const;
131 /** Insert query and result.
132 @param bAvailable
133 Pass sal_False if the search didn't deliver a result. A subsequent
134 lookup() then will return Result::NOT_AVAILABLE.
135 @returns successful insertion.
137 bool insert( const ScAddress & rResultAddress,
138 const QueryCriteria & rCriteria,
139 const ScAddress & rQueryAddress,
140 const bool bAvailable );
142 inline const ScRange& getRange() const { return maRange; }
144 struct Hash
146 size_t operator()( const ScRange & rRange ) const
148 // Lookups are performed on the first column.
149 return rRange.hashStartColumn();
153 private:
155 struct QueryKey
157 SCROW mnRow;
158 SCTAB mnTab;
159 QueryOp meOp : 2;
161 QueryKey( const ScAddress & rAddress, const QueryOp eOp ) :
162 mnRow( rAddress.Row()),
163 mnTab( rAddress.Tab()),
164 meOp( eOp)
168 bool operator==( const QueryKey & r ) const
170 return mnRow == r.mnRow && mnTab == r.mnTab && meOp == r.meOp && meOp != UNKNOWN;
173 struct Hash
175 size_t operator()( const QueryKey & r ) const
177 return (static_cast<size_t>(r.mnTab) << 24) ^
178 (static_cast<size_t>(r.meOp) << 22) ^
179 static_cast<size_t>(r.mnRow);
184 struct QueryCriteriaAndResult
186 QueryCriteria maCriteria;
187 ScAddress maAddress;
189 QueryCriteriaAndResult( const QueryCriteria & rCriteria, const ScAddress & rAddress ) :
190 maCriteria( rCriteria),
191 maAddress( rAddress)
194 ~QueryCriteriaAndResult()
199 typedef ::boost::unordered_map< QueryKey, QueryCriteriaAndResult, QueryKey::Hash, ::std::equal_to< QueryKey > > QueryMap;
200 QueryMap maQueryMap;
201 ScRange maRange;
202 ScDocument * mpDoc;
204 // prevent usage
205 ScLookupCache( const ScLookupCache & );
206 ScLookupCache & operator=( const ScLookupCache & );
211 typedef ::boost::unordered_map< ScRange, ScLookupCache*, ScLookupCache::Hash, ::std::equal_to< ScRange > > ScLookupCacheMap;
213 #endif
215 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */