Repository navigation
Expand file tree
/
Copy pathdata.cpp
More file actions
503 lines (433 loc) · 14.6 KB
/
Copy pathdata.cpp
File metadata and controls
503 lines (433 loc) · 14.6 KB
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Mikael Zayenz Lagerkvist <lagerkvist@gecode.dev>
*
* Copyright:
* Mikael Zayenz Lagerkvist, 2026
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.dev
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
// Ported from gecode/instance/data.cpp at Gecode commit
// 599f977b7855dba2a1a5f67db1892722372067d3.
#include <iris/data.hpp>
#include <iris/handle.hpp>
#include <iris/params.hpp>
#include "numbers.hpp"
#include "value.hpp"
#include <cmath>
#include <string>
#include <utility>
namespace iris {
namespace detail {
/*
* Typed access
*/
std::size_t TypedAccess::leaves(const ValueRef &v) {
return v.value_->leaves();
}
void throw_access(const char *message) { throw AccessError(message); }
/*
* Values
*/
void Value::to_float() {
if (type != ValueType::Int)
return;
for (const std::int64_t v : ints)
floats.push_back(static_cast<double>(v));
ints.clear();
type = ValueType::Float;
}
void Value::push(const Value &s) {
if (leaves() == 0) {
type = s.type;
} else if ((type == ValueType::String) != (s.type == ValueType::String)) {
throw ShapeError{.code = ErrorCode::ValueTypeMix,
.msg = "strings and numbers are mixed"};
} else if ((type == ValueType::Int) && (s.type == ValueType::Float)) {
to_float();
}
switch (s.type) {
case ValueType::Int:
if (type == ValueType::Int)
ints.push_back(s.ints[0]);
else
floats.push_back(static_cast<double>(s.ints[0]));
break;
case ValueType::Float:
floats.push_back(s.floats[0]);
break;
case ValueType::String:
strings.push_back(s.strings[0]);
break;
}
}
void Value::clear_list(ValueType t) {
type = t;
ints.clear();
floats.clear();
strings.clear();
levels.resize(1);
levels[0].assign({0, 0});
}
void Value::shape(const Seq<std::size_t> &dims) {
levels.clear();
std::size_t lists = 1;
for (const std::size_t n : dims) {
Seq<std::size_t> &offsets = levels.emplace_back();
offsets.push_back(0);
for (std::size_t i = 0; i < lists; i++)
offsets.push_back(offsets.back() + n);
lists = offsets.back();
}
}
std::optional<Value::Position>
Value::locate(std::span<const std::size_t> path) const {
Position at{.level = 0, .pos = 0};
for (const std::size_t i : path) {
if ((at.level >= depth()) || (i >= list_size(at.level, at.pos)))
return std::nullopt;
at.pos = levels[at.level][at.pos] + i;
at.level++;
}
return at;
}
std::pair<std::size_t, std::size_t> Value::range(Position at) const {
std::size_t from = at.pos;
std::size_t to = at.pos + 1;
for (std::size_t k = at.level; k < depth(); k++) {
from = levels[k][from];
to = levels[k][to];
}
return {from, to};
}
Value Value::part(Position at) const {
Value r(type);
// The lists of the part at each level, then its leaves
std::size_t from = at.pos;
std::size_t to = at.pos + 1;
for (std::size_t k = at.level; k < depth(); k++) {
Seq<std::size_t> &offsets = r.levels.emplace_back();
for (std::size_t j = from; j <= to; j++)
offsets.push_back(levels[k][j] - levels[k][from]);
from = levels[k][from];
to = levels[k][to];
}
const auto leaves_of = [from, to](const auto &leaves) {
return std::remove_cvref_t<decltype(leaves)>(
leaves.begin() + static_cast<std::ptrdiff_t>(from),
leaves.begin() + static_cast<std::ptrdiff_t>(to));
};
switch (type) {
case ValueType::Int:
r.ints = leaves_of(ints);
break;
case ValueType::Float:
r.floats = leaves_of(floats);
break;
case ValueType::String:
r.strings = leaves_of(strings);
break;
}
return r;
}
Column::Column(ValueType t, std::size_t depth) : c_(t), d_(depth) {}
void Column::start_levels() { c_.levels.assign(d_ + 1, Seq<std::size_t>{0}); }
void Column::skip() {
rows_++;
missing_ = true;
if (!started_)
pending_++;
else if (d_ == 0)
throw ShapeError{.code = ErrorCode::RepeatMissing,
.msg = "is defined in only some repetitions"};
else
c_.levels[1].push_back(c_.levels[1].back());
}
void Column::add(Value &v) {
rows_++;
if (!started_) {
if (missing_ && (v.depth() == 0))
throw ShapeError{.code = ErrorCode::RepeatMissing,
.msg = "is defined in only some repetitions"};
started_ = true;
d_ = v.depth();
c_.type = v.type;
start_levels();
for (std::size_t i = 0; i < pending_; i++)
c_.levels[1].push_back(0);
} else if (v.depth() != d_) {
throw ShapeError{.code = ErrorCode::RepeatDepth,
.msg = "has rows of different depths"};
}
if (v.leaves() > 0) {
// The type follows the values; integers become floats if needed
if (c_.leaves() == 0)
c_.type = v.type;
else if ((c_.type == ValueType::String) != (v.type == ValueType::String))
throw ShapeError{.code = ErrorCode::ValueTypeMix,
.msg = "mixes strings and numbers"};
else if ((c_.type == ValueType::Int) && (v.type == ValueType::Float))
c_.to_float();
}
// The lists of the row follow those of the earlier rows one level down
for (std::size_t k = 0; k < d_; k++) {
Seq<std::size_t> &offsets = c_.levels[k + 1];
const std::size_t base = offsets.back();
for (std::size_t j = 1; j < v.levels[k].size(); j++)
offsets.push_back(base + v.levels[k][j]);
}
switch (c_.type) {
case ValueType::Int:
c_.ints.insert(c_.ints.end(), v.ints.begin(), v.ints.end());
break;
case ValueType::Float:
for (const std::int64_t x : v.ints)
c_.floats.push_back(static_cast<double>(x));
c_.floats.insert(c_.floats.end(), v.floats.begin(), v.floats.end());
break;
case ValueType::String:
for (String &x : v.strings)
c_.strings.push_back(std::move(x));
break;
}
}
Value Column::finish() {
if (!started_) {
// No rows added: every row is missing, so empty if it is a list
if ((d_ == 0) && (rows_ > 0))
throw ShapeError{.code = ErrorCode::RepeatMissing,
.msg = "is defined in no repetition"};
start_levels();
for (std::size_t i = 0; (d_ > 0) && (i < rows_); i++)
c_.levels[1].push_back(0);
}
c_.levels[0].assign({0, rows_});
return std::move(c_);
}
} // namespace detail
/*
* Data access
*/
std::string_view type_name(ValueType type) {
switch (type) {
case ValueType::Int:
return "int";
case ValueType::Float:
return "float";
case ValueType::String:
return "string";
}
std::unreachable();
}
ValueType ValueRef::type() const { return value_->type; }
std::size_t ValueRef::depth() const { return value_->depth() - level_; }
void ValueRef::require_array() const {
if (depth() == 0)
throw AccessError("the value is not an array");
}
void ValueRef::require_single(ValueType type) const {
if (depth() != 0)
throw AccessError("the value is an array, not a single value");
if (value_->type != type)
throw AccessError("the value is of type " +
std::string(type_name(value_->type)) + ", not " +
std::string(type_name(type)));
}
void ValueRef::require_row(ValueType type) const {
if (depth() != 1)
throw AccessError("the value is not a one-dimensional array");
if (value_->type != type)
throw AccessError("the array is of type " +
std::string(type_name(value_->type)) + ", not " +
std::string(type_name(type)));
}
std::size_t ValueRef::size() const {
require_array();
return value_->list_size(level_, pos_);
}
ValueRef ValueRef::operator[](std::size_t i) const {
const std::size_t n = size();
if (i >= n)
throw AccessError("index " + std::to_string(i) + " is out of range (size " +
std::to_string(n) + ")");
return {*value_, level_ + 1, value_->levels[level_][pos_] + i};
}
ValueRef ValueRef::at(std::span<const std::size_t> path) const {
ValueRef r = *this;
for (const std::size_t i : path)
r = r[i];
return r;
}
std::int64_t ValueRef::integer() const {
require_single(ValueType::Int);
return value_->ints[pos_];
}
double ValueRef::number() const {
if (value_->type == ValueType::Int)
return static_cast<double>(integer());
require_single(ValueType::Float);
return value_->floats[pos_];
}
std::string_view ValueRef::text() const {
require_single(ValueType::String);
return value_->strings[pos_];
}
std::span<const std::int64_t> ValueRef::ints() const {
require_row(ValueType::Int);
const auto [from, to] = value_->range({.level = level_, .pos = pos_});
return std::span<const std::int64_t>(value_->ints).subspan(from, to - from);
}
std::span<const double> ValueRef::floats() const {
require_row(ValueType::Float);
const auto [from, to] = value_->range({.level = level_, .pos = pos_});
return std::span<const double>(value_->floats).subspan(from, to - from);
}
namespace {
/// The entries of Data without entries
const detail::DataImpl no_entries;
} // namespace
// Shares no ownership, as no_entries is never destroyed early
Data::Data() : impl_(std::shared_ptr<const detail::DataImpl>(), &no_entries) {}
Data::Data(std::shared_ptr<const detail::DataImpl> impl)
: impl_(std::move(impl)) {}
std::size_t Data::size() const { return impl_->entries.size(); }
std::string_view Data::name(std::size_t i) const {
if (i >= size())
throw AccessError("entry index " + std::to_string(i) + " is out of range");
return impl_->entries[i].name;
}
ValueRef Data::value(std::size_t i) const {
if (i >= size())
throw AccessError("entry index " + std::to_string(i) + " is out of range");
return {impl_->entries[i].value, 0, 0};
}
std::optional<ValueRef> Data::find(std::string_view name) const {
const detail::Value *v = impl_->find(name);
if (v == nullptr)
return std::nullopt;
return ValueRef(*v, 0, 0);
}
ValueRef Data::at(std::string_view name,
std::span<const std::size_t> path) const {
const std::optional<ValueRef> v = find(name);
if (!v)
throw AccessError("no value named '" + std::string(name) + "'");
return v->at(path);
}
std::size_t Data::size(std::string_view name,
std::span<const std::size_t> path) const {
return at(name, path).size();
}
std::int64_t Data::integer(std::string_view name,
std::span<const std::size_t> path) const {
return at(name, path).integer();
}
double Data::number(std::string_view name,
std::span<const std::size_t> path) const {
return at(name, path).number();
}
std::string_view Data::text(std::string_view name,
std::span<const std::size_t> path) const {
return at(name, path).text();
}
std::span<const std::int64_t>
Data::ints(std::string_view name, std::span<const std::size_t> path) const {
return at(name, path).ints();
}
std::span<const double> Data::floats(std::string_view name,
std::span<const std::size_t> path) const {
return at(name, path).floats();
}
/*
* Parameters
*/
Params::Params() : Params(*std::pmr::get_default_resource()) {}
Params::Params(std::pmr::memory_resource &memory) : memory_(&memory) {}
Params::Params(
std::initializer_list<std::pair<std::string_view, std::string_view>> pairs)
: Params(pairs, *std::pmr::get_default_resource()) {}
Params::Params(
std::initializer_list<std::pair<std::string_view, std::string_view>> pairs,
std::pmr::memory_resource &memory)
: Params(memory) {
for (const auto &[name, value] : pairs)
set(name, value);
}
void Params::set(std::string_view name, std::string_view value) {
const detail::MemoryScope scope(*memory_);
// Copy both arguments before changing anything: they may point into
// this Params, and an allocation failure must leave it unchanged.
detail::String owned_name(name);
detail::String owned_value(value);
detail::ParamsImpl &values = own();
for (std::size_t i = 0; i < values.names.size(); i++)
if (values.names[i] == owned_name) {
values.values[i].swap(owned_value);
return;
}
values.names.reserve(values.names.size() + 1);
values.values.reserve(values.values.size() + 1);
// Neither push_back allocates after the reservations, and moving a
// string with an equal allocator does not throw.
values.names.push_back(std::move(owned_name));
values.values.push_back(std::move(owned_value));
}
void Params::set_float(std::string_view name, double value) {
if (std::isfinite(value)) {
detail::FloatBuffer buffer{};
set(name, detail::write_float(value, buffer));
return;
}
// The texts that section 9.2's float conversion rejects, as in Rust
set(name, std::isnan(value) ? "NaN" : (value > 0 ? "inf" : "-inf"));
}
namespace {
/// The values of Params without values. It is built before every scope, as
/// the first read would otherwise bind it to a resource that it outlives:
/// an empty container of a debug MSVC build holds an allocation.
const detail::ParamsImpl no_params;
} // namespace
detail::ParamsImpl &Params::own() {
if (!impl_)
impl_ = detail::make_shared<detail::ParamsImpl>();
else if (impl_.use_count() > 1)
impl_ = detail::make_shared<detail::ParamsImpl>(*impl_);
return *impl_;
}
const detail::ParamsImpl &Params::values() const {
return impl_ ? *impl_ : no_params;
}
std::size_t Params::size() const { return values().names.size(); }
std::string_view Params::name(std::size_t i) const {
if (i >= size())
throw AccessError("parameter index " + std::to_string(i) +
" is out of range");
return values().names[i];
}
std::optional<std::string_view> Params::get(std::string_view name) const {
return values().get(name);
}
} // namespace iris