STK++  0.4
Public Types | Public Member Functions | Protected Attributes
STK::Variable< Type > Class Template Reference

Variable is an implementation of the Base class IVariable using The Array1D class for storing the data. More...

#include <STK_Variable.h>

Inheritance diagram for STK::Variable< Type >:
Inheritance graph
[legend]

List of all members.

Public Types

typedef IArray1D< Variable
< Type > > 
Base
 Type for the Array1DBase Class.

Public Member Functions

 Variable ()
 Default constructor.
 Variable (String const &name)
 constructor
 Variable (Range const &I)
 constructor
 Variable (Range const &I, String const &name)
 constructor
 Variable (Range const &I, Type const &v, String const &name)
 Misc constructor.
 Variable (Variable const &V, bool ref=false)
 Copy ctor.
 Variable (Variable const &V, Range const &I)
 Reference constructor.
virtual ~Variable ()
 Virtual destructor.
Variableoperator= (Variable const &var)
 Operator = : overwrite the Variable with V.
Variableoperator= (Type const &v)
 Operator = : overwrite the Variable with the value v.
void move (Variable const &V)
 move the variable in this
void encode ()
 encode values as ints.
int maxLength (bool with_name) const
int nbMiss () const
virtual int const & size () const
virtual void insertElt (int pos, int const &n=1)
 Insert n elements at the position pos of the container.
virtual void clear ()
 clear Container from all elements and memory allocated.
virtual void erase (int pos, int const &n=1)
 Delete n elements at the pos index from the container.
virtual void shift (int const &beg)
 New first index for the object.
virtual void pushBack (int const &n=1)
 Add n elements at the end of the container.
virtual void popBack (int const &n=1)
 Delete n last elements of the container.
virtual Variableclone (bool ref=false) const
 clone return a ptr on a copy of the Object.
virtual void pushBackNAValues (int const &n=1)
 push back n NA values.
virtual void exportAsString (Variable< String > &V) const
 Overwrite the variable V by converting the data into strings.
virtual Variableoperator<< (Variable< String > const &V)
 Operator << : overwrite the Variable by converting the Strings contained in V into the Type.
virtual Variable< Type > const & operator>> (Variable< String > &V) const
 Operator >> : convert the Variable V into strings.
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.
template<>
void pushBackNAValues (int const &n)
template<>
int importFromString (Variable< String > const &V, std::ios_base &(*f)(std::ios_base &))
template<>
void exportAsString (Variable< String > &V) const
template<>
Variable< String > & operator<< (Variable< String > const &V)
template<>
Variable< String > const & operator>> (Variable< String > &V) const

Protected Attributes

std::map< Type, intcoding_
 store the map String <-> int

Detailed Description

template<class Type>
class STK::Variable< Type >

Variable is an implementation of the Base class IVariable using The Array1D class for storing the data.

It implement all purely virtual methods defined in the IVariable base class.

Definition at line 82 of file STK_Variable.h.


Member Typedef Documentation

template<class Type>
typedef IArray1D< Variable<Type> > STK::Variable< Type >::Base

Type for the Array1DBase Class.

Reimplemented from STK::IArray1D< Variable< Type > >.

Definition at line 87 of file STK_Variable.h.


Constructor & Destructor Documentation

template<class Type>
STK::Variable< Type >::Variable ( ) [inline]

Default constructor.

Definition at line 90 of file STK_Variable.h.

Referenced by STK::Variable< Type >::clone().

               : IVariable(IdTypeImpl<Type>::returnType(), STRING_NA)
               , Base()
    {}
template<class Type>
STK::Variable< Type >::Variable ( String const &  name) [inline]

constructor

Parameters:
name: name of the variable

Definition at line 96 of file STK_Variable.h.

            : IVariable(IdTypeImpl<Type>::returnType(), name)
            , Base()
    {}
template<class Type>
STK::Variable< Type >::Variable ( Range const &  I) [inline]

constructor

Parameters:
I: range of the data

Definition at line 103 of file STK_Variable.h.

            : IVariable(IdTypeImpl<Type>::returnType(), STRING_NA)
            , Base(I)
    {}
template<class Type>
STK::Variable< Type >::Variable ( Range const &  I,
String const &  name 
) [inline]

constructor

Parameters:
I: range of the data
name: name of the variable

Definition at line 111 of file STK_Variable.h.

            : IVariable(IdTypeImpl<Type>::returnType(), name)
            , Base(I)
    {}
template<class Type>
STK::Variable< Type >::Variable ( Range const &  I,
Type const &  v,
String const &  name 
) [inline]

Misc constructor.

Parameters:
I: range of the data
name: name of the variable
v: initial value

Definition at line 120 of file STK_Variable.h.

            : IVariable(IdTypeImpl<Type>::returnType(), name)
            , Base(I, v)
    {}
template<class Type>
STK::Variable< Type >::Variable ( Variable< Type > const &  V,
bool  ref = false 
) [inline]

Copy ctor.

Parameters:
Vthe Variable to copy
reftrue if we want to wrap V

Definition at line 128 of file STK_Variable.h.

            : IVariable(V)
            , Base(V, ref)
    {}
template<class Type>
STK::Variable< Type >::Variable ( Variable< Type > const &  V,
Range const &  I 
) [inline]

Reference constructor.

Parameters:
Vthe Variable to wrap
Irange of the data

Definition at line 136 of file STK_Variable.h.

            : IVariable(V), Base(V, I)
    {}
template<class Type>
virtual STK::Variable< Type >::~Variable ( ) [inline, virtual]

Virtual destructor.

Definition at line 140 of file STK_Variable.h.

{}

Member Function Documentation

template<class Type>
Variable& STK::Variable< Type >::operator= ( Variable< Type > const &  var) [inline]

Operator = : overwrite the Variable with V.

Parameters:
varthe variable to copy

Definition at line 144 of file STK_Variable.h.

References STK::IVariable::name_, and STK::IArray1D< Variable< Type > >::resize().

    {
      // check size
      if (this->range()!=var.range()) this->resize(var.range());
      // copy IVariable part
      this->name_ = var.name_;
      // copy IArray part
      for (int i=this->firstIdx(); i<=this->lastIdx(); i++) this->elt(i) = var.elt(i);
      return *this;
    }
template<class Type>
Variable& STK::Variable< Type >::operator= ( Type const &  v) [inline]

Operator = : overwrite the Variable with the value v.

Parameters:
vthe value to set to the variable

Definition at line 157 of file STK_Variable.h.

    {
      for (int i=this->firstIdx(); i<=this->lastIdx(); i++) this->elt(i)= v;
      return *this;
    }
template<class Type>
void STK::Variable< Type >::move ( Variable< Type > const &  V) [inline]

move the variable in this

Parameters:
Vvariable to move in this

Definition at line 165 of file STK_Variable.h.

References STK::IArray1D< Variable< Type > >::move(), and STK::IVariable::name_.

    { Base::move(V);
      name_ = V.name_;
    }
template<class Type>
void STK::Variable< Type >::encode ( ) [inline]

encode values as ints.

Not used yet.

Definition at line 170 of file STK_Variable.h.

References STK::Variable< Type >::coding_.

    { int code = STKBASEARRAYS;
      std::pair< typename std::map<Type, int>::iterator, bool> ret;
      for (int i=this->firstIdx(); i<= this->lastIdx(); i++)
      { ret=coding_.insert(std::pair<Type, int>(this->elt(i), i));
        if (ret.second==true) { code++;}
      }
    }
template<class Type>
int STK::Variable< Type >::maxLength ( bool  with_name) const [inline]
Returns:
the maximal size of all the fields as String in the variable

Definition at line 179 of file STK_Variable.h.

References STK::Stat::max(), STK::IVariable::name(), and STK::Variable< Type >::size().

    {
      typename String::size_type maxlength = with_name ? this->name().size() : 0;
      // loop over the values
      for (int i=this->firstIdx(); i<=this->lastIdx(); i++)
      { maxlength = std::max(maxlength, this->eltAsString<Type>(i).size() );}
      return int(maxlength);
    }
template<class Type>
int STK::Variable< Type >::nbMiss ( ) const [inline]
Returns:
the number of missing values in the variable

Definition at line 188 of file STK_Variable.h.

    {
      int nbMiss = 0;
      // loop over the values
      for (int i=this->firstIdx(); i<=this->lastIdx(); i++)
      { if (Arithmetic<Type>::isNA(this->elt(i))) nbMiss++;}
      return nbMiss;
    }
template<class Type>
virtual int const& STK::Variable< Type >::size ( ) const [inline, virtual]
Returns:
the number of sample in the variable

Implements STK::IVariable.

Definition at line 198 of file STK_Variable.h.

Referenced by STK::Variable< Type >::importFromString(), and STK::Variable< Type >::maxLength().

{ return Base::size();}
template<class Type>
virtual void STK::Variable< Type >::insertElt ( int  pos,
int const &  n = 1 
) [inline, virtual]

Insert n elements at the position pos of the container.

Parameters:
posindex where to insert elements
nnumber of elements to insert (default 1)

Implements STK::IVariable.

Definition at line 203 of file STK_Variable.h.

References STK::IArray1D< Variable< Type > >::insertElt().

{ Base::insertElt(pos, n);}
template<class Type>
virtual void STK::Variable< Type >::clear ( ) [inline, virtual]

clear Container from all elements and memory allocated.

Implements STK::IVariable.

Definition at line 205 of file STK_Variable.h.

References STK::IArray1D< Variable< Type > >::clear().

template<class Type>
virtual void STK::Variable< Type >::erase ( int  pos,
int const &  n = 1 
) [inline, virtual]

Delete n elements at the pos index from the container.

Parameters:
posindex where to delete elements
nnumber of elements to delete (default 1)

Implements STK::IVariable.

Definition at line 210 of file STK_Variable.h.

References STK::IArray1D< Variable< Type > >::erase().

{ Base::erase(pos,n);}
template<class Type>
virtual void STK::Variable< Type >::shift ( int const &  beg) [inline, virtual]

New first index for the object.

Parameters:
begthe index of the first column to set

Implements STK::IVariable.

Definition at line 214 of file STK_Variable.h.

References STK::IArray1D< Variable< Type > >::shift().

{ Base::shift(beg);}
template<class Type>
virtual void STK::Variable< Type >::pushBack ( int const &  n = 1) [inline, virtual]

Add n elements at the end of the container.

Parameters:
nnumber of elements to add

Implements STK::IVariable.

Definition at line 218 of file STK_Variable.h.

References STK::IArray1D< Variable< Type > >::pushBack().

template<class Type>
virtual void STK::Variable< Type >::popBack ( int const &  n = 1) [inline, virtual]

Delete n last elements of the container.

Parameters:
nnumber of elements to delete

Implements STK::IVariable.

Definition at line 222 of file STK_Variable.h.

References STK::IArray1D< Variable< Type > >::popBack().

template<class Type>
virtual Variable* STK::Variable< Type >::clone ( bool  ref = false) const [inline, virtual]

clone return a ptr on a copy of the Object.

Parameters:
reftrue if we want just a reference

Implements STK::IVariable.

Definition at line 226 of file STK_Variable.h.

References STK::Variable< Type >::Variable().

    { return new Variable(*this, ref);}
template<class Type >
void STK::Variable< Type >::pushBackNAValues ( int const &  n = 1) [inline, virtual]

push back n NA values.

Parameters:
nnumber of NA values to add

Implements STK::IVariable.

Definition at line 270 of file STK_Variable.h.

{ this->insert(Range(this->lastIdx() +1, n), Arithmetic<Type>::NA());}
template<class Type >
void STK::Variable< Type >::exportAsString ( Variable< String > &  V) const [inline, virtual]

Overwrite the variable V by converting the data into strings.

Parameters:
VVariable of String

Definition at line 314 of file STK_Variable.h.

References STK::IArray1D< Variable< Type > >::resize(), and STK::IVariable::setName().

{
  V.resize(this->range());
  V.setName(this->name());
  for (int i=this->firstIdx(); i<=this->lastIdx(); i++)
  { V[i] = this->eltAsString<Type>(i);}
}
template<class Type >
Variable< Type > & STK::Variable< Type >::operator<< ( Variable< String > const &  V) [inline, virtual]

Operator << : overwrite the Variable by converting the Strings contained in V into the Type.

Parameters:
Vthe Variable of string to import

Definition at line 338 of file STK_Variable.h.

References STK::IVariable::name().

{
  this->resize(V.range());
  this->setName(V.name());
  for (int i=V.firstIdx(); i<=V.lastIdx(); i++) this->elt(i) = stringToType<Type>(V[i]);
  return *this;
}
template<class Type >
Variable< Type > const & STK::Variable< Type >::operator>> ( Variable< String > &  V) const [inline, virtual]

Operator >> : convert the Variable V into strings.

Parameters:
VVariable of String

Definition at line 360 of file STK_Variable.h.

References STK::IArray1D< Variable< Type > >::resize(), and STK::IVariable::setName().

{
  V.resize(this->range());
  V.setName(this->name());
  for (int i=this->firstIdx(); i<=this->lastIdx(); i++) V[i] = typeToString<Type>(this->elt(i));
  return *this;
}
template<class Type >
int STK::Variable< Type >::importFromString ( Variable< String > const &  V,
std::ios_base &(*)(std::ios_base &)  f = std::dec 
) [inline]

overwrite the Variable by converting the strings contained in V into the Type.

Parameters:
VVariable of String
fio flags
Returns:
number of successful conversion

Definition at line 290 of file STK_Variable.h.

References STK::Arithmetic< TYPE >::NA(), STK::IVariable::name(), and STK::Variable< Type >::size().

{
  this->resize(V.range());
  this->setName(V.name());
  int nSuccess = V.size();
  for (int i=V.firstIdx(); i<=V.lastIdx(); i++)
    if ( (Arithmetic<String>::isNA(V[i])) || (V[i]==STRING_NA) ) // not Available
      this->elt(i) = Arithmetic<Type>::NA();
    else
    if (!stringToType<Type>(this->elt(i), V[i], f)) nSuccess--;
  return nSuccess;
}
template<>
void STK::Variable< String >::pushBackNAValues ( int const &  n) [inline, virtual]

Specialization for Type = String.

Parameters:
nnumber of NA values to add

Implements STK::IVariable.

Definition at line 264 of file STK_Variable.h.

{ this->insert(Range(this->lastIdx() +1, n), STRING_NA);}
template<>
int STK::Variable< String >::importFromString ( Variable< String > const &  V,
std::ios_base &(*)(std::ios_base &)  f 
) [inline]

Give the number of success.

Parameters:
VVariable of String
fio flags
Returns:
number of successful conversion

Definition at line 279 of file STK_Variable.h.

References STK::Variable< Type >::size().

{ *this = V; return V.size();}
template<>
void STK::Variable< String >::exportAsString ( Variable< String > &  V) const [inline]
Parameters:
VVariable of String

Definition at line 308 of file STK_Variable.h.

{ V = *this;}
template<>
Variable< String > & STK::Variable< String >::operator<< ( Variable< String > const &  V) [inline]
Parameters:
Vthe Variable of string to import

Definition at line 326 of file STK_Variable.h.

References STK::IVariable::name().

{
  this->resize(V.range());
  this->setName(V.name());
  for (int i=V.firstIdx(); i<=V.lastIdx(); i++) this->elt(i) = V[i];
  return *this;
}
template<>
Variable< String > const & STK::Variable< String >::operator>> ( Variable< String > &  V) const [inline]
Parameters:
VVariable of String

Definition at line 349 of file STK_Variable.h.

References STK::IArray1D< Variable< Type > >::resize(), and STK::IVariable::setName().

{
  V.resize(this->range());
  V.setName(this->name());
  for (int i=this->firstIdx(); i<=this->lastIdx(); i++) V[i] = this->elt(i);
  return *this;
}

Member Data Documentation

template<class Type>
std::map<Type, int> STK::Variable< Type >::coding_ [protected]

store the map String <-> int

Definition at line 257 of file STK_Variable.h.

Referenced by STK::Variable< Type >::encode().


The documentation for this class was generated from the following file: