blob: 95f00e5c75999b68945985123cdf55eea5c100e7 [file] [log] [blame]
Kate Stoneb9c1b512016-09-06 20:57:50 +00001//===--------------------- Range.cpp -----------------------------*- C++
2//-*-===//
Enrico Granata7594f142013-06-17 22:51:50 +00003//
4// The LLVM Compiler Infrastructure
5//
6// This file is distributed under the University of Illinois Open Source
7// License. See LICENSE.TXT for details.
8//
9//===----------------------------------------------------------------------===//
10
11#include "lldb/Utility/Range.h"
12
13using namespace lldb_utility;
14
Kate Stoneb9c1b512016-09-06 20:57:50 +000015Range::Range(const Range &rng) : m_low(rng.m_low), m_high(rng.m_high) {
16 InitRange();
Enrico Granata7594f142013-06-17 22:51:50 +000017}
18
Kate Stoneb9c1b512016-09-06 20:57:50 +000019Range::Range(Range::ValueType low, Range::ValueType high)
20 : m_low(low), m_high(high) {
21 InitRange();
Enrico Granata7594f142013-06-17 22:51:50 +000022}
23
Kate Stoneb9c1b512016-09-06 20:57:50 +000024void Range::InitRange() {
25 if (m_low == OPEN_END) {
Enrico Granata7594f142013-06-17 22:51:50 +000026 if (m_high == OPEN_END)
Kate Stoneb9c1b512016-09-06 20:57:50 +000027 m_low = 0;
28 else {
29 // make an empty range
30 m_low = 1;
31 m_high = 0;
32 }
33 }
34}
35
36Range &Range::operator=(const Range &rhs) {
37 if (&rhs != this) {
38 this->m_low = rhs.m_low;
39 this->m_high = rhs.m_high;
40 }
41 return *this;
42}
43
44void Range::Flip() { std::swap(m_high, m_low); }
45
46void Range::Intersection(const Range &other) {
47 m_low = std::max(m_low, other.m_low);
48 m_high = std::min(m_high, other.m_high);
49}
50
51void Range::Union(const Range &other) {
52 m_low = std::min(m_low, other.m_low);
53 m_high = std::max(m_high, other.m_high);
54}
55
56void Range::Iterate(RangeCallback callback) {
57 ValueType counter = m_low;
58 while (counter <= m_high) {
59 bool should_continue = callback(counter);
60 if (!should_continue)
61 return;
62 counter++;
63 }
64}
65
66bool Range::IsEmpty() { return (m_low > m_high); }
67
68Range::ValueType Range::GetSize() {
69 if (m_high == OPEN_END)
70 return OPEN_END;
71 if (m_high >= m_low)
72 return m_high - m_low + 1;
73 return 0;
Enrico Granata7594f142013-06-17 22:51:50 +000074}