Branch libreoffice-24-8-3
[LibreOffice.git] / sc / inc / dpfilteredcache.hxx
blobe7737141dbc3d6e75998b3b77e751ba70a341d69
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 #pragma once
22 #include <sal/types.h>
23 #include "dpitemdata.hxx"
24 #include "calcmacros.hxx"
25 #include "types.hxx"
27 #include <unordered_set>
28 #include <vector>
30 #include <mdds/flat_segment_tree.hpp>
32 namespace com::sun::star::uno
34 class Any;
36 namespace com::sun::star::uno
38 template <typename> class Sequence;
41 class ScDPCache;
42 struct ScDPValue;
43 struct ScQueryParam;
45 /**
46 * This class is only a wrapper to the actual cache, to provide filtering on
47 * the raw data based on the query filter and/or page field filters.
49 class ScDPFilteredCache
51 typedef mdds::flat_segment_tree<SCROW, bool> RowFlagType;
53 public:
54 /** interface class used for filtering of rows. */
55 class FilterBase
57 public:
58 virtual ~FilterBase() {}
59 /** returns true if the matching condition is met for a single cell
60 value, or false otherwise. */
61 virtual bool match(const ScDPItemData& rCellData) const = 0;
63 virtual std::vector<ScDPItemData> getMatchValues() const = 0;
66 /** ordinary single-item filter. */
67 class SingleFilter final : public FilterBase
69 public:
70 explicit SingleFilter(const ScDPItemData& rItem);
72 virtual bool match(const ScDPItemData& rCellData) const override;
73 virtual std::vector<ScDPItemData> getMatchValues() const override;
75 private:
76 ScDPItemData maItem;
79 /** multi-item (group) filter. */
80 class GroupFilter final : public FilterBase
82 public:
83 GroupFilter();
84 virtual bool match(const ScDPItemData& rCellData) const override;
85 virtual std::vector<ScDPItemData> getMatchValues() const override;
86 void addMatchItem(const ScDPItemData& rItem);
87 size_t getMatchItemCount() const;
89 private:
90 ::std::vector<ScDPItemData> maItems;
93 /** single filtering criterion. */
94 struct Criterion
96 sal_Int32 mnFieldIndex;
97 std::shared_ptr<FilterBase> mpFilter;
99 Criterion();
102 ScDPFilteredCache(const ScDPCache& rCache);
103 ~ScDPFilteredCache();
105 sal_Int32 getRowSize() const;
106 sal_Int32 getColSize() const;
108 const ScDPCache& getCache() const { return mrCache; }
110 void fillTable(const ScQueryParam& rQuery, bool bIgnoreEmptyRows, bool bRepeatIfEmpty);
112 void fillTable();
114 /** Check whether a specified row is active or not. When a row is active,
115 it is used in calculation of the results data. A row becomes inactive
116 when it is filtered out by page field. */
117 bool isRowActive(sal_Int32 nRow, sal_Int32* pLastRow = nullptr) const;
119 /** Set filter on/off flag to each row to control visibility. The caller
120 must ensure that the table is filled before calling this function. */
121 void filterByPageDimension(const std::vector<Criterion>& rCriteria,
122 const std::unordered_set<sal_Int32>& rRepeatIfEmptyDims);
124 /** Get the cell instance at specified location within the data grid. Note
125 that the data grid doesn't include the header row. Don't delete the
126 returned object! */
127 const ScDPItemData* getCell(SCCOL nCol, SCROW nRow, bool bRepeatIfEmpty) const;
128 void getValue(ScDPValue& rVal, SCCOL nCol, SCROW nRow) const;
129 OUString getFieldName(SCCOL nIndex) const;
131 /** Get the unique entries for a field specified by index. The caller must
132 make sure that the table is filled before calling function, or it will
133 get an empty collection. */
134 const ::std::vector<SCROW>& getFieldEntries(sal_Int32 nColumn) const;
136 /** Filter the table based on the specified criteria, and copy the
137 result to rTabData. This method is used, for example, to generate
138 a drill-down data table. */
139 void filterTable(const std::vector<Criterion>& rCriteria,
140 css::uno::Sequence<css::uno::Sequence<css::uno::Any>>& rTabData,
141 const std::unordered_set<sal_Int32>& rRepeatIfEmptyDims);
143 void clear();
144 bool empty() const;
146 #if DUMP_PIVOT_TABLE
147 static void dumpRowFlag(const RowFlagType& rFlag);
148 void dump() const;
149 #endif
151 private:
152 ScDPFilteredCache(const ScDPFilteredCache&) = delete;
155 * Check if a given row meets all specified criteria.
157 * @param nRow index of row to be tested.
158 * @param rCriteria a list of criteria
160 bool isRowQualified(sal_Int32 nRow, const ::std::vector<Criterion>& rCriteria,
161 const std::unordered_set<sal_Int32>& rRepeatIfEmptyDims) const;
163 private:
164 /** unique field entries for each field (column). */
165 ::std::vector<::std::vector<SCROW>> maFieldEntries;
167 /** Rows visible by standard filter query. */
168 RowFlagType maShowByFilter;
169 /** Rows visible by page dimension filtering. */
170 RowFlagType maShowByPage;
172 const ScDPCache& mrCache;
175 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */