STK++ 0.9.13
STK_Variable.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: stkpp::DManager
27 * Author: Serge Iovleff, S..._Dot_I..._At_stkpp_Dot_org (see copyright for ...)
28 **/
29
34#ifndef STK_VARIABLE_H
35#define STK_VARIABLE_H
36
38#include "STK_IVariable.h"
39
40namespace STK
41{
42
43template< class Type_> class Variable;
44
45namespace hidden
46{
50template<class Type_>
51struct Traits< Variable<Type_> >
52{
59
60 typedef Type_ Type;
61 typedef typename RemoveConst<Type_>::Type const& TypeConst;
62
63 enum
64 {
65 structure_ = Arrays::vector_,
66 orient_ = Arrays::by_col_,
67 sizeCols_ = 1,
68 sizeRows_ = UnknownSize,
69 size_ = UnknownSize,
70 storage_ = Arrays::dense_ // always dense
71 };
72
74
77
78 typedef int Index;
79
82
83 typedef std::reverse_iterator<Iterator> ReverseIterator;
84 typedef std::reverse_iterator<ConstIterator> ConstReverseIterator;
85};
86
87} // namespace hidden
94template<class Type_>
95class Variable: public IVariable
96 , public IArray1D< Variable<Type_> >
97{
98 public:
105
108
109 enum
110 {
117 };
118
120
121 //typedef MemAllocator<Type_, size_> Allocator;
123
124 using Base::elt;
128 explicit Variable( String const& name = stringNa)
129 : IVariable(IdTypeImpl<Type_>::returnType(), name)
130 , Base()
131 {}
135 explicit Variable( int size, String const& name = stringNa)
136 : IVariable(IdTypeImpl<Type_>::returnType(), name)
137 , Base(size)
138 {}
142 explicit Variable( Range const& I, String const& name = stringNa)
143 : IVariable(IdTypeImpl<Type_>::returnType(), name)
144 , Base(I)
145 {}
150 Variable( Range const& I, Type_ const& v, String const& name)
151 : IVariable(IdTypeImpl<Type_>::returnType(), name)
152 , Base(I)
153 { this->setValue(v);}
158 explicit Variable( Variable const& V, bool ref = false)
159 : IVariable(V)
160 , Base(V, ref)
161 {}
166 explicit Variable( Variable const& V, Range const& I, bool ref = true)
167 : IVariable(V), Base(V, I, true)
168 {}
172 template<class OtherArray>
174 : IVariable(IdTypeImpl<Type_>::returnType(), stringNa)
175 , Base(T, T.range(), true)
176 {}
182 virtual Variable* clone( bool ref = false) const
183 { return new Variable(*this, ref);}
187 void shift1D(int rbeg) { Base::shift(rbeg, this->beginCols());}
192 { Base::resize(I, this->cols()); return *this;}
193
196 virtual void popBack(int n =1) { Base::popBack(n);}
200 void pushBack( int n=1) { Base::pushBack(n);}
204 void push_back( Type_ const& v)
205 { Base::pushBack();
206 this->back() = v;
207 }
213 void insertElt(int pos, int n =1)
214 { Base::insertElt(pos, n);}
218 inline Variable& operator=(Variable const& V)
219 { // copy IVariable part
220 this->name_ = V.name_;
221 return Base::assign(V);
222 }
226 inline Variable& operator=(Type_ const& v) { this->setValue(v); return *this;}
227
231 inline void move(Variable const& V)
232 { Base::move(V);
233 name_ = V.name_;
234 }
236 void encode()
237 { int code = baseIdx;
238 std::pair< typename std::map<Type_, int>::iterator, bool> ret;
239 for (int i=this->begin(); i< this->end(); i++)
240 { ret=coding_.insert(std::pair<Type_, int>(this->elt(i), i));
241 if (ret.second==true) { code++;}
242 }
243 }
245 int maxLength(bool with_name) const
246 {
247 typename String::size_type maxlength = with_name ? this->name().size() : 0;
248 // loop over the values
249 for (int i=this->begin(); i<this->end(); i++)
250 { maxlength = std::max(maxlength, this->template eltAsString<Type_>(i).size() );}
251 return int(maxlength);
252 }
254 int nbMiss() const;
255
256 // IVariable part
258 virtual int size() const { return Base::size();}
260 virtual void clear() { Base::clear();}
265 virtual void erase(int pos, int n=1) { Base::erase(pos,n);}
269 virtual void shift(int beg) { Base::shift(beg);}
273 virtual inline void pushBackNAValues(int n=1);
277 virtual inline void exportAsString( Variable< String >& V) const;
282 virtual inline Variable& operator<<( Variable< String > const& V);
286 virtual inline Variable<Type_> const& operator>>(Variable< String >& V) const;
287
295 , std::ios_base& (*f)(std::ios_base&) = std::dec
296 );
297 protected:
299 std::map<Type_, int> coding_;
300};
301
303template<class Type_>
305{
306 int nbMiss = 0;
307 // loop over the values
308 for (int i=this->begin(); i<this->end(); i++)
309 { if (Arithmetic<Type_>::isNA(this->elt(i))) nbMiss++;}
310 return nbMiss;
311}
315template<>
317{ int first = this->end(), end = first+n;
318 this->insertElt(this->end(), n);
319 for (int i=first; i<end; i++)
320 this->elt(i) = stringNa;
321}
325template<class Type_>
327{
328 int first = this->end(), end = first+n;
329 this->insertElt(this->end(), n);
330 for (int i=first; i<end; i++)
331 this->elt(i) = Arithmetic<Type_>::NA();
332}
339template<>
341 , std::ios_base& (*f)(std::ios_base&)
342 )
343{ *this = V; return V.size();}
350template<class Type_>
352 , std::ios_base& (*f)(std::ios_base&)
353 )
354{
355 this->resize(V.range());
356 this->setName(V.name());
357 int nSuccess = V.size();
358 for (int i=V.begin(); i<V.end(); i++)
359 if ( (Arithmetic<String>::isNA(V[i])) || (V[i]==stringNa) ) // not Available
360 this->elt(i) = Arithmetic<Type_>::NA();
361 else
362 if (!stringToType<Type_>(this->elt(i), V[i], f)) nSuccess--;
363 return nSuccess;
364}
368template<>
370{ V = *this;}
374template<class Type_>
376{
377 V.resize(this->range());
378 V.setName(this->name());
379 for (int i=this->begin(); i<=this->lastIdx(); i++)
380 { V[i] = this->template eltAsString<Type_>(i);}
381}
386template<>
388{
389 this->resize(V.range());
390 this->setName(V.name());
391 for (int i=V.begin(); i<V.end(); i++) this->elt(i) = V[i];
392 return *this;
393}
398template<class Type_>
399inline Variable<Type_>& Variable<Type_>::operator<<( Variable< String > const& V)
400{
401 this->resize(V.range());
402 this->setName(V.name());
403 for (int i=V.begin(); i<V.end(); i++) this->elt(i) = stringToType<Type_>(V[i]);
404 return *this;
405}
409template<>
411{
412 V.resize(this->range());
413 V.setName(this->name());
414 for (int i=this->begin(); i<this->end(); i++) V[i] = this->elt(i);
415 return *this;
416}
420template<class Type_>
422{
423 V.resize(this->range());
424 V.setName(this->name());
425 for (int i=this->begin(); i<this->end(); i++) V[i] = typeToString<Type_>(this->elt(i));
426 return *this;
427}
429template<typename Type_>
431{
432 s << V.name() << _T("\n");
433 return out1D(s, V);
434}
435
436} // namespace STK
437
438#endif //STK_VARIABLE_H
Interface base class for the Array1D, this is an internal header file, included by other Containers l...
Define the Interface base class of all types of Variables.
#define _T(x)
Let x unmodified.
template one dimensional Array.
Variable< Type_ > & popBack(int n=1)
Delete last elts of the container.
Variable< Type_ > & setValue(Type const &value)
set a value to this container.
Variable< Type_ > & assign(IArray1D const &src)
overwrite this with src.
Variable< Type_ > & erase(int pos, int n=1)
Delete n elements at the pos index to the container.
Variable< Type_ > & pushBack(int n=1)
Add n Elements to the end of the container.
void clear()
Clear the object.
Variable< Type_ > & insertElt(int pos, int n=1)
Insert n elements at the position pos of the container.
void move(Variable< Type_ > const &T)
move T to this.
Derived & resize(Range const &I=RowRange())
IVariable is an Interface class for all Variables classes.
String name_
Name of the variable.
String const & name() const
The MultidimRegression class allows to regress a multidimensional output variable among a multivariat...
Index sub-vector region: Specialization when the size is unknown.
Definition STK_Range.h:265
Variable is an implementation of the Base class IVariable using The Array1D class for storing the dat...
int importFromString(Variable< String > const &V, std::ios_base &(*f)(std::ios_base &)=std::dec)
overwrite the Variable by converting the strings contained in V into the Type_.
Variable< Type_ > Row
virtual Variable & operator<<(Variable< String > const &V)
Operator << : overwrite the Variable by converting the Strings contained in V into the Type_.
virtual void exportAsString(Variable< String > &V) const
Overwrite the variable V by converting the data into strings.
Variable(Range const &I, String const &name=stringNa)
Default constructor.
virtual void clear()
clear Container from all elements and memory allocated.
virtual int size() const
virtual Variable< Type_ > const & operator>>(Variable< String > &V) const
Operator >> : convert the Variable V into strings.
hidden::Traits< Variable< Type_ > >::Type Type
Variable & operator=(Type_ const &v)
set the container to a constant value.
int nbMiss() const
Variable< Type_ > & resize1D(Range const &I)
Resize the container.
Variable(Variable const &V, bool ref=false)
copy/reference constructor.
virtual void erase(int pos, int n=1)
Delete n elements at the pos index from the container.
Variable(String const &name=stringNa)
default constructor
Variable< Type_ > SubRow
int maxLength(bool with_name) const
Variable< Type_ > SubArray
void insertElt(int pos, int n=1)
Insert n elements at the position pos of the container.
void push_back(Type_ const &v)
Add an element to the container.
~Variable()
destructor.
void encode()
encode values as ints.
Variable< Type_ > SubVector
hidden::Traits< Variable< Type_ > >::Allocator Allocator
void move(Variable const &V)
move the variable in this
IArray1D< Variable< Type_ > > Base
virtual void pushBackNAValues(int n=1)
push back n NA values.
virtual void popBack(int n=1)
remove n elements to the end of the container
std::map< Type_, int > coding_
store the map String <-> int
Variable< Type_ > Col
virtual void shift(int beg)
New first index for the object.
Variable(Variable const &V, Range const &I, bool ref=true)
reference constructor
Variable & operator=(Variable const &V)
operator = : overwrite the CArray with the Right hand side T.
hidden::Traits< Variable< Type_ > >::TypeConst TypeConst
Variable(int size, String const &name=stringNa)
constructor
virtual Variable * clone(bool ref=false) const
clone return a ptr on a copy of the Object.
void pushBack(int n=1)
Add n elements to the container.
Variable(Range const &I, Type_ const &v, String const &name)
constructor with specified initial value
Variable(IArray1D< OtherArray > const &T)
constructor by reference, ref_=1.
Variable< Type_ > SubCol
void shift1D(int rbeg)
New first index for the object.
ostream & out1D(ostream &os, ITContainer1D< Array > const &V)
Method for displaying any one dimensional Array.
Definition STK_Display.h:75
ostream & operator<<(ostream &s, const CAllocator< Type, SizeRows_, SizeCols_, Orient_ > &V)
output stream for CAllocator.
Definition STK_CArray.h:221
@ dense_
dense matrix/vector/array/expression
@ vector_
column oriented vector/array/expression
@ by_col_
storage by column
String stringNa
Representation of a Not Available value.
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 baseIdx
base index of the containers created in STK++.
std::basic_ostream< Char > ostream
ostream for Char
Definition STK_Stream.h:57
The namespace STK is the main domain space of the Statistical ToolKit project.
Arithmetic properties of STK fundamental types.
static Type NA()
Adding a Non Available (NA) special number.
Implementation of the return type.
ConstDenseRandomIterator< Variable< Type_ > > ConstIterator
std::reverse_iterator< ConstIterator > ConstReverseIterator
DenseRandomIterator< Variable< Type_ > > Iterator
MemAllocator< Type_, UnknownSize > Allocator
RemoveConst< Type_ >::Type const & TypeConst
std::reverse_iterator< Iterator > ReverseIterator