Google OR-Tools v9.9
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.
17
18#include <cmath>
19#include <string>
20#include <vector>
21
22#include "absl/strings/string_view.h"
26#include "ortools/base/types.h"
30
31extern int orfz_lex(YYSTYPE*, void* scanner);
32extern int orfz_get_lineno(void* scanner);
33extern int orfz_debug;
34
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
42namespace operations_research {
43namespace fz {
44// Whether the given list of annotations contains the given identifier
45// (or function call).
46bool 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
59bool 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
68int64_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.
77template <class T>
78const 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
GRBmodel * model
GurobiMPCallbackContext * context
int index
const T & Lookup(const std::vector< T > &v, int index)
bool ContainsId(std::vector< Annotation > *annotations, absl::string_view id)
int64_t ConvertAsIntegerOrDie(double d)
If the argument is an integer, return it as int64_t. Otherwise, die.
bool AllDomainsHaveOneValue(const std::vector< Domain > &domains)
In SWIG mode, we don't want anything besides these top-level includes.
int orfz_lex(YYSTYPE *, void *scanner)
int orfz_debug
int orfz_get_lineno(void *scanner)
void orfz_error(operations_research::fz::ParserContext *context, operations_research::fz::Model *model, bool *ok, void *scanner, const char *str)
This is the context used during parsing.
Definition parser_util.h:31