STK++ 1.0
STK_Variable.h
Go to the documentation of this file.
00001 /*--------------------------------------------------------------------*/
00002 /*     Copyright (C) 2004-2007  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::DManager
00027  * Author:   Serge Iovleff, serge.iovleff@stkpp.org
00028  **/
00029 
00035 #ifndef STK_VARIABLE_H
00036 #define STK_VARIABLE_H
00037 
00038 #include "../../Arrays/include/STK_RecursiveArray1D.h"
00039 #include "../../Arrays/include/STK_Display1D.h"
00040 
00041 #include "STK_IVariable.h"
00042 
00043 namespace STK
00044 {
00045 
00052 template< class TYPE>
00053 class Variable : public IVariable
00054                , public RecursiveArray1D<TYPE, Variable<TYPE> >
00055 {
00057   typedef AllocatorBase<TYPE*> _AllocatorBaseType_;
00059   typedef RecursiveArray1D<TYPE, Variable<TYPE> > _RecArray1DType_;
00060 
00061   public:
00066     Variable( Range const& I = Range()
00067             , String const& name = Arithmetic<String>::NA()
00068             )
00069             : IVariable(IdTypeImpl<TYPE>::returnType(), name)
00070             , _RecArray1DType_(I)
00071     { ;}
00072 
00078     Variable( Range const& I, TYPE const& v, String const& name = Arithmetic<String>::NA())
00079             : IVariable(IdTypeImpl<TYPE>::returnType(), name)
00080             , _RecArray1DType_(I, v)
00081      { ;}
00082 
00087     Variable( Variable const& V, bool ref = false)
00088             : IVariable(V)
00089             , _RecArray1DType_(V, ref)
00090      { ;}
00091 
00097     Variable( Variable const& V, Range const& I, Integer const& col)
00098             : IVariable(V)
00099             , _RecArray1DType_(V, I, col)
00100     { ;}
00101 
00104     virtual ~Variable()
00105     { ;}
00106 
00110     inline TYPE& elt(Integer const& pos)
00111     { return this->data(pos);}
00112 
00116     inline TYPE const& elt(Integer const& pos) const
00117     { return this->data(pos);}
00118 
00122     inline Variable elt(Range const& J) const
00123     {
00124 #ifdef STK_BOUNDS_CHECK
00125       if ((J.first()<this->first()))
00126       { throw out_of_range("Array1D::elt(J) "
00127                                 "J.first()<this->first()");
00128       }
00129       if ((J.last()>this->last()))
00130       { throw out_of_range("Array1D::elt(J) "
00131                                 "J.last()>this->last()");
00132       }
00133 #endif
00134       return Variable(*this, J);
00135     }
00136 
00140     inline Variable& operator=(Variable const& V)
00141     {
00142             IVariable *p3 = this;   // convert this to IVariable
00143       const IVariable *p4 = &V;     // convert V    to IVariable
00144       (*p3) = (*p4);                // use = of IVariable class
00145 
00146       // check size
00147       if (size()!=V.size()) resize(V.range());
00148       // copy without ovelapping.
00149       if (first() < V.first())
00150       { for (Integer i=first(), j=V.first(); j<=V.last(); i++, j++)
00151           this->setData(i, V.data(j));
00152       }
00153       else
00154       { for (Integer i=last(), j=V.last(); j>=V.first(); i--, j--)
00155           this->setData(i, V.data(j));
00156       }
00157       return *this;
00158     }
00159 
00163     Variable& operator=(TYPE const& v)
00164     {
00165       for (Integer i=first(); i<=last(); i++)
00166         this->setData(i, v);
00167       return *this;
00168     }
00169 
00173     virtual Variable* clone( bool ref = false) const
00174     { return new Variable(*this, ref);}
00175 
00179     inline void pushBackNAValues(Integer const& n=1)
00180     {
00181       this->insert(Range(last() +1, n), Arithmetic<TYPE>::NA());
00182     }
00183 
00190     virtual Integer importString( Variable< String > const& V
00191                                 , std::ios_base& (*f)(std::ios_base&) = std::dec
00192                                 )
00193     {
00194       this->resize(V.range());
00195       this->setName(V.name());
00196       Integer nSuccess = V.size();
00197       for (Integer i=V.first(); i<=V.last(); i++)
00198         if (Arithmetic<String>::isNA(V[i])) // not Available
00199           this->at(i) = Arithmetic<TYPE>::NA();
00200         else
00201         if (!stringToType<TYPE>(this->at(i), V[i]))
00202           nSuccess--;
00203       return nSuccess;
00204     }
00205 
00210     virtual Variable const& exportString( Variable< String >& V
00211                                         , std::ios_base &(*f)(std::ios_base&)
00212                                           = std::dec
00213                                         ) const
00214     {
00215       V.resize(this->range());
00216       V.setName(this->name());
00217       for (Integer i=first(); i<=last(); i++)
00218       {
00219         V[i] = typeToString<TYPE>(this->at(i), f);
00220       }
00221       return *this;
00222     }
00223 
00228     virtual Variable& operator<<( Variable< String > const& V)
00229     {
00230       this->resize(V.range());
00231       this->setName(V.name());
00232       for (Integer i=V.first(); i<=V.last(); i++)
00233         stringToType<TYPE>(this->at(i), V[i]);
00234       return *this;
00235     }
00236 
00240     virtual Variable const& operator>>(Variable< String >& V) const
00241     {
00242       V.resize(this->range());
00243       V.setName(this->name());
00244       for (Integer i=first(); i<=last(); i++)
00245         V[i] = typeToString<TYPE>(this->at(i));
00246 
00247       return *this;
00248     }
00251     friend ostream& operator<<(ostream& s, Variable<TYPE> const& V)
00252     {
00253       s << V.name() << STRING_NL;
00254       return out1D(s,V);
00255     }
00256 };
00257 
00258 } // Namespace STK
00259 
00260 #endif //STK_VARIABLE_H