2017-04-07 02:12:24 +08:00
|
|
|
//===--------------------- Range.cpp -----------------------------*- C++-*-===//
|
2013-06-18 06:51:50 +08:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "lldb/Utility/Range.h"
|
|
|
|
|
2017-04-07 02:12:24 +08:00
|
|
|
#include <algorithm>
|
|
|
|
#include <utility>
|
|
|
|
|
2013-06-18 06:51:50 +08:00
|
|
|
using namespace lldb_utility;
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
Range::Range(const Range &rng) : m_low(rng.m_low), m_high(rng.m_high) {
|
|
|
|
InitRange();
|
2013-06-18 06:51:50 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
Range::Range(Range::ValueType low, Range::ValueType high)
|
|
|
|
: m_low(low), m_high(high) {
|
|
|
|
InitRange();
|
2013-06-18 06:51:50 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
void Range::InitRange() {
|
|
|
|
if (m_low == OPEN_END) {
|
|
|
|
if (m_high == OPEN_END)
|
|
|
|
m_low = 0;
|
|
|
|
else {
|
|
|
|
// make an empty range
|
|
|
|
m_low = 1;
|
|
|
|
m_high = 0;
|
2013-06-18 06:51:50 +08:00
|
|
|
}
|
2016-09-07 04:57:50 +08:00
|
|
|
}
|
2013-06-18 06:51:50 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
Range &Range::operator=(const Range &rhs) {
|
|
|
|
if (&rhs != this) {
|
|
|
|
this->m_low = rhs.m_low;
|
|
|
|
this->m_high = rhs.m_high;
|
|
|
|
}
|
|
|
|
return *this;
|
2013-06-18 06:51:50 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
void Range::Flip() { std::swap(m_high, m_low); }
|
2013-06-18 06:51:50 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
void Range::Intersection(const Range &other) {
|
|
|
|
m_low = std::max(m_low, other.m_low);
|
|
|
|
m_high = std::min(m_high, other.m_high);
|
2013-06-18 06:51:50 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
void Range::Union(const Range &other) {
|
|
|
|
m_low = std::min(m_low, other.m_low);
|
|
|
|
m_high = std::max(m_high, other.m_high);
|
2013-06-18 06:51:50 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
void Range::Iterate(RangeCallback callback) {
|
|
|
|
ValueType counter = m_low;
|
|
|
|
while (counter <= m_high) {
|
|
|
|
bool should_continue = callback(counter);
|
|
|
|
if (!should_continue)
|
|
|
|
return;
|
|
|
|
counter++;
|
|
|
|
}
|
2013-06-18 06:51:50 +08:00
|
|
|
}
|
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
bool Range::IsEmpty() { return (m_low > m_high); }
|
2013-06-18 06:51:50 +08:00
|
|
|
|
2016-09-07 04:57:50 +08:00
|
|
|
Range::ValueType Range::GetSize() {
|
|
|
|
if (m_high == OPEN_END)
|
|
|
|
return OPEN_END;
|
|
|
|
if (m_high >= m_low)
|
|
|
|
return m_high - m_low + 1;
|
|
|
|
return 0;
|
2013-06-18 06:51:50 +08:00
|
|
|
}
|