Orcus
pivot.hpp
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This Source Code Form is subject to the terms of the Mozilla Public
4  * License, v. 2.0. If a copy of the MPL was not distributed with this
5  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
6  */
7 
8 #ifndef INCLUDED_ORCUS_SPREADSHEET_PIVOT_HPP
9 #define INCLUDED_ORCUS_SPREADSHEET_PIVOT_HPP
10 
11 #include "../env.hpp"
12 #include "../types.hpp"
13 #include "types.hpp"
14 
15 #include <memory>
16 #include <vector>
17 #include <limits>
18 #include <variant>
19 #include <optional>
20 
21 namespace ixion {
22 
23 struct abs_range_t;
24 
25 }
26 
27 namespace orcus {
28 
29 class string_pool;
30 
31 namespace spreadsheet {
32 
33 class document;
34 
35 using pivot_cache_indices_t = std::vector<size_t>;
36 
37 struct ORCUS_SPM_DLLPUBLIC pivot_cache_record_value_t
38 {
39  using value_type = std::variant<bool, double, std::size_t, std::string_view, date_time_t>;
40 
41  enum class record_type
42  {
43  unknown = 0,
44  boolean,
45  date_time,
46  character,
47  numeric,
48  blank,
49  error,
50  shared_item_index
51  };
52 
53  record_type type;
54  value_type value;
55 
57  pivot_cache_record_value_t(std::string_view s);
59  pivot_cache_record_value_t(size_t index);
60 
61  bool operator== (const pivot_cache_record_value_t& other) const;
62  bool operator!= (const pivot_cache_record_value_t& other) const;
63 };
64 
65 using pivot_cache_record_t = std::vector<pivot_cache_record_value_t>;
66 
67 struct ORCUS_SPM_DLLPUBLIC pivot_cache_item_t
68 {
69  using value_type = std::variant<bool, double, std::string_view, date_time_t, error_value_t>;
70 
71  enum class item_type
72  {
73  unknown = 0, boolean, date_time, character, numeric, blank, error
74  };
75 
76  item_type type;
77  value_type value;
78 
80  pivot_cache_item_t(std::string_view s);
81  pivot_cache_item_t(double numeric);
82  pivot_cache_item_t(bool boolean);
83  pivot_cache_item_t(const date_time_t& date_time);
84  pivot_cache_item_t(error_value_t error);
85 
88 
89  bool operator< (const pivot_cache_item_t& other) const;
90  bool operator== (const pivot_cache_item_t& other) const;
91 
92  pivot_cache_item_t& operator= (pivot_cache_item_t other);
93 
94  void swap(pivot_cache_item_t& other);
95 };
96 
97 using pivot_cache_items_t = std::vector<pivot_cache_item_t>;
98 
102 struct ORCUS_SPM_DLLPUBLIC pivot_cache_group_data_t
103 {
104  struct ORCUS_SPM_DLLPUBLIC range_grouping_type
105  {
106  pivot_cache_group_by_t group_by = pivot_cache_group_by_t::range;
107 
108  bool auto_start = true;
109  bool auto_end = true;
110 
111  double start = 0.0;
112  double end = 0.0;
113  double interval = 1.0;
114 
115  date_time_t start_date;
116  date_time_t end_date;
117 
118  range_grouping_type() = default;
119  range_grouping_type(const range_grouping_type& other) = default;
120  };
121 
125  pivot_cache_indices_t base_to_group_indices;
126 
127  std::optional<range_grouping_type> range_grouping;
128 
132  pivot_cache_items_t items;
133 
135  size_t base_field;
136 
137  pivot_cache_group_data_t(size_t _base_field);
140 
141  pivot_cache_group_data_t() = delete;
142 };
143 
144 struct ORCUS_SPM_DLLPUBLIC pivot_cache_field_t
145 {
150  std::string_view name;
151 
152  pivot_cache_items_t items;
153 
154  std::optional<double> min_value;
155  std::optional<double> max_value;
156 
157  std::optional<date_time_t> min_date;
158  std::optional<date_time_t> max_date;
159 
160  std::unique_ptr<pivot_cache_group_data_t> group_data;
161 
163  pivot_cache_field_t(std::string_view _name);
166 };
167 
168 class ORCUS_SPM_DLLPUBLIC pivot_cache
169 {
170  struct impl;
171  std::unique_ptr<impl> mp_impl;
172 
173 public:
174  using fields_type = std::vector<pivot_cache_field_t>;
175  using records_type = std::vector<pivot_cache_record_t>;
176 
177  pivot_cache(pivot_cache_id_t cache_id, string_pool& sp);
178  ~pivot_cache();
179 
186  void insert_fields(fields_type fields);
187 
188  void insert_records(records_type record);
189 
190  size_t get_field_count() const;
191 
200  const pivot_cache_field_t* get_field(size_t index) const;
201 
202  pivot_cache_id_t get_id() const;
203 
204  const records_type& get_all_records() const;
205 };
206 
207 class ORCUS_SPM_DLLPUBLIC pivot_collection
208 {
209  struct impl;
210  std::unique_ptr<impl> mp_impl;
211 
212 public:
214  ~pivot_collection();
215 
225  std::string_view sheet_name, const ixion::abs_range_t& range, std::unique_ptr<pivot_cache>&& cache);
226 
233  void insert_worksheet_cache(std::string_view table_name, std::unique_ptr<pivot_cache>&& cache);
234 
240  size_t get_cache_count() const;
241 
242  const pivot_cache* get_cache(
243  std::string_view sheet_name, const ixion::abs_range_t& range) const;
244 
245  pivot_cache* get_cache(pivot_cache_id_t cache_id);
246 
247  const pivot_cache* get_cache(pivot_cache_id_t cache_id) const;
248 };
249 
250 }}
251 
252 #endif
253 
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Definition: document.hpp:54
Definition: pivot.hpp:169
void insert_fields(fields_type fields)
const pivot_cache_field_t * get_field(size_t index) const
Definition: pivot.hpp:208
void insert_worksheet_cache(std::string_view sheet_name, const ixion::abs_range_t &range, std::unique_ptr< pivot_cache > &&cache)
void insert_worksheet_cache(std::string_view table_name, std::unique_ptr< pivot_cache > &&cache)
Definition: string_pool.hpp:26
Definition: types.hpp:527
std::string_view name
Definition: pivot.hpp:150
pivot_cache_indices_t base_to_group_indices
Definition: pivot.hpp:125
pivot_cache_items_t items
Definition: pivot.hpp:132
size_t base_field
Definition: pivot.hpp:135