-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcommon-python.i
More file actions
151 lines (137 loc) · 3.26 KB
/
Copy pathcommon-python.i
File metadata and controls
151 lines (137 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Add the following for windows, otherwise it tries to link
// to python debug libraries (not always available)
%begin %{
#ifdef _MSC_VER
#define SWIG_PYTHON_INTERPRETER_NO_DEBUG
#include <corecrt.h>
#endif
%}
%include "carrays.i"
%include <std_string.i>
%array_class(double, dblArray);
%array_class(int, intArray);
// Pass array as array and size
%typemap(in) (int nvars, const int* vars) {
/* Check if is a list */
if (PyList_Check($input)) {
int size = (int)PyList_Size($input);
int i = 0;
$1 = size;
$2 = (int*)malloc(size * sizeof(int));
for (i = 0; i < size; i++) {
PyObject* o = PyList_GetItem($input, i);
if (PyInt_Check(o)) {
$2[i] = PyInt_AsLong(o);
}
else {
PyErr_SetString(PyExc_TypeError, "list must contain floating-point numbers");
free($2);
return NULL;
}
}
}
else if ($input == Py_None) {
$2 = NULL;
}
else {
PyErr_SetString(PyExc_TypeError, "not a list");
return NULL;
}
}
%typemap(freearg) (int nvars, const int* vars) {
free($2);
}
// Pass python list as double array
%typemap(in) const double* {
/* Check if is a list */
if (PyList_Check($input)) {
int size = (int)PyList_Size($input);
int i = 0;
$1 = (double*)malloc(size * sizeof(double));
for (i = 0; i < size; i++) {
PyObject* o = PyList_GetItem($input, i);
if (PyFloat_Check(o) || PyInt_Check(o)) {
$1[i] = PyFloat_AsDouble(o);
}
else {
PyErr_SetString(PyExc_TypeError, "list must contain floating-point numbers");
free($1);
return NULL;
}
}
}
else if ($input == Py_None) {
$1 = NULL;
}
else {
PyErr_SetString(PyExc_TypeError, "not a list");
return NULL;
}
}
%typemap(freearg) (const double*) {
free($1);
}
%typemap(in) const int* {
/* Check if is a list */
if (PyList_Check($input)) {
int size = (int)PyList_Size($input);
int i = 0;
$1 = (int*)malloc(size * sizeof(int));
for (i = 0; i < size; i++) {
PyObject* o = PyList_GetItem($input, i);
if (PyInt_Check(o)) {
$1[i] = PyInt_AsLong(o);
}
else {
PyErr_SetString(PyExc_TypeError, "list must contain integer numbers");
free($1);
return NULL;
}
}
}
else if ($input == Py_None) {
$1 = NULL;
}
else {
PyErr_SetString(PyExc_TypeError, "not a list");
return NULL;
}
}
%typemap(freearg) (const int*) {
free($1);
}
%include <stdint.i>
%typemap(in, numinputs = 0, noblock = 1) int* len {
int templen;
$1 = &templen;
}
%typemap(out) double* getSolutionVector(int* len) {
int i;
$result = PyList_New(templen);
for (i = 0; i < templen; i++) {
PyObject* o = PyFloat_FromDouble((double)$1[i]);
PyList_SetItem($result, i, o);
}
delete $1;
}
%typemap(out) myobj get(int type) {
switch ($1.type)
{
case 0:
$result = PyUnicode_FromString($1.str);
break;
case 1:
$result = PyInt_FromLong($1.integer);
break;
case 2:
$result = PyFloat_FromDouble($1.dbl);
break;
}
}
// The following are useful to return python dictionaries when not using the -builtin switch
%ignore "getVarMap";
%ignore "getVarMapInverse";
%ignore "getVarMapFiltered";
%ignore doAddCut;
%ignore getAMPLWhereImpl;
%include "../../../exceptions-python.i"