Skip to content

Commit 62f2a86

Browse files
committed
Use enum class rather than plain enum.
1 parent 3c9d27f commit 62f2a86

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

ortools/math_opt/solvers/xpress_solver.cc

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -281,7 +281,11 @@ class ScopedSolverContext {
281281
struct OneControl {
282282
int id;
283283
std::variant<int64_t, double, std::string> value;
284-
enum { INT_CONTROL, DBL_CONTROL, STR_CONTROL }; // Matches std::variant<>::index;
284+
enum {
285+
INT_CONTROL,
286+
DBL_CONTROL,
287+
STR_CONTROL
288+
}; // Matches std::variant<>::index;
285289
};
286290
/** Controls to be reset in the destructor. */
287291
std::vector<OneControl> modifiedControls;
@@ -290,9 +294,7 @@ class ScopedSolverContext {
290294
ScopedSolverContext(Xpress* xpress) : removeInterrupterCallback(nullptr) {
291295
shared_ctx.xpress = xpress;
292296
}
293-
absl::Status Set(int id, int32_t value) {
294-
return Set(id, int64_t(value));
295-
}
297+
absl::Status Set(int id, int32_t value) { return Set(id, int64_t(value)); }
296298
absl::Status Set(int id, int64_t value) {
297299
ASSIGN_OR_RETURN(int64_t old, shared_ctx.xpress->GetIntControl64(id));
298300
modifiedControls.push_back({id, old});
@@ -741,11 +743,11 @@ class ScopedSolverContext {
741743
};
742744

743745
/** Different modes for ExtractSingleton(). */
744-
typedef enum {
745-
SingletonForSOS, /**< SOS constraint. */
746-
SingletonForSOCBound, /**< Second order cone constraint bound. */
747-
SingletonForSOCNorm /**< Second order cone constraint norm. */
748-
} SingletonType;
746+
enum class SingletonType {
747+
SOS, /**< SOS constraint. */
748+
SOCBound, /**< Second order cone constraint bound. */
749+
SOCNorm /**< Second order cone constraint norm. */
750+
};
749751

750752
// ortools supports SOS constraints and second order cone constraints on
751753
// expressions. Xpress only supports these constructs on singleton variables.
@@ -762,23 +764,23 @@ absl::StatusOr<std::optional<XpressSolver::VarId>> ExtractSingleton(
762764
// We have a single variable in the expression and no constant.
763765
double const coef = expr.coefficients(0);
764766
switch (type) {
765-
case SingletonForSOS:
767+
case SingletonType::SOS:
766768
// A non-zero coefficient does not change anything, so is allowed.
767769
if (coef == 0.0) {
768770
return util::InvalidArgumentErrorBuilder()
769771
<< "Xpress does not support coefficient " << coef
770772
<< " in SOS (consider using auxiliary variables?)";
771773
}
772774
break;
773-
case SingletonForSOCBound: // fallthrough
774-
case SingletonForSOCNorm:
775+
case SingletonType::SOCBound: // fallthrough
776+
case SingletonType::SOCNorm:
775777
// We are going to square the coefficient, so anything non-negative
776778
// is allowed.
777779
if (coef < 0) {
778780
return util::InvalidArgumentErrorBuilder()
779781
<< "Xpress does not support coefficient " << coef
780782
<< " in a second order cone constraint "
781-
<< (type == SingletonForSOCBound ? "bound" : "norm")
783+
<< (type == SingletonType::SOCBound ? "bound" : "norm")
782784
<< " (consider using auxiliary variables?)";
783785
}
784786
break;
@@ -788,14 +790,14 @@ absl::StatusOr<std::optional<XpressSolver::VarId>> ExtractSingleton(
788790
} else if (expr.ids_size() == 0) {
789791
// The expression is constant.
790792
switch (type) {
791-
case SingletonForSOS:
793+
case SingletonType::SOS:
792794
// Any non-zero constant would force all other variables to 0.
793795
// Any zero constant would be redundant.
794796
// Both are edge cases that we do not support at the moment.
795797
return util::InvalidArgumentErrorBuilder()
796798
<< "Xpress does not support constant expressions in SOS "
797799
"(consider using auxiliary variables?)";
798-
case SingletonForSOCBound:
800+
case SingletonType::SOCBound:
799801
// We are going to square the bound, so it should not be negative.
800802
if (constant < 0.0) {
801803
return util::InvalidArgumentErrorBuilder()
@@ -804,7 +806,7 @@ absl::StatusOr<std::optional<XpressSolver::VarId>> ExtractSingleton(
804806
"auxiliary variables?)";
805807
}
806808
break;
807-
case SingletonForSOCNorm:
809+
case SingletonType::SOCNorm:
808810
// Constant entries in the norm are not supported (we would have to
809811
// move them to the right-hand side).
810812
return util::InvalidArgumentErrorBuilder()
@@ -820,7 +822,8 @@ absl::StatusOr<std::optional<XpressSolver::VarId>> ExtractSingleton(
820822
"second order cone constraint norm"};
821823
return util::InvalidArgumentErrorBuilder()
822824
<< "Xpress does not support general linear expressions in "
823-
<< name[type] << " (consider using auxiliary variables?)";
825+
<< name[static_cast<int>(type)]
826+
<< " (consider using auxiliary variables?)";
824827
}
825828
}
826829

@@ -1128,7 +1131,7 @@ absl::Status XpressSolver::AddSOS(
11281131
// Note: A constant value in an SOS forces all others to zero. At the
11291132
// moment we do not support this. We consider this an edge case.
11301133
ASSIGN_OR_RETURN(std::optional<VarId> x,
1131-
ExtractSingleton(expr, SingletonForSOS, nullptr));
1134+
ExtractSingleton(expr, SingletonType::SOS, nullptr));
11321135
colind.push_back(variables_map_.at(x.value()));
11331136
refval.push_back(weight);
11341137
}
@@ -1301,7 +1304,7 @@ absl::Status XpressSolver::AddSecondOrderConeConstraints(
13011304
auto const& ub = soc.upper_bound();
13021305
double coef;
13031306
ASSIGN_OR_RETURN(std::optional<VarId> const x0,
1304-
ExtractSingleton(ub, SingletonForSOCBound, &coef));
1307+
ExtractSingleton(ub, SingletonType::SOCBound, &coef));
13051308
if (x0.has_value()) {
13061309
cols.push_back(variables_map_.at(x0.value()));
13071310
coefs.push_back(-coef * coef);
@@ -1311,7 +1314,7 @@ absl::Status XpressSolver::AddSecondOrderConeConstraints(
13111314

13121315
for (auto const& arg : soc.arguments_to_norm()) {
13131316
ASSIGN_OR_RETURN(std::optional<VarId> const x,
1314-
ExtractSingleton(arg, SingletonForSOCNorm, &coef));
1317+
ExtractSingleton(arg, SingletonType::SOCNorm, &coef));
13151318
cols.push_back(variables_map_.at(x.value()));
13161319
coefs.push_back(coef * coef);
13171320
}

0 commit comments

Comments
 (0)