7 #ifndef RETDEC_COMMON_RANGE_H
8 #define RETDEC_COMMON_RANGE_H
14 #include <type_traits>
28 virtual const char*
what() const noexcept
override
30 return "Invalid Range: end is greater than start";
44 template <
typename T>
class Range
87 noexcept(std::is_nothrow_move_constructible<RangeType>::value) :
253 return !(*
this == rhs);
260 return out << std::hex << std::showbase
279 template <
typename T>
286 using iterator =
typename std::vector<RangeType>::iterator;
312 decltype(
auto) operator[](std::
size_t index) {
return _ranges[index]; }
313 decltype(
auto) operator[](std::
size_t index)
const {
return _ranges[index]; }
327 template <
typename RangeT>
328 std::pair<iterator,bool>
insert(RangeT&& range)
331 auto startItr = std::lower_bound(
335 [](
const auto& range,
const auto& start) {
336 return range.getEnd() < start;
339 auto endItr = std::upper_bound(
343 [](
const auto&
end,
const auto& range) {
344 return end < range.getStart();
350 if (startItr == endItr)
352 auto it =
_ranges.insert(startItr, std::forward<RangeT>(range));
354 return std::make_pair(it,
true);
360 auto newStart = std::min(range.getStart(), startItr->getStart());
361 auto newEnd = std::max(range.getEnd(), (endItr - 1)->getEnd());
362 bool startChanged = startItr->getStart() != newStart;
363 bool endChanged = startItr->getEnd() != newEnd;
365 if (startItr + 1 != endItr)
367 _ranges.erase(startItr + 1, endItr);
371 return std::make_pair(startItr, startChanged || endChanged);
374 template <
typename RangeT>
394 auto pos = std::lower_bound(
402 return (last->contains(e)) ? (&(*last)) : (
nullptr);
405 if (pos !=
_ranges.begin() && pos->getStart() != e)
410 return pos->contains(e) ? &(*pos) :
nullptr;
421 return rr ? *rr == r :
false;
431 while (pos !=
_ranges.end() && pos->getStart() <= r.
getEnd())
434 || pos->contains(r.
getEnd())
virtual const char * what() const noexcept override
Definition: range.h:28
InvalidRangeException() noexcept
Definition: range.h:24
virtual ~InvalidRangeException()=default
InvalidRangeException(const InvalidRangeException &) noexcept=default
typename std::vector< RangeType >::const_iterator const_iterator
Definition: range.h:287
auto back() const
Definition: range.h:310
auto front()
Definition: range.h:307
bool operator==(const RangeContainer &o) const
Definition: range.h:297
Range< T > RangeType
Definition: range.h:283
auto begin() const
Definition: range.h:302
std::vector< RangeType > _ranges
Definition: range.h:480
bool containsExact(const RangeType &r) const
Definition: range.h:418
std::size_t size() const
Definition: range.h:304
RangeContainer & operator=(const RangeContainer &)=default
void remove(const RangeElementType &s, const RangeElementType &e)
Definition: range.h:463
void clear()
Definition: range.h:306
bool empty() const
Definition: range.h:305
RangeContainer(RangeContainer &&)=default
std::pair< iterator, bool > insert(const RangeElementType &s, const RangeElementType &e)
Definition: range.h:380
RangeType value_type
Definition: range.h:289
bool contains(const RangeElementType &e) const
Definition: range.h:413
std::pair< iterator, bool > insert(const_iterator, RangeT &&range)
Definition: range.h:375
auto back()
Definition: range.h:309
std::pair< iterator, bool > insert(RangeT &&range)
Definition: range.h:328
auto front() const
Definition: range.h:308
typename std::vector< RangeType >::iterator iterator
Definition: range.h:286
void remove(const RangeType &r)
Definition: range.h:424
RangeContainer(const RangeContainer &)=default
T RangeElementType
Definition: range.h:284
bool operator!=(const RangeContainer &o) const
Definition: range.h:298
auto end() const
Definition: range.h:303
RangeContainer & operator=(RangeContainer &&)=default
auto end()
Definition: range.h:301
const RangeType * getRange(const RangeElementType &e) const
Definition: range.h:387
auto begin()
Definition: range.h:300
friend std::ostream & operator<<(std::ostream &out, const RangeContainer< RangeElementType > &r)
Definition: range.h:468
Range(const RangeType &start, const RangeType &end)
Definition: range.h:63
RangeType _end
Definition: range.h:266
RangeType getSize() const
Definition: range.h:183
bool contains(const Range< RangeType > &o) const
Definition: range.h:209
void setEnd(const RangeType &end)
Definition: range.h:153
Range()
Definition: range.h:52
const RangeType & getEnd() const
Definition: range.h:129
bool contains(const RangeType &value) const
Definition: range.h:196
void setStart(const RangeType &start)
Definition: range.h:139
bool operator!=(const Range< RangeType > &rhs) const
Definition: range.h:251
bool overlaps(const Range< RangeType > &o) const
Definition: range.h:218
Range(const Range< RangeType > &range)
Definition: range.h:76
void setStartEnd(const RangeType &start, const RangeType &end)
Definition: range.h:168
RangeType _start
Definition: range.h:265
friend std::ostream & operator<<(std::ostream &out, const Range< RangeType > &r)
Definition: range.h:256
Range(Range< RangeType > &&range) noexcept(std::is_nothrow_move_constructible< RangeType >::value)
Definition: range.h:86
const RangeType & getStart() const
Definition: range.h:119
Range & operator=(const Range< RangeType > &rhs)=default
bool operator<(const Range< RangeType > &rhs) const
Definition: range.h:240
T RangeType
Definition: range.h:47
bool operator==(const Range< RangeType > &rhs) const
Definition: range.h:229
Definition: archive_wrapper.h:19