LOK: tilebench improvements
[LibreOffice.git] / sc / inc / dpfilteredcache.hxx
blob33205ac2dd9a2d3bbd0b527bf30354bbbcca787a
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_DPFILTEREDCACHE_HXX
21 #define INCLUDED_SC_INC_DPFILTEREDCACHE_HXX
23 #include <sal/types.h>
24 #include <osl/mutex.hxx>
25 #include "global.hxx"
26 #include "dpitemdata.hxx"
27 #include "calcmacros.hxx"
29 #include <memory>
30 #include <unordered_set>
31 #include <vector>
33 #include <mdds/flat_segment_tree.hpp>
35 class ScDPItemData;
36 class ScDPCache;
37 class ScDocument;
38 class ScRange;
39 struct ScDPValue;
40 struct ScQueryParam;
42 /**
43 * This class is only a wrapper to the actual cache, to provide filtering on
44 * the raw data based on the query filter and/or page field filters.
46 class SC_DLLPUBLIC ScDPFilteredCache
48 typedef mdds::flat_segment_tree<SCROW, bool> RowFlagType;
50 public:
51 /** interface class used for filtering of rows. */
52 class FilterBase
54 public:
55 virtual ~FilterBase() {}
56 /** returns true if the matching condition is met for a single cell
57 value, or false otherwise. */
58 virtual bool match( const ScDPItemData& rCellData ) const = 0;
60 virtual std::vector<ScDPItemData> getMatchValues() const = 0;
63 /** ordinary single-item filter. */
64 class SingleFilter : public FilterBase
66 public:
67 explicit SingleFilter(const ScDPItemData &rItem);
69 virtual bool match(const ScDPItemData& rCellData) const override;
70 virtual std::vector<ScDPItemData> getMatchValues() const override;
72 private:
73 ScDPItemData maItem;
76 /** multi-item (group) filter. */
77 class GroupFilter : public FilterBase
79 public:
80 GroupFilter();
81 virtual bool match(const ScDPItemData& rCellData) const override;
82 virtual std::vector<ScDPItemData> getMatchValues() const override;
83 void addMatchItem(const ScDPItemData& rItem);
84 size_t getMatchItemCount() const;
86 private:
87 ::std::vector<ScDPItemData> maItems;
90 /** single filtering criterion. */
91 struct Criterion
93 sal_Int32 mnFieldIndex;
94 std::shared_ptr<FilterBase> mpFilter;
96 Criterion();
99 ScDPFilteredCache(const ScDPCache& rCache);
100 ~ScDPFilteredCache();
102 sal_Int32 getRowSize() const;
103 sal_Int32 getColSize() const;
105 const ScDPCache& getCache() const { return mrCache;}
107 void fillTable(const ScQueryParam& rQuery, bool bIgnoreEmptyRows, bool bRepeatIfEmpty);
109 void fillTable();
111 /** Check whether a specified row is active or not. When a row is active,
112 it is used in calculation of the results data. A row becomes inactive
113 when it is filtered out by page field. */
114 bool isRowActive(sal_Int32 nRow, sal_Int32* pLastRow = nullptr) const;
116 /** Set filter on/off flag to each row to control visibility. The caller
117 must ensure that the table is filled before calling this function. */
118 void filterByPageDimension(const std::vector<Criterion>& rCriteria, const std::unordered_set<sal_Int32>& rRepeatIfEmptyDims);
120 /** Get the cell instance at specified location within the data grid. Note
121 that the data grid doesn't include the header row. Don't delete the
122 returned object! */
123 const ScDPItemData* getCell(SCCOL nCol, SCROW nRow, bool bRepeatIfEmpty) const;
124 void getValue( ScDPValue& rVal, SCCOL nCol, SCROW nRow) const;
125 OUString getFieldName(SCCOL nIndex) const;
127 /** Get the unique entries for a field specified by index. The caller must
128 make sure that the table is filled before calling function, or it will
129 get an empty collection. */
130 const ::std::vector<SCROW>& getFieldEntries( sal_Int32 nColumn ) const;
132 /** Filter the table based on the specified criteria, and copy the
133 result to rTabData. This method is used, for example, to generate
134 a drill-down data table. */
135 void filterTable(const std::vector<Criterion>& rCriteria,
136 css::uno::Sequence< css::uno::Sequence< css::uno::Any > >& rTabData,
137 const std::unordered_set<sal_Int32>& rRepeatIfEmptyDims);
139 void clear();
140 bool empty() const;
142 #if DUMP_PIVOT_TABLE
143 static void dumpRowFlag( const RowFlagType& rFlag );
144 void dump() const;
145 #endif
147 private:
148 ScDPFilteredCache(const ScDPFilteredCache&) = delete;
151 * Check if a given row meets all specified criteria.
153 * @param nRow index of row to be tested.
154 * @param rCriteria a list of criteria
156 bool isRowQualified(sal_Int32 nRow, const ::std::vector<Criterion>& rCriteria, const std::unordered_set<sal_Int32>& rRepeatIfEmptyDims) const;
158 private:
160 /** unique field entires for each field (column). */
161 ::std::vector< ::std::vector<SCROW> > maFieldEntries;
163 /** Rows visible by standard filter query. */
164 RowFlagType maShowByFilter;
165 /** Rows visible by page dimension filtering. */
166 RowFlagType maShowByPage;
168 const ScDPCache& mrCache;
170 #endif
172 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */