-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyknapsack.cpp
More file actions
31 lines (25 loc) · 843 Bytes
/
pyknapsack.cpp
File metadata and controls
31 lines (25 loc) · 843 Bytes
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
#include <iostream>
#include <pybind11/pybind11.h>
#include <pybind11/numpy.h> // provides the array_t template
#include "knapsack.hpp"
namespace py = pybind11;
using namespace std;
using namespace pybind11::literals;
int py_knapSack(int W, py::array_t<int> wt, py::array_t<int> val) {
py::buffer_info wt_buf = wt.request();
py::buffer_info val_buf = val.request();
auto wt_ptr = static_cast<int *>(wt_buf.ptr);
auto val_ptr = static_cast<int *>(val_buf.ptr);
int wt_len = wt_buf.size;
int val_len = val_buf.size;
int n = min(wt_len, val_len);
return knapSack(W, wt_ptr, val_ptr, n);
}
PYBIND11_MODULE(pyknapsack, m) {
m.doc() = "pybind11 knapsack plugin";
m.def(
"knapSack", [](int W, py::array_t<int> wt, py::array_t<int>val) {
return py_knapSack(W, wt, val);
}
);
}