Zachary Turner | 4479ac1 | 2017-04-06 18:12:24 | [diff] [blame] | 1 | //===--------------------- Range.cpp -----------------------------*- C++-*-===// |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 | [diff] [blame] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "lldb/Utility/Range.h" |
| 11 | |
Zachary Turner | 4479ac1 | 2017-04-06 18:12:24 | [diff] [blame] | 12 | #include <algorithm> |
| 13 | #include <utility> |
| 14 | |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 | [diff] [blame] | 15 | using namespace lldb_utility; |
| 16 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 17 | Range::Range(const Range &rng) : m_low(rng.m_low), m_high(rng.m_high) { |
| 18 | InitRange(); |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 | [diff] [blame] | 19 | } |
| 20 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 21 | Range::Range(Range::ValueType low, Range::ValueType high) |
| 22 | : m_low(low), m_high(high) { |
| 23 | InitRange(); |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 | [diff] [blame] | 24 | } |
| 25 | |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 26 | void Range::InitRange() { |
| 27 | if (m_low == OPEN_END) { |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 | [diff] [blame] | 28 | if (m_high == OPEN_END) |
Kate Stone | b9c1b51 | 2016-09-06 20:57:50 | [diff] [blame] | 29 | m_low = 0; |
| 30 | else { |
| 31 | // make an empty range |
| 32 | m_low = 1; |
| 33 | m_high = 0; |
| 34 | } |
| 35 | } |
| 36 | } |
| 37 | |
| 38 | Range &Range::operator=(const Range &rhs) { |
| 39 | if (&rhs != this) { |
| 40 | this->m_low = rhs.m_low; |
| 41 | this->m_high = rhs.m_high; |
| 42 | } |
| 43 | return *this; |
| 44 | } |
| 45 | |
| 46 | void Range::Flip() { std::swap(m_high, m_low); } |
| 47 | |
| 48 | void Range::Intersection(const Range &other) { |
| 49 | m_low = std::max(m_low, other.m_low); |
| 50 | m_high = std::min(m_high, other.m_high); |
| 51 | } |
| 52 | |
| 53 | void Range::Union(const Range &other) { |
| 54 | m_low = std::min(m_low, other.m_low); |
| 55 | m_high = std::max(m_high, other.m_high); |
| 56 | } |
| 57 | |
| 58 | void Range::Iterate(RangeCallback callback) { |
| 59 | ValueType counter = m_low; |
| 60 | while (counter <= m_high) { |
| 61 | bool should_continue = callback(counter); |
| 62 | if (!should_continue) |
| 63 | return; |
| 64 | counter++; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | bool Range::IsEmpty() { return (m_low > m_high); } |
| 69 | |
| 70 | Range::ValueType Range::GetSize() { |
| 71 | if (m_high == OPEN_END) |
| 72 | return OPEN_END; |
| 73 | if (m_high >= m_low) |
| 74 | return m_high - m_low + 1; |
| 75 | return 0; |
Enrico Granata | 7594f14 | 2013-06-17 22:51:50 | [diff] [blame] | 76 | } |