-
Notifications
You must be signed in to change notification settings - Fork 47
Expand file tree
/
Copy pathtsp_params.h
More file actions
69 lines (46 loc) · 2.47 KB
/
tsp_params.h
File metadata and controls
69 lines (46 loc) · 2.47 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
#ifndef NODE_OR_TOOLS_TSP_PARAMS_D4D3AF3A298F_H
#define NODE_OR_TOOLS_TSP_PARAMS_D4D3AF3A298F_H
#include <nan.h>
#include <stdexcept>
#include "params.h"
struct TSPSolverParams {
TSPSolverParams(const Nan::FunctionCallbackInfo<v8::Value>& info);
std::int32_t numNodes;
CostMatrix costs;
};
struct TSPSearchParams {
TSPSearchParams(const Nan::FunctionCallbackInfo<v8::Value>& info);
std::int32_t computeTimeLimit;
std::int32_t depotNode;
v8::Local<v8::Function> callback;
};
// Impl.
TSPSolverParams::TSPSolverParams(const Nan::FunctionCallbackInfo<v8::Value>& info) {
if (info.Length() != 1 || !info[0]->IsObject())
throw std::runtime_error{"Single object argument expected: SolverOptions"};
auto opts = info[0].As<v8::Object>();
auto maybeNumNodes = Nan::Get(opts, Nan::New("numNodes").ToLocalChecked());
auto maybeCostMatrix = Nan::Get(opts, Nan::New("costs").ToLocalChecked());
auto numNodesOk = !maybeNumNodes.IsEmpty() && maybeNumNodes.ToLocalChecked()->IsNumber();
auto costMatrixOk = !maybeCostMatrix.IsEmpty() && maybeCostMatrix.ToLocalChecked()->IsArray();
if (!numNodesOk || !costMatrixOk)
throw std::runtime_error{"SolverOptions expects 'numNodes' (Number), 'costs' (Array)"};
numNodes = Nan::To<std::int32_t>(maybeNumNodes.ToLocalChecked()).FromJust();
auto costMatrix = maybeCostMatrix.ToLocalChecked().As<v8::Array>();
costs = makeMatrixFrom2dArray<CostMatrix>(numNodes, costMatrix);
}
TSPSearchParams::TSPSearchParams(const Nan::FunctionCallbackInfo<v8::Value>& info) {
if (info.Length() != 2 || !info[0]->IsObject() || !info[1]->IsFunction())
throw std::runtime_error{"Two arguments expected: SearchOptions (Object) and callback (Function)"};
auto opts = info[0].As<v8::Object>();
auto maybeComputeTimeLimit = Nan::Get(opts, Nan::New("computeTimeLimit").ToLocalChecked());
auto maybeDepotNode = Nan::Get(opts, Nan::New("depotNode").ToLocalChecked());
auto computeTimeLimitOk = !maybeComputeTimeLimit.IsEmpty() && maybeComputeTimeLimit.ToLocalChecked()->IsNumber();
auto depotNodeOk = !maybeDepotNode.IsEmpty() && maybeDepotNode.ToLocalChecked()->IsNumber();
if (!computeTimeLimitOk || !depotNodeOk)
throw std::runtime_error{"SearchOptions expects 'computeTimeLimit' (Number), 'depotNode' (Number)"};
computeTimeLimit = Nan::To<std::int32_t>(maybeComputeTimeLimit.ToLocalChecked()).FromJust();
depotNode = Nan::To<std::int32_t>(maybeDepotNode.ToLocalChecked()).FromJust();
callback = info[1].As<v8::Function>();
}
#endif