LSST Applications g063fba187b+fee0456c91,g0f08755f38+ea96e5a5a3,g1653933729+a8ce1bb630,g168dd56ebc+a8ce1bb630,g1a2382251a+90257ff92a,g20f6ffc8e0+ea96e5a5a3,g217e2c1bcf+937a289c59,g28da252d5a+daa7da44eb,g2bbee38e9b+253935c60e,g2bc492864f+253935c60e,g3156d2b45e+6e55a43351,g32e5bea42b+31359a2a7a,g347aa1857d+253935c60e,g35bb328faa+a8ce1bb630,g3a166c0a6a+253935c60e,g3b1af351f3+a8ce1bb630,g3e281a1b8c+c5dd892a6c,g414038480c+416496e02f,g41af890bb2+afe91b1188,g599934f4f4+0db33f7991,g7af13505b9+e36de7bce6,g80478fca09+da231ba887,g82479be7b0+a4516e59e3,g858d7b2824+ea96e5a5a3,g89c8672015+f4add4ffd5,g9125e01d80+a8ce1bb630,ga5288a1d22+bc6ab8dfbd,gb58c049af0+d64f4d3760,gc28159a63d+253935c60e,gcab2d0539d+3f2b72788c,gcf0d15dbbd+4ea9c45075,gda6a2b7d83+4ea9c45075,gdaeeff99f8+1711a396fd,ge79ae78c31+253935c60e,gef2f8181fd+3031e3cf99,gf0baf85859+c1f95f4921,gfa517265be+ea96e5a5a3,gfa999e8aa5+17cd334064,w.2024.50
LSST Data Management Base Package
|
A RangeSet
is a set of unsigned 64 bit integers.
More...
#include <RangeSet.h>
Classes | |
struct | Iterator |
A constant iterator over the ranges (represented as 2-tuples) in a RangeSet. More... | |
Public Types | |
using | difference_type = ptrdiff_t |
using | size_type = size_t |
using | value_type = std::tuple<std::uint64_t, std::uint64_t> |
using | const_iterator = Iterator |
Public Member Functions | |
RangeSet (RangeSet const &)=default | |
RangeSet (RangeSet &&)=default | |
RangeSet & | operator= (RangeSet const &)=default |
RangeSet & | operator= (RangeSet &&)=default |
RangeSet ()=default | |
The default constructor creates an empty set. | |
template<typename InputIterator , typename = typename std::enable_if< std::is_base_of< std::input_iterator_tag, typename std::iterator_traits<InputIterator>::iterator_category >::value >::type> | |
RangeSet (InputIterator a, InputIterator b) | |
This generic constructor creates a set containing the integers or ranges obtained by dereferencing the iterators in [a, b). | |
template<typename Container , typename = typename std::enable_if< std::is_base_of< std::input_iterator_tag, typename std::iterator_traits<decltype( std::begin(std::declval<typename std::add_const<Container>::type>()) )>::iterator_category, ::value &&std::is_base_of< std::input_iterator_tag, typename std::iterator_traits< decltype(std::end(std::declval< typename std::add_const< Container >::type >()))>::iterator_category , ::value , ::type > | |
RangeSet (Container const &c) | |
This generic constructor creates a set containing the integers or integer ranges in the given container. | |
RangeSet & | simplify (std::uint32_t n) |
simplify simplifies this range set by "coarsening" its ranges. | |
RangeSet | simplified (std::uint32_t n) const |
simplified returns a simplified copy of this set. | |
RangeSet & | scale (std::uint64_t i) |
scale multiplies the endpoints of each range in this set by the given integer. | |
RangeSet | scaled (std::uint64_t i) const |
scaled returns a scaled copy of this set. | |
void | clear () |
clear removes all integers from this set. | |
void | fill () |
fill adds all the unsigned 64 bit integers to this set. | |
bool | empty () const |
empty checks whether there are any integers in this set. | |
bool | full () const |
full checks whether all integers in the universe of range sets, [0, 2^64), are in this set. | |
Iterator | beginc () const |
beginc returns a constant iterator to the first range in the complement of this set. | |
Iterator | endc () const |
endc returns a constant iterator to to the range after the last one in the complement of this set. | |
size_t | max_size () const |
max_size returns the maximum number of ranges a set can hold. | |
size_t | size () const |
size returns the number of ranges in this set. | |
std::uint64_t | cardinality () const |
cardinality returns the number of integers in this set. | |
void | swap (RangeSet &s) |
bool | isValid () const |
isValid checks that this RangeSet is in a valid state. | |
RangeSet (std::uint64_t u) | |
RangeSet (std::uint64_t first, std::uint64_t last) | |
template<typename U , typename = typename std::enable_if< std::is_convertible<U, std::uint64_t>::value >::type> | |
RangeSet (std::tuple< U, U > const &range) | |
RangeSet (std::initializer_list< std::uint64_t >) | |
RangeSet (std::initializer_list< std::pair< std::uint64_t, std::uint64_t > >) | |
bool | operator== (RangeSet const &rs) const |
bool | operator!= (RangeSet const &rs) const |
void | insert (std::uint64_t u) |
template<typename U , typename = typename std::enable_if< std::is_convertible<U, std::uint64_t>::value >::type> | |
void | insert (std::tuple< U, U > const &range) |
void | insert (std::uint64_t first, std::uint64_t last) |
void | erase (std::uint64_t u) |
template<typename U , typename = typename std::enable_if< std::is_convertible<U, std::uint64_t>::value >::type> | |
void | erase (std::tuple< U, U > const &range) |
void | erase (std::uint64_t first, std::uint64_t last) |
Set operations | |
RangeSet & | complement () |
complement replaces this set S with U ∖ S, where U is the universe of range sets, [0, 2^64). | |
RangeSet | complemented () const |
complemented returns a complemented copy of this set. | |
RangeSet | intersection (RangeSet const &s) const |
intersection returns the intersection of this set and s. | |
RangeSet | join (RangeSet const &s) const |
join returns the union of this set and s. | |
RangeSet | difference (RangeSet const &s) const |
difference returns the difference between this set and s. | |
RangeSet | symmetricDifference (RangeSet const &s) const |
symmetricDifference returns the symmetric difference of this set and s. | |
RangeSet | operator~ () const |
The ~ operator returns the complement of this set. | |
RangeSet | operator& (RangeSet const &s) const |
The & operator returns the intersection of this set and s. | |
RangeSet | operator| (RangeSet const &s) const |
The | operator returns the union of this set and s. | |
RangeSet | operator- (RangeSet const &s) const |
The - operator returns the difference between this set and s. | |
RangeSet | operator^ (RangeSet const &s) const |
The ^ operator returns the symmetric difference between this set and s. | |
RangeSet & | operator&= (RangeSet const &s) |
The &= operator assigns the intersection of this set and s to this set. | |
RangeSet & | operator|= (RangeSet const &s) |
The |= operator assigns the union of this set and s to this set. | |
RangeSet & | operator-= (RangeSet const &s) |
The -= operator assigns the difference between this set and s to this set. | |
RangeSet & | operator^= (RangeSet const &s) |
The ^= operator assigns the symmetric difference between this set and s to this set. | |
bool | intersects (std::uint64_t u) const |
bool | intersects (std::uint64_t first, std::uint64_t last) const |
bool | intersects (RangeSet const &s) const |
bool | contains (std::uint64_t u) const |
bool | contains (std::uint64_t first, std::uint64_t last) const |
bool | contains (RangeSet const &s) const |
bool | isWithin (std::uint64_t u) const |
bool | isWithin (std::uint64_t first, std::uint64_t last) const |
bool | isWithin (RangeSet const &s) const |
bool | isDisjointFrom (std::uint64_t u) const |
bool | isDisjointFrom (std::uint64_t first, std::uint64_t last) const |
bool | isDisjointFrom (RangeSet const &s) const |
Iterator | begin () const |
Iterator | cbegin () const |
Iterator | end () const |
Iterator | cend () const |
A RangeSet
is a set of unsigned 64 bit integers.
Internally, elements in the set are tracked using a sorted vector of disjoint, non-empty, half-open ranges, which is memory efficient for sets containing many consecutive integers.
Given a hierarchical pixelization of the sphere and a simple spherical region, a RangeSet is a good way to store the indexes of pixels intersecting the region. For an in-depth discussion of this use case, see:
Efficient data structures for masks on 2D grids M. Reinecke and E. Hivon Astronomy & Astrophysics, Volume 580, id.A132, 9 pp.
http://www.aanda.org/articles/aa/abs/2015/08/aa26549-15/aa26549-15.html
The beginning and end points of the disjoint, non-empty, half-open integer ranges in the set are stored in a std::vector<std::uint64_t>, with monotonically increasing values, except for the last one. Each pair of consecutive elements [begin, end) in the vector is a non-empty half-open range, where the value of end is defined as the integer obtained by adding one to the largest element in the range.
Mathematically, a half-open range with largest element equal to 2^64 - 1 would have an end point of 2^64. But arithmetic for unsigned 64 bit integers is modular, and adding 1 to 2^64 - 1 "wraps around" to 0. So in practice, ranges containing the largest std::uint64_t value have an end point of 0. Note that overflow is undefined for signed integers, which motivates the use of unsigned 64 bit integers for this class.
The first and last values of the internal vector are are always 0, even if no range in the set has a beginning or end point of 0. To illustrate why, consider the contents of the vector for a set containing a single integer, 3:
[0, 3, 4, 0]
The range obtained by extracting pairs of elements from the vector starting at index 1 is [3, 4), which corresponds to the contents of the set. The ranges obtained by starting at index 0 are [0, 3) and [4, 0). They correspond to the unsigned 64 bit integers not in the set.
The use of bookended half open ranges means that simply toggling the index of the first range between 0 and 1 corresponds to complementing the set. This allows many simplifications in the implementation - for example, set union and difference can be implemented in terms of set intersection and complement, since A ∪ B = ¬(¬A ∩ ¬B) and A ∖ B = A ∩ ¬B.
Many of the RangeSet methods accept ranges of integers [first, last) as input, either directly or in the form of a tuple. The values in a range are generated by starting with a value equal to first, and incrementing it until last is reached. If first == last, the range is full (it contains all possible std::uint64_t values), and if first > last, it wraps around - that is, it contains all std::uint64_t values except for [last, first).
The ranges in a set can be iterated over. Set modification may invalidate all iterators.
Definition at line 106 of file RangeSet.h.
Definition at line 185 of file RangeSet.h.
using lsst::sphgeom::RangeSet::difference_type = ptrdiff_t |
Definition at line 182 of file RangeSet.h.
using lsst::sphgeom::RangeSet::size_type = size_t |
Definition at line 183 of file RangeSet.h.
Definition at line 184 of file RangeSet.h.
|
default |
|
default |
|
default |
The default constructor creates an empty set.
|
inlineexplicit |
This constructor creates a set containing the given integer(s) or integer range(s).
Definition at line 198 of file RangeSet.h.
|
inline |
This constructor creates a set containing the given integer(s) or integer range(s).
Definition at line 202 of file RangeSet.h.
|
inlineexplicit |
This constructor creates a set containing the given integer(s) or integer range(s).
Definition at line 212 of file RangeSet.h.
lsst::sphgeom::RangeSet::RangeSet | ( | std::initializer_list< std::uint64_t > | list | ) |
This constructor creates a set containing the given integer(s) or integer range(s).
Definition at line 101 of file RangeSet.cc.
lsst::sphgeom::RangeSet::RangeSet | ( | std::initializer_list< std::pair< std::uint64_t, std::uint64_t > > | list | ) |
This constructor creates a set containing the given integer(s) or integer range(s).
Definition at line 105 of file RangeSet.cc.
|
inline |
This generic constructor creates a set containing the integers or ranges obtained by dereferencing the iterators in [a, b).
It is hidden via SFINAE if InputIterator is not a standard input iterator.
Definition at line 242 of file RangeSet.h.
|
inlineexplicit |
This generic constructor creates a set containing the integers or integer ranges in the given container.
It is hidden via SFINAE if Container does not provide constant input iterators over its elements.
Definition at line 271 of file RangeSet.h.
|
inline |
This function returns a constant iterator to the first range in this set.
Definition at line 523 of file RangeSet.h.
|
inline |
beginc
returns a constant iterator to the first range in the complement of this set.
Definition at line 536 of file RangeSet.h.
std::uint64_t lsst::sphgeom::RangeSet::cardinality | ( | ) | const |
cardinality
returns the number of integers in this set.
Note that 0 is returned both for full and empty sets (a full set contains 2^64 integers, which is 0 modulo 2^64).
Definition at line 307 of file RangeSet.cc.
|
inline |
This function returns a constant iterator to the first range in this set.
Definition at line 524 of file RangeSet.h.
|
inline |
This function returns a constant iterator to the range after the last one in this set.
Definition at line 531 of file RangeSet.h.
|
inline |
clear
removes all integers from this set.
Definition at line 508 of file RangeSet.h.
|
inline |
complement
replaces this set S with U ∖ S, where U is the universe of range sets, [0, 2^64).
It runs in constant time.
Definition at line 335 of file RangeSet.h.
|
inline |
complemented
returns a complemented copy of this set.
Definition at line 338 of file RangeSet.h.
bool lsst::sphgeom::RangeSet::contains | ( | RangeSet const & | s | ) | const |
contains
returns true iff every one of the given integers is in this set.
Definition at line 287 of file RangeSet.cc.
bool lsst::sphgeom::RangeSet::contains | ( | std::uint64_t | first, |
std::uint64_t | last ) const |
contains
returns true iff every one of the given integers is in this set.
Definition at line 271 of file RangeSet.cc.
|
inline |
contains
returns true iff every one of the given integers is in this set.
Definition at line 442 of file RangeSet.h.
difference
returns the difference between this set and s.
Definition at line 164 of file RangeSet.cc.
|
inline |
empty
checks whether there are any integers in this set.
Definition at line 514 of file RangeSet.h.
|
inline |
This function returns a constant iterator to the range after the last one in this set.
Definition at line 530 of file RangeSet.h.
|
inline |
endc
returns a constant iterator to to the range after the last one in the complement of this set.
Definition at line 540 of file RangeSet.h.
|
inline |
erase
removes the given integers from this set.
The strong exception safety guarantee is provided.
Definition at line 323 of file RangeSet.h.
void lsst::sphgeom::RangeSet::erase | ( | std::uint64_t | first, |
std::uint64_t | last ) |
erase
removes the given integers from this set.
The strong exception safety guarantee is provided.
Definition at line 128 of file RangeSet.cc.
|
inline |
erase
removes the given integers from this set.
The strong exception safety guarantee is provided.
Definition at line 313 of file RangeSet.h.
|
inline |
fill
adds all the unsigned 64 bit integers to this set.
Definition at line 511 of file RangeSet.h.
|
inline |
full
checks whether all integers in the universe of range sets, [0, 2^64), are in this set.
Definition at line 518 of file RangeSet.h.
|
inline |
insert
adds the given integer(s) to this set.
It is strongly exception safe, and runs in amortized constant time if the given integers extend or follow the last (largest) range in this set. Otherwise, the worst case run time is O(N), where N is the number of ranges in the set.
Definition at line 302 of file RangeSet.h.
void lsst::sphgeom::RangeSet::insert | ( | std::uint64_t | first, |
std::uint64_t | last ) |
insert
adds the given integer(s) to this set.
It is strongly exception safe, and runs in amortized constant time if the given integers extend or follow the last (largest) range in this set. Otherwise, the worst case run time is O(N), where N is the number of ranges in the set.
Definition at line 111 of file RangeSet.cc.
|
inline |
insert
adds the given integer(s) to this set.
It is strongly exception safe, and runs in amortized constant time if the given integers extend or follow the last (largest) range in this set. Otherwise, the worst case run time is O(N), where N is the number of ranges in the set.
Definition at line 292 of file RangeSet.h.
bool lsst::sphgeom::RangeSet::intersects | ( | RangeSet const & | s | ) | const |
intersects
returns true iff the intersection of this set and the given integers is non-empty.
Definition at line 264 of file RangeSet.cc.
bool lsst::sphgeom::RangeSet::intersects | ( | std::uint64_t | first, |
std::uint64_t | last ) const |
intersects
returns true iff the intersection of this set and the given integers is non-empty.
Definition at line 248 of file RangeSet.cc.
|
inline |
intersects
returns true iff the intersection of this set and the given integers is non-empty.
Definition at line 432 of file RangeSet.h.
|
inline |
isDisjointFrom
returns true iff the intersection of this set and the given integers is empty.
Definition at line 468 of file RangeSet.h.
|
inline |
isDisjointFrom
returns true iff the intersection of this set and the given integers is empty.
Definition at line 464 of file RangeSet.h.
|
inline |
isDisjointFrom
returns true iff the intersection of this set and the given integers is empty.
Definition at line 462 of file RangeSet.h.
bool lsst::sphgeom::RangeSet::isValid | ( | ) | const |
isValid
checks that this RangeSet is in a valid state.
It is intended for use by unit tests, but calling it in other contexts is harmless. A return value of false means the RangeSet implementation isn't preserving its invariants, i.e. has a bug.
Definition at line 384 of file RangeSet.cc.
|
inline |
isWithin
returns true iff every integer in this set is also one of the given integers.
Definition at line 456 of file RangeSet.h.
bool lsst::sphgeom::RangeSet::isWithin | ( | std::uint64_t | first, |
std::uint64_t | last ) const |
isWithin
returns true iff every integer in this set is also one of the given integers.
Definition at line 294 of file RangeSet.cc.
|
inline |
isWithin
returns true iff every integer in this set is also one of the given integers.
Definition at line 452 of file RangeSet.h.
join
returns the union of this set and s.
Definition at line 152 of file RangeSet.cc.
|
inline |
max_size
returns the maximum number of ranges a set can hold.
Definition at line 543 of file RangeSet.h.
|
inline |
Two RangeSet instances are equal iff they contain the same integers.
Definition at line 280 of file RangeSet.h.
The & operator returns the intersection of this set and s.
Definition at line 365 of file RangeSet.h.
The &= operator assigns the intersection of this set and s to this set.
It is strongly exception safe.
Definition at line 386 of file RangeSet.h.
The - operator returns the difference between this set and s.
Definition at line 375 of file RangeSet.h.
The -= operator assigns the difference between this set and s to this set.
It is strongly exception safe.
Definition at line 406 of file RangeSet.h.
|
inline |
Two RangeSet instances are equal iff they contain the same integers.
Definition at line 276 of file RangeSet.h.
The ^ operator returns the symmetric difference between this set and s.
Definition at line 380 of file RangeSet.h.
The ^= operator assigns the symmetric difference between this set and s to this set.
It is strongly exception safe.
Definition at line 418 of file RangeSet.h.
The | operator returns the union of this set and s.
Definition at line 370 of file RangeSet.h.
The |= operator assigns the union of this set and s to this set.
It is strongly exception safe.
Definition at line 396 of file RangeSet.h.
|
inline |
The ~ operator returns the complement of this set.
Definition at line 358 of file RangeSet.h.
RangeSet & lsst::sphgeom::RangeSet::scale | ( | std::uint64_t | i | ) |
scale
multiplies the endpoints of each range in this set by the given integer.
Given ranges that correspond to pixel indexes in a hierarchical pixelization of the sphere like HTM or Q3C, scaling by 4 corresponds to increasing the subdivision level of the pixelization by 1.
Definition at line 361 of file RangeSet.cc.
|
inline |
scaled
returns a scaled copy of this set.
Definition at line 501 of file RangeSet.h.
|
inline |
simplified
returns a simplified copy of this set.
Definition at line 486 of file RangeSet.h.
RangeSet & lsst::sphgeom::RangeSet::simplify | ( | std::uint32_t | n | ) |
simplify
simplifies this range set by "coarsening" its ranges.
The result is defined as the union of the ranges obtained by rounding existing range beginnings down to the nearest multiple of 2^n, and rounding the ends up. Therefore, simplifying a range set always results in a superset of the original set.
This function replaces many small ranges with fewer coarser ranges. If the ranges correspond to pixels in a hierarchical pixelization of the sphere that overlap a region R, then this operation can be thought of as computing a lower resolution representation of the coverage of R.
Definition at line 315 of file RangeSet.cc.
|
inline |
|
inline |
symmetricDifference
returns the symmetric difference of this set and s.
Definition at line 173 of file RangeSet.cc.