.Net: Add support for delegate function as TransitCallback (Fix #997)
This commit is contained in:
@@ -15,6 +15,9 @@ namespace Google.OrTools.ConstraintSolver {
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
public delegate long TransitCallback(long FromIndex, long ToIndex);
|
||||
public delegate long UnaryTransitCallback(long FromIndex);
|
||||
|
||||
public partial class Solver : IDisposable {
|
||||
public IntVar[] MakeIntVarArray(int count, long min, long max) {
|
||||
IntVar[] array = new IntVar[count];
|
||||
|
||||
@@ -77,6 +77,17 @@ class RoutingSearchParameters;
|
||||
self->AddVectorDimension(values.data(), capacity,
|
||||
fix_start_cumul_to_zero, name);
|
||||
}
|
||||
|
||||
int RegisterTransitCallback(operations_research::TransitCallback c) {
|
||||
return $self->RegisterTransitCallback([c](int64 i, int64 j) {
|
||||
return (*c)(i, j);
|
||||
});
|
||||
}
|
||||
int RegisterUnaryTransitCallback(operations_research::UnaryTransitCallback c) {
|
||||
return $self->RegisterUnaryTransitCallback([c](int64 i) {
|
||||
return (*c)(i);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
%rename("%(camelcase)s", %$isfunction) "";
|
||||
|
||||
@@ -77,3 +77,21 @@ DEFINE_INDEX_TYPE(operations_research::RoutingDisjunctionIndex);
|
||||
DEFINE_INDEX_TYPE(operations_research::RoutingVehicleClassIndex);
|
||||
|
||||
%include "ortools/constraint_solver/routing_types.h"
|
||||
|
||||
%{
|
||||
namespace operations_research {
|
||||
typedef int64 (*TransitCallback)(int64, int64);
|
||||
typedef int64 (*UnaryTransitCallback)(int64);
|
||||
} // namespace operations_research
|
||||
%}
|
||||
|
||||
%define %DEFINE_CALLBACK(TYPE, CSTYPE)
|
||||
%typemap(ctype) TYPE, TYPE& "void*"
|
||||
%typemap(in) TYPE %{ $1 = (TYPE)$input; %}
|
||||
%typemap(in) TYPE& %{ $1 = (TYPE*)&$input; %}
|
||||
%typemap(imtype, out="IntPtr") TYPE, TYPE& "CSTYPE"
|
||||
%typemap(cstype, out="IntPtr") TYPE, TYPE& "CSTYPE"
|
||||
%typemap(csin) TYPE, TYPE& "$csinput"
|
||||
%enddef
|
||||
%DEFINE_CALLBACK(operations_research::TransitCallback, TransitCallback)
|
||||
%DEFINE_CALLBACK(operations_research::UnaryTransitCallback, UnaryTransitCallback)
|
||||
|
||||
Reference in New Issue
Block a user