Skip to content

Commit

Permalink
upgrade to nanobind 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Yikai-Liao committed May 30, 2024
1 parent 4e8d55e commit 09e3bba
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
18 changes: 9 additions & 9 deletions py_src/bind_vector_copy.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,10 @@ nanobind::class_<Vector> bind_vector_copy(nanobind::handle scope, const char *na
cl.def(init<const Vector &>(),
"Copy constructor");

cl.def("__init__", [](Vector *v, typed<iterable, detail::iterable_type_id<Value>> &seq) {
cl.def("__init__", [](Vector *v, typed<iterable, Value> &seq) {
new (v) Vector();
v->reserve(len_hint(seq.value));
for (handle h : seq.value)
v->reserve(len_hint(seq));
for (handle h : seq)
v->push_back(cast<Value>(h));
}, "Construct from an iterable object");

Expand Down Expand Up @@ -248,10 +248,10 @@ nanobind::class_<std::shared_ptr<Vector>> bind_shared_vector_copy(nanobind::hand
new (self) self_t(std::move(std::make_shared<Vector>(*other)));
}, "Shallow Copy constructor");

cl.def("__init__", [](self_t &v, typed<iterable, detail::iterable_type_id<Value>> &seq) {
cl.def("__init__", [](self_t &v, typed<iterable, Value> &seq) {
new (&v) self_t(std::move(std::make_shared<Vector>()));
v->reserve(len_hint(seq.value));
for (handle h : seq.value)
v->reserve(len_hint(seq));
for (handle h : seq)
v->push_back(cast<Value>(h));
}, "Construct from an iterable object");

Expand Down Expand Up @@ -457,10 +457,10 @@ nanobind::class_<std::shared_ptr<pycontainer::pyvec<T>>> bind_shared_pyvec(nanob
new (self) self_t(std::move(std::make_shared<Vector>(*other)));
}, "Shallow Copy constructor");

cl.def("__init__", [](self_t &v, typed<iterable, detail::iterable_type_id<ValueRef>> &seq) {
cl.def("__init__", [](self_t &v, typed<iterable, ValueRef> &seq) {
new (&v) self_t(std::move(std::make_shared<Vector>()));
v->reserve(len_hint(seq.value));
for (handle h : seq.value)
v->reserve(len_hint(seq));
for (handle h : seq)
v->push_back(*cast<ValueRef>(h));
}, "Construct from an iterable object");

Expand Down
19 changes: 12 additions & 7 deletions py_src/core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,14 @@ auto bind_track(nb::module_& m, const std::string& name_) {
mode_enums[i] = str_to_pianoroll_mode(modes[i]);
}
auto pianoroll = TrackPianoroll::from_track(*self, mode_enums, pitch_range, encode_velocity);
auto* data = const_cast<u8*>(pianoroll.release());
nb::capsule owner(data, [](void* d) noexcept { delete[] (u8*) d; });
return nb::ndarray<nb::numpy, pianoroll_t>{
const_cast<u8*>(pianoroll.release()), {
data, {
std::get<0>(pianoroll.dims()),
std::get<1>(pianoroll.dims()),
std::get<2>(pianoroll.dims()),
}
}, owner
};
});
}
Expand Down Expand Up @@ -522,12 +524,15 @@ auto bind_score(nb::module_& m, const std::string& name_) {
}
auto pianoroll
= ScorePianoroll::from_score(*self, mode_enums, pitch_range, encode_velocity);
auto* data = const_cast<u8*>(pianoroll.release());
nb::capsule owner(data, [](void* d) noexcept { delete[] (u8*) d; });
return nb::ndarray<nb::numpy, pianoroll_t>{
const_cast<u8*>(pianoroll.release()),
{std::get<0>(pianoroll.dims()),
std::get<1>(pianoroll.dims()),
std::get<2>(pianoroll.dims()),
std::get<3>(pianoroll.dims())}
data, {
std::get<0>(pianoroll.dims()),
std::get<1>(pianoroll.dims()),
std::get<2>(pianoroll.dims()),
std::get<3>(pianoroll.dims())
}, owner
};
},
nb::arg("modes"),
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ requires = [
"setuptools>=42",
"scikit-build>=0.13",
"scikit-build-core>=0.3.3",
"nanobind>=1.8.0",
"nanobind>=2.0.0",
"cmake>=3.20",
"ninja",
]
Expand Down

0 comments on commit 09e3bba

Please sign in to comment.