reduce swig file to their python part
This commit is contained in:
@@ -18,18 +18,4 @@
|
||||
#include "algorithms/knapsack_solver.h"
|
||||
%}
|
||||
|
||||
#if defined(SWIGJAVA)
|
||||
%rename (bestSolutionContains) operations_research::KnapsackSolver::BestSolutionContains;
|
||||
%rename (getName) operations_research::KnapsackSolver::GetName;
|
||||
%rename (init) operations_research::KnapsackSolver::Init;
|
||||
%rename (solve) operations_research::KnapsackSolver::Solve;
|
||||
%rename (useReduction) operations_research::KnapsackSolver::use_reduction;
|
||||
%rename (setUseReduction) operations_research::KnapsackSolver::set_use_reduction;
|
||||
#endif // SWIGJAVA
|
||||
|
||||
#if defined(SWIGCSHARP)
|
||||
%rename (UseReduction) operations_research::KnapsackSolver::use_reduction;
|
||||
%rename (SetUseReduction) operations_research::KnapsackSolver::set_use_reduction;
|
||||
#endif // SWIGCSHARP
|
||||
|
||||
%include "algorithms/knapsack_solver.h"
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -22,7 +22,6 @@
|
||||
#include "graph/min_cost_flow.h"
|
||||
%}
|
||||
|
||||
#if defined(SWIGPYTHON)
|
||||
// Instructs swig to ignore the std::vector<int>* parameters as inputs since
|
||||
// they are actually outputs. Note that the name "result" is important and
|
||||
// only such parameters will be ignored.
|
||||
@@ -32,102 +31,6 @@
|
||||
|
||||
// Apply std::vector<int> to python list conversion.
|
||||
%apply std::vector<int>* OUTPUT {std::vector<int>* result}
|
||||
#endif // SWIGPYTHON
|
||||
|
||||
#if defined(SWIGJAVA)
|
||||
// Rename rules on EbertGraph.
|
||||
%rename(reserve) operations_research::EbertGraphBase::Reserve;
|
||||
%rename(numNodes) operations_research::StarGraphBase::num_nodes;
|
||||
%rename(numArcs) operations_research::StarGraphBase::num_arcs;
|
||||
%rename(maxNumNodes) operations_research::StarGraphBase::max_num_nodes;
|
||||
%rename(maxNumArcs) operations_research::StarGraphBase::max_num_arcs;
|
||||
%rename(addArc) operations_research::EbertGraphBase::AddArc;
|
||||
|
||||
// Rename rules on MaxFlow.
|
||||
%rename (getSourceNodeIndex) operations_research::GenericMaxFlow::GetSourceNodeIndex;
|
||||
%rename (getSinkNodeIndex) operations_research::GenericMaxFlow::GetSinkNodeIndex;
|
||||
%rename (setArcCapacity) operations_research::GenericMaxFlow::SetArcCapacity;
|
||||
%rename (setArcFlow) operations_research::GenericMaxFlow::SetArcFlow;
|
||||
%rename (solve) operations_research::GenericMaxFlow::Solve;
|
||||
%rename (getOptimalFlow) operations_research::GenericMaxFlow::GetOptimalFlow;
|
||||
%rename (flow) operations_research::GenericMaxFlow::Flow;
|
||||
%rename (capacity) operations_research::GenericMaxFlow::Capacity;
|
||||
|
||||
// Rename rules on SimpleMaxFlow.
|
||||
%rename(addArcWithCapacity) operations_research::SimpleMaxFlow::AddArcWithCapacity;
|
||||
%rename(getNumNodes) operations_research::SimpleMaxFlow::NumNodes;
|
||||
%rename(getNumArcs) operations_research::SimpleMaxFlow::NumArcs;
|
||||
%rename(getTail) operations_research::SimpleMaxFlow::Tail;
|
||||
%rename(getHead) operations_research::SimpleMaxFlow::Head;
|
||||
%rename(getCapacity) operations_research::SimpleMaxFlow::Capacity;
|
||||
%rename(solve) operations_research::SimpleMaxFlow::Solve;
|
||||
%rename(getOptimalFlow) operations_research::SimpleMaxFlow::OptimalFlow;
|
||||
%rename(getFlow) operations_research::SimpleMaxFlow::Flow;
|
||||
%rename(getSourceSideMinCut) operations_research::SimpleMaxFlow::GetSourceSideMinCut;
|
||||
%rename(getSinkSideMinCut) operations_research::SimpleMaxFlow::GetSinkSideMinCut;
|
||||
|
||||
// Rename rules on MinCostFlow.
|
||||
%rename(setNodeSupply) operations_research::GenericMinCostFlow::SetNodeSupply;
|
||||
%rename(setArcUnitCost) operations_research::GenericMinCostFlow::SetArcUnitCost;
|
||||
%rename(setArcCapacity) operations_research::GenericMinCostFlow::SetArcCapacity;
|
||||
%rename(setArcFlow) operations_research::GenericMinCostFlow::SetArcFlow;
|
||||
%rename(solve) operations_research::GenericMinCostFlow::Solve;
|
||||
%rename(getOptimalCost) operations_research::GenericMinCostFlow::GetOptimalCost;
|
||||
%rename(flow) operations_research::GenericMinCostFlow::Flow;
|
||||
%rename(capacity) operations_research::GenericMinCostFlow::Capacity;
|
||||
%rename(cost) operations_research::GenericMinCostFlow::Cost;
|
||||
%rename(supply) operations_research::GenericMinCostFlow::Supply;
|
||||
|
||||
// Rename rules on SimpleMinCostFlow.
|
||||
%rename(addArcWithCapacityAndUnitCost) operations_research::SimpleMinCostFlow::AddArcWithCapacityAndUnitCost;
|
||||
%rename(setNodeSupply) operations_research::SimpleMinCostFlow::SetNodeSupply;
|
||||
%rename(solve) operations_research::SimpleMinCostFlow::Solve;
|
||||
%rename(getOptimalCost) operations_research::SimpleMinCostFlow::OptimalCost;
|
||||
%rename(getNumNodes) operations_research::SimpleMinCostFlow::NumNodes;
|
||||
%rename(getNumArcs) operations_research::SimpleMinCostFlow::NumArcs;
|
||||
%rename(getTail) operations_research::SimpleMinCostFlow::Tail;
|
||||
%rename(getHead) operations_research::SimpleMinCostFlow::Head;
|
||||
%rename(getFlow) operations_research::SimpleMinCostFlow::Flow;
|
||||
%rename(getCapacity) operations_research::SimpleMinCostFlow::Capacity;
|
||||
%rename(getUnitCost) operations_research::SimpleMinCostFlow::UnitCost;
|
||||
%rename(getSupply) operations_research::SimpleMinCostFlow::Supply;
|
||||
|
||||
// Rename rules on SimpleLinearSumAssignment.
|
||||
%rename(addArcWithCost) operations_research::SimpleLinearSumAssignment::AddArcWithCost;
|
||||
%rename(getNumNodes) operations_research::SimpleLinearSumAssignment::NumNodes;
|
||||
%rename(getNumArcs) operations_research::SimpleLinearSumAssignment::NumArcs;
|
||||
%rename(getLeftNode) operations_research::SimpleLinearSumAssignment::LeftNode;
|
||||
%rename(getRightNode) operations_research::SimpleLinearSumAssignment::RightNode;
|
||||
%rename(getCost) operations_research::SimpleLinearSumAssignment::Cost;
|
||||
%rename(solve) operations_research::SimpleLinearSumAssignment::Solve;
|
||||
%rename(getOptimalCost) operations_research::SimpleLinearSumAssignment::OptimalCost;
|
||||
%rename(getRightMate) operations_research::SimpleLinearSumAssignment::RightMate;
|
||||
%rename(getAssignmentCost) operations_research::SimpleLinearSumAssignment::AssignmentCost;
|
||||
|
||||
#endif // SWIGJAVA
|
||||
|
||||
#if defined(SWIGCSHARP)
|
||||
// Rename rules on EbertGraph.
|
||||
%rename(EndArcIndex) operations_research::StarGraphBase::end_arc_index;
|
||||
%rename(EndNodeIndex) operations_research::StarGraphBase::end_node_index;
|
||||
%rename(MaxEndArcIndex) operations_research::StarGraphBase::max_end_arc_index;
|
||||
%rename(MaxEndNodeIndex) operations_research::StarGraphBase::max_end_node_index;
|
||||
%rename(MaxNumArcs) operations_research::StarGraphBase::max_num_arcs;
|
||||
%rename(MaxNumArcs) operations_research::StarGraphBase::max_num_arcs;
|
||||
%rename(MaxNumNodes) operations_research::StarGraphBase::max_num_nodes;
|
||||
%rename(MaxNumNodes) operations_research::StarGraphBase::max_num_nodes;
|
||||
%rename(NumArcs) operations_research::StarGraphBase::num_arcs;
|
||||
%rename(NumNodes) operations_research::StarGraphBase::num_nodes;
|
||||
|
||||
// Rename rules on MaxFlow.
|
||||
%rename (Graph) operations_research::MaxFlow::graph;
|
||||
%rename (GetStatus) operations_research::MaxFlow::status;
|
||||
|
||||
// Rename rules on MinCostFlow.
|
||||
%rename (Graph) operations_research::MinCostFlow::graph;
|
||||
%rename (GetStatus) operations_research::MinCostFlow::status;
|
||||
#endif // SWIGCSHARP
|
||||
|
||||
|
||||
// Use the SimpleLinearSumAssignment in SWIG (java and python).
|
||||
%rename(ComplexLinearSumAssignment) operations_research::LinearSumAssignment;
|
||||
|
||||
@@ -23,7 +23,6 @@
|
||||
#include "linear_solver/linear_solver_ext.h"
|
||||
%}
|
||||
|
||||
#ifdef SWIGPYTHON
|
||||
// Define the renaming of methods.
|
||||
%ignore MakeBoolVarArray;
|
||||
%ignore MakeIntVarArray;
|
||||
@@ -481,661 +480,6 @@ PY_PROTO_TYPEMAP(ortools.linear_solver.linear_solver2_pb2,
|
||||
PY_PROTO_TYPEMAP(ortools.linear_solver.linear_solver2_pb2,
|
||||
operations_research::new_proto::MPModelRequest,
|
||||
MPModelRequest);
|
||||
#endif // SWIGPYTHON
|
||||
|
||||
#ifdef SWIGJAVA
|
||||
|
||||
%module(directors="1") operations_research_solver;
|
||||
|
||||
%typemap(javaimports) SWIGTYPE %{
|
||||
import java.lang.reflect.*;
|
||||
%}
|
||||
|
||||
namespace operations_research {
|
||||
// Rename rules on MPVariable.
|
||||
%rename (basisStatus) MPVariable::basis_status;
|
||||
%rename (reducedCost) MPVariable::reduced_cost;
|
||||
%rename (setBounds) MPVariable::SetBounds;
|
||||
%rename (setInteger) MPVariable::SetInteger;
|
||||
%rename (setLb) MPVariable::SetLB;
|
||||
%rename (setUb) MPVariable::SetUB;
|
||||
%rename (solutionValue) MPVariable::solution_value;
|
||||
|
||||
// Rename rules on MPConstraint.
|
||||
%rename (basisStatus) MPConstraint::basis_status;
|
||||
%rename (dualValue) MPConstraint::dual_value;
|
||||
%rename (getCoefficient) MPConstraint::GetCoefficient;
|
||||
%rename (setBounds) MPConstraint::SetBounds;
|
||||
%rename (setCoefficient) MPConstraint::SetCoefficient;
|
||||
%rename (setLb) MPConstraint::SetLB;
|
||||
%rename (setUb) MPConstraint::SetUB;
|
||||
%rename (setIsLazy) MPConstraint::set_is_lazy;
|
||||
%rename (isLazy) MPConstraint::is_lazy;
|
||||
|
||||
// Rename rules on MPObjective.
|
||||
%rename (addOffset) MPObjective::AddOffset;
|
||||
%rename (bestBound) MPObjective::BestBound;
|
||||
%rename (clear) MPObjective::Clear;
|
||||
%rename (getCoefficient) MPObjective::GetCoefficient;
|
||||
%rename (setCoefficient) MPObjective::SetCoefficient;
|
||||
%rename (setMaximization) MPObjective::SetMaximization;
|
||||
%rename (setMinimization) MPObjective::SetMinimization;
|
||||
%rename (setOffset) MPObjective::SetOffset;
|
||||
%rename (setOptimizationDirection) MPObjective::SetOptimizationDirection;
|
||||
%rename (value) MPObjective::Value;
|
||||
|
||||
// Rename rules on MPSolverParameters.
|
||||
%rename (getDoubleParam) MPSolverParameters::GetDoubleParam;
|
||||
%rename (getIntegerParam) MPSolverParameters::GetIntegerParam;
|
||||
%rename (reset) MPSolverParameters::Reset;
|
||||
%rename (resetDoubleParam) MPSolverParameters::ResetDoubleParam;
|
||||
%rename (resetIntegerParam) MPSolverParameters::ResetIntegerParam;
|
||||
%rename (setDoubleParam) MPSolverParameters::SetDoubleParam;
|
||||
%rename (setIntegerParam) MPSolverParameters::SetIntegerParam;
|
||||
|
||||
// Rename rules on MPSolver.
|
||||
%rename (checkAllNamesValidity) MPSolver::CheckAllNamesValidity;
|
||||
%rename (checkNameValidity) MPSolver::CheckNameValidity;
|
||||
%rename (clear) MPConstraint::Clear;
|
||||
%rename (clear) MPObjective::Clear;
|
||||
%rename (clear) MPSolver::Clear;
|
||||
%rename (computeExactConditionNumber) MPSolver::ComputeExactConditionNumber;
|
||||
%rename (loadModelFromProto) MPSolver::LoadModelFromProto;
|
||||
%rename (lookupVariableOrNull) MPSolver::LookupVariableOrNull;
|
||||
%rename (lookupConstraintOrNull) MPSolver::LookupConstraintOrNull;
|
||||
%rename (makeBoolVar) MPSolver::MakeBoolVar;
|
||||
%rename (makeIntVar) MPSolver::MakeIntVar;
|
||||
%rename (makeNumVar) MPSolver::MakeNumVar;
|
||||
%rename (makeConstraint) MPSolver::MakeRowConstraint;
|
||||
%rename (makeVar) MPSolver::MakeVar;
|
||||
%rename (minimization) MPSolver::Minimization;
|
||||
%rename (numConstraints) MPSolver::NumConstraints;
|
||||
%rename (numVariables) MPSolver::NumVariables;
|
||||
%rename (objective) MPSolver::MutableObjective;
|
||||
%rename (reset) MPSolver::Reset;
|
||||
%rename (setMaximization) MPObjective::SetMaximization();
|
||||
%rename (setMinimization) MPObjective::SetMinimization();
|
||||
%rename (setCoefficient) MPObjective::SetCoefficient;
|
||||
%rename (setOffset) MPObjective::SetOffset;
|
||||
%rename (setOptimizationDirection) MPObjective::SetOptimizationDirection;
|
||||
%rename (setTimeLimit) MPSolver::set_time_limit;
|
||||
%rename (setWriteModelFilename) MPSolver::set_write_model_filename;
|
||||
%rename (solve) MPSolver::Solve;
|
||||
%rename (solverVersion) MPSolver::SolverVersion;
|
||||
%rename (suppressOutput) MPSolver::SuppressOutput;
|
||||
%rename (timeLimit) MPSolver::time_limit;
|
||||
%rename (wallTime) MPSolver::wall_time;
|
||||
%rename (writeModelFilename) MPSolver::write_model_filename;
|
||||
// Ignore non-mutable version of the objective accessor.
|
||||
%ignore MPSolver::Objective;
|
||||
// Ignore Make*VarArray: see replacement java code below.
|
||||
%ignore MPSolver::MakeVarArray;
|
||||
%ignore MPSolver::MakeNumVarArray;
|
||||
%ignore MPSolver::MakeIntVarArray;
|
||||
%ignore MPSolver::MakeBoolVarArray;
|
||||
// The following 6 methods that use protocol buffers as output
|
||||
// arguments are replaced by methods that return a protocol buffer,
|
||||
// see code below.
|
||||
%ignore MPSolver::ExportModelToNewProto;
|
||||
%ignore MPSolver::FillSolutionResponseProto;
|
||||
%ignore MPSolver::SolveWithProto;
|
||||
%ignore MPSolver::ExportModel;
|
||||
%ignore MPSolver::FillSolutionResponse;
|
||||
%ignore MPSolver::SolveWithProtocolBuffers;
|
||||
// Ignore export to string methods.
|
||||
%ignore operations_research::MPSolver::ExportModelAsLpFormat;
|
||||
%ignore operations_research::MPSolver::ExportModelAsMpsFormat;
|
||||
// Access to the underlying solver is available only in C++.
|
||||
%ignore MPSolver::underlying_solver;
|
||||
|
||||
|
||||
// Add java code on MPSolver.
|
||||
%typemap(javacode) MPSolver %{
|
||||
public MPVariable[] makeVarArray(int count,
|
||||
double lb,
|
||||
double ub,
|
||||
boolean integer) {
|
||||
MPVariable[] array = new MPVariable[count];
|
||||
for (int i = 0; i < count; ++i) {
|
||||
array[i] = makeVar(lb, ub, integer, "");
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public MPVariable[] makeVarArray(int count,
|
||||
double lb,
|
||||
double ub,
|
||||
boolean integer,
|
||||
String var_name) {
|
||||
MPVariable[] array = new MPVariable[count];
|
||||
for (int i = 0; i < count; ++i) {
|
||||
array[i] = makeVar(lb, ub, integer, var_name + i);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public MPVariable[] makeNumVarArray(int count, double lb, double ub) {
|
||||
return makeVarArray(count, lb, ub, false);
|
||||
}
|
||||
|
||||
public MPVariable[] makeNumVarArray(int count,
|
||||
double lb,
|
||||
double ub,
|
||||
String var_name) {
|
||||
return makeVarArray(count, lb, ub, false, var_name);
|
||||
}
|
||||
|
||||
public MPVariable[] makeIntVarArray(int count, double lb, double ub) {
|
||||
return makeVarArray(count, lb, ub, true);
|
||||
}
|
||||
|
||||
public MPVariable[] makeIntVarArray(int count,
|
||||
double lb,
|
||||
double ub,
|
||||
String var_name) {
|
||||
return makeVarArray(count, lb, ub, true, var_name);
|
||||
}
|
||||
|
||||
public MPVariable[] makeBoolVarArray(int count) {
|
||||
return makeVarArray(count, 0.0, 1.0, true);
|
||||
}
|
||||
|
||||
public MPVariable[] makeBoolVarArray(int count, String var_name) {
|
||||
return makeVarArray(count, 0.0, 1.0, true, var_name);
|
||||
}
|
||||
|
||||
public static final int getSolverEnum(String solver_name)
|
||||
throws java.lang.ClassNotFoundException,
|
||||
java.lang.NoSuchFieldException,
|
||||
java.lang.IllegalAccessException {
|
||||
Class c = Class.forName("com.google.ortools.linearsolver.MPSolver");
|
||||
Field field = c.getField(solver_name);
|
||||
return field.getInt(null);
|
||||
}
|
||||
%}
|
||||
|
||||
%extend MPSolver {
|
||||
std::string exportModelAsLpFormat(bool obfuscated) {
|
||||
std::string output;
|
||||
if (!self->ExportModelAsLpFormat(obfuscated, &output)) return "";
|
||||
return output;
|
||||
}
|
||||
|
||||
std::string exportModelAsMpsFormat(bool fixed_format, bool obfuscated) {
|
||||
std::string output;
|
||||
if (!self->ExportModelAsMpsFormat(fixed_format, obfuscated, &output)) {
|
||||
return "";
|
||||
}
|
||||
return output;
|
||||
}
|
||||
}
|
||||
} // namespace operations_research
|
||||
|
||||
#endif // SWIGJAVA
|
||||
|
||||
#if defined(SWIGCSHARP)
|
||||
|
||||
%module(directors="1") operations_research_linear_solver;
|
||||
|
||||
%typemap(csimports) SWIGTYPE %{
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Runtime.InteropServices;
|
||||
%}
|
||||
|
||||
namespace operations_research {
|
||||
// Use system infinity.
|
||||
%ignore MPSolver::infinity;
|
||||
// Automatic renaming to camel case.
|
||||
%rename("%(camelcase)s", %$isfunction) "";
|
||||
// Rename MakeRowConstraint into MakeConstraint
|
||||
%rename (MakeConstraint) MPSolver::MakeRowConstraint;
|
||||
// Rename classes, remove MP prefix.
|
||||
%rename (Solver) MPSolver;
|
||||
%rename (Variable) MPVariable;
|
||||
%rename (Constraint) MPConstraint;
|
||||
%rename (Objective) MPObjective;
|
||||
%rename (SolverParameters) MPSolverParameters;
|
||||
|
||||
// Ignore code on MPSolver, see replacement java code below.
|
||||
%ignore MPSolver::MakeVarArray;
|
||||
%ignore MPSolver::MakeNumVarArray;
|
||||
%ignore MPSolver::MakeIntVarArray;
|
||||
%ignore MPSolver::MakeBoolVarArray;
|
||||
// The following 3 methods that use protocol buffers as output
|
||||
// arguments are replaced by methods that return a protocol buffer,
|
||||
// see code below.
|
||||
%ignore MPSolver::ExportModel;
|
||||
%ignore MPSolver::ExportModelToNewProto;
|
||||
%ignore MPSolver::FillSolutionResponse;
|
||||
%ignore MPSolver::SolveWithProtocolBuffers;
|
||||
// Ignore Objective(), use MutableObjective() instead.
|
||||
%ignore MPSolver::Objective;
|
||||
%rename (Objective) MPSolver::MutableObjective;
|
||||
// Ignore export to string methods.
|
||||
%ignore operations_research::MPSolver::ExportModelAsLpFormat;
|
||||
%ignore operations_research::MPSolver::ExportModelAsMpsFormat;
|
||||
|
||||
%typemap(cscode) MPVariable %{
|
||||
public static LinearExpr operator+(Variable a, double v)
|
||||
{
|
||||
return new VarWrapper(a) + v;
|
||||
}
|
||||
|
||||
public static LinearExpr operator+(double v, Variable a)
|
||||
{
|
||||
return a + v;
|
||||
}
|
||||
|
||||
public static LinearExpr operator+(Variable a, LinearExpr b)
|
||||
{
|
||||
return new VarWrapper(a) + b;
|
||||
}
|
||||
|
||||
public static LinearExpr operator+(Variable a, Variable b)
|
||||
{
|
||||
return new VarWrapper(a) + new VarWrapper(b);
|
||||
}
|
||||
|
||||
public static LinearExpr operator+(LinearExpr a, Variable b)
|
||||
{
|
||||
return a + new VarWrapper(b);
|
||||
}
|
||||
|
||||
public static LinearExpr operator-(Variable a, double v)
|
||||
{
|
||||
return new VarWrapper(a) - v;
|
||||
}
|
||||
|
||||
public static LinearExpr operator-(double v, Variable a)
|
||||
{
|
||||
return v - new VarWrapper(a);
|
||||
}
|
||||
|
||||
public static LinearExpr operator-(Variable a, LinearExpr b)
|
||||
{
|
||||
return new VarWrapper(a) - b;
|
||||
}
|
||||
|
||||
public static LinearExpr operator-(LinearExpr a, Variable b)
|
||||
{
|
||||
return a - new VarWrapper(b);
|
||||
}
|
||||
|
||||
public static LinearExpr operator-(Variable a, Variable b)
|
||||
{
|
||||
return new VarWrapper(a) - new VarWrapper(b);
|
||||
}
|
||||
|
||||
public static LinearExpr operator-(Variable a)
|
||||
{
|
||||
return - new VarWrapper(a);
|
||||
}
|
||||
|
||||
public static LinearExpr operator*(Variable a, double v)
|
||||
{
|
||||
return new VarWrapper(a) * v;
|
||||
}
|
||||
|
||||
public static LinearExpr operator/(Variable a, double v)
|
||||
{
|
||||
return new VarWrapper(a) / v;
|
||||
}
|
||||
|
||||
public static LinearExpr operator*(double v, Variable a)
|
||||
{
|
||||
return v * new VarWrapper(a);
|
||||
}
|
||||
|
||||
public static RangeConstraint operator==(Variable a, double v)
|
||||
{
|
||||
return new VarWrapper(a) == v;
|
||||
}
|
||||
|
||||
public static RangeConstraint operator==(double v, Variable a)
|
||||
{
|
||||
return v == new VarWrapper(a);
|
||||
}
|
||||
|
||||
public static RangeConstraint operator!=(Variable a, double v)
|
||||
{
|
||||
return new VarWrapper(a) != v;
|
||||
}
|
||||
|
||||
public static RangeConstraint operator!=(double v, Variable a)
|
||||
{
|
||||
return new VarWrapper(a) != v;
|
||||
}
|
||||
|
||||
public static Equality operator==(Variable a, LinearExpr b)
|
||||
{
|
||||
return new VarWrapper(a) == b;
|
||||
}
|
||||
|
||||
public static Equality operator==(LinearExpr a, Variable b)
|
||||
{
|
||||
return a == new VarWrapper(b);
|
||||
}
|
||||
|
||||
public static VarEquality operator==(Variable a, Variable b)
|
||||
{
|
||||
return new VarEquality(a, b, true);
|
||||
}
|
||||
|
||||
public static Equality operator!=(Variable a, LinearExpr b)
|
||||
{
|
||||
return new VarWrapper(a) != b;
|
||||
}
|
||||
|
||||
public static Equality operator!=(LinearExpr a, Variable b)
|
||||
{
|
||||
return a != new VarWrapper(b);
|
||||
}
|
||||
|
||||
public static VarEquality operator!=(Variable a, Variable b)
|
||||
{
|
||||
return new VarEquality(a, b, false);
|
||||
}
|
||||
|
||||
public static RangeConstraint operator<=(Variable a, double v)
|
||||
{
|
||||
return new VarWrapper(a) <= v;
|
||||
}
|
||||
|
||||
public static RangeConstraint operator>=(Variable a, double v)
|
||||
{
|
||||
return new VarWrapper(a) >= v;
|
||||
}
|
||||
|
||||
public static RangeConstraint operator<=(double v, Variable a)
|
||||
{
|
||||
return new VarWrapper(a) >= v;
|
||||
}
|
||||
|
||||
public static RangeConstraint operator>=(double v, Variable a)
|
||||
{
|
||||
return new VarWrapper(a) <= v;
|
||||
}
|
||||
|
||||
public static RangeConstraint operator<=(Variable a, LinearExpr b)
|
||||
{
|
||||
return new VarWrapper(a) <= b;
|
||||
}
|
||||
|
||||
public static RangeConstraint operator>=(Variable a, LinearExpr b)
|
||||
{
|
||||
return new VarWrapper(a) >= b;
|
||||
}
|
||||
|
||||
public static RangeConstraint operator<=(Variable a, Variable b)
|
||||
{
|
||||
return new VarWrapper(a) <= new VarWrapper(b);
|
||||
}
|
||||
|
||||
public static RangeConstraint operator>=(Variable a, Variable b)
|
||||
{
|
||||
return new VarWrapper(a) >= new VarWrapper(b);
|
||||
}
|
||||
|
||||
public static RangeConstraint operator<=(LinearExpr a, Variable b)
|
||||
{
|
||||
return a <= new VarWrapper(b);
|
||||
}
|
||||
|
||||
public static RangeConstraint operator>=(LinearExpr a, Variable b)
|
||||
{
|
||||
return a >= new VarWrapper(b);
|
||||
}
|
||||
%}
|
||||
|
||||
%extend MPSolver {
|
||||
std::string ExportModelAsLpFormat(bool obfuscated) {
|
||||
std::string output;
|
||||
if (!self->ExportModelAsLpFormat(obfuscated, &output)) return "";
|
||||
return output;
|
||||
}
|
||||
|
||||
std::string ExportModelAsMpsFormat(bool fixed_format, bool obfuscated) {
|
||||
std::string output;
|
||||
if (!self->ExportModelAsMpsFormat(fixed_format, obfuscated, &output)) {
|
||||
return "";
|
||||
}
|
||||
return output;
|
||||
}
|
||||
}
|
||||
|
||||
// Add csharp code on Solver.
|
||||
%typemap(cscode) MPSolver %{
|
||||
public Variable[] MakeVarArray(int count,
|
||||
double lb,
|
||||
double ub,
|
||||
bool integer) {
|
||||
Variable[] array = new Variable[count];
|
||||
for (int i = 0; i < count; ++i) {
|
||||
array[i] = MakeVar(lb, ub, integer, "");
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public Variable[] MakeVarArray(int count,
|
||||
double lb,
|
||||
double ub,
|
||||
bool integer,
|
||||
string var_name) {
|
||||
Variable[] array = new Variable[count];
|
||||
for (int i = 0; i < count; ++i) {
|
||||
array[i] = MakeVar(lb, ub, integer, var_name + i);
|
||||
}
|
||||
return array;
|
||||
}
|
||||
|
||||
public Variable[,] MakeVarMatrix(int rows,
|
||||
int cols,
|
||||
double lb,
|
||||
double ub,
|
||||
bool integer) {
|
||||
Variable[,] matrix = new Variable[rows, cols];
|
||||
for (int i = 0; i < rows; ++i) {
|
||||
for (int j = 0; j < cols; ++j) {
|
||||
matrix[i,j] = MakeVar(lb, ub, integer, "");
|
||||
}
|
||||
}
|
||||
return matrix;
|
||||
}
|
||||
|
||||
public Variable[,] MakeVarMatrix(int rows,
|
||||
int cols,
|
||||
double lb,
|
||||
double ub,
|
||||
bool integer,
|
||||
string name) {
|
||||
Variable[,] matrix = new Variable[rows, cols];
|
||||
for (int i = 0; i < rows; ++i) {
|
||||
for (int j = 0; j < cols; ++j) {
|
||||
string var_name = name + "[" + i + ", " + j +"]";
|
||||
matrix[i,j] = MakeVar(lb, ub, integer, var_name);
|
||||
}
|
||||
}
|
||||
return matrix;
|
||||
}
|
||||
|
||||
public Variable[] MakeNumVarArray(int count, double lb, double ub) {
|
||||
return MakeVarArray(count, lb, ub, false);
|
||||
}
|
||||
|
||||
public Variable[] MakeNumVarArray(int count,
|
||||
double lb,
|
||||
double ub,
|
||||
string var_name) {
|
||||
return MakeVarArray(count, lb, ub, false, var_name);
|
||||
}
|
||||
|
||||
public Variable[,] MakeNumVarMatrix(int rows,
|
||||
int cols,
|
||||
double lb,
|
||||
double ub) {
|
||||
Variable[,] matrix = new Variable[rows, cols];
|
||||
for (int i = 0; i < rows; ++i) {
|
||||
for (int j = 0; j < cols; ++j) {
|
||||
matrix[i,j] = MakeNumVar(lb, ub, "");
|
||||
}
|
||||
}
|
||||
return matrix;
|
||||
}
|
||||
|
||||
public Variable[,] MakeNumVarMatrix(int rows,
|
||||
int cols,
|
||||
double lb,
|
||||
double ub,
|
||||
string name) {
|
||||
Variable[,] matrix = new Variable[rows, cols];
|
||||
for (int i = 0; i < rows; ++i) {
|
||||
for (int j = 0; j < cols; ++j) {
|
||||
string var_name = name + "[" + i + ", " + j +"]";
|
||||
matrix[i,j] = MakeNumVar(lb, ub, var_name);
|
||||
}
|
||||
}
|
||||
return matrix;
|
||||
}
|
||||
|
||||
public Variable[] MakeIntVarArray(int count, double lb, double ub) {
|
||||
return MakeVarArray(count, lb, ub, true);
|
||||
}
|
||||
|
||||
public Variable[] MakeIntVarArray(int count,
|
||||
double lb,
|
||||
double ub,
|
||||
string var_name) {
|
||||
return MakeVarArray(count, lb, ub, true, var_name);
|
||||
}
|
||||
|
||||
public Variable[,] MakeIntVarMatrix(int rows,
|
||||
int cols,
|
||||
double lb,
|
||||
double ub) {
|
||||
Variable[,] matrix = new Variable[rows, cols];
|
||||
for (int i = 0; i < rows; ++i) {
|
||||
for (int j = 0; j < cols; ++j) {
|
||||
matrix[i,j] = MakeIntVar(lb, ub, "");
|
||||
}
|
||||
}
|
||||
return matrix;
|
||||
}
|
||||
|
||||
public Variable[,] MakeIntVarMatrix(int rows,
|
||||
int cols,
|
||||
double lb,
|
||||
double ub,
|
||||
string name) {
|
||||
Variable[,] matrix = new Variable[rows, cols];
|
||||
for (int i = 0; i < rows; ++i) {
|
||||
for (int j = 0; j < cols; ++j) {
|
||||
string var_name = name + "[" + i + ", " + j +"]";
|
||||
matrix[i,j] = MakeIntVar(lb, ub, var_name);
|
||||
}
|
||||
}
|
||||
return matrix;
|
||||
}
|
||||
|
||||
public Variable[] MakeBoolVarArray(int count) {
|
||||
return MakeVarArray(count, 0.0, 1.0, true);
|
||||
}
|
||||
|
||||
public Variable[] MakeBoolVarArray(int count, string var_name) {
|
||||
return MakeVarArray(count, 0.0, 1.0, true, var_name);
|
||||
}
|
||||
|
||||
public Variable[,] MakeBoolVarMatrix(int rows, int cols) {
|
||||
Variable[,] matrix = new Variable[rows, cols];
|
||||
for (int i = 0; i < rows; ++i) {
|
||||
for (int j = 0; j < cols; ++j) {
|
||||
matrix[i,j] = MakeBoolVar("");
|
||||
}
|
||||
}
|
||||
return matrix;
|
||||
}
|
||||
|
||||
public Variable[,] MakeBoolVarMatrix(int rows, int cols, string name) {
|
||||
Variable[,] matrix = new Variable[rows, cols];
|
||||
for (int i = 0; i < rows; ++i) {
|
||||
for (int j = 0; j < cols; ++j) {
|
||||
string var_name = name + "[" + i + ", " + j +"]";
|
||||
matrix[i,j] = MakeBoolVar(var_name);
|
||||
}
|
||||
}
|
||||
return matrix;
|
||||
}
|
||||
|
||||
public static int GetSolverEnum(String solverType) {
|
||||
System.Reflection.FieldInfo fieldInfo =
|
||||
typeof(Solver).GetField(solverType);
|
||||
if (fieldInfo != null) {
|
||||
return (int)fieldInfo.GetValue(null);
|
||||
} else {
|
||||
throw new System.ApplicationException("Solver not supported");
|
||||
}
|
||||
}
|
||||
|
||||
public static Solver CreateSolver(String name, String type) {
|
||||
System.Reflection.FieldInfo fieldInfo =
|
||||
typeof(Solver).GetField(type);
|
||||
if (fieldInfo != null) {
|
||||
return new Solver(name, (int)fieldInfo.GetValue(null));
|
||||
} else {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public Constraint Add(LinearConstraint constraint) {
|
||||
return constraint.Extract(this);
|
||||
}
|
||||
|
||||
public void Minimize(LinearExpr expr)
|
||||
{
|
||||
Objective().Clear();
|
||||
Objective().SetMinimization();
|
||||
Dictionary<Variable, double> coefficients =
|
||||
new Dictionary<Variable, double>();
|
||||
double constant = expr.Visit(coefficients);
|
||||
foreach (KeyValuePair<Variable, double> pair in coefficients)
|
||||
{
|
||||
Objective().SetCoefficient(pair.Key, pair.Value);
|
||||
}
|
||||
Objective().SetOffset(constant);
|
||||
}
|
||||
|
||||
public void Maximize(LinearExpr expr)
|
||||
{
|
||||
Objective().Clear();
|
||||
Objective().SetMaximization();
|
||||
Dictionary<Variable, double> coefficients =
|
||||
new Dictionary<Variable, double>();
|
||||
double constant = expr.Visit(coefficients);
|
||||
foreach (KeyValuePair<Variable, double> pair in coefficients)
|
||||
{
|
||||
Objective().SetCoefficient(pair.Key, pair.Value);
|
||||
}
|
||||
Objective().SetOffset(constant);
|
||||
}
|
||||
|
||||
public void Minimize(Variable var)
|
||||
{
|
||||
Objective().Clear();
|
||||
Objective().SetMinimization();
|
||||
Objective().SetCoefficient(var, 1.0);
|
||||
}
|
||||
|
||||
public void Maximize(Variable var)
|
||||
{
|
||||
Objective().Clear();
|
||||
Objective().SetMaximization();
|
||||
Objective().SetCoefficient(var, 1.0);
|
||||
}
|
||||
%}
|
||||
|
||||
} // namespace operations_research
|
||||
|
||||
#endif // SWIGCSHARP
|
||||
|
||||
// Wrap linear_solver includes
|
||||
%include "linear_solver/linear_solver.h"
|
||||
|
||||
@@ -19,7 +19,6 @@
|
||||
using std::string;
|
||||
%}
|
||||
|
||||
#if defined(SWIGPYTHON)
|
||||
namespace operations_research {
|
||||
// ----- Callback Wrapping -----
|
||||
%{
|
||||
@@ -148,268 +147,3 @@ static bool PyCallbackBool(PyObject* pyfunc) {
|
||||
$1 = NewPermanentCallback(&PyCallbackBool, $input);
|
||||
}
|
||||
} // namespace operations_research
|
||||
#endif // SWIGPYTHON
|
||||
|
||||
#if defined(SWIGJAVA)
|
||||
|
||||
%{
|
||||
#include "base/jniutil.h"
|
||||
#include "base/scoped_ptr.h"
|
||||
%}
|
||||
|
||||
%module(directors="1") main
|
||||
%feature("director") LongResultCallback1;
|
||||
%feature("director") LongResultCallback2;
|
||||
%feature("director") LongResultCallback3;
|
||||
%include <std_vector.i>
|
||||
%{
|
||||
#include <vector>
|
||||
#include "base/callback.h"
|
||||
#include "base/integral_types.h"
|
||||
|
||||
// When a director is created for a class with SWIG, the C++ part of the
|
||||
// director keeps a JNI global reference to the Java part. This global reference
|
||||
// only gets deleted in the destructor of the C++ part, but by default, this
|
||||
// only happens when the Java part is processed by the GC (however, this never
|
||||
// happens, because there is the JNI global reference...).
|
||||
//
|
||||
// To break the cycle, it is necessary to delete the C++ part manually. For the
|
||||
// callback classes, this is done by deriving them from the respective C++
|
||||
// ResultCallback classes. When the java callback class is asked for a C++
|
||||
// callback class, it hands over its C++ part. It is expected, that whoever
|
||||
// receives the C++ callback class, owns it and destroys it after they no longer
|
||||
// need it. But by destroying it, they also break the reference cycle and the
|
||||
// Java part may be processed by the GC.
|
||||
//
|
||||
// However, this behavior also means that the callback class can only be used
|
||||
// in one context and that if its C++ callback class is not received by someone
|
||||
// who destroys it in the end, it will stay in memory forever.
|
||||
class LongResultCallback1 : private ResultCallback1<int64, int64> {
|
||||
public:
|
||||
LongResultCallback1() : used_as_permanent_handler_(false) {}
|
||||
virtual int64 run(int64) = 0;
|
||||
ResultCallback1<int64, int64>* GetPermanentCallback() {
|
||||
CHECK(!used_as_permanent_handler_);
|
||||
used_as_permanent_handler_ = true;
|
||||
return this;
|
||||
}
|
||||
virtual ~LongResultCallback1() {}
|
||||
private:
|
||||
virtual bool IsRepeatable() const { return true; }
|
||||
|
||||
virtual int64 Run(int64 i) {
|
||||
return run(i);
|
||||
}
|
||||
bool used_as_permanent_handler_;
|
||||
};
|
||||
|
||||
class LongResultCallback2 : private ResultCallback2<int64, int64, int64> {
|
||||
public:
|
||||
LongResultCallback2() : used_as_permanent_handler_(false) {}
|
||||
virtual ~LongResultCallback2() {}
|
||||
virtual int64 run(int64, int64) = 0;
|
||||
ResultCallback2<int64, int64, int64>* GetPermanentCallback() {
|
||||
CHECK(!used_as_permanent_handler_);
|
||||
used_as_permanent_handler_ = true;
|
||||
return this;
|
||||
}
|
||||
private:
|
||||
virtual bool IsRepeatable() const { return true; }
|
||||
|
||||
virtual int64 Run(int64 i, int64 j) {
|
||||
return run(i, j);
|
||||
}
|
||||
bool used_as_permanent_handler_;
|
||||
};
|
||||
|
||||
class LongResultCallback3 : private ResultCallback3<int64, int64, int64, int64> {
|
||||
public:
|
||||
LongResultCallback3() : used_as_permanent_handler_(false) {}
|
||||
virtual ~LongResultCallback3() {}
|
||||
virtual int64 run(int64, int64, int64) = 0;
|
||||
ResultCallback3<int64, int64, int64, int64>* GetPermanentCallback() {
|
||||
CHECK(!used_as_permanent_handler_);
|
||||
used_as_permanent_handler_ = true;
|
||||
return this;
|
||||
}
|
||||
private:
|
||||
virtual bool IsRepeatable() const { return true; }
|
||||
|
||||
virtual int64 Run(int64 i, int64 j, int64 k) {
|
||||
return run(i, j, k);
|
||||
}
|
||||
bool used_as_permanent_handler_;
|
||||
};
|
||||
|
||||
%}
|
||||
|
||||
class LongResultCallback1 : private ResultCallback1<int64, int64> {
|
||||
public:
|
||||
virtual int64 run(int64) = 0;
|
||||
ResultCallback1<int64, int64>* GetPermanentCallback();
|
||||
virtual ~LongResultCallback1();
|
||||
private:
|
||||
virtual bool IsRepeatable() const;
|
||||
virtual int64 Run(int64 i);
|
||||
bool used_as_permanent_handler_;
|
||||
};
|
||||
class LongResultCallback2 : private ResultCallback2<int64, int64, int64> {
|
||||
public:
|
||||
virtual int64 run(int64, int64) = 0;
|
||||
ResultCallback2<int64, int64, int64>* GetPermanentCallback();
|
||||
virtual ~LongResultCallback2();
|
||||
private:
|
||||
virtual bool IsRepeatable() const;
|
||||
virtual int64 Run(int64 i, int64 j);
|
||||
bool used_as_permanent_handler_;
|
||||
};
|
||||
class LongResultCallback3 : private ResultCallback3<int64, int64, int64, int64> {
|
||||
public:
|
||||
virtual int64 run(int64, int64, int64) = 0;
|
||||
ResultCallback3<int64, int64, int64, int64>* GetPermanentCallback();
|
||||
virtual ~LongResultCallback3();
|
||||
private:
|
||||
virtual bool IsRepeatable() const;
|
||||
virtual int64 Run(int64 i, int64 j, int64 k);
|
||||
bool used_as_permanent_handler_;
|
||||
};
|
||||
|
||||
// Typemaps for callbacks in java.
|
||||
%typemap(jstype) ResultCallback1<int64, int64>* "LongResultCallback1";
|
||||
%typemap(javain) ResultCallback1<int64, int64>* "$descriptor(ResultCallback1<int64, int64>*).getCPtr($javainput.GetPermanentCallback())";
|
||||
%typemap(jstype) ResultCallback2<int64, int64, int64>* "LongResultCallback2";
|
||||
%typemap(javain) ResultCallback2<int64, int64, int64>* "$descriptor(ResultCallback2<int64, int64, int64>*).getCPtr($javainput.GetPermanentCallback())";
|
||||
|
||||
%typemap(jstype) ResultCallback3<int64, int64, int64, int64>*
|
||||
"LongResultCallback3";
|
||||
%typemap(javain) ResultCallback3<int64, int64, int64, int64>*
|
||||
"$descriptor(ResultCallback3<int64, int64, int64, int64>*).getCPtr($javainput.GetPermanentCallback())";
|
||||
#endif // SWIGJAVA
|
||||
|
||||
#if defined(SWIGCSHARP)
|
||||
%include base/base.swig
|
||||
|
||||
%{
|
||||
#include <vector>
|
||||
#include "base/callback.h"
|
||||
#include "base/integral_types.h"
|
||||
%}
|
||||
|
||||
%module(directors="1") main
|
||||
%feature("director") LongResultCallback1;
|
||||
%feature("director") LongResultCallback2;
|
||||
%feature("director") LongResultCallback3;
|
||||
%{
|
||||
#include <vector>
|
||||
#include "base/callback.h"
|
||||
#include "base/integral_types.h"
|
||||
|
||||
class LongResultCallback1 : public ResultCallback1<int64, int64> {
|
||||
public:
|
||||
LongResultCallback1() : used_as_permanent_handler_(false) {}
|
||||
virtual ~LongResultCallback1() {}
|
||||
virtual int64 Run(int64 i) = 0;
|
||||
ResultCallback1<int64, int64>* GetPermanentCallback() {
|
||||
CHECK(!used_as_permanent_handler_);
|
||||
used_as_permanent_handler_ = true;
|
||||
return this;
|
||||
}
|
||||
private:
|
||||
virtual bool IsRepeatable() const { return true; }
|
||||
|
||||
bool used_as_permanent_handler_;
|
||||
};
|
||||
|
||||
class LongResultCallback2 : public ResultCallback2<int64, int64, int64> {
|
||||
public:
|
||||
LongResultCallback2() : used_as_permanent_handler_(false) {}
|
||||
virtual ~LongResultCallback2() {}
|
||||
virtual int64 Run(int64, int64) = 0;
|
||||
ResultCallback2<int64, int64, int64>* GetPermanentCallback() {
|
||||
CHECK(!used_as_permanent_handler_);
|
||||
used_as_permanent_handler_ = true;
|
||||
return this;
|
||||
}
|
||||
private:
|
||||
virtual bool IsRepeatable() const { return true; }
|
||||
|
||||
bool used_as_permanent_handler_;
|
||||
};
|
||||
|
||||
class LongResultCallback3 : public ResultCallback3<int64, int64, int64, int64> {
|
||||
public:
|
||||
LongResultCallback3() : used_as_permanent_handler_(false) {}
|
||||
virtual ~LongResultCallback3() {}
|
||||
virtual int64 Run(int64, int64, int64) = 0;
|
||||
ResultCallback3<int64, int64, int64, int64>* GetPermanentCallback() {
|
||||
CHECK(!used_as_permanent_handler_);
|
||||
used_as_permanent_handler_ = true;
|
||||
return this;
|
||||
}
|
||||
private:
|
||||
virtual bool IsRepeatable() const { return true; }
|
||||
|
||||
bool used_as_permanent_handler_;
|
||||
};
|
||||
|
||||
%}
|
||||
|
||||
%typemap(cscode) LongResultCallback1 %{
|
||||
public SWIGTYPE_p_ResultCallback1T_long_long_long_long_t DisownAndGetPermanentCallback() {
|
||||
swigCMemOwn = false;
|
||||
return GetPermanentCallback();
|
||||
}
|
||||
%}
|
||||
|
||||
%typemap(cscode) LongResultCallback2 %{
|
||||
public SWIGTYPE_p_ResultCallback2T_long_long_long_long_long_long_t DisownAndGetPermanentCallback() {
|
||||
swigCMemOwn = false;
|
||||
return GetPermanentCallback();
|
||||
}
|
||||
%}
|
||||
|
||||
%typemap(cscode) LongResultCallback3 %{
|
||||
public SWIGTYPE_p_ResultCallback3T_long_long_long_long_long_long_long_long_t DisownAndGetPermanentCallback() {
|
||||
swigCMemOwn = false;
|
||||
return GetPermanentCallback();
|
||||
}
|
||||
%}
|
||||
|
||||
class LongResultCallback1 : private ResultCallback1<int64, int64> {
|
||||
public:
|
||||
virtual int64 Run(int64 i) = 0;
|
||||
ResultCallback1<int64, int64>* GetPermanentCallback();
|
||||
virtual ~LongResultCallback1();
|
||||
private:
|
||||
virtual bool IsRepeatable() const;
|
||||
bool used_as_permanent_handler_;
|
||||
};
|
||||
class LongResultCallback2 : private ResultCallback2<int64, int64, int64> {
|
||||
public:
|
||||
virtual int64 Run(int64, int64) = 0;
|
||||
ResultCallback2<int64, int64, int64>* GetPermanentCallback();
|
||||
virtual ~LongResultCallback2();
|
||||
private:
|
||||
virtual bool IsRepeatable() const;
|
||||
bool used_as_permanent_handler_;
|
||||
};
|
||||
class LongResultCallback3 : private ResultCallback3<int64, int64, int64, int64> {
|
||||
public:
|
||||
virtual int64 Run(int64, int64, int64) = 0;
|
||||
ResultCallback3<int64, int64, int64, int64>* GetPermanentCallback();
|
||||
virtual ~LongResultCallback3();
|
||||
private:
|
||||
virtual bool IsRepeatable() const;
|
||||
bool used_as_permanent_handler_;
|
||||
};
|
||||
|
||||
// Typemaps for callbacks in csharp.
|
||||
%typemap(cstype) ResultCallback1<int64, int64>* "LongResultCallback1";
|
||||
%typemap(csin) ResultCallback1<int64, int64>* "$descriptor(ResultCallback1<int64, int64>*).getCPtr($csinput.DisownAndGetPermanentCallback())";
|
||||
%typemap(cstype) ResultCallback2<int64, int64, int64>* "LongResultCallback2";
|
||||
%typemap(csin) ResultCallback2<int64, int64, int64>* "$descriptor(ResultCallback2<int64, int64, int64>*).getCPtr($csinput.DisownAndGetPermanentCallback())";
|
||||
%typemap(cstype) ResultCallback3<int64, int64, int64, int64>*
|
||||
"LongResultCallback3";
|
||||
%typemap(csin) ResultCallback3<int64, int64, int64, int64>*
|
||||
"$descriptor(ResultCallback3<int64, int64, int64, int64>*).getCPtr($csinput.DisownAndGetPermanentCallback())";
|
||||
#endif // SWIGCSHARP
|
||||
|
||||
@@ -18,7 +18,6 @@
|
||||
using std::string;
|
||||
%}
|
||||
|
||||
#if defined(SWIGPYTHON)
|
||||
%{
|
||||
inline int PyIntOrLong_Check(PyObject* obj) {
|
||||
return PyInt_Check(obj) || PyLong_Check(obj);
|
||||
@@ -246,433 +245,3 @@ PY_LIST_OUTPUT_TYPEMAP(double, PyFloat_Check, PyFloat_FromDouble);
|
||||
%enddef // PY_PROTO_TYPEMAP
|
||||
|
||||
} // namespace operations_research
|
||||
#endif // SWIGPYTHON
|
||||
|
||||
#if defined(SWIGJAVA)
|
||||
|
||||
%{
|
||||
#include "base/jniutil.h"
|
||||
#include "base/scoped_ptr.h"
|
||||
%}
|
||||
|
||||
%include <std_vector.i>
|
||||
%{
|
||||
#include <vector>
|
||||
#include "base/integral_types.h"
|
||||
%}
|
||||
|
||||
// Typemaps to represent const std::vector<CType>& arguments as arrays of JavaType.
|
||||
%define VECTOR_AS_JAVA_ARRAY(CType, JavaType, JavaTypeName)
|
||||
%typemap(jni) const std::vector<CType>& "j" #JavaType "Array"
|
||||
%typemap(jtype) const std::vector<CType>& #JavaType "[]"
|
||||
%typemap(jstype) const std::vector<CType>& #JavaType "[]"
|
||||
%typemap(javain) const std::vector<CType>& "$javainput"
|
||||
%typemap(in) const std::vector<CType>& %{
|
||||
if($input) {
|
||||
$1 = new std::vector<CType>;
|
||||
const int size = jenv->GetArrayLength($input);
|
||||
$1->reserve(size);
|
||||
j ## JavaType *values = jenv->Get ## JavaTypeName ## ArrayElements((j ## JavaType ## Array)$input, NULL);
|
||||
for (int i = 0; i < size; ++i) {
|
||||
JavaType value = values[i];
|
||||
$1->emplace_back(value);
|
||||
}
|
||||
jenv->Release ## JavaTypeName ## ArrayElements((j ## JavaType ## Array)$input, values, JNI_ABORT);
|
||||
}
|
||||
else {
|
||||
SWIG_JavaThrowException(jenv, SWIG_JavaNullPointerException, "null table");
|
||||
return $null;
|
||||
}
|
||||
%}
|
||||
%typemap(freearg) const std::vector<CType>& {
|
||||
delete $1;
|
||||
}
|
||||
%typemap(out) const std::vector<CType>& %{
|
||||
$result = jenv->New ## JavaTypeName ## Array($1->size());
|
||||
jenv->Set ## JavaTypeName ## ArrayRegion(
|
||||
$result, 0, $1->size(), reinterpret_cast<j ## CType*>(&(*$1)[0]));
|
||||
%}
|
||||
%typemap(javaout) const std::vector<CType> & {
|
||||
return $jnicall;
|
||||
}
|
||||
%enddef
|
||||
VECTOR_AS_JAVA_ARRAY(int, int, Int);
|
||||
VECTOR_AS_JAVA_ARRAY(int64, long, Long);
|
||||
|
||||
// Convert long[][] to std::vector<std::vector<int64> >
|
||||
%typemap(jni) const std::vector<std::vector<int64> >& "jobjectArray"
|
||||
%typemap(jtype) const std::vector<std::vector<int64> >& "long[][]"
|
||||
%typemap(jstype) const std::vector<std::vector<int64> >& "long[][]"
|
||||
%typemap(javain) const std::vector<std::vector<int64> >& "$javainput"
|
||||
%typemap(in) const std::vector<std::vector<int64> >& (std::vector<std::vector<int64> > result) {
|
||||
const int size = jenv->GetArrayLength($input);
|
||||
result.clear();
|
||||
result.resize(size);
|
||||
for (int index1 = 0; index1 < size; ++index1) {
|
||||
jlongArray inner_array =
|
||||
(jlongArray)jenv->GetObjectArrayElement($input, index1);
|
||||
const int tuple_size = jenv->GetArrayLength(inner_array);
|
||||
result[index1].reserve(tuple_size);
|
||||
jlong* const values =
|
||||
jenv->GetLongArrayElements((jlongArray)inner_array, NULL);
|
||||
for (int index2 = 0; index2 < tuple_size; ++index2) {
|
||||
const int64 value = values[index2];
|
||||
result[index1].emplace_back(value);
|
||||
}
|
||||
jenv->ReleaseLongArrayElements((jlongArray)inner_array, values, JNI_ABORT);
|
||||
jenv->DeleteLocalRef(inner_array);
|
||||
}
|
||||
$1 = &result;
|
||||
}
|
||||
|
||||
// SWIG macros to be used in generating Java wrappers for C++ protocol
|
||||
// message parameters. Each protocol message is serialized into
|
||||
// byte[] before passing into (or returning from) C++ code.
|
||||
|
||||
// If the C++ function expects an input protocol message:
|
||||
// foo(const MyProto* message,...)
|
||||
// Use PROTO_INPUT macro:
|
||||
// PROTO_INPUT(MyProto, com.google.proto.protos.test.MyProto, message)
|
||||
//
|
||||
// if the C++ function returns a protocol message:
|
||||
// MyProto* foo();
|
||||
// Use PROTO2_RETURN macro:
|
||||
// PROTO2_RETURN(MyProto, com.google.proto.protos.test.MyProto, giveOwnership)
|
||||
// -> the 'giveOwnership' parameter should be true iff the C++ function
|
||||
// returns a new proto which should be deleted by the client.
|
||||
|
||||
// Passing each protocol message from Java to C++ by value. Each ProtocolMessage
|
||||
// is serialized into byte[] when it is passed from Java to C++, the C++ code
|
||||
// deserializes into C++ native protocol message.
|
||||
//
|
||||
// @param CppProtoType the fully qualified C++ protocol message type
|
||||
// @param JavaProtoType the corresponding fully qualified Java protocol message
|
||||
// type
|
||||
// @param param_name the parameter name
|
||||
%define PROTO_INPUT(CppProtoType, JavaProtoType, param_name)
|
||||
%typemap(jni) PROTO_TYPE* INPUT, PROTO_TYPE& INPUT "jbyteArray"
|
||||
%typemap(jtype) PROTO_TYPE* INPUT, PROTO_TYPE& INPUT "byte[]"
|
||||
%typemap(jstype) PROTO_TYPE* INPUT, PROTO_TYPE& INPUT "JavaProtoType"
|
||||
%typemap(javain) PROTO_TYPE* INPUT, PROTO_TYPE& INPUT "$javainput.toByteArray()"
|
||||
%typemap(in) PROTO_TYPE* INPUT (CppProtoType temp),
|
||||
PROTO_TYPE& INPUT (CppProtoType temp) {
|
||||
int proto_size = 0;
|
||||
std::unique_ptr<char[]> proto_buffer(
|
||||
JNIUtil::MakeCharArray(jenv, $input, &proto_size));
|
||||
bool parsed_ok = temp.ParseFromArray(proto_buffer.get(), proto_size);
|
||||
if (!parsed_ok) {
|
||||
SWIG_JavaThrowException(jenv,
|
||||
SWIG_JavaRuntimeException,
|
||||
"Unable to parse CppProtoType protocol message.");
|
||||
}
|
||||
$1 = &temp;
|
||||
}
|
||||
|
||||
%apply PROTO_TYPE& INPUT { const CppProtoType& param_name }
|
||||
%apply PROTO_TYPE& INPUT { CppProtoType& param_name }
|
||||
%apply PROTO_TYPE* INPUT { const CppProtoType* param_name }
|
||||
%apply PROTO_TYPE* INPUT { CppProtoType* param_name }
|
||||
|
||||
%enddef // end PROTO_INPUT
|
||||
|
||||
%define PROTO2_RETURN(CppProtoType, JavaProtoType, giveOwnership)
|
||||
%typemap(jni) CppProtoType* "jbyteArray"
|
||||
%typemap(jtype) CppProtoType* "byte[]"
|
||||
%typemap(jstype) CppProtoType* "JavaProtoType"
|
||||
%typemap(javaout) CppProtoType* {
|
||||
byte[] buf = $jnicall;
|
||||
if (buf == null || buf.length == 0) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return JavaProtoType.parseFrom(buf);
|
||||
} catch (com.google.protobuf.InvalidProtocolBufferException e) {
|
||||
throw new RuntimeException(
|
||||
"Unable to parse JavaProtoType protocol message.");
|
||||
}
|
||||
}
|
||||
%typemap(out) CppProtoType* {
|
||||
std::unique_ptr<char[]> buf(new char[$1->ByteSize()]);
|
||||
$1->SerializeWithCachedSizesToArray(reinterpret_cast<uint8*>(buf.get()));
|
||||
$result = JNIUtil::MakeJByteArray(jenv, buf.get(), $1->ByteSize());
|
||||
if (giveOwnership) {
|
||||
// To prevent a memory leak.
|
||||
delete $1;
|
||||
$1 = NULL;
|
||||
}
|
||||
}
|
||||
%enddef // end PROTO2_RETURN
|
||||
|
||||
#endif // SWIGJAVA
|
||||
|
||||
#if defined(SWIGCSHARP)
|
||||
%include base/base.swig
|
||||
|
||||
////////////////////////////////////////////////
|
||||
//
|
||||
// CS_TYPEMAP_STDVECTOR
|
||||
//
|
||||
// Map c# arrays to c++ vectors for POD types.
|
||||
//
|
||||
////////////////////////////////////////////////
|
||||
|
||||
// This typemap will transform a .net array into a pair length, c array.
|
||||
// This pair is then used to rebuild a vector from it.
|
||||
%define CS_TYPEMAP_STDVECTOR(TYPE, CTYPE, CSHARPTYPE)
|
||||
|
||||
%typemap(ctype) const std::vector<TYPE>& %{ int length$argnum, CTYPE* %}
|
||||
%typemap(imtype) const std::vector<TYPE>& %{ int length$argnum, CSHARPTYPE[] %}
|
||||
%typemap(cstype) const std::vector<TYPE>& %{ CSHARPTYPE[] %}
|
||||
%typemap(csin) const std::vector<TYPE>& "$csinput.Length, $csinput"
|
||||
%typemap(freearg) const std::vector<TYPE>& { delete $1; }
|
||||
%typemap(in) const std::vector<TYPE>& %{
|
||||
$1 = new std::vector<TYPE>;
|
||||
$1->reserve(length$argnum);
|
||||
for(int i = 0; i < length$argnum; ++i)
|
||||
$1->emplace_back($input[i]);
|
||||
%}
|
||||
%enddef // CS_TYPEMAP_STDVECTOR
|
||||
|
||||
////////////////////////////////////////////////
|
||||
//
|
||||
// CS_TYPEMAP_STDVECTOR_IN1
|
||||
//
|
||||
// Map c# bi-dimensional arrays to c++ vectors of vectors for POD types.
|
||||
//
|
||||
////////////////////////////////////////////////
|
||||
|
||||
// This typemap will transform a multidimensional array into a triplet
|
||||
// size_z, size_y, flattened array. This is then recomposed in the
|
||||
// final part to rebuild the std::vector<std::vector<T>> part.
|
||||
%define CS_TYPEMAP_STDVECTOR_IN1(TYPE, CTYPE, CSHARPTYPE)
|
||||
%typemap(ctype) const std::vector<std::vector<TYPE> >& %{ int len$argnum_1, int len$argnum_2, CTYPE* %}
|
||||
%typemap(imtype) const std::vector<std::vector<TYPE> >& %{ int len$argnum_1, int len$argnum_2, CSHARPTYPE[] %}
|
||||
%typemap(cstype) const std::vector<std::vector<TYPE> >& %{ CSHARPTYPE[,] %}
|
||||
%typemap(csin) const std::vector<std::vector<TYPE> >& "$csinput.GetLength(0), $csinput.GetLength(1), Google.OrTools.Util.NestedArrayHelper.GetFlatArray($csinput)"
|
||||
%typemap(in) const std::vector<std::vector<TYPE> >& (std::vector<std::vector<TYPE> > result) %{
|
||||
|
||||
const int size_x = len$argnum_1;
|
||||
const int size_y = len$argnum_2;
|
||||
|
||||
result.clear();
|
||||
result.resize(size_x);
|
||||
|
||||
TYPE* inner_array = reinterpret_cast<TYPE*>($input);
|
||||
|
||||
for (int index1 = 0; index1 < size_x; ++index1) {
|
||||
result[index1].reserve(size_y);
|
||||
for (int index2 = 0; index2 < size_y; ++index2) {
|
||||
const TYPE value = inner_array[index1 * size_y + index2];
|
||||
result[index1].emplace_back(value);
|
||||
}
|
||||
}
|
||||
|
||||
$1 = &result;
|
||||
%}
|
||||
%enddef // CS_TYPEMAP_STDVECTOR_IN1
|
||||
|
||||
////////////////////////////////////////////////
|
||||
//
|
||||
// CS_TYPEMAP_PTRARRAY
|
||||
//
|
||||
////////////////////////////////////////////////
|
||||
|
||||
// This typemap will perform the same transformation for an array of object.
|
||||
// The result is an vector of the C objects.
|
||||
%define CS_TYPEMAP_PTRARRAY(CTYPE, TYPE)
|
||||
|
||||
%typemap(cscode) CTYPE %{
|
||||
public static IntPtr[] getCPtr(TYPE[] arr) {
|
||||
IntPtr[] pointers = new IntPtr[arr.Length];
|
||||
for (int i = 0; i < arr.Length; i++) {
|
||||
pointers[i] = (IntPtr)TYPE.getCPtr(arr[i]);
|
||||
}
|
||||
return pointers;
|
||||
}
|
||||
%}
|
||||
|
||||
%typemap(ctype) CTYPE** "CTYPE**"
|
||||
|
||||
%typemap(imtype,
|
||||
inattributes="[In, Out, MarshalAs(UnmanagedType.LPArray)]",
|
||||
outattributes="[return: MarshalAs(UnmanagedType.LPArray)]")
|
||||
CTYPE** "IntPtr[]"
|
||||
|
||||
%typemap(cstype) CTYPE** "TYPE[]"
|
||||
%typemap(csin) CTYPE** "TYPE.getCPtr($csinput)"
|
||||
%typemap(in) CTYPE** "$1 = $input;"
|
||||
%typemap(freearg) CTYPE** ""
|
||||
%typemap(argout) CTYPE** ""
|
||||
|
||||
%enddef // CS_TYPEMAP_PTRARRAY
|
||||
|
||||
////////////////////////////////////////////////
|
||||
//
|
||||
// CS_TYPEMAP_STDVECTOR_OBJECT
|
||||
//
|
||||
// Map c# arrays to c++ vectors for swiged C++ objects.
|
||||
//
|
||||
////////////////////////////////////////////////
|
||||
|
||||
%define CS_TYPEMAP_STDVECTOR_OBJECT(CTYPE, TYPE)
|
||||
|
||||
%typemap(cscode) CTYPE %{
|
||||
public static IntPtr[] getCPtr(TYPE[] arr) {
|
||||
IntPtr[] pointers = new IntPtr[arr.Length];
|
||||
for (int i = 0; i < arr.Length; i++)
|
||||
pointers[i] = (IntPtr)TYPE.getCPtr(arr[i]);
|
||||
return pointers;
|
||||
}
|
||||
%}
|
||||
|
||||
%typemap(ctype) const std::vector<CTYPE*>& "int length$argnum, CTYPE**"
|
||||
%typemap(imtype) const std::vector<CTYPE*>& "int length$argnum, IntPtr[]"
|
||||
%typemap(cstype) const std::vector<CTYPE*>& "TYPE[]"
|
||||
%typemap(csin) const std::vector<CTYPE*>& "$csinput.Length, TYPE.getCPtr($csinput)"
|
||||
%typemap(in) const std::vector<CTYPE*>& (std::vector<CTYPE*> result) {
|
||||
result.reserve(length$argnum);
|
||||
for (int i = 0; i < length$argnum; i++) {
|
||||
result.emplace_back($input[i]);
|
||||
}
|
||||
$1 = &result;
|
||||
}
|
||||
%enddef // CS_TYPEMAP_STDVECTOR_OBJECT
|
||||
|
||||
%{
|
||||
#include <vector>
|
||||
#include "base/integral_types.h"
|
||||
%}
|
||||
|
||||
%{
|
||||
#include <vector>
|
||||
#include "base/integral_types.h"
|
||||
%}
|
||||
|
||||
CS_TYPEMAP_STDVECTOR(int64, int64, long)
|
||||
CS_TYPEMAP_STDVECTOR(int, int, int)
|
||||
CS_TYPEMAP_STDVECTOR_IN1(int64, int64, long)
|
||||
CS_TYPEMAP_STDVECTOR_IN1(int, int, int)
|
||||
|
||||
// SWIG macros to be used in generating C# wrappers for C++ protocol
|
||||
// message parameters. Each protocol message is serialized into
|
||||
// byte[] before passing into (or returning from) C++ code.
|
||||
|
||||
// If the C++ function expects an input protocol message, transferring
|
||||
// ownership to the caller (in C++):
|
||||
// foo(const MyProto* message,...)
|
||||
// Use PROTO_INPUT macro:
|
||||
// PROTO_INPUT(MyProto, Google.Proto.Protos.Test.MyProto, message)
|
||||
//
|
||||
// if the C++ function returns a protocol message:
|
||||
// MyProto* foo();
|
||||
// Use PROTO2_RETURN macro:
|
||||
// PROTO2_RETURN(MyProto, Google.Proto.Protos.Test.MyProto, true)
|
||||
//
|
||||
// Replace true by false if the C++ function returns a pointer to a
|
||||
// protocol message object whose ownership is not transferred to the
|
||||
// (C++) caller.
|
||||
//
|
||||
// Passing each protocol message from C# to C++ by value. Each ProtocolMessage
|
||||
// is serialized into byte[] when it is passed from C# to C++, the C++ code
|
||||
// deserializes into C++ native protocol message.
|
||||
//
|
||||
// @param CppProtoType the fully qualified C++ protocol message type
|
||||
// @param CSharpProtoType the corresponding fully qualified C# protocol message
|
||||
// type
|
||||
// @param param_name the parameter name
|
||||
// @param deleteCppReturn indicates that the resulting object is a native
|
||||
// (java, c#, python) object, and thus the C++ proto can be safely deleted
|
||||
// after the conversion.
|
||||
%define PROTO_INPUT(CppProtoType, CSharpProtoType, param_name)
|
||||
%typemap(ctype) PROTO_TYPE* INPUT, PROTO_TYPE& INPUT "int proto_size, char*"
|
||||
%typemap(imtype) PROTO_TYPE* INPUT, PROTO_TYPE& INPUT "int proto_size, byte[]"
|
||||
%typemap(cstype) PROTO_TYPE* INPUT, PROTO_TYPE& INPUT "CSharpProtoType"
|
||||
%typemap(csin) PROTO_TYPE* INPUT, PROTO_TYPE& INPUT "$csinput.GetByteArrayLength(), $csinput.ToByteArray()"
|
||||
%typemap(in) PROTO_TYPE* INPUT (CppProtoType temp), PROTO_TYPE& INPUT (CppProtoType temp) {
|
||||
int proto_size = 0;
|
||||
std::unique_ptr<char[]> proto_buffer($input);
|
||||
bool parsed_ok = temp.ParseFromArray(proto_buffer.get(), proto_size);
|
||||
if (!parsed_ok) {
|
||||
SWIG_CSharpSetPendingException(
|
||||
SWIG_CSharpSystemException,
|
||||
"Unable to parse CppProtoType protocol message.");
|
||||
}
|
||||
$1 = &temp;
|
||||
}
|
||||
|
||||
%apply PROTO_TYPE& INPUT { const CppProtoType& param_name }
|
||||
%apply PROTO_TYPE& INPUT { CppProtoType& param_name }
|
||||
%apply PROTO_TYPE* INPUT { const CppProtoType* param_name }
|
||||
%apply PROTO_TYPE* INPUT { CppProtoType* param_name }
|
||||
|
||||
%enddef // end PROTO_INPUT
|
||||
|
||||
%define PROTO2_RETURN(CppProtoType, CSharpProtoType, deleteCppReturn)
|
||||
%typemap(ctype) CppProtoType* "char*"
|
||||
%typemap(imtype) CppProtoType* "byte[]"
|
||||
%typemap(cstype) CppProtoType* "CSharpProtoType"
|
||||
%typemap(csout) CppProtoType* {
|
||||
byte[] buf = $imcall;
|
||||
if (buf == null || buf.Length == 0) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return CSharpProtoType.ParseFrom(buf);
|
||||
}
|
||||
catch (Google.Protobuf.InvalidProtocolBufferException e) {
|
||||
throw new SystemException(
|
||||
"Unable to parse CSharpProtoType protocol message.");
|
||||
}
|
||||
}
|
||||
%typemap(out) CppProtoType* {
|
||||
std::unique_ptr<char[]> buf(new char[$1->ByteSize()]);
|
||||
$1->SerializeWithCachedSizesToArray(reinterpret_cast<uint8*>(buf.get()));
|
||||
$result = buf.get();
|
||||
if (deleteCppReturn) {
|
||||
// To prevent a memory leak.
|
||||
delete $1;
|
||||
$1 = NULL;
|
||||
}
|
||||
}
|
||||
%enddef // end PROTO2_RETURN_AND_DELETE
|
||||
|
||||
%define CS_VECTOR_RETURN0(Namespace, Class, Method, RType)
|
||||
%ignore Namespace::Class::Method;
|
||||
|
||||
%extend Namespace::Class {
|
||||
int Method ## Size() const {
|
||||
return self->Method().size();
|
||||
}
|
||||
RType Method ## ValueAt(int index) const {
|
||||
return self->Method()[index];
|
||||
}
|
||||
}
|
||||
%enddef // CS_VECTOR_RETURN0
|
||||
|
||||
%define CS_VECTOR_RETURN1(Namespace, Class, Method, RType, ArgType)
|
||||
%ignore Namespace::Class::Method;
|
||||
|
||||
%extend Namespace::Class {
|
||||
int Method ## Size(ArgType a) const {
|
||||
return self->Method(a).size();
|
||||
}
|
||||
RType Method ## ValueAt(ArgType a, int index) const {
|
||||
return self->Method(a)[index];
|
||||
}
|
||||
}
|
||||
%enddef // CS_VECTOR_RETURN1
|
||||
|
||||
%define CS_VECTOR_RETURN2(Namespace, Class, Method, RType,
|
||||
ArgType1, ArgType2)
|
||||
%ignore Namespace::Class::Method;
|
||||
|
||||
%extend Namespace::Class {
|
||||
int Method ## Size(ArgType1 a, ArgType2 b) const {
|
||||
return self->Method(a, b).size();
|
||||
}
|
||||
RType Method ## ValueAt(ArgType1 a, ArgType2 b, int index) const {
|
||||
return self->Method(a, b)[index];
|
||||
}
|
||||
}
|
||||
%enddef // CS_VECTOR_RETURN2
|
||||
#endif // SWIGCSHARP
|
||||
|
||||
Reference in New Issue
Block a user