22#include "pybind11/pybind11.h"
30using namespace pybind11::literals;
38uint64_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 "
51RangeSet 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));
69py::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);
90 cls.def(py::init([](uint64_t
a, uint64_t
b) {
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());
107 "first"_a,
"last"_a);
111 "first"_a,
"last"_a);
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^=);
133 cls.def(
"__getitem__", [](
RangeSet const &
self, py::int_ i) {
135 return py::cast(
self.begin()[j]);
138 cls.def(
"intersects",
141 cls.def(
"intersects",
142 (
bool (
RangeSet::*)(uint64_t, uint64_t)
const) &
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());
173 "first"_a,
"last"_a);
178 cls.def(
"isDisjointFrom",
181 cls.def(
"isDisjointFrom",
182 (
bool (
RangeSet::*)(uint64_t, uint64_t)
const) &
184 "first"_a,
"last"_a);
185 cls.def(
"isDisjointFrom",
203 cls.def(
"ranges", &ranges);
207 cls.def(
"__repr__", [](
RangeSet const &
self) {
208 return py::str(
"RangeSet({!s})").format(ranges(
self));
211 cls.def(
"__reduce__", [cls](
RangeSet const &
self) {
212 return py::make_tuple(cls, py::make_tuple(ranges(
self)));
This file provides a type for representing integer sets.
A RangeSet is a set of unsigned 64 bit integers.
RangeSet intersection(RangeSet const &s) const
intersection returns the intersection of this set and s.
bool intersects(uint64_t u) const
void clear()
clear removes all integers from this set.
bool isWithin(uint64_t u) const
bool full() const
full checks whether all integers in the universe of range sets, [0, 2^64), are in this set.
bool contains(uint64_t u) const
RangeSet join(RangeSet const &s) const
join returns the union of this set and s.
RangeSet & scale(uint64_t i)
scale multiplies the endpoints of each range in this set by the given integer.
bool isDisjointFrom(uint64_t u) const
RangeSet simplified(uint32_t n) const
simplified returns a simplified copy of this set.
bool empty() const
empty checks whether there are any integers in this set.
RangeSet scaled(uint64_t i) const
scaled returns a scaled copy of this set.
RangeSet & complement()
complement replaces this set S with U ∖ S, where U is the universe of range sets, [0,...
size_t size() const
size returns the number of ranges in this set.
void fill()
fill adds all the unsigned 64 bit integers to this set.
RangeSet complemented() const
complemented returns a complemented copy of this set.
RangeSet difference(RangeSet const &s) const
difference returns the difference between this set and s.
RangeSet & simplify(uint32_t n)
simplify simplifies this range set by "coarsening" its ranges.
uint64_t cardinality() const
cardinality returns the number of integers in this set.
RangeSet symmetricDifference(RangeSet const &s) const
symmetricDifference returns the symmetric difference of this set and s.
daf::base::PropertyList * list
ptrdiff_t convertIndex(ptrdiff_t len, pybind11::int_ i)
Convert a Python index i over a sequence with length len to a non-negative (C++ style) index,...
void defineClass(Pybind11Class &cls)
A base class for image defects.