-
Notifications
You must be signed in to change notification settings - Fork 0
/
cvrp.cpp
321 lines (286 loc) · 9.39 KB
/
cvrp.cpp
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#include "php_CVRP.h"
#include "routermanager.h"
#include <iostream>
zend_object_handlers routermanager_object_handlers;
struct routermanager_object {
zend_object std;
RouterManager *routermanager;
};
void routermanager_free_storage(void *object TSRMLS_DC)
{
routermanager_object *obj = (routermanager_object *)object;
delete obj->routermanager;
zend_hash_destroy(obj->std.properties);
FREE_HASHTABLE(obj->std.properties);
efree(obj);
}
zend_object_value routermanager_create_handler(zend_class_entry *type TSRMLS_DC)
{
zend_object_value retval;
routermanager_object *obj = (routermanager_object *)emalloc(sizeof(routermanager_object));
memset(obj, 0, sizeof(routermanager_object));
obj->std.ce = type;
ALLOC_HASHTABLE(obj->std.properties);
zend_hash_init(obj->std.properties, 0, NULL, ZVAL_PTR_DTOR, 0);
object_properties_init(&obj->std, type);
retval.handle = zend_objects_store_put(obj, NULL,
routermanager_free_storage, NULL TSRMLS_CC);
retval.handlers = &routermanager_object_handlers;
return retval;
}
zend_class_entry *routermanager_ce;
PHP_METHOD(RouterManager, __construct)
{
std::cout << "try to construct" << std::endl;
std::cout.flush();
zval *object = getThis();
char *folder;
int l;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &folder, &l) == FAILURE) {
std::cout << "couldnt parse" <<std::endl;
RETURN_NULL();
}
std::cout << "parse " << folder << std::endl;
RouterManager *routerM = new RouterManager(folder);
routermanager_object *obj = (routermanager_object *)zend_object_store_get_object(object TSRMLS_CC);
obj->routermanager = routerM;
}
PHP_METHOD(RouterManager, calculate)
{
std::cout << "try to calculate";
routermanager_object *obj = (routermanager_object *)zend_object_store_get_object(
getThis() TSRMLS_CC);
RouterManager *routermanager = obj->routermanager;
if (routermanager != NULL) {
routermanager->calculate();
}
}
PHP_METHOD(RouterManager, isFinished)
{
routermanager_object *obj = (routermanager_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
RouterManager *routermanager = obj->routermanager;
if (routermanager == NULL)
{
RETURN_NULL();
}
bool finished = routermanager->isFinished();
RETURN_BOOL(finished);
}
PHP_METHOD(RouterManager, getUsedTrucks)
{
routermanager_object *obj = (routermanager_object *)zend_object_store_get_object(
getThis() TSRMLS_CC);
RouterManager *routermanager = obj->routermanager;
if (routermanager == NULL) {
RETURN_NULL();
}
std::vector<unsigned int> trucksLs = routermanager->getUsedTrucks();
//std::cout << "passing points num " << passingPntsLs.size() << "\n path length is " << path.size() <<"\n";
HashTable *resTable;
ALLOC_HASHTABLE(resTable);
zend_hash_init(resTable, 1000000, NULL, NULL, 0);
for (int i = 0; i < trucksLs.size(); i ++)
{
int id = trucksLs[i];
zval *zId;
MAKE_STD_ZVAL(zId);
ZVAL_LONG(zId, id);
if (zend_hash_next_index_insert(resTable, &zId, sizeof(zval*), NULL) == SUCCESS) {
Z_ADDREF_P(zId);
}
}
zval* res;
MAKE_STD_ZVAL(res);
Z_TYPE_P(res) = IS_ARRAY;
Z_ARRVAL_P(res) = resTable;
//zval_ptr_dtor(&res);
//Z_ADDREF_PP(res);
bool copy = false;
bool dtor = false;
RETURN_ZVAL(res, NULL, NULL);
}
PHP_METHOD(RouterManager, getRouteByTruck)
{
routermanager_object *obj = (routermanager_object *)zend_object_store_get_object(
getThis() TSRMLS_CC);
RouterManager *routermanager = obj->routermanager;
if (routermanager == NULL) {
RETURN_NULL();
}
int routeId = -1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &routeId) == FAILURE) {
RETURN_NULL();
}
std::vector<unsigned int> customersLs = routermanager->getRouteByTruck(routeId);
//std::cout << "passing points num " << passingPntsLs.size() << "\n path length is " << path.size() <<"\n";
HashTable *resTable;
ALLOC_HASHTABLE(resTable);
zend_hash_init(resTable, 1000000, NULL, NULL, 0);
for (int i = 0; i < customersLs.size(); i ++)
{
int id = customersLs[i];
zval *zId;
MAKE_STD_ZVAL(zId);
ZVAL_LONG(zId, id);
if (zend_hash_next_index_insert(resTable, &zId, sizeof(zval*), NULL) == SUCCESS) {
Z_ADDREF_P(zId);
}
}
zval* res;
MAKE_STD_ZVAL(res);
Z_TYPE_P(res) = IS_ARRAY;
Z_ARRVAL_P(res) = resTable;
//zval_ptr_dtor(&res);
//Z_ADDREF_PP(res);
bool copy = false;
bool dtor = false;
RETURN_ZVAL(res, NULL, NULL);
}
PHP_METHOD(RouterManager, getRouteNoTruck)
{
routermanager_object *obj = (routermanager_object *)zend_object_store_get_object(
getThis() TSRMLS_CC);
RouterManager *routermanager = obj->routermanager;
if (routermanager == NULL) {
RETURN_NULL();
}
int routeId = -1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &routeId) == FAILURE) {
RETURN_NULL();
}
std::vector<unsigned int> customersLs = routermanager->getRouteNoTruck(routeId);
//std::cout << "passing points num " << passingPntsLs.size() << "\n path length is " << path.size() <<"\n";
HashTable *resTable;
ALLOC_HASHTABLE(resTable);
zend_hash_init(resTable, 1000000, NULL, NULL, 0);
for (int i = 0; i < customersLs.size(); i ++)
{
int id = customersLs[i];
zval *zId;
MAKE_STD_ZVAL(zId);
ZVAL_LONG(zId, id);
if (zend_hash_next_index_insert(resTable, &zId, sizeof(zval*), NULL) == SUCCESS) {
Z_ADDREF_P(zId);
}
}
zval* res;
MAKE_STD_ZVAL(res);
Z_TYPE_P(res) = IS_ARRAY;
Z_ARRVAL_P(res) = resTable;
//zval_ptr_dtor(&res);
//Z_ADDREF_PP(res);
bool copy = false;
bool dtor = false;
RETURN_ZVAL(res, NULL, NULL);
}
PHP_METHOD(RouterManager, routeNoTruckNum)
{
routermanager_object *obj = (routermanager_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
RouterManager *routermanager = obj->routermanager;
if (routermanager == NULL) {
RETURN_NULL();
}
int num = routermanager->routeNoTruckNum();
RETURN_LONG(num);
}
PHP_METHOD(RouterManager, routeLengthByTruck)
{
routermanager_object *obj = (routermanager_object *)zend_object_store_get_object(
getThis() TSRMLS_CC);
RouterManager *routermanager = obj->routermanager;
if (routermanager == NULL) {
RETURN_NULL();
}
int routeId = -1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &routeId) == FAILURE) {
RETURN_NULL();
}
double length = routermanager->routeLengthByTruck(routeId);
RETURN_DOUBLE(length);
}
PHP_METHOD(RouterManager, routeBeginTimeByTruck)
{
routermanager_object *obj = (routermanager_object *)zend_object_store_get_object(
getThis() TSRMLS_CC);
RouterManager *routermanager = obj->routermanager;
if (routermanager == NULL) {
RETURN_NULL();
}
int routeId = -1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &routeId) == FAILURE) {
RETURN_NULL();
}
double beginTime = routermanager->routeBeginTimeByTruck(routeId);
RETURN_DOUBLE(beginTime);
}
PHP_METHOD(RouterManager, routeEndTimeByTruck)
{
routermanager_object *obj = (routermanager_object *)zend_object_store_get_object(
getThis() TSRMLS_CC);
RouterManager *routermanager = obj->routermanager;
if (routermanager == NULL) {
RETURN_NULL();
}
int routeId = -1;
if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &routeId) == FAILURE) {
RETURN_NULL();
}
double endTime = routermanager->routeEndTimeByTruck(routeId);
RETURN_DOUBLE(endTime);
}
PHP_METHOD(RouterManager, getProgress)
{
routermanager_object *obj = (routermanager_object *)zend_object_store_get_object(getThis() TSRMLS_CC);
RouterManager *routermanager = obj->routermanager;
if (routermanager == NULL) {
RETURN_NULL();
}
int pr = routermanager->getProgress();
RETURN_LONG(pr);
}
zend_function_entry routermanager_methods[] = {
PHP_ME(RouterManager, __construct, NULL, ZEND_ACC_PUBLIC | ZEND_ACC_CTOR)
PHP_ME(RouterManager, calculate, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RouterManager, getUsedTrucks, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RouterManager, getRouteByTruck, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RouterManager, routeLengthByTruck, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RouterManager, routeBeginTimeByTruck, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RouterManager, routeEndTimeByTruck, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RouterManager, isFinished, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RouterManager, routeNoTruckNum, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RouterManager, getRouteNoTruck, NULL, ZEND_ACC_PUBLIC)
PHP_ME(RouterManager, getProgress, NULL, ZEND_ACC_PUBLIC)
{NULL, NULL, NULL}
};
PHP_MINIT_FUNCTION(CVRP)
{
zend_class_entry ce;
INIT_CLASS_ENTRY(ce, "RouterManager", routermanager_methods);
routermanager_ce = zend_register_internal_class(&ce TSRMLS_CC);
routermanager_ce->create_object = routermanager_create_handler;
memcpy(&routermanager_object_handlers,
zend_get_std_object_handlers(), sizeof(zend_object_handlers));
routermanager_object_handlers.clone_obj = NULL;
return SUCCESS;
}
zend_module_entry CVRP_module_entry = {
#if ZEND_MODULE_API_NO >= 20010901
STANDARD_MODULE_HEADER,
#endif
PHP_CVRP_EXTNAME,
NULL, /* Functions */
PHP_MINIT(CVRP),
NULL, /* MSHUTDOWN */
NULL, /* RINIT */
NULL, /* RSHUTDOWN */
NULL, /* MINFO */
#if ZEND_MODULE_API_NO >= 20010901
PHP_CVRP_EXTVER,
#endif
STANDARD_MODULE_PROPERTIES
};
#ifdef COMPILE_DL_CVRP
extern "C" {
ZEND_GET_MODULE(CVRP)
}
#endif