Google OR-Tools
v9.11
a fast and portable software suite for combinatorial optimization
Loading...
Searching...
No Matches
parser_util.cc
Go to the documentation of this file.
1
// Copyright 2010-2024 Google LLC
2
// Licensed under the Apache License, Version 2.0 (the "License");
3
// you may not use this file except in compliance with the License.
4
// You may obtain a copy of the License at
5
//
6
// http://www.apache.org/licenses/LICENSE-2.0
7
//
8
// Unless required by applicable law or agreed to in writing, software
9
// distributed under the License is distributed on an "AS IS" BASIS,
10
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11
// See the License for the specific language governing permissions and
12
// limitations under the License.
13
14
// Utility functions used by the code in parser.yy
15
// Included in parser.tab.cc.
16
#include "
ortools/flatzinc/parser_util.h
"
17
18
#include <cmath>
19
#include <string>
20
#include <vector>
21
22
#include "absl/strings/string_view.h"
23
#include "
ortools/base/logging.h
"
24
#include "
ortools/base/mathutil.h
"
25
#include "
ortools/base/stl_util.h
"
26
#include "
ortools/base/types.h
"
27
#include "
ortools/flatzinc/model.h
"
28
#include "
ortools/flatzinc/parser.tab.hh
"
29
#include "
ortools/util/string_array.h
"
30
31
extern
int
orfz_lex
(
YYSTYPE
*,
void
* scanner);
32
extern
int
orfz_get_lineno
(
void
* scanner);
33
extern
int
orfz_debug
;
34
35
void
orfz_error
(
operations_research::fz::ParserContext
*
context
,
36
operations_research::fz::Model
*
model
,
bool
* ok,
void
* scanner,
37
const
char
* str) {
38
LOG(ERROR) <<
"Error: "
<< str <<
" in line no. "
<<
orfz_get_lineno
(scanner);
39
*ok =
false
;
40
}
41
42
namespace
operations_research
{
43
namespace
fz {
44
// Whether the given list of annotations contains the given identifier
45
// (or function call).
46
bool
ContainsId
(std::vector<Annotation>* annotations, absl::string_view
id
) {
47
if
(annotations !=
nullptr
) {
48
for
(
int
i = 0; i < annotations->size(); ++i) {
49
if
(((*annotations)[i].type ==
Annotation::IDENTIFIER
||
50
(*annotations)[i].type ==
Annotation::FUNCTION_CALL
) &&
51
(*annotations)[i].
id
==
id
) {
52
return
true
;
53
}
54
}
55
}
56
return
false
;
57
}
58
59
bool
AllDomainsHaveOneValue
(
const
std::vector<Domain>& domains) {
60
for
(
int
i = 0; i < domains.size(); ++i) {
61
if
(!domains[i].HasOneValue()) {
62
return
false
;
63
}
64
}
65
return
true
;
66
}
67
68
int64_t
ConvertAsIntegerOrDie
(
double
d) {
69
const
double
rounded = std::round(d);
70
const
int64_t i =
static_cast<
int64_t
>
(rounded);
71
CHECK_LE(std::abs(
static_cast<
double
>
(i) - rounded), 1e-9);
72
return
i;
73
}
74
75
// Array in flatzinc are 1 based. We use this trivial wrapper for all flatzinc
76
// arrays.
77
template
<
class
T>
78
const
T&
Lookup
(
const
std::vector<T>& v,
int
index
) {
79
// TODO(user): replace this by a macro for better logging.
80
CHECK_GE(
index
, 1);
81
CHECK_LE(
index
, v.size());
82
return
v[
index
- 1];
83
}
84
}
// namespace fz
85
}
// namespace operations_research
logging.h
operations_research::fz::Model
Definition
model.h:341
model.h
model
GRBmodel * model
Definition
gurobi_interface.cc:300
context
GurobiMPCallbackContext * context
Definition
gurobi_interface.cc:540
index
int index
Definition
local_search.cc:2838
mathutil.h
operations_research::fz::Lookup
const T & Lookup(const std::vector< T > &v, int index)
Definition
parser_util.cc:78
operations_research::fz::ContainsId
bool ContainsId(std::vector< Annotation > *annotations, absl::string_view id)
Definition
parser_util.cc:46
operations_research::fz::ConvertAsIntegerOrDie
int64_t ConvertAsIntegerOrDie(double d)
If the argument is an integer, return it as int64_t. Otherwise, die.
Definition
parser_util.cc:68
operations_research::fz::AllDomainsHaveOneValue
bool AllDomainsHaveOneValue(const std::vector< Domain > &domains)
Definition
parser_util.cc:59
operations_research
In SWIG mode, we don't want anything besides these top-level includes.
Definition
binary_indexed_tree.h:21
parser.tab.hh
orfz_lex
int orfz_lex(YYSTYPE *, void *scanner)
orfz_debug
int orfz_debug
orfz_get_lineno
int orfz_get_lineno(void *scanner)
orfz_error
void orfz_error(operations_research::fz::ParserContext *context, operations_research::fz::Model *model, bool *ok, void *scanner, const char *str)
Definition
parser_util.cc:35
parser_util.h
stl_util.h
string_array.h
operations_research::fz::Annotation::FUNCTION_CALL
@ FUNCTION_CALL
Definition
model.h:265
operations_research::fz::Annotation::IDENTIFIER
@ IDENTIFIER
Definition
model.h:264
operations_research::fz::LexerInfo
Definition
parser_util.h:76
operations_research::fz::ParserContext
This is the context used during parsing.
Definition
parser_util.h:31
types.h
ortools
flatzinc
parser_util.cc
Generated by
1.12.0