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 "orcus/env.hpp"
12 #include "orcus/pstring.hpp"
13 #include "orcus/types.hpp"
14 #include "orcus/spreadsheet/types.hpp"
15 
16 #include <memory>
17 #include <vector>
18 #include <limits>
19 
20 #include <boost/optional.hpp>
21 
22 namespace ixion {
23 
24 struct abs_range_t;
25 
26 }
27 
28 namespace orcus {
29 
30 class string_pool;
31 
32 namespace spreadsheet {
33 
34 class document;
35 
36 using pivot_cache_indices_t = std::vector<size_t>;
37 
38 struct ORCUS_SPM_DLLPUBLIC pivot_cache_record_value_t
39 {
40  enum class value_type
41  {
42  unknown = 0,
43  boolean,
44  date_time,
45  character,
46  numeric,
47  blank,
48  error,
49  shared_item_index
50  };
51 
52  value_type type;
53 
54  union
55  {
56  bool boolean;
57 
58  struct
59  {
60  // This must point to an interned string instance. May not be
61  // null-terminated.
62  const char* p;
63 
64  size_t n; // Length of the string value.
65 
66  } character;
67 
68  struct
69  {
70  int year;
71  int month;
72  int day;
73  int hour;
74  int minute;
75  double second;
76 
77  } date_time;
78 
79  double numeric;
80 
81  size_t shared_item_index;
82 
83  // TODO : add error value.
84 
85  } value;
86 
88  pivot_cache_record_value_t(const char* cp, size_t cn);
90  pivot_cache_record_value_t(size_t index);
91 
92  bool operator== (const pivot_cache_record_value_t& other) const;
93  bool operator!= (const pivot_cache_record_value_t& other) const;
94 };
95 
96 using pivot_cache_record_t = std::vector<pivot_cache_record_value_t>;
97 
98 struct ORCUS_SPM_DLLPUBLIC pivot_cache_item_t
99 {
100  enum class item_type
101  {
102  unknown = 0, boolean, date_time, character, numeric, blank, error
103  };
104 
105  item_type type;
106 
107  union
108  {
109  struct
110  {
111  // This must point to an interned string instance. May not be
112  // null-terminated.
113  const char* p;
114 
115  size_t n; // Length of the string value.
116 
117  } character;
118 
119  struct
120  {
121  int year;
122  int month;
123  int day;
124  int hour;
125  int minute;
126  double second;
127 
128  } date_time;
129 
130  double numeric;
131 
132  error_value_t error;
133 
134  bool boolean;
135 
136  } value;
137 
139  pivot_cache_item_t(const char* cp, size_t cn);
140  pivot_cache_item_t(double numeric);
141  pivot_cache_item_t(bool boolean);
142  pivot_cache_item_t(const date_time_t& date_time);
143  pivot_cache_item_t(error_value_t error);
144 
147 
148  bool operator< (const pivot_cache_item_t& other) const;
149  bool operator== (const pivot_cache_item_t& other) const;
150 
151  pivot_cache_item_t& operator= (pivot_cache_item_t other);
152 
153  void swap(pivot_cache_item_t& other);
154 };
155 
156 using pivot_cache_items_t = std::vector<pivot_cache_item_t>;
157 
161 struct ORCUS_SPM_DLLPUBLIC pivot_cache_group_data_t
162 {
163  struct ORCUS_SPM_DLLPUBLIC range_grouping_type
164  {
165  pivot_cache_group_by_t group_by = pivot_cache_group_by_t::range;
166 
167  bool auto_start = true;
168  bool auto_end = true;
169 
170  double start = 0.0;
171  double end = 0.0;
172  double interval = 1.0;
173 
174  date_time_t start_date;
175  date_time_t end_date;
176 
177  range_grouping_type() = default;
178  range_grouping_type(const range_grouping_type& other) = default;
179  };
180 
184  pivot_cache_indices_t base_to_group_indices;
185 
186  boost::optional<range_grouping_type> range_grouping;
187 
191  pivot_cache_items_t items;
192 
194  size_t base_field;
195 
196  pivot_cache_group_data_t(size_t _base_field);
199 
200  pivot_cache_group_data_t() = delete;
201 };
202 
203 struct ORCUS_SPM_DLLPUBLIC pivot_cache_field_t
204 {
210 
211  pivot_cache_items_t items;
212 
213  boost::optional<double> min_value;
214  boost::optional<double> max_value;
215 
216  boost::optional<date_time_t> min_date;
217  boost::optional<date_time_t> max_date;
218 
219  std::unique_ptr<pivot_cache_group_data_t> group_data;
220 
222  pivot_cache_field_t(const pstring& _name);
225 };
226 
227 class ORCUS_SPM_DLLPUBLIC pivot_cache
228 {
229  struct impl;
230  std::unique_ptr<impl> mp_impl;
231 
232 public:
233  using fields_type = std::vector<pivot_cache_field_t>;
234  using records_type = std::vector<pivot_cache_record_t>;
235 
236  pivot_cache(pivot_cache_id_t cache_id, string_pool& sp);
237  ~pivot_cache();
238 
245  void insert_fields(fields_type fields);
246 
247  void insert_records(records_type record);
248 
249  size_t get_field_count() const;
250 
259  const pivot_cache_field_t* get_field(size_t index) const;
260 
261  pivot_cache_id_t get_id() const;
262 
263  const records_type& get_all_records() const;
264 };
265 
266 class ORCUS_SPM_DLLPUBLIC pivot_collection
267 {
268  struct impl;
269  std::unique_ptr<impl> mp_impl;
270 
271 public:
273  ~pivot_collection();
274 
282  void insert_worksheet_cache(
283  const pstring& sheet_name, const ixion::abs_range_t& range, std::unique_ptr<pivot_cache>&& cache);
284 
290  size_t get_cache_count() const;
291 
292  const pivot_cache* get_cache(
293  const pstring& sheet_name, const ixion::abs_range_t& range) const;
294 
295  pivot_cache* get_cache(pivot_cache_id_t cache_id);
296 
297  const pivot_cache* get_cache(pivot_cache_id_t cache_id) const;
298 };
299 
300 }}
301 
302 #endif
303 
304 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
Definition: pstring.hpp:24
Definition: types.hpp:403
Definition: pivot.hpp:266
Definition: document.hpp:47
size_t base_field
Definition: pivot.hpp:194
pivot_cache_indices_t base_to_group_indices
Definition: pivot.hpp:184
Definition: document.hpp:18
pivot_cache_items_t items
Definition: pivot.hpp:191
Definition: string_pool.hpp:22
Definition: pivot.hpp:227
Definition: base64.hpp:15
pstring name
Definition: pivot.hpp:209