BitArray indexing from the end

This commit is contained in:
Joachim Schoeberl 2021-12-29 21:06:52 +01:00
parent 8e38334b9a
commit 44d626f727
2 changed files with 3 additions and 1 deletions

@ -1 +1 @@
Subproject commit c16da993094988141101ac4d96a9b2c92f9ac714
Subproject commit e7e2c79f3f520f78ffc39fcb34f7919003102733

View File

@ -37,12 +37,14 @@ PYBIND11_MODULE(pyngcore, m) // NOLINT
.def("__len__", &BitArray::Size)
.def("__getitem__", [] (BitArray & self, int i)
{
if (i < 0) i+=self.Size();
if (i < 0 || i >= self.Size())
throw py::index_error();
return self.Test(i);
}, py::arg("pos"), "Returns bit from given position")
.def("__setitem__", [] (BitArray & self, int i, bool b)
{
if (i < 0) i+=self.Size();
if (i < 0 || i >= self.Size())
throw py::index_error();
if (b) self.SetBit(i); else self.Clear(i);