39#ifndef GECODE_INSTANCE_IMPL_HPP
40#define GECODE_INSTANCE_IMPL_HPP
57 return (c >=
'0') && (c <=
'9');
61 return ((c >=
'a') && (c <=
'z')) || ((c >=
'A') && (c <=
'Z'));
69 return (c ==
' ') || (c ==
'\t') || (c ==
'\n') || (c ==
'\r') || (c ==
'\v') || (c ==
'\f');
73 return (c ==
' ') || (c ==
'\t') || (c ==
'\r');
77 return (c ==
'+') || (c ==
'-');
81 return (c ==
'e') || (c ==
'E');
90 inline size_t number_end(
const std::string& s,
size_t p,
bool sign,
bool real) {
92 for (; q < s.size(); q++) {
96 if (sign && (q == p) &&
is_sign(c))
107 bool parse_int(
const std::string& t,
long long int& v);
129 long long int d = std::min(2LL*cap,
static_cast<long long int>(INT_MAX));
130 int c = std::max(std::max(m,
static_cast<int>(d)), 2);
131 T* y =
static_cast<T*
>(
heap.ralloc(
sizeof(T)*
static_cast<size_t>(c)));
132 for (
int i=0; i<n; i++) {
133 new (&y[i]) T(std::move(x[i]));
143 : x(nullptr), n(0), cap(0) {}
149 for (
int i=0; i<s.n; i++)
153 Seq(
Seq&& s) noexcept : x(s.x), n(s.n), cap(s.cap) {
154 s.x =
nullptr; s.n = 0; s.cap = 0;
158 std::swap(x,s.x); std::swap(n,s.n); std::swap(cap,s.cap);
168 int size(
void)
const {
return n; }
170 bool empty(
void)
const {
return n == 0; }
173 if (m > cap) grow(m);
180 new (&x[n]) T(std::move(t));
191 new (&x[n]) T(std::move(t));
193 new (&x[n]) T(std::move(v));
209 assert((i >= 0) && (i < n));
214 assert((i >= 0) && (i < n));
230 T*
end(
void) {
return x+n; }
232 const T*
begin(
void)
const {
return x; }
234 const T*
end(
void)
const {
return x+n; }
306 default:
return strs.size();
318 for (
long long int v :
ints)
319 reals.push(
static_cast<double>(v));
328 throw ShapeError(
"strings and numbers are mixed");
335 else reals.push(
static_cast<double>(s.
ints[0]));
338 default:
strs.push(s.
strs[0]);
break;
359 void range(
int level,
int pos,
int& from,
int& to)
const;
459 OR,
AND,
EQ,
NE,
LT,
GT,
LE,
GE,
ADD,
SUB,
MUL,
DIV,
MOD,
POW,
NEG,
NOT
512 return "#" + std::to_string(
id);
516 return !n.empty() && (n[0] ==
'#');
543 std::unique_ptr<Expr>
lo;
545 std::unique_ptr<Expr>
hi;
565 return (
lo !=
nullptr) || (
hi !=
nullptr) || (
named !=
nullptr);
627 LINE,
FIND,
SKIP,
EXPECT,
INDEX,
REPEAT,
SECTIONS,
IF,
LET,
CHECK,
696 [[noreturn]]
void fail_at(
const std::string& origin,
int line,
const std::string& m);
725 Data read(
const std::string& input,
const std::string&
origin,
const Params& p,
bool catalog)
const;
Block body
Statements run after the line matched.
Pattern pattern
Line pattern.
bool last
Whether the sections statement ends after this arm.
bool optional
Whether the input may lack a line matching the arm.
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.
Seq< Entry > entries
The entries in definition order.
const Value * find(const std::string &n) const
Return entry n or null.
Named values read from an instance.
A name that a block may define, with the type and depth of its values.
ValueType type
Type of the leaves.
Entry(void)
Initialize as the empty integer list without a name.
Entry(const std::string &n, Value &&v)
Initialize as v named n.
Op
Operator of unary and binary expressions.
const Stmt * elements
For a comprehension, the repeat whose records are the elements of its array, or null.
std::string var
Name bound by a comprehension.
long long int i
Integer literal.
std::string s
String literal, name, operator, or function.
int open
1 for a list of records of records, 0 for a record
enum Gecode::Instance::Expr::Kind kind
int record
Index of a record name among the records of its check (-1 if none).
Seq< std::unique_ptr< Expr > > args
Operands or arguments.
const Stmt * records
The repeat whose records or lists of records this is, or null.
Expr(Kind k)
Initialize with kind k.
enum Gecode::Instance::Expr::Op op
Implementation of Params.
Seq< std::string > names
Names.
Seq< std::string > values
Values.
Values for the param statements of a spec.
std::string text
Literal text, capture name, or variable name.
enum Gecode::Instance::Pattern::Item::Kind kind
bool required
Whether a space item needs at least one blank.
Type type
What a capture reads.
Item(Kind k)
Initialize with kind k.
Line pattern such as "DIMENSION : {n:int}".
std::string source
Source text.
A record name of a check.
std::string name
The name of the values.
std::string label
The name in messages: the name, or the list of records.
Seq< const Stmt * > repeats
The repeats between the check and the definition, outermost first.
std::string starts
Name of the record starts of the innermost of these repeats.
Growable array on the Gecode heap.
Seq & operator=(Seq s) noexcept
Assign elements of s.
Seq(const Seq &s)
Copy elements of s.
bool empty(void) const
Test whether empty.
~Seq(void)
Release memory.
const T & back(void) const
Return last element.
void reserve(int m)
Make room for m elements.
void push(const T &v)
Append a copy of v.
Seq(Seq &&s) noexcept
Take elements of s.
T * end(void)
Iteration support.
int size(void) const
Return number of elements.
T * begin(void)
Iteration support.
const T * end(void) const
Iteration support.
const T * begin(void) const
Iteration support.
T & operator[](int i)
Return element i.
void pop(void)
Remove last element.
void clear(void)
Remove all elements.
T & back(void)
Return last element.
void push(T &&v)
Append v.
Error about the shape or type of a value, located by the caller.
ShapeError(const std::string &m)
Initialize with message m.
SpecImpl(const std::string &text, const std::string &origin)
Parse text from origin.
std::string name
Format name.
Data read(const std::string &input, const std::string &origin, const Params &p, bool catalog) const
Read input from origin.
std::string origin
Where the spec comes from.
bool until_eof
Repeat until end of input.
bool has_until
Repeat or sections end at a token from words.
Seq< Record > records
Record names of a check.
bool skip_other
Sections skip unknown lines.
enum Gecode::Instance::Stmt::Kind kind
std::string message
Message of a check statement.
std::string chars
Characters for tokens and separators statements.
bool until_eol
List ends at end of line.
Stmt(Kind k, int l)
Initialize with kind k at spec line l.
std::string each
Name of each record of a repeat, inside and after it.
std::string store
Name for the terminating token found.
std::unique_ptr< Expr > offset
Added to every number read.
int id
Number of a repeat, in the order of the spec.
int line
Line in the spec.
Pattern pattern
Pattern for line and find.
std::unique_ptr< Expr > sentinel
List terminator.
Seq< Arm > arms
Arms of sections.
std::string name
Name defined.
Block body
Body, then-branch.
Seq< std::unique_ptr< Expr > > dims
Array dimensions.
Type type
What read and list statements read, or the type a type statement defines.
std::string list
Name of the list of records of a repeat.
Seq< std::string > words
Prefixes, literal tokens, or terminating tokens.
std::unique_ptr< Expr > expr
Count, condition, layout, value, or default.
Type of the values that a declaration or a capture reads.
bool ranged(void) const
Test whether the type has a range, possibly through its name.
std::unique_ptr< Expr > lo
Lower bound of a range, or null.
const Stmt * named
The type statement defining a named type, or null.
std::string blank
Symbols that stand for 0.
Type use(const Stmt *d) const
Return a use of the type defined by type statement d.
bool exclusive
Whether the upper bound is excluded.
std::string name
Name of a named type.
ReadType read
What the type reads.
std::string alphabet
Symbols of an alphabet type.
std::string strip
Symbols to skip.
long long int base
Value of the first symbol.
std::unique_ptr< Expr > hi
Upper bound of a range, or null.
Type(ReadType r=ReadType::INT)
Initialize as r.
A value: scalar or nested (possibly ragged) array.
int leaves(void) const
Return number of leaves.
ValueType type
Type of the leaves.
void to_float(void)
Convert integer leaves to floats.
static Value real(double v)
Float scalar.
Seq< long long int > ints
Integer leaves.
void push(const Value &s)
Append the value of scalar s, adapting the type.
static Value string(const std::string &v)
String scalar.
Value(void)
The empty integer list.
Seq< std::string > strs
String leaves.
void range(int level, int pos, int &from, int &to) const
Find leaf range of list pos at level level.
double as_double(int i=0) const
Return value of numeric scalar or leaf i as a double.
bool is_number(void) const
Test whether numeric scalar.
Seq< Seq< int > > starts
Start of each list among the lists or leaves one level down (see index).
bool is_scalar(void) const
Test whether scalar.
Seq< Seq< int > > lengths
List lengths per level.
Value(ValueType t)
Scalar of type t whose leaf is still to be added.
int depth
Nesting depth (0 for scalars).
void index(void)
Compute starts, needed by range.
void clear_list(ValueType t)
Make this the empty list of type t, keeping the memory for reuse.
Seq< double > reals
Float leaves.
bool is_empty_list(void) const
Test whether this is an empty list.
int size(void) const
Return length of outermost list.
static Value integer(long long int v)
Integer scalar.
Heap heap
The single global heap.
Reading instance files described by reader specs.
void resolve(Block &b, const std::string &origin)
Resolve the names in the expressions of b, the body of a spec from origin.
ValueType
Type of the values in a data entry.
@ FLOAT
Floating point numbers.
bool is_digit(char c)
Test whether c is a digit.
std::string record_starts(int id)
Return the name of the record starts of the repeat numbered id.
bool parse_float(const std::string &t, double &v)
Parse decimal float t into v, and return whether it is one.
std::string read_file(const std::string &path)
Read file path into a string.
void declared(const Block &b, Seq< Decl > &out)
Add the names that block b may define to out.
bool is_space(char c)
Test whether c is white space.
ReadType
What a declaration or a capture reads.
@ NUM
An integer if possible, otherwise a float.
@ REST
The rest of the line as a string.
@ SYMBOL
A character of an alphabet, as an integer.
@ WORD
A token as a string.
bool is_alnum(char c)
Test whether c can occur in a name.
bool parse_int(const std::string &t, long long int &v)
Parse decimal integer t into v, and return whether it is one.
size_t number_end(const std::string &s, size_t p, bool sign, bool real)
Return where the number starting at position p of s ends.
void fail_at(const std::string &origin, int line, const std::string &m)
Throw an error with message m for line line of the spec from origin.
bool is_exponent(char c)
Test whether c starts the exponent of a float.
bool is_letter(char c)
Test whether c is a letter.
bool is_blank(char c)
Test whether c is white space within a line.
Seq< std::unique_ptr< Stmt > > Block
Sequence of statements.
bool is_sign(char c)
Test whether c is a sign.
bool is_internal(const std::string &n)
Test whether n is a name used internally (see record_starts).
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.