1 /* 2 * KiRouter - a push-and-(sometimes-)shove PCB router 3 * 4 * Copyright (C) 2013-2014 CERN 5 * Author: Tomasz Wlostowski <tomasz.wlostowski@cern.ch> 6 * 7 * This program is free software: you can redistribute it and/or modify it 8 * under the terms of the GNU General Public License as published by the 9 * Free Software Foundation, either version 3 of the License, or (at your 10 * option) any later version. 11 * 12 * This program is distributed in the hope that it will be useful, but 13 * WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * General Public License for more details. 16 * 17 * You should have received a copy of the GNU General Public License along 18 * with this program. If not, see <http://www.gnu.org/licenses/>. 19 */ 20 21 #ifndef __RANGE_H 22 #define __RANGE_H 23 24 template<class T> 25 class RANGE 26 { 27 public: RANGE(T aMin,T aMax)28 RANGE( T aMin, T aMax ) : 29 m_min( aMin ), 30 m_max( aMax ), 31 m_defined( true ) {} 32 RANGE()33 RANGE(): 34 m_defined( false ) {} 35 MinV()36 T MinV() const 37 { 38 return m_min; 39 } 40 MaxV()41 T MaxV() const 42 { 43 return m_max; 44 } 45 Set(T aMin,T aMax)46 void Set( T aMin, T aMax ) const 47 { 48 m_max = aMax; 49 m_min = aMin; 50 } 51 Grow(T aValue)52 void Grow( T aValue ) 53 { 54 if( !m_defined ) 55 { 56 m_min = aValue; 57 m_max = aValue; 58 m_defined = true; 59 } 60 else 61 { 62 m_min = std::min( m_min, aValue ); 63 m_max = std::max( m_max, aValue ); 64 } 65 } 66 Inside(const T & aValue)67 bool Inside( const T& aValue ) const 68 { 69 if( !m_defined ) 70 return true; 71 72 return aValue >= m_min && aValue <= m_max; 73 } 74 Overlaps(const RANGE<T> & aOther)75 bool Overlaps ( const RANGE<T>& aOther ) const 76 { 77 if( !m_defined || !aOther.m_defined ) 78 return true; 79 80 return m_max >= aOther.m_min && m_min <= aOther.m_max; 81 } 82 Defined()83 bool Defined() const 84 { 85 return m_defined; 86 } 87 88 private: 89 T m_min, m_max; 90 bool m_defined; 91 }; 92 93 #endif 94