|
STK++ 1.0
|
00001 /*--------------------------------------------------------------------*/ 00002 /* Copyright (C) 2004-2009 Serge Iovleff 00003 00004 This program is free software; you can redistribute it and/or modify 00005 it under the terms of the GNU Lesser General Public License as 00006 published by the Free Software Foundation; either version 2 of the 00007 License, or (at your option) any later version. 00008 00009 This program is distributed in the hope that it will be useful, 00010 but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 GNU Lesser General Public License for more details. 00013 00014 You should have received a copy of the GNU Lesser General Public 00015 License along with this program; if not, write to the 00016 Free Software Foundation, Inc., 00017 59 Temple Place, 00018 Suite 330, 00019 Boston, MA 02111-1307 00020 USA 00021 00022 Contact : Serge.Iovleff@stkpp.org 00023 */ 00024 00025 /* 00026 * Project: stkpp::STKernel::Base 00027 * Purpose: implement the Range class. 00028 * Author: Serge Iovleff, serge.iovleff@stkpp.org 00029 * 00030 **/ 00031 00037 #include "../include/STK_Range.h" 00038 #include "../include/STK_Misc.h" 00039 00040 #include "../include/STK_String_Util.h" 00041 00042 namespace STK 00043 { 00044 00048 Range::Range( Integer const& last) 00049 : first_(1) 00050 , last_(last) 00051 , size_(last) 00052 { ;} 00053 00059 Range::Range( Integer const& first, Integer const& last) 00060 : first_(first) 00061 , last_(last) 00062 , size_(last-first+1) 00063 { ;} 00064 00065 00069 Range::Range( Range const& I) 00070 : first_(I.first()) 00071 , last_(I.last()) 00072 , size_(I.size()) 00073 { ;} 00074 00075 Range::~Range() { ;} 00076 00077 /*--------------------------------------------------------------------*/ 00078 /* Manipulation of the index. */ 00083 Range& Range::set(Integer const& first, Integer const& last) 00084 { 00085 first_ = first; 00086 last_ = last; 00087 size_ = last - first + 1; 00088 return *this; 00089 } 00090 00094 Range& Range::shift(Integer const& first) 00095 { 00096 return inc(first - first_); // use the inc method 00097 } 00098 00102 Range& Range::inc(Integer const& inc) 00103 { 00104 first_ +=inc; 00105 last_ +=inc; 00106 return *this; 00107 } 00111 Range& Range::incFirst(Integer const& inc) 00112 { 00113 first_ +=inc; 00114 size_ -=inc; 00115 return *this; 00116 } 00120 Range& Range::incLast(Integer const& inc) 00121 { 00122 last_ +=inc; 00123 size_ +=inc; 00124 return *this; 00125 } 00126 00130 Range& Range::dec(Integer const& dec) 00131 { 00132 first_ -=dec; 00133 last_ -=dec; 00134 return *this; 00135 } 00139 Range& Range::decFirst(Integer const& dec) 00140 { 00141 first_ -=dec; 00142 size_ +=dec; 00143 return *this; 00144 } 00148 Range& Range::decLast(Integer const& dec) 00149 { 00150 last_ -=dec; 00151 size_ -=dec; 00152 return *this; 00153 } 00154 00159 Range& Range::sup(Range const& I) 00160 { 00161 first_ = min(first_, I.first_); 00162 last_ = max(last_, I.last_); 00163 size_ = last_ - first_ +1; 00164 return *this; 00165 } 00166 00171 Range& Range::inf(Range const& I) 00172 { 00173 first_ = max(first_, I.first_); 00174 last_ = min(last_, I.last_); 00175 size_ = last_ - first_ +1; 00176 return *this; 00177 } 00183 Range Range::sup(Range const& I, Range const& J) 00184 { 00185 return Range(min(I.first_, J.first_), max(I.last_, J.last_)); 00186 } 00187 00193 Range Range::inf(Range const& I, Range const& J) 00194 { 00195 return Range(max(I.first_, J.first_), min(I.last_, J.last_)); 00196 } 00197 00199 Range Range::plus(Integer const& inc) 00200 { 00201 return *this+inc; 00202 } 00204 Range Range::minus(Integer const& dec) 00205 { 00206 return *this-dec; 00207 ; 00208 } 00209 00213 Range& Range::operator+=(Integer const& inc) 00214 { first_ += inc; last_ += inc; return *this;} 00215 00219 Range& Range::operator-=(Integer const& dec) 00220 { first_ -= dec; last_ -= dec; return *this;} 00221 00226 ostream& operator<< (ostream& s, Range const& I) 00227 { 00228 s << I.first_ << _T(":") << I.last_; 00229 return s; 00230 } 00231 00240 istream& operator>> (istream& s, Range& I) 00241 { 00242 String num; 00243 s >> std::skipws; 00244 // get first number 00245 std::getline(s, num, _T(':')); 00246 // check if the istream is exhausted 00247 if (s.eof()) 00248 { 00249 I.first_ = 1; 00250 if (!stringToType(I.last_, num)) I.last_ =0; 00251 return s; 00252 } 00253 // otherwise we encounter a ":", thus skip the current char 00254 if (!stringToType(I.first_, num)) I.first_ =1; 00255 s.peek(); 00256 if ((s >> I.last_).fail()) 00257 { 00258 I.first_ =1; I.last_ =0; 00259 } 00260 return s; 00261 } 00262 00264 Range operator-(Range const& I, Integer const& dec) 00265 { 00266 return(Range(I.first() - dec, I.last() - dec)); 00267 } 00268 00270 Range operator+(Range const& I, Integer const& inc) 00271 { 00272 return(Range(I.first() + inc, I.last() + inc)); 00273 } 00274 00275 00276 } // Namespace STK