1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include "orcus/spreadsheet/pivot.hpp"
#include "orcus/spreadsheet/document.hpp"
#include "orcus/string_pool.hpp"
#include <ixion/address.hpp>
#include <unordered_map>
#include <cassert>
#include <sstream>
namespace orcus { namespace spreadsheet {
pivot_cache_record_value_t::pivot_cache_record_value_t() :
type(record_type::unknown), value(false) {}
pivot_cache_record_value_t::pivot_cache_record_value_t(std::string_view s) :
type(record_type::character), value(s)
{
}
pivot_cache_record_value_t::pivot_cache_record_value_t(double v) :
type(record_type::numeric), value(v)
{
}
pivot_cache_record_value_t::pivot_cache_record_value_t(size_t index) :
type(record_type::shared_item_index), value(index)
{
}
bool pivot_cache_record_value_t::operator== (const pivot_cache_record_value_t& other) const
{
return type == other.type && value == other.value;
}
bool pivot_cache_record_value_t::operator!= (const pivot_cache_record_value_t& other) const
{
return !operator==(other);
}
pivot_cache_item_t::pivot_cache_item_t() : type(item_type::unknown) {}
pivot_cache_item_t::pivot_cache_item_t(std::string_view s) :
type(item_type::character), value(s)
{
}
pivot_cache_item_t::pivot_cache_item_t(double numeric) :
type(item_type::numeric), value(numeric)
{
}
pivot_cache_item_t::pivot_cache_item_t(bool boolean) :
type(item_type::boolean), value(boolean)
{
}
pivot_cache_item_t::pivot_cache_item_t(const date_time_t& date_time) :
type(item_type::date_time), value(date_time)
{
}
pivot_cache_item_t::pivot_cache_item_t(error_value_t error) :
type(item_type::error), value(error)
{
}
pivot_cache_item_t::pivot_cache_item_t(const pivot_cache_item_t& other) :
type(other.type), value(other.value)
{
}
pivot_cache_item_t::pivot_cache_item_t(pivot_cache_item_t&& other) :
type(other.type), value(std::move(other.value))
{
other.type = item_type::unknown;
other.value = false;
}
bool pivot_cache_item_t::operator< (const pivot_cache_item_t& other) const
{
if (type != other.type)
return type < other.type;
return value < other.value;
}
bool pivot_cache_item_t::operator== (const pivot_cache_item_t& other) const
{
return type == other.type && value == other.value;
}
pivot_cache_item_t& pivot_cache_item_t::operator= (pivot_cache_item_t other)
{
swap(other);
return *this;
}
void pivot_cache_item_t::swap(pivot_cache_item_t& other)
{
std::swap(type, other.type);
std::swap(value, other.value);
}
pivot_cache_group_data_t::pivot_cache_group_data_t(size_t _base_field) :
base_field(_base_field) {}
pivot_cache_group_data_t::pivot_cache_group_data_t(const pivot_cache_group_data_t& other) :
base_to_group_indices(other.base_to_group_indices),
range_grouping(other.range_grouping),
items(other.items),
base_field(other.base_field) {}
pivot_cache_group_data_t::pivot_cache_group_data_t(pivot_cache_group_data_t&& other) :
base_to_group_indices(std::move(other.base_to_group_indices)),
range_grouping(std::move(other.range_grouping)),
items(std::move(other.items)),
base_field(other.base_field) {}
pivot_cache_field_t::pivot_cache_field_t() {}
pivot_cache_field_t::pivot_cache_field_t(std::string_view _name) : name(_name) {}
pivot_cache_field_t::pivot_cache_field_t(const pivot_cache_field_t& other) :
name(other.name),
items(other.items),
min_value(other.min_value),
max_value(other.max_value),
min_date(other.min_date),
max_date(other.max_date),
group_data(std::make_unique<pivot_cache_group_data_t>(*other.group_data)) {}
pivot_cache_field_t::pivot_cache_field_t(pivot_cache_field_t&& other) :
name(other.name),
items(std::move(other.items)),
min_value(std::move(other.min_value)),
max_value(std::move(other.max_value)),
min_date(std::move(other.min_date)),
max_date(std::move(other.max_date)),
group_data(std::move(other.group_data))
{
other.name = std::string_view{};
}
struct pivot_cache::impl
{
pivot_cache_id_t m_cache_id;
string_pool& m_string_pool;
std::string_view m_src_sheet_name;
pivot_cache::fields_type m_fields;
pivot_cache::records_type m_records;
impl(pivot_cache_id_t cache_id, string_pool& sp) :
m_cache_id(cache_id), m_string_pool(sp) {}
};
pivot_cache::pivot_cache(pivot_cache_id_t cache_id, string_pool& sp) :
mp_impl(std::make_unique<impl>(cache_id, sp)) {}
pivot_cache::~pivot_cache() {}
void pivot_cache::insert_fields(fields_type fields)
{
mp_impl->m_fields = std::move(fields);
}
void pivot_cache::insert_records(records_type records)
{
mp_impl->m_records = std::move(records);
}
size_t pivot_cache::get_field_count() const
{
return mp_impl->m_fields.size();
}
const pivot_cache_field_t* pivot_cache::get_field(size_t index) const
{
return index < mp_impl->m_fields.size() ? &mp_impl->m_fields[index] : nullptr;
}
pivot_cache_id_t pivot_cache::get_id() const
{
return mp_impl->m_cache_id;
}
const pivot_cache::records_type& pivot_cache::get_all_records() const
{
return mp_impl->m_records;
}
namespace {
constexpr const ixion::sheet_t ignored_sheet = -1;
struct worksheet_range
{
std::string_view sheet; /// it must be an interned string with the document.
ixion::abs_range_t range; /// sheet indices are ignored.
worksheet_range(std::string_view _sheet, ixion::abs_range_t _range) :
sheet(std::move(_sheet)), range(std::move(_range))
{
range.first.sheet = ignored_sheet;
range.last.sheet = ignored_sheet;
}
bool operator== (const worksheet_range& other) const
{
return sheet == other.sheet && range == other.range;
}
struct hash
{
std::hash<std::string_view> ps_hasher;
ixion::abs_range_t::hash range_hasher;
size_t operator() (const worksheet_range& v) const
{
assert(v.range.first.sheet == ignored_sheet);
assert(v.range.last.sheet == ignored_sheet);
size_t n = ps_hasher(v.sheet);
n ^= range_hasher(v.range);
return n;
}
};
};
using range_map_type = std::unordered_map<worksheet_range, std::unordered_set<pivot_cache_id_t>, worksheet_range::hash>;
using name_map_type = std::unordered_map<std::string_view, std::unordered_set<pivot_cache_id_t>>;
using caches_type = std::unordered_map<pivot_cache_id_t, std::unique_ptr<pivot_cache>>;
}
struct pivot_collection::impl
{
document& m_doc;
range_map_type m_worksheet_range_map; /// mapping of sheet name & range pair to cache ID.
name_map_type m_table_map; /// mapping of table name to cache ID.
caches_type m_caches;
impl(document& doc) : m_doc(doc) {}
void ensure_unique_cache(pivot_cache_id_t cache_id)
{
if (m_caches.count(cache_id) > 0)
{
std::ostringstream os;
os << "Pivot cache with the ID of " << cache_id << " already exists.";
throw std::invalid_argument(os.str());
}
}
};
pivot_collection::pivot_collection(document& doc) : mp_impl(std::make_unique<impl>(doc)) {}
pivot_collection::~pivot_collection() {}
void pivot_collection::insert_worksheet_cache(
std::string_view sheet_name, const ixion::abs_range_t& range,
std::unique_ptr<pivot_cache>&& cache)
{
// First, ensure that no caches exist for the cache ID.
pivot_cache_id_t cache_id = cache->get_id();
mp_impl->ensure_unique_cache(cache_id);
// Check and see if there is already a cache for this location. If yes,
// overwrite the existing cache.
mp_impl->m_caches[cache_id] = std::move(cache);
worksheet_range key(sheet_name, range);
range_map_type& range_map = mp_impl->m_worksheet_range_map;
auto it = range_map.find(key);
if (it == range_map.end())
{
// sheet name must be interned with the document it belongs to.
key.sheet = mp_impl->m_doc.get_string_pool().intern(key.sheet).first;
range_map.insert(range_map_type::value_type(std::move(key), {cache_id}));
return;
}
auto& id_set = it->second;
id_set.insert(cache_id);
}
void pivot_collection::insert_worksheet_cache(
std::string_view table_name, std::unique_ptr<pivot_cache>&& cache)
{
// First, ensure that no caches exist for the cache ID.
pivot_cache_id_t cache_id = cache->get_id();
mp_impl->ensure_unique_cache(cache_id);
mp_impl->m_caches[cache_id] = std::move(cache);
name_map_type& name_map = mp_impl->m_table_map;
auto it = name_map.find(table_name);
if (it == name_map.end())
{
// First cache to be associated with this name.
std::string_view table_name_interned =
mp_impl->m_doc.get_string_pool().intern(table_name).first;
name_map.insert(name_map_type::value_type(table_name_interned, {cache_id}));
return;
}
auto& id_set = it->second;
id_set.insert(cache_id);
}
size_t pivot_collection::get_cache_count() const
{
return mp_impl->m_caches.size();
}
const pivot_cache* pivot_collection::get_cache(
std::string_view sheet_name, const ixion::abs_range_t& range) const
{
worksheet_range wr(sheet_name, range);
auto it = mp_impl->m_worksheet_range_map.find(wr);
if (it == mp_impl->m_worksheet_range_map.end())
return nullptr;
// Pick the first cache ID.
assert(!it->second.empty());
pivot_cache_id_t cache_id = *it->second.cbegin();
return mp_impl->m_caches[cache_id].get();
}
namespace {
template<typename _CachesT, typename _CacheT>
_CacheT* get_cache_impl(_CachesT& caches, pivot_cache_id_t cache_id)
{
auto it = caches.find(cache_id);
return it == caches.end() ? nullptr : it->second.get();
}
}
pivot_cache* pivot_collection::get_cache(pivot_cache_id_t cache_id)
{
return get_cache_impl<caches_type, pivot_cache>(mp_impl->m_caches, cache_id);
}
const pivot_cache* pivot_collection::get_cache(pivot_cache_id_t cache_id) const
{
return get_cache_impl<const caches_type, const pivot_cache>(mp_impl->m_caches, cache_id);
}
}}
/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|