1//===--------------------- Range.cpp -----------------------------*- C++ -*-===//
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
12using namespace lldb_utility;
13
14Range::Range (const Range& rng) :
15m_low(rng.m_low),
16m_high(rng.m_high)
17{
18    InitRange();
19}
20
21Range::Range (Range::ValueType low,
22              Range::ValueType high) :
23m_low(low),
24m_high(high)
25{
26    InitRange();
27}
28
29void
30Range::InitRange ()
31{
32    if (m_low == OPEN_END)
33    {
34        if (m_high == OPEN_END)
35            m_low = 0;
36        else
37        {
38            // make an empty range
39            m_low = 1;
40            m_high = 0;
41        }
42    }
43}
44
45Range&
46Range::operator = (const Range& rhs)
47{
48    if (&rhs != this)
49    {
50        this->m_low = rhs.m_low;
51        this->m_high = rhs.m_high;
52    }
53    return *this;
54}
55
56void
57Range::Flip ()
58{
59    std::swap(m_high, m_low);
60}
61
62void
63Range::Intersection (const Range& other)
64{
65    m_low = std::max(m_low,other.m_low);
66    m_high = std::min(m_high,other.m_high);
67}
68
69void
70Range::Union (const Range& other)
71{
72    m_low = std::min(m_low,other.m_low);
73    m_high = std::max(m_high,other.m_high);
74}
75
76void
77Range::Iterate (RangeCallback callback)
78{
79    ValueType counter = m_low;
80    while (counter <= m_high)
81    {
82        bool should_continue = callback(counter);
83        if (!should_continue)
84            return;
85        counter++;
86    }
87}
88
89bool
90Range::IsEmpty ()
91{
92    return (m_low > m_high);
93}
94
95Range::ValueType
96Range::GetSize ()
97{
98    if (m_high == OPEN_END)
99        return OPEN_END;
100    if (m_high >= m_low)
101        return m_high - m_low + 1;
102    return 0;
103}
104