43#include <unordered_set>
65 if (impl.use_count() > 1)
66 impl = std::make_shared<ParamsImpl>(*impl);
67 for (
int i=0; i<impl->names.
size(); i++)
68 if (impl->names[i] ==
name) {
69 impl->values[i] = value;
72 impl->names.push(
name);
73 impl->values.push(value);
79 while (b <= assignments.
size()) {
80 size_t e = assignments.find(
',', b);
81 if (e == std::string::npos)
82 e = assignments.size();
83 std::string a = assignments.substr(b, e-b);
87 size_t eq = a.find(
'=');
88 if ((eq == std::string::npos) || (eq == 0))
89 throw Error(
"parameter '" + a +
"' must have the form name=value");
90 set(a.substr(0,eq), a.substr(eq+1));
96 for (
int i=0; i<impl->names.
size(); i++)
97 if (impl->names[i] ==
name) {
98 value = impl->values[i];
106 return impl->names.
size();
111 if ((i < 0) || (i >= impl->names.
size()))
112 throw Error(
"parameter index out of range");
113 return impl->names[i];
122 : rows(0), pending(0), started(false), missing(false), d(depth) {
133 throw ShapeError(
"is defined in only some repetitions");
135 c.lengths[1].push(0);
142 if (missing && (v.depth == 0))
143 throw ShapeError(
"is defined in only some repetitions");
149 for (
int k=0; k<=d; k++)
151 for (
int i=0; i<pending; i++)
152 c.lengths[1].push(0);
153 }
else if (v.depth != d) {
154 throw ShapeError(
"has rows of different depths");
156 if (v.leaves() > 0) {
161 throw ShapeError(
"mixes strings and numbers");
165 for (
int k=0; k<d; k++)
166 for (
int l : v.lengths[k])
167 c.lengths[k+1].push(l);
170 for (
long long int x : v.ints) c.ints.push(x);
173 for (
long long int x : v.ints) c.reals.push(
static_cast<double>(x));
174 for (
double x : v.reals) c.reals.push(x);
177 for (std::string& x : v.strs) c.strs.push(std::move(x));
186 if ((d == 0) && (rows > 0))
187 throw ShapeError(
"is defined in no repetition");
190 for (
int k=0; k<=d; k++)
192 for (
int i=0; (d > 0) && (i<rows); i++)
193 c.lengths[1].push(0);
196 c.lengths[0].push(rows);
207 for (
int k=0; k<
depth; k++) {
223 from = pos; to = pos+1;
224 for (
int k=level; k<
depth; k++) {
238 where(
const std::string& n,
const IntArgs&
path,
int np) {
239 std::string s =
"'" + n;
240 for (
int i=0; i<np; i++)
241 s +=
"[" + std::to_string(
path[i]) +
"]";
247 lookup(
const std::shared_ptr<const DataImpl>& impl,
const std::string& n) {
248 const Value* v = impl->find(n);
250 throw Error(
"no value named '" + n +
"'");
262 locate(
const Value& v,
const std::string& n,
const IntArgs&
path,
bool leaf,
int& level,
int& pos) {
263 int np =
path.size();
264 if (leaf ? (np != v.depth) : (np >= v.depth))
265 throw Error(where(n,
path,np) + (leaf ?
" is not a single value" :
" is not an array"));
267 for (
int i=0; i<np; i++) {
268 int len = v.lengths[level][pos];
269 if ((
path[i] < 0) || (
path[i] >= len))
270 throw Error(
"index out of range in " + where(n,
path,i+1) +
" (size " + std::to_string(len) +
")");
271 pos = v.starts[level][pos] +
path[i];
278 narrow(
long long int x,
const std::string& n) {
279 if ((x < INT_MIN) || (x > INT_MAX))
280 throw Error(
"value " + std::to_string(x) +
" in '" + n +
"' does not fit into an int");
281 return static_cast<int>(x);
285 integer_at(
const std::shared_ptr<const DataImpl>& impl,
const std::string& n,
const IntArgs&
path) {
286 const Value&
v = lookup(impl,n);
288 int i = locate(v,n,
path,
true,level,pos);
290 throw Error(where(n,
path,
path.size()) +
" is not an integer");
295 number_at(
const std::shared_ptr<const DataImpl>& impl,
const std::string& n,
const IntArgs&
path) {
296 const Value&
v = lookup(impl,n);
298 int i = locate(v,n,
path,
true,level,pos);
301 return v.as_double(i);
305 text_at(
const std::shared_ptr<const DataImpl>& impl,
const std::string& n,
const IntArgs&
path) {
306 const Value&
v = lookup(impl,n);
308 int i = locate(v,n,
path,
true,level,pos);
315 size_at(
const std::shared_ptr<const DataImpl>& impl,
const std::string& n,
const IntArgs&
path) {
316 const Value&
v = lookup(impl,n);
318 locate(v,n,
path,
false,level,pos);
319 return v.lengths[level][
pos];
323 ints_at(
const std::shared_ptr<const DataImpl>& impl,
const std::string& n,
const IntArgs&
path) {
324 const Value&
v = lookup(impl,n);
326 locate(v,n,
path,
false,level,pos);
328 throw Error(where(n,
path,
path.size()) +
" does not contain integers");
330 v.range(level,pos,from,to);
332 for (
int i=from;
i<to;
i++)
333 a[i-from] = narrow(
v.ints[i],n);
338 numbers_at(
const std::shared_ptr<const DataImpl>& impl,
const std::string& n,
const IntArgs&
path) {
339 const Value&
v = lookup(impl,n);
341 locate(v,n,
path,
false,level,pos);
343 throw Error(where(n,
path,
path.size()) +
" does not contain numbers");
345 v.range(level,pos,from,to);
346 ArgArray<double>
a(to-from);
347 for (
int i=from;
i<to;
i++)
348 a[i-from] =
v.as_double(i);
359 return impl->entries.size();
364 if ((i < 0) || (i >= impl->entries.
size()))
365 throw Error(
"entry index out of range");
366 return impl->entries[i].name;
371 return impl->find(n) !=
nullptr;
376 return lookup(impl,n).type;
381 return lookup(impl,n).depth;
386 return size_at(impl,n,
path);
391 return integer_at(impl,n,
path);
396 return number_at(impl,n,
path);
401 return text_at(impl,n,
path);
406 return ints_at(impl,n,
path);
411 return numbers_at(impl,n,
path);
427 print_real(std::ostream& os,
double f) {
429 int digits = (std::fabs(f) >= 1.0) && (std::fabs(f) < 1e17)
430 ?
static_cast<int>(std::floor(std::log10(std::fabs(f)))) + 1 : 1;
431 for (
int prec=std::max(digits,1); prec<=17; prec++) {
433 std::ostringstream ss;
434 ss.imbue(std::locale::classic());
435 ss << std::setprecision(prec) << f;
436 std::istringstream back(ss.str());
437 back.imbue(std::locale::classic());
440 if ((g == f) || (prec == 17)) {
441 std::string t = ss.str();
442 if (t.find_first_of(
".e") == std::string::npos)
452 print_quoted(std::ostream& os,
const std::string& s) {
455 if ((c ==
'"') || (c ==
'\\')) {
457 }
else if (c ==
'\n') {
459 }
else if (c ==
'\t') {
461 }
else if (
static_cast<unsigned char>(c) < 0x20) {
463 static const char hex[] =
"0123456789abcdef";
464 os <<
"\\x" << hex[(
c >> 4) & 0xf] << hex[c & 0xf];
474 print_leaf(std::ostream& os,
const Value& v,
int i) {
478 default: print_quoted(os,
v.strs[i]);
break;
484 print_name(std::ostream& os,
const std::string& n) {
485 static const std::unordered_set<std::string> keywords = {
486 "ann",
"annotation",
"any",
"array",
"bool",
"case",
"constraint",
"default",
"diff",
"div",
"else",
"elseif",
487 "endif",
"enum",
"false",
"float",
"function",
"if",
"in",
"include",
"int",
"intersect",
"let",
"list",
488 "maximize",
"minimize",
"mod",
"not",
"of",
"op",
"opt",
"output",
"par",
"predicate",
"record",
"satisfy",
489 "set",
"solve",
"string",
"subset",
"superset",
"symdiff",
"test",
"then",
"true",
"tuple",
"type",
"union",
490 "var",
"where",
"xor"
493 bool plain = !n.empty() &&
is_letter(n[0]) && (keywords.count(n) == 0);
497 os <<
'\'' << n <<
'\'';
507 print_list(std::ostream& os,
const Value& v,
Seq<int>& next,
int level) {
508 int pos = next[level]++;
509 int len =
v.lengths[level][
pos];
511 for (
int i=0;
i<len;
i++) {
512 if (i > 0) os <<
", ";
513 if (level+1 ==
v.depth) {
514 print_leaf(os, v, next[
v.depth]++);
517 print_list(os, v, next, level+1);
526 rectangular(
const Value& v) {
527 for (
int k=1; k<
v.depth; k++)
528 for (
int l :
v.lengths[k])
529 if (l !=
v.lengths[k][0])
538 for (
const Entry& en : impl->entries) {
540 const std::string& n = en.
name;
544 print_leaf(os, v, 0);
550 if (rectangular(v) && (v.depth <= 6)) {
553 if ((v.depth == 2) && (v.size() > 0) && (v.lengths[1][0] > 0)) {
554 int cols = v.lengths[1][0];
556 for (
int i=0; i<nl; i++) {
558 os << (i > 0 ?
"\n | " :
" ");
561 print_leaf(os, v, i);
568 for (
int k=0; k<v.depth; k++)
569 empty = empty || (v.lengths[k].size() == 0) || (v.lengths[k][0] == 0);
570 bool nd = (v.depth > 2) || ((v.depth == 2) && empty);
572 os <<
"array" << v.depth <<
"d(";
573 for (
int k=0; k<v.depth; k++)
574 os <<
"1.." << (v.lengths[k].size() > 0 ? v.lengths[k][0] : 0) <<
", ";
577 for (
int i=0; i<nl; i++) {
578 if (i > 0) os <<
", ";
579 print_leaf(os, v, i);
581 os <<
"]" << (nd ?
")" :
"") <<
";\n";
585 std::string
type = std::string(
"array[int] of ") +
587 for (
int k=1; k<v.depth; k++)
588 type =
"array[int] of tuple(" +
type +
")";
589 os <<
"% " << n <<
": " <<
type <<
"\n";
593 for (
int k=0; k<=v.depth; k++)
595 print_list(os, v, next, 0);
int size(void) const
Return size of array (number of elements).
Argument array for non-primitive types.
Exception(const char *l, const char *i) noexcept
Initialize with location l and information i.
FloatNum size(void) const
Return size of float value (distance between maximum and minimum).
Column(ValueType t, int depth)
Empty column for rows of type t and depth depth.
void skip(void)
Add a missing row.
Value finish(void)
Return the array.
void add(Value &v)
Add a row v; throws ShapeError with a phrase completing the name.
IntArgs ints(const std::string &n, const IntArgs &path=IntArgs()) const
Return all integers in the array or row n at path.
int size(const std::string &n, const IntArgs &path=IntArgs()) const
Return the length of the array or row n at path.
void dzn(std::ostream &os) const
Print all entries as MiniZinc data.
double number(const std::string &n, const IntArgs &path=IntArgs()) const
Return the number n at path (integers are converted).
ValueType type(const std::string &n) const
Return the value type of entry n.
std::string text(const std::string &n, const IntArgs &path=IntArgs()) const
Return the string n at path.
int depth(const std::string &n) const
Return the depth of entry n (0 for scalars).
bool has(const std::string &n) const
Test whether there is an entry n.
std::string name(int i) const
Return name of entry i, in the order they were defined.
long long int integer(const std::string &n, const IntArgs &path=IntArgs()) const
Return the integer n at path.
ArgArray< double > numbers(const std::string &n, const IntArgs &path=IntArgs()) const
Return all numbers in the array or row n at path.
int entries(void) const
Return number of entries.
Exception for errors in specs and instance files
Error(const std::string &m)
Initialize with message m.
std::string msg
The full message.
virtual const char * what(void) const noexcept override
Return the full message.
Implementation of Params.
Params(void)
No parameters.
std::string name(int i) const
Return the name of parameter i.
bool get(const std::string &name, std::string &value) const
Return whether name is set, and if so store its value in value.
int size(void) const
Return number of parameters.
void set(const std::string &name, const std::string &value)
Set parameter name to value.
Growable array on the Gecode heap.
void reserve(int m)
Make room for m elements.
void push(const T &v)
Append a copy of v.
void clear(void)
Remove all elements.
Error about the shape or type of a value, located by the caller.
A value: scalar or nested (possibly ragged) array.
void range(int level, int pos, int &from, int &to) const
Find leaf range of list pos at level level.
Seq< Seq< int > > starts
Start of each list among the lists or leaves one level down (see index).
Seq< Seq< int > > lengths
List lengths per level.
int depth
Nesting depth (0 for scalars).
void index(void)
Compute starts, needed by range.
int size(void) const
Return length of outermost list.
Passing integer arguments.
bool pos(const View &x)
Test whether x is positive.
Reading instance files described by reader specs.
ValueType
Type of the values in a data entry.
@ FLOAT
Floating point numbers.
bool is_letter(char c)
Test whether c is a letter.
Gecode toplevel namespace
void path(Home home, const IntVarArgs &x, IntVar s, IntVar e, IntPropLevel ipl=IPL_DEF)
Post propagator such that x forms a Hamiltonian path.
LinIntExpr sum(const IntVarArgs &x)
Construct linear expression as sum of integer variables.
Gecode::FloatVal a(-8, 5)
Gecode::IntArgs i({1, 2, 3, 4})