Skip to content

Commit 77cd493

Browse files
committed
Improved C++ code
1 parent d03214e commit 77cd493

File tree

9 files changed

+65
-76
lines changed

9 files changed

+65
-76
lines changed

ext/or-tools/assignment.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#include <ortools/graph/assignment.h>
2-
3-
#include "ext.h"
2+
#include <rice/rice.hpp>
3+
#include <rice/stl.hpp>
44

55
using operations_research::SimpleLinearSumAssignment;
66

ext/or-tools/bin_packing.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,28 @@
1-
#include <ortools/algorithms/knapsack_solver.h>
1+
#include <string>
2+
#include <vector>
23

3-
#include "ext.h"
4+
#include <ortools/algorithms/knapsack_solver.h>
5+
#include <rice/rice.hpp>
6+
#include <rice/stl.hpp>
47

58
using operations_research::KnapsackSolver;
69

710
using Rice::Array;
811
using Rice::Object;
912
using Rice::Symbol;
1013

11-
namespace Rice::detail
12-
{
14+
namespace Rice::detail {
1315
template<>
14-
struct Type<KnapsackSolver::SolverType>
15-
{
16+
struct Type<KnapsackSolver::SolverType> {
1617
static bool verify() { return true; }
1718
};
1819

1920
template<>
20-
class From_Ruby<KnapsackSolver::SolverType>
21-
{
21+
class From_Ruby<KnapsackSolver::SolverType> {
2222
public:
2323
Convertible is_convertible(VALUE value) { return Convertible::Cast; }
2424

25-
KnapsackSolver::SolverType convert(VALUE x)
26-
{
25+
KnapsackSolver::SolverType convert(VALUE x) {
2726
auto s = Symbol(x).str();
2827
if (s == "branch_and_bound") {
2928
return KnapsackSolver::KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER;
@@ -32,7 +31,7 @@ namespace Rice::detail
3231
}
3332
}
3433
};
35-
}
34+
} // namespace Rice::detail
3635

3736
void init_bin_packing(Rice::Module& m) {
3837
Rice::define_class_under<KnapsackSolver>(m, "KnapsackSolver")

ext/or-tools/constraint.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
#include <string>
2+
#include <vector>
3+
14
#include <google/protobuf/text_format.h>
25
#include <ortools/sat/cp_model.h>
3-
4-
#include "ext.h"
6+
#include <rice/rice.hpp>
7+
#include <rice/stl.hpp>
58

69
using operations_research::Domain;
710
using operations_research::sat::BoolVar;
@@ -27,22 +30,18 @@ using Rice::Symbol;
2730
Class rb_cBoolVar;
2831
Class rb_cSatIntVar;
2932

30-
namespace Rice::detail
31-
{
33+
namespace Rice::detail {
3234
template<>
33-
struct Type<LinearExpr>
34-
{
35+
struct Type<LinearExpr> {
3536
static bool verify() { return true; }
3637
};
3738

3839
template<>
39-
class From_Ruby<LinearExpr>
40-
{
40+
class From_Ruby<LinearExpr> {
4141
public:
4242
Convertible is_convertible(VALUE value) { return Convertible::Cast; }
4343

44-
LinearExpr convert(VALUE v)
45-
{
44+
LinearExpr convert(VALUE v) {
4645
LinearExpr expr;
4746

4847
Rice::Object utils = Rice::define_module("ORTools").const_get("Utils");
@@ -64,7 +63,7 @@ namespace Rice::detail
6463
return expr;
6564
}
6665
};
67-
}
66+
} // namespace Rice::detail
6867

6968
void init_constraint(Rice::Module& m) {
7069
Rice::define_class_under<Domain>(m, "Domain")

ext/or-tools/ext.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#include <ortools/base/version.h>
22
#include <ortools/init/init.h>
3-
4-
#include "ext.h"
3+
#include <rice/rice.hpp>
4+
#include <rice/stl.hpp>
55

66
using operations_research::CppBridge;
77
using operations_research::CppFlags;
@@ -15,8 +15,7 @@ void init_network_flows(Rice::Module& m);
1515
void init_routing(Rice::Module& m);
1616

1717
extern "C"
18-
void Init_ext()
19-
{
18+
void Init_ext() {
2019
auto m = Rice::define_module("ORTools");
2120

2221
m.define_singleton_function("lib_version", &operations_research::OrToolsVersionString);

ext/or-tools/ext.h

Lines changed: 0 additions & 4 deletions
This file was deleted.

ext/or-tools/linear.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1-
#include <ortools/linear_solver/linear_solver.h>
1+
#include <memory>
2+
#include <string>
23

3-
#include "ext.h"
4+
#include <ortools/linear_solver/linear_solver.h>
5+
#include <rice/rice.hpp>
6+
#include <rice/stl.hpp>
47

58
using operations_research::MPConstraint;
69
using operations_research::MPObjective;
@@ -15,21 +18,17 @@ using Rice::Object;
1518
using Rice::String;
1619
using Rice::Symbol;
1720

18-
namespace Rice::detail
19-
{
21+
namespace Rice::detail {
2022
template<>
21-
struct Type<MPSolver::OptimizationProblemType>
22-
{
23+
struct Type<MPSolver::OptimizationProblemType> {
2324
static bool verify() { return true; }
2425
};
2526

2627
template<>
27-
struct From_Ruby<MPSolver::OptimizationProblemType>
28-
{
28+
struct From_Ruby<MPSolver::OptimizationProblemType> {
2929
Convertible is_convertible(VALUE value) { return Convertible::Cast; }
3030

31-
static MPSolver::OptimizationProblemType convert(VALUE x)
32-
{
31+
static MPSolver::OptimizationProblemType convert(VALUE x) {
3332
auto s = Symbol(x).str();
3433
if (s == "glop") {
3534
return MPSolver::OptimizationProblemType::GLOP_LINEAR_PROGRAMMING;
@@ -40,7 +39,7 @@ namespace Rice::detail
4039
}
4140
}
4241
};
43-
}
42+
} // namespace Rice::detail
4443

4544
void init_linear(Rice::Module& m) {
4645
Rice::define_class_under<MPVariable>(m, "MPVariable")

ext/or-tools/math_opt.cpp

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
#include "absl/log/check.h"
2-
#include "absl/status/statusor.h"
3-
#include "ortools/base/init_google.h"
4-
#include "ortools/math_opt/cpp/math_opt.h"
1+
#include <string>
52

6-
#include "ext.h"
3+
#include <absl/log/check.h>
4+
#include <absl/status/statusor.h>
5+
#include <ortools/base/init_google.h>
6+
#include <ortools/math_opt/cpp/math_opt.h>
7+
#include <rice/rice.hpp>
8+
#include <rice/stl.hpp>
79

810
using operations_research::math_opt::LinearConstraint;
911
using operations_research::math_opt::Model;
@@ -15,21 +17,17 @@ using operations_research::math_opt::Termination;
1517
using operations_research::math_opt::TerminationReason;
1618
using operations_research::math_opt::Variable;
1719

18-
namespace Rice::detail
19-
{
20+
namespace Rice::detail {
2021
template<>
21-
struct Type<SolverType>
22-
{
22+
struct Type<SolverType> {
2323
static bool verify() { return true; }
2424
};
2525

2626
template<>
27-
struct From_Ruby<SolverType>
28-
{
27+
struct From_Ruby<SolverType> {
2928
Convertible is_convertible(VALUE value) { return Convertible::Cast; }
3029

31-
static SolverType convert(VALUE x)
32-
{
30+
static SolverType convert(VALUE x) {
3331
auto s = Symbol(x).str();
3432
if (s == "gscip") {
3533
return SolverType::kGscip;
@@ -56,7 +54,7 @@ namespace Rice::detail
5654
}
5755
}
5856
};
59-
}
57+
} // namespace Rice::detail
6058

6159
void init_math_opt(Rice::Module& m) {
6260
auto mathopt = Rice::define_module_under(m, "MathOpt");
@@ -67,7 +65,7 @@ void init_math_opt(Rice::Module& m) {
6765
.define_method(
6866
"_eql?",
6967
[](Variable& self, Variable &other) {
70-
return (bool) (self == other);
68+
return static_cast<bool>(self == other);
7169
});
7270

7371
Rice::define_class_under<LinearConstraint>(mathopt, "LinearConstraint");

ext/or-tools/network_flows.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
#include <vector>
2+
13
#include <ortools/graph/max_flow.h>
24
#include <ortools/graph/min_cost_flow.h>
3-
4-
#include "ext.h"
5+
#include <rice/rice.hpp>
6+
#include <rice/stl.hpp>
57

68
using operations_research::NodeIndex;
79
using operations_research::SimpleMaxFlow;

ext/or-tools/routing.cpp

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
#include <string>
2+
#include <vector>
3+
14
#include <ortools/constraint_solver/routing.h>
25
#include <ortools/constraint_solver/routing_parameters.h>
3-
4-
#include "ext.h"
6+
#include <rice/rice.hpp>
7+
#include <rice/stl.hpp>
58

69
using operations_research::Assignment;
710
using operations_research::ConstraintSolverParameters;
@@ -24,37 +27,31 @@ using Rice::Object;
2427
using Rice::String;
2528
using Rice::Symbol;
2629

27-
namespace Rice::detail
28-
{
30+
namespace Rice::detail {
2931
template<>
30-
struct Type<RoutingNodeIndex>
31-
{
32+
struct Type<RoutingNodeIndex> {
3233
static bool verify() { return true; }
3334
};
3435

3536
template<>
36-
class From_Ruby<RoutingNodeIndex>
37-
{
37+
class From_Ruby<RoutingNodeIndex> {
3838
public:
3939
Convertible is_convertible(VALUE value) { return Convertible::Cast; }
4040

41-
RoutingNodeIndex convert(VALUE x)
42-
{
41+
RoutingNodeIndex convert(VALUE x) {
4342
const RoutingNodeIndex index{From_Ruby<int>().convert(x)};
4443
return index;
4544
}
4645
};
4746

4847
template<>
49-
class To_Ruby<RoutingNodeIndex>
50-
{
48+
class To_Ruby<RoutingNodeIndex> {
5149
public:
52-
VALUE convert(RoutingNodeIndex const & x)
53-
{
50+
VALUE convert(RoutingNodeIndex const & x) {
5451
return To_Ruby<int>().convert(x.value());
5552
}
5653
};
57-
}
54+
} // namespace Rice::detail
5855

5956
void init_routing(Rice::Module& m) {
6057
auto rb_cRoutingSearchParameters = Rice::define_class_under<RoutingSearchParameters>(m, "RoutingSearchParameters");
@@ -332,7 +329,7 @@ void init_routing(Rice::Module& m) {
332329
.define_method("add_resource_group", &RoutingModel::AddResourceGroup)
333330
.define_method("dimension_resource_group_indices", &RoutingModel::GetDimensionResourceGroupIndices)
334331
.define_method("dimension_resource_group_index", &RoutingModel::GetDimensionResourceGroupIndex)
335-
.define_method("add_disjunction", &RoutingModel::AddDisjunction, Rice::Arg("_indices"), Rice::Arg("_penalty"), Rice::Arg("_max_cardinality") = (int64_t)1, Rice::Arg("_penalty_cost_behavior") = RoutingModel::PenaltyCostBehavior::PENALIZE_ONCE)
332+
.define_method("add_disjunction", &RoutingModel::AddDisjunction, Rice::Arg("_indices"), Rice::Arg("_penalty"), Rice::Arg("_max_cardinality") = static_cast<int64_t>(1), Rice::Arg("_penalty_cost_behavior") = RoutingModel::PenaltyCostBehavior::PENALIZE_ONCE)
336333
.define_method("disjunction_indices", &RoutingModel::GetDisjunctionIndices)
337334
.define_method("disjunction_penalty", &RoutingModel::GetDisjunctionPenalty)
338335
.define_method("disjunction_max_cardinality", &RoutingModel::GetDisjunctionMaxCardinality)

0 commit comments

Comments
 (0)