Bump version to 4.3-4
[LibreOffice.git] / sc / inc / lookupcache.hxx
blob411ae6c5476393802a3c898257feb107529c2e57
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_INC_LOOKUPCACHE_HXX
21 #define INCLUDED_SC_INC_LOOKUPCACHE_HXX
23 #include "address.hxx"
24 #include "global.hxx"
25 #include <formula/token.hxx>
26 #include <svl/listener.hxx>
28 #include <boost/unordered_map.hpp>
30 class ScDocument;
31 struct ScQueryEntry;
33 /** Lookup cache for one range used with interpreter functions such as VLOOKUP
34 and MATCH. Caches query for a specific row and the resulting address looked
35 up, in case other lookups of the same query in the same row are to be
36 performed, which usually occur to obtain a different offset column of the
37 same query.
40 class ScLookupCache : public SvtListener
42 public:
44 enum Result
46 NOT_CACHED, /// Query not found in cache.
47 CRITERIA_DIFFERENT, /// Different criteria for same query position exists.
48 NOT_AVAILABLE, /// Criteria not available in lookup range.
49 FOUND /// Criteria found.
52 enum QueryOp
54 UNKNOWN,
55 EQUAL,
56 LESS_EQUAL,
57 GREATER_EQUAL
60 class QueryCriteria
62 union
64 double mfVal;
65 const OUString *mpStr;
67 bool mbAlloc : 1;
68 bool mbString : 1;
69 QueryOp meOp : 2;
71 void deleteString()
73 if (mbAlloc && mbString)
74 delete mpStr;
77 // prevent usage
78 QueryCriteria();
79 QueryCriteria & operator=( const QueryCriteria & r );
81 public:
83 explicit QueryCriteria( const ScQueryEntry & rEntry );
84 QueryCriteria( const QueryCriteria & r );
85 ~QueryCriteria();
87 QueryOp getQueryOp() const { return meOp; }
89 void setDouble( double fVal )
91 deleteString();
92 mbAlloc = mbString = false;
93 mfVal = fVal;
96 void setString( const OUString * pStr )
98 deleteString();
99 mbAlloc = false;
100 mbString = true;
101 mpStr = pStr;
104 void setString( const OUString & rStr )
106 deleteString();
107 mbAlloc = mbString = true;
108 mpStr = new OUString( rStr);
111 bool operator==( const QueryCriteria & r ) const
113 return meOp == r.meOp && mbString == r.mbString &&
114 (mbString ? (*mpStr == *r.mpStr) : (mfVal == r.mfVal));
119 /// MUST be new'd because Notify() deletes.
120 ScLookupCache( ScDocument * pDoc, const ScRange & rRange );
121 virtual ~ScLookupCache();
122 /// Remove from document structure and delete (!) cache on modify hint.
123 virtual void Notify( const SfxHint& rHint ) SAL_OVERRIDE;
125 /// @returns document address in o_rAddress if Result==FOUND
126 Result lookup( ScAddress & o_rResultAddress,
127 const QueryCriteria & rCriteria,
128 const ScAddress & rQueryAddress ) const;
130 /** Insert query and result.
131 @param bAvailable
132 Pass sal_False if the search didn't deliver a result. A subsequent
133 lookup() then will return Result::NOT_AVAILABLE.
134 @returns successful insertion.
136 bool insert( const ScAddress & rResultAddress,
137 const QueryCriteria & rCriteria,
138 const ScAddress & rQueryAddress,
139 const bool bAvailable );
141 inline const ScRange& getRange() const { return maRange; }
143 struct Hash
145 size_t operator()( const ScRange & rRange ) const
147 // Lookups are performed on the first column.
148 return rRange.hashStartColumn();
152 private:
154 struct QueryKey
156 SCROW mnRow;
157 SCTAB mnTab;
158 QueryOp meOp : 2;
160 QueryKey( const ScAddress & rAddress, const QueryOp eOp ) :
161 mnRow( rAddress.Row()),
162 mnTab( rAddress.Tab()),
163 meOp( eOp)
167 bool operator==( const QueryKey & r ) const
169 return mnRow == r.mnRow && mnTab == r.mnTab && meOp == r.meOp && meOp != UNKNOWN;
172 struct Hash
174 size_t operator()( const QueryKey & r ) const
176 return (static_cast<size_t>(r.mnTab) << 24) ^
177 (static_cast<size_t>(r.meOp) << 22) ^
178 static_cast<size_t>(r.mnRow);
183 struct QueryCriteriaAndResult
185 QueryCriteria maCriteria;
186 ScAddress maAddress;
188 QueryCriteriaAndResult( const QueryCriteria & rCriteria, const ScAddress & rAddress ) :
189 maCriteria( rCriteria),
190 maAddress( rAddress)
193 ~QueryCriteriaAndResult()
198 typedef ::boost::unordered_map< QueryKey, QueryCriteriaAndResult, QueryKey::Hash, ::std::equal_to< QueryKey > > QueryMap;
199 QueryMap maQueryMap;
200 ScRange maRange;
201 ScDocument * mpDoc;
203 // prevent usage
204 ScLookupCache( const ScLookupCache & );
205 ScLookupCache & operator=( const ScLookupCache & );
210 typedef ::boost::unordered_map< ScRange, ScLookupCache*, ScLookupCache::Hash, ::std::equal_to< ScRange > > ScLookupCacheMap;
212 #endif
214 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */