port csharp to new styleguide

This commit is contained in:
lperron@google.com
2014-06-12 15:07:04 +00:00
parent 4b076f8b1f
commit 4bb702ec1b
9 changed files with 2019 additions and 38 deletions

View File

@@ -48,14 +48,15 @@ clean_csharp:
-$(DEL) $(LIB_DIR)$S$(LIBPREFIX)Google.OrTools*.pdb
-$(DEL) $(LIB_DIR)$S$(LIBPREFIX)Google.OrTools*.exp
-$(DEL) $(LIB_DIR)$S$(LIBPREFIX)Google.OrTools*.netmodule
-$(DEL) $(GEN_DIR)$Slinear_solver$S*csharp_wrap*
-$(DEL) $(GEN_DIR)$Sconstraint_solver$S*csharp_wrap*
-$(DEL) $(GEN_DIR)$Salgorithms$S*csharp_wrap*
-$(DEL) $(GEN_DIR)$Sgraph$S*csharp_wrap*
-$(DEL) $(GEN_DIR)$Slinear_solver$Scsharp$S*csharp_wrap*
-$(DEL) $(GEN_DIR)$Sconstraint_solver$Scsharp$S*csharp_wrap*
-$(DEL) $(GEN_DIR)$Salgorithms$Scsharp$S*csharp_wrap*
-$(DEL) $(GEN_DIR)$Sgraph$Scsharp$S*csharp_wrap*
-$(DEL) $(GEN_DIR)$Scom$Sgoogle$Sortools$Slinearsolver$S*.cs
-$(DEL) $(GEN_DIR)$Scom$Sgoogle$Sortools$Sconstraintsolver$S*.cs
-$(DEL) $(GEN_DIR)$Scom$Sgoogle$Sortools$Sknapsacksolver$S*.cs
-$(DEL) $(GEN_DIR)$Scom$Sgoogle$Sortools$Sgraph$S*.cs
-$(DEL) $(GEN_DIR)$Scom$Sgoogle$Sortools$Sutil$S*.cs
-$(DEL) $(OBJ_DIR)$Sswig$S*csharp_wrap.$O
-$(DEL) $(BIN_DIR)$S*.exe
@@ -64,23 +65,22 @@ clean_csharp:
csharportools: $(BIN_DIR)/Google.OrTools.dll
$(GEN_DIR)/linear_solver/linear_solver_csharp_wrap.cc: \
$(SRC_DIR)/linear_solver/linear_solver.swig \
$(SRC_DIR)/base/base.swig $(SRC_DIR)/util/data.swig \
$(SRC_DIR)/linear_solver/csharp/linear_solver.swig \
$(SRC_DIR)/base/base.swig $(SRC_DIR)/util/csharp/data.swig \
$(SRC_DIR)/linear_solver/linear_solver.h \
$(GEN_DIR)/linear_solver/linear_solver2.pb.h
$(SWIG_BINARY) $(SWIG_INC) -I$(INC_DIR) -c++ -csharp -o $(GEN_DIR)$Slinear_solver$Slinear_solver_csharp_wrap.cc -module operations_research_linear_solver -namespace Google.OrTools.LinearSolver -dllimport "Google.OrTools.$(DYNAMIC_SWIG_LIB_SUFFIX)" -outdir $(GEN_DIR)$Scom$Sgoogle$Sortools$Slinearsolver $(SRC_DIR)/linear_solver$Slinear_solver.swig
$(SWIG_BINARY) $(SWIG_INC) -I$(INC_DIR) -c++ -csharp -o $(GEN_DIR)$Slinear_solver$Slinear_solver_csharp_wrap.cc -module operations_research_linear_solver -namespace Google.OrTools.LinearSolver -dllimport "Google.OrTools.$(DYNAMIC_SWIG_LIB_SUFFIX)" -outdir $(GEN_DIR)$Scom$Sgoogle$Sortools$Slinearsolver $(SRC_DIR)/linear_solver$Scsharp$Slinear_solver.swig
$(OBJ_DIR)/swig/linear_solver_csharp_wrap.$O: $(GEN_DIR)/linear_solver/linear_solver_csharp_wrap.cc
$(CCC) $(CFLAGS) -c $(GEN_DIR)/linear_solver/linear_solver_csharp_wrap.cc $(OBJ_OUT)$(OBJ_DIR)$Sswig$Slinear_solver_csharp_wrap.$O
$(GEN_DIR)/constraint_solver/constraint_solver_csharp_wrap.cc: \
$(SRC_DIR)/constraint_solver/routing.swig \
$(SRC_DIR)/constraint_solver/constraint_solver.swig \
$(SRC_DIR)/constraint_solver/csharp/routing.swig \
$(SRC_DIR)/constraint_solver/csharp/constraint_solver.swig \
$(SRC_DIR)/base/base.swig \
$(SRC_DIR)/util/callback.swig \
$(SRC_DIR)/util/data.swig \
$(SRC_DIR)/util/csharp/data.swig \
$(SRC_DIR)/constraint_solver/constraint_solver.h
$(SWIG_BINARY) $(SWIG_INC) -I$(INC_DIR) -c++ -csharp -o $(GEN_DIR)$Sconstraint_solver$Sconstraint_solver_csharp_wrap.cc -module operations_research_constraint_solver -namespace Google.OrTools.ConstraintSolver -dllimport "Google.OrTools.$(DYNAMIC_SWIG_LIB_SUFFIX)" -outdir $(GEN_DIR)$Scom$Sgoogle$Sortools$Sconstraintsolver $(SRC_DIR)$Sconstraint_solver$Srouting.swig
$(SWIG_BINARY) $(SWIG_INC) -I$(INC_DIR) -c++ -csharp -o $(GEN_DIR)$Sconstraint_solver$Sconstraint_solver_csharp_wrap.cc -module operations_research_constraint_solver -namespace Google.OrTools.ConstraintSolver -dllimport "Google.OrTools.$(DYNAMIC_SWIG_LIB_SUFFIX)" -outdir $(GEN_DIR)$Scom$Sgoogle$Sortools$Sconstraintsolver $(SRC_DIR)$Sconstraint_solver$Scsharp$Srouting.swig
$(SED) -i -e 's/CSharp_new_Solver/CSharp_new_CpSolver/g' $(GEN_DIR)/com/google/ortools/constraintsolver/*cs $(GEN_DIR)/constraint_solver/constraint_solver_csharp_wrap.*
$(SED) -i -e 's/CSharp_delete_Solver/CSharp_delete_CpSolver/g' $(GEN_DIR)/com/google/ortools/constraintsolver/*cs $(GEN_DIR)/constraint_solver/constraint_solver_csharp_wrap.*
$(SED) -i -e 's/CSharp_Solver_Solve/CSharp_CpSolver_Solve/g' $(GEN_DIR)/com/google/ortools/constraintsolver/*cs $(GEN_DIR)/constraint_solver/constraint_solver_csharp_wrap.*
@@ -95,31 +95,28 @@ $(OBJ_DIR)/swig/constraint_solver_csharp_wrap.$O: \
$(CCC) $(CFLAGS) -c $(GEN_DIR)$Sconstraint_solver$Sconstraint_solver_csharp_wrap.cc $(OBJ_OUT)$(OBJ_DIR)$Sswig$Sconstraint_solver_csharp_wrap.$O
$(GEN_DIR)/algorithms/knapsack_solver_csharp_wrap.cc: \
$(SRC_DIR)/algorithms/knapsack_solver.swig \
$(SRC_DIR)/algorithms/knapsack_solver.swig \
$(SRC_DIR)/algorithms/csharp/knapsack_solver.swig \
$(SRC_DIR)/base/base.swig \
$(SRC_DIR)/util/data.swig \
$(SRC_DIR)/util/csharp/data.swig \
$(SRC_DIR)/algorithms/knapsack_solver.h
$(SWIG_BINARY) $(SWIG_INC) -I$(INC_DIR) -c++ -csharp -o $(GEN_DIR)$Salgorithms$Sknapsack_solver_csharp_wrap.cc -module operations_research_algorithms -namespace Google.OrTools.Algorithms -dllimport "Google.OrTools.$(DYNAMIC_SWIG_LIB_SUFFIX)" -outdir $(GEN_DIR)$Scom$Sgoogle$Sortools$Sknapsacksolver $(SRC_DIR)$Salgorithms$Sknapsack_solver.swig
$(SWIG_BINARY) $(SWIG_INC) -I$(INC_DIR) -c++ -csharp -o $(GEN_DIR)$Salgorithms$Sknapsack_solver_csharp_wrap.cc -module operations_research_algorithms -namespace Google.OrTools.Algorithms -dllimport "Google.OrTools.$(DYNAMIC_SWIG_LIB_SUFFIX)" -outdir $(GEN_DIR)$Scom$Sgoogle$Sortools$Sknapsacksolver $(SRC_DIR)$Salgorithms$Scsharp$Sknapsack_solver.swig
$(OBJ_DIR)/swig/knapsack_solver_csharp_wrap.$O: $(GEN_DIR)/algorithms/knapsack_solver_csharp_wrap.cc
$(CCC) $(CFLAGS) -c $(GEN_DIR)/algorithms/knapsack_solver_csharp_wrap.cc $(OBJ_OUT)$(OBJ_DIR)$Sswig$Sknapsack_solver_csharp_wrap.$O
$(GEN_DIR)/graph/graph_csharp_wrap.cc: \
$(SRC_DIR)/graph/graph.swig \
$(SRC_DIR)/graph/csharp/graph.swig \
$(SRC_DIR)/base/base.swig \
$(SRC_DIR)/util/data.swig \
$(SRC_DIR)/util/csharp/data.swig \
$(SRC_DIR)/graph/max_flow.h \
$(SRC_DIR)/graph/min_cost_flow.h
$(SWIG_BINARY) $(SWIG_INC) -I$(INC_DIR) -c++ -csharp -o $(GEN_DIR)$Sgraph$Sgraph_csharp_wrap.cc -module operations_research_graph -namespace Google.OrTools.Graph -dllimport "Google.OrTools.$(DYNAMIC_SWIG_LIB_SUFFIX)" -outdir $(GEN_DIR)$Scom$Sgoogle$Sortools$Sgraph $(SRC_DIR)$Sgraph$Sgraph.swig
$(SWIG_BINARY) $(SWIG_INC) -I$(INC_DIR) -c++ -csharp -o $(GEN_DIR)$Sgraph$Sgraph_csharp_wrap.cc -module operations_research_graph -namespace Google.OrTools.Graph -dllimport "Google.OrTools.$(DYNAMIC_SWIG_LIB_SUFFIX)" -outdir $(GEN_DIR)$Scom$Sgoogle$Sortools$Sgraph $(SRC_DIR)$Sgraph$Scsharp$Sgraph.swig
$(OBJ_DIR)/swig/graph_csharp_wrap.$O: $(GEN_DIR)/graph/graph_csharp_wrap.cc
$(CCC) $(CFLAGS) -c $(GEN_DIR)$Sgraph$Sgraph_csharp_wrap.cc $(OBJ_OUT)$(OBJ_DIR)$Sswig$Sgraph_csharp_wrap.$O
$(BIN_DIR)/Google.OrTools.dll: \
$(OBJ_DIR)/swig/linear_solver_csharp_wrap.$O \
$(SRC_DIR)/com/google/ortools/linearsolver/LinearExpr.cs \
$(SRC_DIR)/com/google/ortools/linearsolver/LinearConstraint.cs \
$(OBJ_DIR)/swig/constraint_solver_csharp_wrap.$O \
$(OBJ_DIR)/swig/knapsack_solver_csharp_wrap.$O \
$(OBJ_DIR)/swig/graph_csharp_wrap.$O \
@@ -128,13 +125,15 @@ $(BIN_DIR)/Google.OrTools.dll: \
$(SRC_DIR)/com/google/ortools/constraintsolver/IntArrayHelper.cs \
$(SRC_DIR)/com/google/ortools/constraintsolver/ValCstPair.cs \
$(SRC_DIR)/com/google/ortools/constraintsolver/NetDecisionBuilder.cs \
$(SRC_DIR)/com/google/ortools/linearsolver/LinearExpr.cs \
$(SRC_DIR)/com/google/ortools/linearsolver/LinearConstraint.cs \
$(SRC_DIR)/com/google/ortools/util/NestedArrayHelper.cs \
$(STATIC_ALL_DEPS)
ifeq ($(SYSTEM),win)
$(CSC) /target:module /out:$(LIB_DIR)$S$(LIBPREFIX)Google.OrTools.netmodule /warn:0 /nologo /debug $(GEN_DIR)\\com\\google\\ortools\\linearsolver\\*.cs $(SRC_DIR)\\com\\google\\ortools\\linearsolver\\*.cs $(GEN_DIR)\\com\\google\\ortools\\constraintsolver\\*.cs $(SRC_DIR)\\com\\google\\ortools\\constraintsolver\\*.cs $(GEN_DIR)\\com\\google\\ortools\\knapsacksolver\\*.cs $(GEN_DIR)\\com\\google\\ortools\\graph\\*.cs $(SRC_DIR)\\com\\google\\ortools\\util\\*.cs
$(DYNAMIC_LD) $(SIGNING_FLAGS) $(LDOUT)$(BIN_DIR)$SGoogle.OrTools.dll $(LIB_DIR)$S$(LIBPREFIX)Google.OrTools.netmodule $(OBJ_DIR)$Sswig$Slinear_solver_csharp_wrap.$O $(OBJ_DIR)$Sswig$Sconstraint_solver_csharp_wrap.$O $(OBJ_DIR)$Sswig$Sknapsack_solver_csharp_wrap.$O $(OBJ_DIR)$Sswig$Sgraph_csharp_wrap.$O $(STATIC_ALL_LNK) $(STATIC_LD_FLAGS)
else
$(CSC) /target:library /out:$(BIN_DIR)/Google.OrTools.dll /warn:0 /nologo /debug $(GEN_DIR)/com/google/ortools/linearsolver/*.cs $(SRC_DIR)/com/google/ortools/linearsolver/*.cs $(GEN_DIR)/com/google/ortools/constraintsolver/*.cs $(SRC_DIR)/com/google/ortools/constraintsolver/*.cs $(GEN_DIR)/com/google/ortools/knapsacksolver/*.cs $(GEN_DIR)/com/google/ortools/graph/*.cs $(SRC_DIR)/com/google/ortools/util/*.cs
$(CSC) /target:library /out:$(BIN_DIR)/Google.OrTools.dll /warn:0 /nologo /debug $(SRC_DIR)/com/google/ortools/util/*.cs $(GEN_DIR)/com/google/ortools/linearsolver/*.cs $(SRC_DIR)/com/google/ortools/linearsolver/*.cs $(GEN_DIR)/com/google/ortools/constraintsolver/*.cs $(SRC_DIR)/com/google/ortools/constraintsolver/*.cs $(GEN_DIR)/com/google/ortools/knapsacksolver/*.cs $(GEN_DIR)/com/google/ortools/graph/*.cs
$(DYNAMIC_LD) $(LDOUT)$(LIB_DIR)$S$(LIBPREFIX)Google.OrTools.$(DYNAMIC_SWIG_LIB_SUFFIX) $(OBJ_DIR)/swig/linear_solver_csharp_wrap.$O $(OBJ_DIR)/swig/constraint_solver_csharp_wrap.$O $(OBJ_DIR)/swig/knapsack_solver_csharp_wrap.$O $(OBJ_DIR)/swig/graph_csharp_wrap.$O $(STATIC_ALL_LNK) $(STATIC_LD_FLAGS)
endif

View File

@@ -0,0 +1,13 @@
// TODO(user): Refactor this file to adhere to the SWIG style guide.
%include util/csharp/data.swig
// Include the file we want to wrap a first time.
%{
#include "algorithms/knapsack_solver.h"
%}
%rename (UseReduction) operations_research::KnapsackSolver::use_reduction;
%rename (SetUseReduction) operations_research::KnapsackSolver::set_use_reduction;
%include "algorithms/knapsack_solver.h"

View File

@@ -11,23 +11,24 @@
// See the License for the specific language governing permissions and
// limitations under the License.
namespace Google.OrTools.Util
namespace Google.OrTools {
using System;
using System.Collections.Generic;
public static class NestedArrayHelper
{
using System;
using System.Collections.Generic;
public static T[] GetFlatArray<T>(T[,] arr) {
int flatLength = arr.GetLength(0) * arr.GetLength(1);
int idx = 0;
T[] flat = new T[flatLength];
public static class NestedArrayHelper
{
public static T[] GetFlatArray<T>(T[,] arr) {
int flatLength = arr.GetLength(0) * arr.GetLength(1);
int idx = 0;
T[] flat = new T[flatLength];
for (int i = 0; i < arr.GetLength(0); i++)
for (int j = 0; j < arr.GetLength(1); j++)
flat[idx++] = arr[i,j];
for (int i = 0; i < arr.GetLength(0); i++)
for (int j = 0; j < arr.GetLength(1); j++)
flat[idx++] = arr[i,j];
return flat;
}
return flat;
}
} // namespace Google.OrTools.Util
}
} // namespace Google.OrTools

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,98 @@
// TODO(user): Refactor this file to adhere to the SWIG style guide.
%include "constraint_solver/csharp/constraint_solver.swig"
// Include the file we want to wrap a first time.
%{
#include "constraint_solver/routing.h"
%}
// Convert RoutingModel::NodeIndex to (32-bit signed) integers.
%typemap(ctype) operations_research::RoutingModel::NodeIndex "int"
%typemap(imtype) operations_research::RoutingModel::NodeIndex "int"
%typemap(cstype) operations_research::RoutingModel::NodeIndex "int"
%typemap(csin) operations_research::RoutingModel::NodeIndex "$csinput"
%typemap(csout) operations_research::RoutingModel::NodeIndex {
return $imcall;
}
%typemap(in) operations_research::RoutingModel::NodeIndex {
$1 = operations_research::RoutingModel::NodeIndex($input);
}
%typemap(out) operations_research::RoutingModel::NodeIndex {
$result = $1.value();
}
%typemap(csvarin) operations_research::RoutingModel::NodeIndex
%{
set { $imcall; }
%}
%typemap(csvarout, excode=SWIGEXCODE) operations_research::RoutingModel::NodeIndex
%{
get {
return $imcall;
}
%}
// Convert std::vector<RoutingModel::NodeIndex> to/from int arrays.
CS_TYPEMAP_STDVECTOR(operations_research::RoutingModel::NodeIndex, int, int);
CS_TYPEMAP_STDVECTOR_IN1(operations_research::RoutingModel::NodeIndex, int, int);
// Create input mapping for NodeEvaluator2
%module(directors="1") main
%feature("director") NodeEvaluator2;
%{
class NodeEvaluator2 {
public:
int64 RunAux(operations_research::RoutingModel::NodeIndex i,
operations_research::RoutingModel::NodeIndex j) {
return Run(i.value(), j.value());
}
virtual int64 Run(int i, int j) = 0;
operations_research::RoutingModel::NodeEvaluator2* GetPermanentCallback() {
return NewPermanentCallback(this, &NodeEvaluator2::RunAux);
}
virtual ~NodeEvaluator2() {}
};
%}
class NodeEvaluator2 {
public:
virtual ~NodeEvaluator2();
virtual int64 Run(int i, int j) = 0;
operations_research::RoutingModel::NodeEvaluator2* GetPermanentCallback();
};
%typemap(cstype) operations_research::RoutingModel::NodeEvaluator2* "NodeEvaluator2";
%typemap(csin) operations_research::RoutingModel::NodeEvaluator2* "$descriptor(ResultCallback2<int64, _RoutingModel_NodeIndex, _RoutingModel_NodeIndex>*).getCPtr($csinput.GetPermanentCallback())";
%ignore operations_research::RoutingModel::AddVectorDimension(
const int64* values,
int64 capacity,
const string& name);
%ignore operations_research::RoutingModel::AddMatrixDimension(
const int64* const* values,
int64 capacity,
const string& name);
%extend operations_research::RoutingModel {
void AddVectorDimension(const std::vector<int64>& values,
int64 capacity,
bool fix_start_cumul_to_zero,
const string& name) {
DCHECK_EQ(values.size(), self->nodes());
self->AddVectorDimension(values.data(), capacity,
fix_start_cumul_to_zero, name);
}
}
%ignore operations_research::RoutingModel::WrapIndexEvaluator(
Solver::IndexEvaluator2* evaluator);
%ignore operations_research::RoutingModel::RoutingModel(
int nodes, int vehicles,
const std::vector<std::pair<NodeIndex, NodeIndex> >& start_end);
%rename("%(camelcase)s", %$isfunction) "";
// Wrap cp includes
%include constraint_solver/routing.h

View File

@@ -0,0 +1,54 @@
// TODO(user): Refactor this file to adhere to the SWIG style guide.
%include util/csharp/data.swig
// First phase: #include the file we want to wrap.
%{
#include "util/zvector.h"
#include "graph/ebert_graph.h"
#include "graph/assignment.h"
#include "graph/max_flow.h"
#include "graph/min_cost_flow.h"
%}
// 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;
// Use the SimpleLinearSumAssignment.
%rename(ComplexLinearSumAssignment) operations_research::LinearSumAssignment;
%rename(LinearSumAssignment) operations_research::SimpleLinearSumAssignment;
// Second phase: %include the same files.
%include "util/zvector.h"
%include "graph/ebert_graph.h"
%include "graph/assignment.h"
%include "graph/max_flow.h"
%include "graph/min_cost_flow.h"
%template(StarStarGraphBase) operations_research::StarGraphBase<operations_research::NodeIndex, operations_research::ArcIndex, operations_research::EbertGraph<operations_research::NodeIndex, operations_research::ArcIndex> >;
%template(StarGraphCore) operations_research::EbertGraphBase<operations_research::NodeIndex, operations_research::ArcIndex, operations_research::EbertGraph<operations_research::NodeIndex, operations_research::ArcIndex> >;
%template(StarGraph) operations_research::EbertGraph<operations_research::NodeIndex, operations_research::ArcIndex>;
%template(ForwardStarStarGraphBase) operations_research::StarGraphBase<operations_research::NodeIndex, operations_research::ArcIndex, operations_research::ForwardEbertGraph<operations_research::NodeIndex, operations_research::ArcIndex> >;
%template(ForwardStarGraphCore) operations_research::EbertGraphBase<operations_research::NodeIndex, operations_research::ArcIndex, operations_research::ForwardEbertGraph<operations_research::NodeIndex, operations_research::ArcIndex> >;
%template(ForwardStarGraph) operations_research::ForwardEbertGraph<operations_research::NodeIndex, operations_research::ArcIndex>;
%template(ForwardStarStaticStarGraphBase) operations_research::StarGraphBase<operations_research::NodeIndex, operations_research::ArcIndex, operations_research::ForwardStaticGraph<operations_research::NodeIndex, operations_research::ArcIndex> >;
%template(ForwardStarStaticGraph) operations_research::ForwardStaticGraph<operations_research::NodeIndex, operations_research::ArcIndex>;
%template(MaxFlow) operations_research::GenericMaxFlow<operations_research::EbertGraph<operations_research::NodeIndex, operations_research::ArcIndex> >;
%template(MinCostFlow) operations_research::GenericMinCostFlow<operations_research::EbertGraph<operations_research::NodeIndex, operations_research::ArcIndex>, operations_research::FlowQuantity, operations_research::CostValue>;

View File

@@ -0,0 +1,488 @@
// Copyright 2010-2013 Google
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
%include base/base.swig
%include util/csharp/data.swig
// Swig doesn't like module initializers.
#define DECLARE_MODULE_INITIALIZER(x);
// Include the file we want to wrap a first time.
%{
#include "linear_solver/linear_solver.h"
#include "linear_solver/linear_solver2.pb.h"
#include "linear_solver/linear_solver_ext.h"
%}
%module(directors="1") operations_research;
%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 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
// Wrap linear_solver includes
%include "linear_solver/linear_solver.h"
%include "linear_solver/linear_solver_ext.h"

280
src/util/csharp/data.swig Normal file
View File

@@ -0,0 +1,280 @@
// Copyright 2010-2013 Google
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
%include base/base.swig
%{
#include <vector>
#include "base/integral_types.h"
using std::string;
%}
%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.
//
////////////////////////////////////////////////
%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), 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
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

View File

@@ -0,0 +1,17 @@
// This is the C# SWIG wrapper for ../tuple_set.h. See that file.
// TODO(user): Refactor this file to comply with the SWIG style guide.
%include "base/base.swig"
// This .swig include provides a lot of typemap conversion tools.
// TODO(user): see if we really need it.
%include util/csharp/data.swig
%{
// TODO(user): See if we really need <vector>.
#include <vector>
#include "base/integral_types.h"
#include "util/tuple_set.h"
%}
%include util/tuple_set.h