STK++ 0.9.13
STK_Range.h
Go to the documentation of this file.
1/*--------------------------------------------------------------------*/
2/* Copyright (C) 2004-2016 Serge Iovleff, Université Lille 1, Inria
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
13
14 You should have received a copy of the GNU Lesser General Public
15 License along with this program; if not, write to the
16 Free Software Foundation, Inc.,
17 59 Temple Place,
18 Suite 330,
19 Boston, MA 02111-1307
20 USA
21
22 Contact: S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
23*/
24
25/*
26 * Project: STKernel::Base
27 * Purpose: Define the Range class.
28 * Author: Serge Iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
29 **/
30
36#ifndef STK_RANGE_H
37#define STK_RANGE_H
38
39#include <map>
40
45#if defined(STKBASEARRAYS)
46const int firstIdx_ = STKBASEARRAYS;
47#else
48const int firstIdx_ = 0; // default is 0 based array
49#endif
50
51
53#define _R(first,last) Range(first, last, 0)
54
55namespace STK
56{
57// forward declaration
58template<int Size_> class TRange;
60
61template<int Size_>
63
67template<class Derived>
68class RangeBase: public IRecursiveTemplate<Derived>
69{
70 protected:
76 inline RangeBase( int begin): begin_(begin) {}
80 inline RangeBase( RangeBase const& I): begin_(I.begin_) {}
84 template<class OtherDerived>
86
87 public:
89 inline ~RangeBase() {}
93 inline int begin() const { return begin_;};
94
99 template<int OtherSize_>
100 inline bool isIn(TRange<OtherSize_> const& I) const
101 { return ((this->asDerived().begin() >= I.begin()) && (this->asDerived().end() <= I.end()));}
106 template<int OtherSize_>
107 inline bool isContaining(TRange<OtherSize_> const& I) const
108 { return ((begin_<= I.begin())&&(this->asDerived().end()>=I.end()));}
113 inline bool isContaining(int i) const { return ((begin_<=i)&&(i<this->asDerived().end()));}
114
119 inline Derived& operator+=(int i) { return this->asDerived().inc(i);}
124 inline Derived& operator-=(int i) { return this->asDerived().dec(i);}
125
130 template<int OtherSize_>
131 inline bool operator==(TRange<OtherSize_> const& I) const
132 { return ((begin_ == I.begin()) && (this->asDerived().end() == I.end()));}
137 template<int OtherSize_>
138 inline bool operator!=(TRange<OtherSize_> const& I) const
139 { return ((begin_ != I.begin()) || (this->asDerived().end() != I.end()));}
140
141 protected:
142 int begin_;
143};
144
159template<int Size_>
160class TRange: public RangeBase< TRange<Size_> >
161{
162 public:
164 using Base::begin_;
165
169 inline TRange( int size = Size_): Base() {}
173 inline TRange( int first, int ): Base(first) {}
177 inline TRange( int first, int , bool ): Base(first) {}
181 inline TRange(TRange const& I): Base(I) {}
185 template<int OtherSize_>
186 inline TRange(TRange<OtherSize_> const& I): Base(I) {}
188 inline ~TRange() {}
192 inline int end() const { return begin_ + Size_;};
196 inline int size() const { return Size_;};
200 inline bool empty() const { return Size_<=0;};
201
202 // backward compatibility
206 inline int lastIdx() const { return begin_ + Size_ -1;};
207
210 inline TRange& shift(int begin) { begin_= begin; return *this;}
214 inline TRange& inc(int inc =1) { begin_ +=inc; return *this;}
218 inline TRange& dec(int dec=1) { begin_ -=dec; return *this;}
219
223 inline TRange& incFirst(int inc=1) { begin_ +=inc; return *this;}
227 inline TRange& decFirst(int dec=1) { begin_ -=dec; return *this;}
228
232 inline TRange& incEnd(int inc =1) { return *this;}
236 inline TRange& decEnd(int dec =1){ return *this;}
237
238 // backward compatibility
242 inline TRange& incLast(int inc =1) { return *this;}
246 inline TRange& decLast(int dec =1){ return *this;}
247};
248
263template<>
264class TRange<UnknownSize>: public RangeBase< TRange<UnknownSize> >
265{
266 public:
268 using Base::begin_;
269
273 inline TRange( int size =0): Base(), size_(size) {}
277 inline TRange( int first, int size): Base(first), size_(size) {}
282 inline TRange( int first, int last, bool junk): Base(first), size_(last+1-first) {}
287 inline TRange(TRange const& I): Base(I), size_(I.size_) {}
292 template<int OtherSize_>
293 inline TRange(TRange<OtherSize_> const& I): Base(I), size_(I.size()) {}
295 inline ~TRange() {}
299 inline int end() const { return begin_+ size_;}
303 inline int size() const { return size_;}
307 inline bool empty() const { return size_<=0;}
308
309 // backward compatibility
313 inline int lastIdx() const { return(end()-1);}
314
317 inline TRange& shift(int first) { return inc(first - begin_);}
321 inline TRange& inc(int inc =1){ begin_ +=inc; return *this;}
325 inline TRange& dec(int dec =1) { begin_ -=dec; return *this;}
330 inline TRange& incFirst(int inc =1) { begin_ +=inc; size_ -=inc; return *this;}
334 inline TRange& decFirst(int dec =1) { begin_ -=dec; size_ +=dec; return *this;}
335
339 inline TRange& incEnd(int inc =1) { size_ +=inc; return *this;}
343 inline TRange& decEnd(int dec =1){ size_ -=dec; return *this;}
344
345 // backward compatibility
349 inline TRange& incLast(int inc =1) { size_ +=inc; return *this;}
353 inline TRange& decLast(int dec =1){ size_ -=dec; return *this;}
354
359 template<int OtherSize_>
361 {
362 begin_ = std::min(begin_, I.begin());
363 size_ = std::max(end(), I.end()) - begin_;
364 return *this;
365 }
370 template<int OtherSize_>
372 {
373 begin_ = std::max(begin_, I.begin());
374 size_ = std::min(end(), I.end()) - begin_;
375 return *this;
376 }
377
386 friend istream& operator>> (istream& is, Range& I);
387
388 private:
389 int size_;
390
391};
392
397template<int Size_>
398struct Arithmetic< TRange<Size_> >: public std::numeric_limits< TRange<Size_> >
399{
401 static inline TRange<Size_> NA() throw()
402 { return TRange<Size_>(std::numeric_limits<int>::min(), std::numeric_limits<int>::min(), 0);}
404 static const bool hasNA = true;
408 static inline bool isNA(TRange<Size_> const& x) throw()
409 { return (x.begin() == std::numeric_limits<int>::min());}
413 static inline bool isInfinite(TRange<Size_> const& x) throw() { return false; }
417 static inline bool isFinite(TRange<Size_> const& x) throw() { return (!isNA(x) && !isInfinite(x));}
418};
419
423template<int Size_>
425{
427 static inline Base::IdType returnType() { return(Base::range_);}
428};
429
430
432{
433 String str;
434 std::getline(is, str, _T(':'));
435 // get first number
436 if (!stringToType(I.begin_, str))
437 {
439 is.clear(); is.setstate(std::ios::failbit);
440 return is;
441 }
442 // check if the istream is exhausted
443 if (is.eof())
444 {
445 I.size_ = 1;
446 return is;
447 }
448 // skip the current char ":"
449 is.peek();
450 // get second number
451 if ((is >> I.size_).fail())
452 {
454 is.clear(); is.setstate(std::ios::failbit);
455 return is;
456 }
457 else { I.size_ -= I.begin_ ;}
458 return is;
459}
460
467template<int SizeI_, int SizeJ_>
469{ return Range(std::min(I.begin(), J.begin()), std::max(I.lastIdx(), J.lastIdx()), 0);}
476template<int SizeI_, int SizeJ_>
478{ return Range(std::max(I.begin(), J.begin()), std::min(I.lastIdx(), J.lastIdx()), 0);}
483template<int SizeI_>
485{ return Range(I.begin()+1, I.lastIdx(), 0);}
490template<int SizeI_>
492{ return Range(I.begin(), I.lastIdx()+1, 0);}
497template<int SizeI_>
499{ return Range(I.begin()-1, I.lastIdx(), 0);}
504template<int SizeI_>
506{ return Range(I.begin(), I.lastIdx()-1, 0);}
507
514template<int Size_>
516{
518 : os << I.begin() << _T(":") << I.lastIdx();
519 return os;
520}
521
529{ return stringToType<Range>(str);}
530
538template<int Size_>
540{
541 typename std::map<String, TRange<Size_> >::const_iterator it=mapping.find(str);
542 return (it == mapping.end()) ? Arithmetic< TRange<Size_> >::NA() : it->second;
543}
544
551template<int Size_>
552String rangeToString( TRange<Size_> const& value, std::ios_base& (*f)(std::ios_base&) = std::dec)
553{
554 if (Arithmetic<TRange<Size_> >::isNA(value)) return stringNa;
556 os << f << value;
557 return os.str();
558}
559
566template<int Size_>
568{
569 typename std::map<TRange<Size_> , String>::const_iterator it=mapping.find(value);
570 if (it == mapping.end()) return stringNa;
571 return it->second;
572}
573
579template<>
580inline String typeToString<Range >( Range const& t, std::ios_base& (*f)(std::ios_base&))
581{ return rangeToString(t, f);}
582
583
584} // namespace STK
585
586#endif // STK_RANGE_H
#define _T(x)
Let x unmodified.
Interface base class for all classes implementing the curious recursive template paradigm.
Derived & asDerived()
static cast : return a reference of this with a cast to the derived class.
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
Base class for TRange class.
Definition STK_Range.h:69
Derived & operator-=(int i)
shift range a:b becomes (a+i):(b+i)
Definition STK_Range.h:124
Derived & operator+=(int i)
shift range a:b becomes (a+i):(b+i)
Definition STK_Range.h:119
bool operator!=(TRange< OtherSize_ > const &I) const
compare this range with range I
Definition STK_Range.h:138
bool isIn(TRange< OtherSize_ > const &I) const
check if this TRange in include in an other TRange
Definition STK_Range.h:100
RangeBase(int begin)
constructor.
Definition STK_Range.h:76
int begin_
First index.
Definition STK_Range.h:142
RangeBase()
Default constructor.
Definition STK_Range.h:72
int begin() const
get the first index of the TRange.
Definition STK_Range.h:93
bool operator==(TRange< OtherSize_ > const &I) const
compare this range with range I
Definition STK_Range.h:131
bool isContaining(TRange< OtherSize_ > const &I) const
check if the TRange I is include in the this TRange
Definition STK_Range.h:107
RangeBase(RangeBase const &I)
Copy constructor.
Definition STK_Range.h:80
~RangeBase()
destructor.
Definition STK_Range.h:89
bool isContaining(int i) const
Return true if i is in this TRange.
Definition STK_Range.h:113
RangeBase(RangeBase< OtherDerived > const &I)
Copy constructor.
Definition STK_Range.h:85
Index sub-vector region: Specialization when the size is unknown.
Definition STK_Range.h:265
RangeBase< TRange< UnknownSize > > Base
Definition STK_Range.h:267
TRange & dec(int dec=1)
create the TRange [begin_+inc, end_).
Definition STK_Range.h:325
TRange(TRange< OtherSize_ > const &I)
Copy constructor.
Definition STK_Range.h:293
TRange(int first, int last, bool junk)
Complete constructor.
Definition STK_Range.h:282
TRange & incLast(int inc=1)
create the TRange [begin_, end_+inc)
Definition STK_Range.h:349
TRange & decLast(int dec=1)
create the TRange [begin_, end_-dec)
Definition STK_Range.h:353
TRange & decFirst(int dec=1)
create the TRange [begin_-dec, end_)
Definition STK_Range.h:334
TRange & inc(int inc=1)
create the TRange [begin_+inc, end_+inc_).
Definition STK_Range.h:321
int lastIdx() const
get the last index of the TRange.
Definition STK_Range.h:313
TRange & decEnd(int dec=1)
create the TRange [begin_, end_-dec)
Definition STK_Range.h:343
bool empty() const
check if the range is empty or not.
Definition STK_Range.h:307
int end() const
get the ending index of the TRange.
Definition STK_Range.h:299
TRange(TRange const &I)
Copy constructor.
Definition STK_Range.h:287
TRange & inf(TRange< OtherSize_ > const &I)
Take the largest value of begin_ and I.begin_ for begin_ and the lowest value of end_ and I....
Definition STK_Range.h:371
int size_
theoretic Dimension size_ = end_- begin_
Definition STK_Range.h:389
TRange & incEnd(int inc=1)
create the TRange [begin_, end_+inc)
Definition STK_Range.h:339
TRange(int size=0)
constructor.
Definition STK_Range.h:273
TRange & shift(int first)
Shift the TRange giving the first element: the size is not modified.
Definition STK_Range.h:317
int size() const
get the size of the TRange (the number of elements).
Definition STK_Range.h:303
TRange & sup(TRange< OtherSize_ > const &I)
Take the lowest value of begin_ and I.begin_ for begin_ and the largest value of end_ and I....
Definition STK_Range.h:360
TRange & incFirst(int inc=1)
create the TRange [begin_-dec, end_-dec)
Definition STK_Range.h:330
TRange(int first, int size)
Complete constructor.
Definition STK_Range.h:277
Index sub-vector region with fixed size.
Definition STK_Range.h:161
TRange & incLast(int inc=1)
create the TRange [begin_, end_+inc)
Definition STK_Range.h:242
int end() const
get the ending index of the TRange.
Definition STK_Range.h:192
TRange & decFirst(int dec=1)
create the TRange [begin_-dec, end_]
Definition STK_Range.h:227
TRange(int size=Size_)
Default constructor.
Definition STK_Range.h:169
RangeBase< TRange< Size_ > > Base
Definition STK_Range.h:163
int size() const
get the size of the TRange (the number of elements).
Definition STK_Range.h:196
~TRange()
destructor.
Definition STK_Range.h:188
TRange & dec(int dec=1)
create the TRange [begin_-dec, end_-dec]
Definition STK_Range.h:218
TRange(int first, int, bool)
Complete constructor.
Definition STK_Range.h:177
TRange & inc(int inc=1)
create the TRange [begin_+inc, end_+inc_].
Definition STK_Range.h:214
TRange & incFirst(int inc=1)
create the TRange [begin_+inc, end_].
Definition STK_Range.h:223
TRange(TRange const &I)
copy constructor
Definition STK_Range.h:181
int lastIdx() const
get the last index of the TRange.
Definition STK_Range.h:206
bool empty() const
check if the range is empty or not.
Definition STK_Range.h:200
TRange & shift(int begin)
Shift the TRange giving the first element.
Definition STK_Range.h:210
TRange(TRange< OtherSize_ > const &I)
copy constructor
Definition STK_Range.h:186
TRange(int first, int)
Full constructor.
Definition STK_Range.h:173
TRange & decLast(int dec=1)
create the TRange [begin_, end_-dec)
Definition STK_Range.h:246
TRange & decEnd(int dec=1)
create the TRange [begin_, end_-dec)
Definition STK_Range.h:236
TRange & incEnd(int inc=1)
create the TRange [begin_, end_+inc)
Definition STK_Range.h:232
bool isNA(Type const &x)
utility method allowing to know if a value is a NA (Not Available) value
ostream & operator<<(ostream &s, const CAllocator< Type, SizeRows_, SizeCols_, Orient_ > &V)
output stream for CAllocator.
Definition STK_CArray.h:221
Range stringToRange(String const &str)
Convert a String to a Range.
Definition STK_Range.h:528
bool stringToType(Type &t, String const &s, std::ios_base &(*f)(std::ios_base &)=std::dec)
convert a String to Type
Definition STK_String.h:195
String rangeToString(TRange< Size_ > const &value, std::ios_base &(*f)(std::ios_base &)=std::dec)
Convert an Range to a String.
Definition STK_Range.h:552
String stringNa
Representation of a Not Available value.
String typeToString< Range >(Range const &t, std::ios_base &(*f)(std::ios_base &))
Specialization for Range.
Definition STK_Range.h:580
IdType
Id for the Type of a variable.
@ range_
Range type.
Range inf(TRange< SizeI_ > const &I, TRange< SizeJ_ > const &J)
compute inf(I,J).
Definition STK_Range.h:477
Range incLast(TRange< SizeI_ > const &I)
if I=a:b, return a:b+1
Definition STK_Range.h:491
Range decLast(TRange< SizeI_ > const &I)
if I=a:b, return a:b-1
Definition STK_Range.h:505
Range sup(TRange< SizeI_ > const &I, TRange< SizeJ_ > const &J)
compute sup(I,J).
Definition STK_Range.h:468
Range incFirst(TRange< SizeI_ > const &I)
if I=a:b, return a+1:b
Definition STK_Range.h:484
Range decFirst(TRange< SizeI_ > const &I)
if I=a:b, return a-1:b
Definition STK_Range.h:498
std::basic_string< Char > String
STK fundamental type of a String.
const int UnknownSize
This value means that an integer is not known at compile-time, and that instead the value is stored i...
const int firstIdx_
base index of the containers created in STK++.
Definition STK_Range.h:48
std::basic_ostream< Char > ostream
ostream for Char
Definition STK_Stream.h:57
std::basic_ostringstream< Char > ostringstream
ostringstream (ostringstream) for Char
Definition STK_Stream.h:65
std::basic_istream< Char > istream
istream for Char
Definition STK_Stream.h:55
The namespace STK is the main domain space of the Statistical ToolKit project.
istream & operator>>(istream &is, Binary &value)
Overloading of the istream >> for the type Binary.
Definition STK_Binary.h:104
TRange< UnknownSize > Range
Definition STK_Range.h:59
static TRange< Size_ > NA()
Adding a Non Available (NA) special number.
Definition STK_Range.h:401
static bool isNA(TRange< Size_ > const &x)
Test if x is a Non Available (NA) special number.
Definition STK_Range.h:408
static bool isFinite(TRange< Size_ > const &x)
test if x is finite.
Definition STK_Range.h:417
static bool isInfinite(TRange< Size_ > const &x)
test if x is infinite.
Definition STK_Range.h:413
Arithmetic properties of STK fundamental types.
static bool isInfinite(Type const &x)
static const bool hasNA
True if the type has a representation for a "Not Available."
static Type NA()
Adding a Non Available (NA) special number.
static bool isNA(Type const &x)
static Base::IdType returnType()
Definition STK_Range.h:427
Implementation of the return type.