LSST Applications g063fba187b+cac8b7c890,g0f08755f38+6aee506743,g1653933729+a8ce1bb630,g168dd56ebc+a8ce1bb630,g1a2382251a+b4475c5878,g1dcb35cd9c+8f9bc1652e,g20f6ffc8e0+6aee506743,g217e2c1bcf+73dee94bd0,g28da252d5a+1f19c529b9,g2bbee38e9b+3f2625acfc,g2bc492864f+3f2625acfc,g3156d2b45e+6e55a43351,g32e5bea42b+1bb94961c2,g347aa1857d+3f2625acfc,g35bb328faa+a8ce1bb630,g3a166c0a6a+3f2625acfc,g3e281a1b8c+c5dd892a6c,g3e8969e208+a8ce1bb630,g414038480c+5927e1bc1e,g41af890bb2+8a9e676b2a,g7af13505b9+809c143d88,g80478fca09+6ef8b1810f,g82479be7b0+f568feb641,g858d7b2824+6aee506743,g89c8672015+f4add4ffd5,g9125e01d80+a8ce1bb630,ga5288a1d22+2903d499ea,gb58c049af0+d64f4d3760,gc28159a63d+3f2625acfc,gcab2d0539d+b12535109e,gcf0d15dbbd+46a3f46ba9,gda6a2b7d83+46a3f46ba9,gdaeeff99f8+1711a396fd,ge79ae78c31+3f2625acfc,gef2f8181fd+0a71e47438,gf0baf85859+c1f95f4921,gfa517265be+6aee506743,gfa999e8aa5+17cd334064,w.2024.51
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.