RangeSet.hpp

A collection of ranges that can be operated on collectively.

Note

Status: BETA

template<typename T>
class RangeSet
#include <RangeSet.hpp>

RangeSet maintains a collection of ranges. The ranges are exclusive of the endpoint and kept sorted and non-adjacent (i.e., there is a gap between successive ranges).

Public Types

using range_t = Range<T, false>
using this_t = RangeSet<T>

Public Functions

RangeSet() = default
inline explicit RangeSet(range_t start_range)
inline RangeSet(T start, T end)
RangeSet(const RangeSet&) = default
RangeSet(RangeSet&&) = default
inline RangeSet(const std::string &bitstring)
RangeSet &operator=(const RangeSet&) = default
RangeSet &operator=(RangeSet&&) = default
inline RangeSet &operator=(const std::string &bitstring)
bool operator (const RangeSet &) const =default
inline bool Has(T val) const
inline bool HasRange(range_t range) const
inline bool IsEmpty() const
inline T GetStart() const
Returns:

Overall start of all ranges (or max value if no ranges exist.)

inline T GetEnd() const
Returns:

Overall end of all ranges (or min value if no ranges exist.)

inline size_t GetNumRanges() const
inline T GetSize() const

Calculate the total combined size of all ranges.

inline String ToString() const

Present this set of ranges as a string.

inline const vector<range_t> &GetRanges() const &
inline bool HasOverlap(range_t range) const
inline T CalcOverlap(range_t range) const
inline RangeSet &Clear()

Remove all ranges in the set.

inline RangeSet &SetAll()

Set a single range that includes all value.

inline RangeSet &Shift(T shift)

Shift all ranges by a fixed amount.

Parameters:

shift – How much should the range be shifted by?

inline RangeSet &ShiftUp(T shift)
inline RangeSet &ShiftDown(T shift)
inline this_t CalcShift(T shift) const
inline this_t CalcShiftDown(T shift) const
inline this_t CalcShiftUp(T shift) const
inline RangeSet &Insert(T val)

Insert a value into this range set.

Parameters:

val – Value to insert.

Returns:

This RangeSet after insertion.

inline RangeSet &Insert(range_t in)

Insert a whole range into this set, merging other ranges if needed.

Parameters:

in – New range to include.

Returns:

This RangeSet after insertion.

inline RangeSet &Insert(const this_t &in_set)

Merge an entire range set into this one.

Note

Can be optimized to handle big set mergers more efficiently!

Parameters:

in_set – Range set to add in.

Returns:

This RangeSet after insertion.

inline RangeSet &InsertRange(T start, T stop)

Insert a range into this set, specifying the start and end points.

Parameters:
  • start – Beginning of new range to include.

  • stop – Ending of new range to include (range is not inclusive of stop)

Returns:

This RangeSet after insertion.

inline RangeSet &Remove(T val)

Remove a single value from this RangeSet.

Parameters:

val – Value to remove

Returns:

This RangeSet after removal.

inline RangeSet &RemoveTo(T val)

Remove all ranges (or partial range) less than a target value.

Parameters:

val – New floor for ranges.

Returns:

This RangeSet after removal.

inline RangeSet &RemoveFrom(T val)

Remove all ranges (or partial range) greater than a target value.

Parameters:

val – New cap for ranges.

Returns:

This RangeSet after removal.

inline RangeSet &Remove(range_t rm_range)

Remove a whole Range from this RangeSet.

Parameters:

rm_range – Range to remove

Returns:

This RangeSet after removal.

inline RangeSet &Remove(const this_t &in_set)

Remove all ranges in an entire range set from this one.

Note

Can be optimized to handle big sets more efficiently!

Parameters:

in_set – Range set to remove.

Returns:

This RangeSet after removal.

inline RangeSet &RemoveRange(T start, T stop)
inline RangeSet &KeepOnly(T start, T stop)

Remove everything outside of the provided range.

inline RangeSet &KeepOnly(range_t keep_range)

Remove everything outside of the provided range.

inline RangeSet &KeepOnly(const this_t &in_set)

Remove everything outside of the provided set of ranges.

inline this_t CalcInverse() const

Calculate the inverted range set, swapping included and excluded values.

Returns:

The inverted RangeSet.

inline this_t &Invert()
inline this_t operator~() const
inline this_t operator|(const this_t &in) const
inline this_t operator&(const this_t &in) const
inline this_t operator^(const this_t &in)
inline this_t operator<<(const T shift) const
inline this_t operator>>(const T shift) const
inline bool operator[](T val) const
inline this_t &operator|=(const this_t &in)
inline this_t &operator&=(const this_t &in)
inline this_t &operator^=(const this_t &in)
inline this_t &operator<<=(const T shift)
inline this_t &operator>>=(const T shift)
inline explicit operator bool() const
inline bool OK() const

Check for internal errors in this RangeSet.

Public Static Functions

static inline constexpr T MaxLimit()
static inline constexpr T MinLimit()

Public Static Attributes

static constexpr bool is_integral = std::is_integral<T>()

Private Functions

inline size_t _FindRange(T value) const
inline void _InsertRange(size_t id, range_t range)
inline void _RemoveRange(size_t id)
inline void _RemoveRanges(size_t id, size_t count)
inline void _PruneEmptyFront()
inline void _PruneEmptyBack()
inline void _CleanupMerge(size_t id)
inline void _FromString(String in)

Private Members

vector<range_t> range_set

Friends

inline friend std::ostream &operator<<(std::ostream &out, const this_t &range)

Overload ostream operator to return Print.