blob: 9d1d28ea484bd1dd8c43afe35ab01815b14186a3 [file] [log] [blame]
Zachary Turner4479ac12017-04-06 18:12:241//===--------------------- Range.cpp -----------------------------*- C++-*-===//
Enrico Granata7594f142013-06-17 22:51:502//
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 Turner4479ac12017-04-06 18:12:2412#include <algorithm>
13#include <utility>
14
Enrico Granata7594f142013-06-17 22:51:5015using namespace lldb_utility;
16
Kate Stoneb9c1b512016-09-06 20:57:5017Range::Range(const Range &rng) : m_low(rng.m_low), m_high(rng.m_high) {
18 InitRange();
Enrico Granata7594f142013-06-17 22:51:5019}
20
Kate Stoneb9c1b512016-09-06 20:57:5021Range::Range(Range::ValueType low, Range::ValueType high)
22 : m_low(low), m_high(high) {
23 InitRange();
Enrico Granata7594f142013-06-17 22:51:5024}
25
Kate Stoneb9c1b512016-09-06 20:57:5026void Range::InitRange() {
27 if (m_low == OPEN_END) {
Enrico Granata7594f142013-06-17 22:51:5028 if (m_high == OPEN_END)
Kate Stoneb9c1b512016-09-06 20:57:5029 m_low = 0;
30 else {
31 // make an empty range
32 m_low = 1;
33 m_high = 0;
34 }
35 }
36}
37
38Range &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
46void Range::Flip() { std::swap(m_high, m_low); }
47
48void 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
53void 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
58void 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
68bool Range::IsEmpty() { return (m_low > m_high); }
69
70Range::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 Granata7594f142013-06-17 22:51:5076}