Home | History | Annotate | Download | only in Utility
      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 
     12 using namespace lldb_utility;
     13 
     14 Range::Range (const Range& rng) :
     15 m_low(rng.m_low),
     16 m_high(rng.m_high)
     17 {
     18     InitRange();
     19 }
     20 
     21 Range::Range (Range::ValueType low,
     22               Range::ValueType high) :
     23 m_low(low),
     24 m_high(high)
     25 {
     26     InitRange();
     27 }
     28 
     29 void
     30 Range::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 
     45 Range&
     46 Range::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 
     56 void
     57 Range::Flip ()
     58 {
     59     std::swap(m_high, m_low);
     60 }
     61 
     62 void
     63 Range::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 
     69 void
     70 Range::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 
     76 void
     77 Range::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 
     89 bool
     90 Range::IsEmpty ()
     91 {
     92     return (m_low > m_high);
     93 }
     94 
     95 Range::ValueType
     96 Range::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