LibreOffice Module sc (master) 1
rangecache.hxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#pragma once
21
22#include "address.hxx"
23#include "queryentry.hxx"
24#include <o3tl/hash_combine.hxx>
25#include <svl/listener.hxx>
26
27#include <memory>
28#include <unordered_map>
29
30class ScDocument;
32struct ScQueryParam;
34
44class ScSortedRangeCache final : public SvtListener
45{
46public:
48 ScSortedRangeCache(ScDocument* pDoc, const ScRange& rRange, const ScQueryParam& param,
49 ScInterpreterContext* context, bool invalid = false);
50
52 bool isValid() const { return mValid; }
53
55 virtual void Notify(const SfxHint& rHint) override;
56
57 const ScRange& getRange() const { return maRange; }
58
59 enum class ValueType
60 {
61 Values,
62 StringsCaseSensitive,
63 StringsCaseInsensitive
64 };
65 struct HashKey
66 {
71 bool operator==(const HashKey& other) const
72 {
73 return range == other.range && valueType == other.valueType && queryOp == other.queryOp
74 && queryType == other.queryType;
75 }
76 };
78 static HashKey makeHashKey(const ScRange& range, const ScQueryParam& param);
79
80 struct Hash
81 {
82 size_t operator()(const HashKey& key) const
83 {
84 // Range should be just one column.
85 size_t hash = key.range.hashStartColumn();
87 o3tl::hash_combine(hash, key.queryOp);
89 return hash;
90 }
91 };
92
93 const std::vector<SCROW>& sortedRows() const { return mSortedRows; }
94 size_t size() const { return mSortedRows.size(); }
95 size_t indexForRow(SCROW row) const
96 {
97 assert(row >= maRange.aStart.Row() && row <= maRange.aEnd.Row());
98 assert(mRowToIndex[row - maRange.aStart.Row()] != mSortedRows.max_size());
99 return mRowToIndex[row - maRange.aStart.Row()];
100 }
101 SCROW rowForIndex(size_t index) const { return mSortedRows[index]; }
102
103private:
104 // Rows sorted by their value.
105 std::vector<SCROW> mSortedRows;
106 std::vector<size_t> mRowToIndex; // indexed by 'SCROW - maRange.aStart.Row()'
109 bool mValid;
113
116};
117
118// Struct because including lookupcache.hxx in document.hxx isn't wanted.
120{
121 std::unordered_map<ScSortedRangeCache::HashKey, std::unique_ptr<ScSortedRangeCache>,
124};
125
126/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SCROW Row() const
Definition: address.hxx:274
ScAddress aEnd
Definition: address.hxx:498
size_t hashStartColumn() const
Hash start column and start and end rows.
Definition: address.hxx:767
ScAddress aStart
Definition: address.hxx:497
Sorted cache for one range used with interpreter functions such as VLOOKUP and MATCH.
Definition: rangecache.hxx:45
ScSortedRangeCache & operator=(const ScSortedRangeCache &)=delete
SCROW rowForIndex(size_t index) const
Definition: rangecache.hxx:101
size_t size() const
Definition: rangecache.hxx:94
std::vector< SCROW > mSortedRows
Definition: rangecache.hxx:105
std::vector< size_t > mRowToIndex
Definition: rangecache.hxx:106
virtual void Notify(const SfxHint &rHint) override
Remove from document structure and delete (!) cache on modify hint.
Definition: rangecache.cxx:176
ScQueryEntry::QueryType mQueryType
Definition: rangecache.hxx:112
bool isValid() const
Returns if the cache is usable.
Definition: rangecache.hxx:52
ScSortedRangeCache(ScDocument *pDoc, const ScRange &rRange, const ScQueryParam &param, ScInterpreterContext *context, bool invalid=false)
MUST be new'd because Notify() deletes.
Definition: rangecache.cxx:52
HashKey getHashKey() const
Definition: rangecache.hxx:77
static HashKey makeHashKey(const ScRange &range, const ScQueryParam &param)
Definition: rangecache.cxx:188
ValueType mValueType
Definition: rangecache.hxx:110
const std::vector< SCROW > & sortedRows() const
Definition: rangecache.hxx:93
ScDocument * mpDoc
Definition: rangecache.hxx:108
const ScRange & getRange() const
Definition: rangecache.hxx:57
size_t indexForRow(SCROW row) const
Definition: rangecache.hxx:95
ScSortedRangeCache(const ScSortedRangeCache &)=delete
ScQueryOp
Definition: global.hxx:834
index
std::enable_if_t<(sizeof(N)==4)> hash_combine(N &nSeed, T const *pValue, size_t nCount)
std::unordered_map< ScSortedRangeCache::HashKey, std::unique_ptr< ScSortedRangeCache >, ScSortedRangeCache::Hash > aCacheMap
Definition: rangecache.hxx:123
ScQueryEntry::QueryType queryType
Definition: rangecache.hxx:70
bool operator==(const HashKey &other) const
Definition: rangecache.hxx:71
size_t operator()(const HashKey &key) const
Definition: rangecache.hxx:82
sal_Int32 SCROW
Definition: types.hxx:17