22 #include "pybind11/pybind11.h"
30 using namespace pybind11::literals;
38 uint64_t _uint64(py::handle
const &obj) {
40 return obj.cast<uint64_t>();
41 }
catch (py::cast_error
const &) {
42 throw py::value_error(
43 "RangeSet elements and range beginning and "
44 "end points must be non-negative integers "
51 RangeSet makeRangeSet(py::iterable iterable) {
53 for (py::handle item : iterable) {
54 PyObject *o = item.ptr();
55 if (PySequence_Check(o) && PySequence_Size(o) == 2) {
56 uint64_t
first = _uint64(py::reinterpret_steal<py::object>(
57 PySequence_GetItem(o, 0)));
58 uint64_t last = _uint64(py::reinterpret_steal<py::object>(
59 PySequence_GetItem(o, 1)));
60 rs.insert(
first, last);
62 rs.insert(_uint64(item));
69 py::list ranges(RangeSet
const &
self) {
72 list.append(py::make_tuple(py::int_(std::get<0>(t)),
73 py::int_(std::get<1>(t))));
88 cls.def(py::init<>());
89 cls.def(py::init<uint64_t>(),
"integer"_a);
94 cls.def(py::init<RangeSet const &>(),
"rangeSet"_a);
96 [](py::iterable iterable) {
97 return new RangeSet(makeRangeSet(iterable));
100 cls.def(
"__eq__", &RangeSet::operator==, py::is_operator());
101 cls.def(
"__ne__", &RangeSet::operator!=, py::is_operator());
103 cls.def(
"insert", (
void (
RangeSet::*)(uint64_t)) & RangeSet::insert,
106 (
void (
RangeSet::*)(uint64_t, uint64_t)) & RangeSet::insert,
107 "first"_a,
"last"_a);
111 "first"_a,
"last"_a);
113 cls.def(
"complement", &RangeSet::complement);
114 cls.def(
"complemented", &RangeSet::complemented);
115 cls.def(
"intersection", &RangeSet::intersection,
"rangeSet"_a);
118 cls.def(
"union", &RangeSet::join,
"rangeSet"_a);
119 cls.def(
"difference", &RangeSet::difference,
"rangeSet"_a);
120 cls.def(
"symmetricDifference", &RangeSet::symmetricDifference,
122 cls.def(
"__invert__", &RangeSet::operator~, py::is_operator());
123 cls.def(
"__and__", &RangeSet::operator&, py::is_operator());
124 cls.def(
"__or__", &RangeSet::operator|, py::is_operator());
125 cls.def(
"__sub__", &RangeSet::operator-, py::is_operator());
126 cls.def(
"__xor__", &RangeSet::operator^, py::is_operator());
127 cls.def(
"__iand__", &RangeSet::operator&=);
128 cls.def(
"__ior__", &RangeSet::operator|=);
129 cls.def(
"__isub__", &RangeSet::operator-=);
130 cls.def(
"__ixor__", &RangeSet::operator^=);
132 cls.def(
"__len__", &RangeSet::size);
133 cls.def(
"__getitem__", [](
RangeSet const &
self, py::int_ i) {
134 auto j = python::convertIndex(
static_cast<ptrdiff_t
>(
self.size()), i);
135 return py::cast(
self.begin()[j]);
138 cls.def(
"intersects",
139 (
bool (
RangeSet::*)(uint64_t)
const) & RangeSet::intersects,
141 cls.def(
"intersects",
142 (
bool (
RangeSet::*)(uint64_t, uint64_t)
const) &
143 RangeSet::intersects,
144 "first"_a,
"last"_a);
145 cls.def(
"intersects",
154 "first"_a,
"last"_a);
158 cls.def(
"__contains__",
160 "integer"_a, py::is_operator());
161 cls.def(
"__contains__",
163 "first"_a,
"last"_a, py::is_operator());
164 cls.def(
"__contains__",
166 "rangeSet"_a, py::is_operator());
169 (
bool (
RangeSet::*)(uint64_t)
const) & RangeSet::isWithin,
172 (
bool (
RangeSet::*)(uint64_t, uint64_t)
const) & RangeSet::isWithin,
173 "first"_a,
"last"_a);
178 cls.def(
"isDisjointFrom",
179 (
bool (
RangeSet::*)(uint64_t)
const) & RangeSet::isDisjointFrom,
181 cls.def(
"isDisjointFrom",
182 (
bool (
RangeSet::*)(uint64_t, uint64_t)
const) &
183 RangeSet::isDisjointFrom,
184 "first"_a,
"last"_a);
185 cls.def(
"isDisjointFrom",
187 RangeSet::isDisjointFrom,
190 cls.def(
"simplify", &RangeSet::simplify,
"n"_a);
193 cls.def(
"scaled", &RangeSet::scaled,
"factor"_a);
194 cls.def(
"fill", &RangeSet::fill);
195 cls.def(
"clear", &RangeSet::clear);
196 cls.def(
"empty", &RangeSet::empty);
197 cls.def(
"full", &RangeSet::full);
198 cls.def(
"size", &RangeSet::size);
199 cls.def(
"cardinality", &RangeSet::cardinality);
202 cls.def(
"isValid", &RangeSet::cardinality);
203 cls.def(
"ranges", &ranges);
206 [](
RangeSet const &
self) {
return py::str(ranges(
self)); });
208 return py::str(
"RangeSet({!s})").format(ranges(
self));
212 return py::make_tuple(
cls, py::make_tuple(ranges(
self)));