STK::IArrayBase< DATA > Class Template Reference
[Subproject STKernel::Tcontainer]

Templated Interface base class for all arrays. More...

#include <STK_IArrayBase.h>

Inherits STK::IContainerRef.

List of all members.

Public Member Functions

 IArrayBase (const Inx &I=Inx())
virtual ~IArrayBase ()
 IArrayBase (const IArrayBase &T, bool ref=false)
Integer const & getFirstData () const
Integer const & getLastData () const
Integer const & getSizeData () const
DATA const & getData (const Integer &pos) const
DATA & getData (const Integer &pos)
void setData (const Integer &pos, const DATA &data=DATA())
void swap (IArrayBase &T)

Static Public Member Functions

static Integer evalCapacity (const Integer &m)

Protected Member Functions

 IArrayBase (DATA *q, const Inx &I)
void incPtrData (const Integer &inc)
void decPtrData (const Integer &dec)
void mallocPtrData (const Integer &size, const Integer &inc=0)
void freePtrData ()

Private Member Functions

void setPtrData (DATA *const &q=(DATA *) NULL)
void setFirstData (Integer first=0)
void setLastData (Integer last=-1)
void setSizeData (Integer size=0)

Private Attributes

DATA * ptr_data_
Integer first_data_
Integer last_data_
Integer size_data_


Detailed Description

template<class DATA>
class STK::IArrayBase< DATA >

The IArrayBase class is an interface base class for all arrays stored in memory : it contains the main pointer on the datas. An array stored in memory should alway be wrapped in some way or be a wrapper of an other array.

The class DATA can be any base type or container class.

Definition at line 112 of file STK_IArrayBase.h.


Constructor & Destructor Documentation

template<class DATA>
STK::IArrayBase< DATA >::IArrayBase ( const Inx I = Inx()  )  [inline]

Default Ctor.

Parameters:
I range of the datas

Definition at line 132 of file STK_IArrayBase.h.

00132                                       : IContainerRef(false)
00133                                       , ptr_data_((DATA*)NULL)
00134                                       , first_data_(I.first())
00135                                       , last_data_(I.last())
00136                                       , size_data_(I.size())
00137     {
00138       if ( size_data_>0)
00139         mallocPtrData( size_data_, first_data_);
00140     }

template<class DATA>
virtual STK::IArrayBase< DATA >::~IArrayBase (  )  [inline, virtual]

Virtual Dtor.

Definition at line 144 of file STK_IArrayBase.h.

00145     { if (!this->isRef()) // free Allocated memory
00146         this->freePtrData();
00147     }

template<class DATA>
STK::IArrayBase< DATA >::IArrayBase ( const IArrayBase< DATA > &  T,
bool  ref = false 
) [inline]

Copy Ctor.

Parameters:
T : the array to copy
ref : is this a wrapper of T ?

Definition at line 153 of file STK_IArrayBase.h.

00154               : IContainerRef(ref)
00155     {
00156       if (ref) // set T datas
00157       {
00158         ptr_data_   = T.ptr_data_;
00159         first_data_ = T.first_data_;
00160         last_data_  = T.last_data_;
00161         size_data_  = T.size_data_;
00162       }
00163       else // set default
00164       {
00165         ptr_data_   = (DATA*)NULL;
00166         first_data_ = 1;
00167         last_data_  = 0;
00168         size_data_  = 0;
00169       }
00170     }

template<class DATA>
STK::IArrayBase< DATA >::IArrayBase ( DATA *  q,
const Inx I 
) [inline, protected]

Wrapper Ctor.

Parameters:
q ptr to the datas to wrap
I range of the datas wrapped

Definition at line 177 of file STK_IArrayBase.h.

00177                                       : IContainerRef(true)
00178                                       , ptr_data_(q)
00179                                       , first_data_(I.first())
00180                                       , last_data_(I.last())
00181                                       , size_data_(I.size())
00182     { ;}


Member Function Documentation

template<class DATA>
Integer const& STK::IArrayBase< DATA >::getFirstData (  )  const [inline]

Get the first index of the datas.

Definition at line 189 of file STK_IArrayBase.h.

00190     { return first_data_;}

template<class DATA>
Integer const& STK::IArrayBase< DATA >::getLastData (  )  const [inline]

Get the last index of the datas.

Definition at line 194 of file STK_IArrayBase.h.

00195     { return last_data_;}

template<class DATA>
Integer const& STK::IArrayBase< DATA >::getSizeData (  )  const [inline]

Get the size of the datas.

Definition at line 199 of file STK_IArrayBase.h.

00200     { return size_data_;}

template<class DATA>
DATA const& STK::IArrayBase< DATA >::getData ( const Integer pos  )  const [inline]

Get the const element number pos.

Parameters:
pos the position of the element we get

Definition at line 205 of file STK_IArrayBase.h.

Referenced by STK::Array1D< Real >::Array1D(), STK::Array1D< String >::Array1D(), STK::Array2D< Real >::Array2D(), STK::Array2D< TYPE >::Array2D(), STK::ArrayHo< Real >::ArrayHo(), STK::ArrayHo< TYPE >::ArrayHo(), STK::Array1D< String >::insertElts(), STK::MatrixLowerTriangular::MatrixLowerTriangular(), STK::MatrixUpperTriangular::MatrixUpperTriangular(), STK::IArray1DBase< Integer, Integer, Array1D< Integer > >::moveElt(), STK::Array1D< Real >::operator=(), STK::MatrixUpperTriangular::operator=(), STK::MatrixLowerTriangular::operator=(), STK::Array2D< Real >::operator=(), STK::Array2D< TYPE >::operator=(), STK::Array1D< String >::operator=(), and STK::IArray2DBase< Real, Real *, ArrayHo< Real >, Array1D< Real >, MatrixUpperTriangular >::transferColumn().

00206     {
00207 #ifdef STK_BOUNDS_CHECK
00208       if (pos < first_data_)
00209       { throw std::out_of_range("IArrayBase::getData(pos) const "
00210                            "IArrayBase::first_data_ > pos");
00211       }
00212       if (pos > last_data_)
00213       { throw std::out_of_range("IArrayBase::getData(pos) const "
00214                            "IArrayBase::last_data_ < pos");
00215       }
00216 #endif
00217       return ptr_data_[pos];
00218     }

template<class DATA>
DATA& STK::IArrayBase< DATA >::getData ( const Integer pos  )  [inline]

Get the element number pos.

Parameters:
pos the position of the element we get

Definition at line 223 of file STK_IArrayBase.h.

00224     {
00225 #ifdef STK_BOUNDS_CHECK
00226       if (pos < first_data_)
00227       { throw std::out_of_range("IArrayBase::getData(pos) "
00228                            "IArrayBase::first_data_ > pos");
00229       }
00230       if (pos > last_data_)
00231       { throw std::out_of_range("IArrayBase::getData(pos) "
00232                            "IArrayBase::last_data_ < pos");
00233       }
00234 #endif
00235       return ptr_data_[pos];
00236     }

template<class DATA>
void STK::IArrayBase< DATA >::setData ( const Integer pos,
const DATA &  data = DATA() 
) [inline]

Set the element number pos : this method is not destined to the end-user. No check about the given adress is performed in this interface class.

Parameters:
pos the position of the element we set
data the value we set

Definition at line 244 of file STK_IArrayBase.h.

Referenced by STK::IArray1DBase< Integer, Integer, Array1D< Integer > >::moveElt(), and STK::IArray2DBase< Real, Real *, ArrayHo< Real >, Array1D< Real >, MatrixUpperTriangular >::transferColumn().

00247    {
00248 #ifdef STK_BOUNDS_CHECK
00249       if (pos < first_data_)
00250       { throw std::out_of_range("IArrayBase::setData(pos, data) "
00251                            "IArrayBase::first_data_ > pos");
00252       }
00253       if (pos > last_data_)
00254       { throw std::out_of_range("IArrayBase::setData(pos, data) "
00255                            "IArrayBase::last_data_ > pos");
00256       }
00257 #endif
00258      ptr_data_[pos] = data;
00259    }

template<class DATA>
void STK::IArrayBase< DATA >::swap ( IArrayBase< DATA > &  T  )  [inline]

swap this with T.

Parameters:
T the container to swap

Definition at line 264 of file STK_IArrayBase.h.

00265     {
00266       // copy main ptr data and range of this
00267       DATA*   auxMainPtr(ptr_data_);
00268       Integer auxFirst(first_data_);
00269       Integer auxLast(last_data_);
00270       Integer auxSize(size_data_);
00271       bool    auxRef(this->isRef());
00272 
00273       // overwrite main ptr data of this
00274       this->setPtrData(T.ptr_data_);
00275       this->setFirstData(T.first_data_);
00276       this->setLastData(T.last_data_);
00277       this->setSizeData(T.size_data_);
00278       this->setRef(T.isRef());
00279 
00280       // overwrite main PTRCOL of T
00281       T.setPtrData(auxMainPtr);
00282       T.setFirstData(auxFirst);
00283       T.setLastData(auxLast);
00284       T.setSizeData(auxSize);
00285       T.setRef(auxRef);
00286     }

template<class DATA>
static Integer STK::IArrayBase< DATA >::evalCapacity ( const Integer m  )  [inline, static]

Return n+m, where n is the first number such that m < 2^n. if m <=0 return 0

Parameters:
m the size of the container

Definition at line 292 of file STK_IArrayBase.h.

00293     {
00294       Integer n = 0;
00295       Integer b = m;
00296       for (Integer k=1 ; k <= b; n++, k <<= 1);
00297       return m+n;
00298     }

template<class DATA>
void STK::IArrayBase< DATA >::incPtrData ( const Integer inc  )  [inline, protected]

Increment the adress of the datas : this method is not destined to the end-user.

Parameters:
inc the increment to apply

Definition at line 305 of file STK_IArrayBase.h.

Referenced by STK::IArrayBase< TYPE >::freePtrData().

00306     {
00307       // translate ptr_data_ if there exists data
00308       if (this->ptr_data_)
00309       {
00310          ptr_data_ += inc; first_data_ -= inc; last_data_ -= inc;
00311       }
00312     }

template<class DATA>
void STK::IArrayBase< DATA >::decPtrData ( const Integer dec  )  [inline, protected]

Decrement the adress of the datas : this method is not destined to the end-user.

Parameters:
dec the increment to apply

Definition at line 318 of file STK_IArrayBase.h.

Referenced by STK::IArrayBase< TYPE >::mallocPtrData().

00319     {
00320       // translate ptr_data_ if there exists data
00321       if (this->ptr_data_)
00322       {
00323         ptr_data_ -= dec; first_data_ += dec; last_data_ += dec;
00324       }
00325     }

template<class DATA>
void STK::IArrayBase< DATA >::mallocPtrData ( const Integer size,
const Integer inc = 0 
) [inline, protected]

protected function for main ptr memory allocation.

Parameters:
size the size to reserve in memory
inc the increment to apply to ptr_data_ after allocation

Definition at line 331 of file STK_IArrayBase.h.

Referenced by STK::IArrayBase< TYPE >::IArrayBase().

00332     {
00333       // check if there is memory allocated
00334       if ((ptr_data_)&&(!this->isRef())) freePtrData();
00335       // check size
00336       if (size <= 0) return;
00337       // allocate memory
00338       try
00339       {
00340         setPtrData(new DATA[size]);
00341       }
00342       catch (std::bad_alloc & error)  // if an alloc error occur
00343       {
00344         // initialize to default
00345         this->setPtrData();
00346         this->setFirstData();
00347         this->setLastData();
00348         this->setSizeData();
00349         // and throw an exception
00350         throw std::runtime_error("IArrayBase::mallocMainPtr(size, inc) "
00351                             "memory allocation failed.");
00352       }
00353       // set initial values
00354       this->setFirstData(0);     // first index is 0
00355       this->setLastData(size-1); // last index is size -1
00356       this->setSizeData(size);   // number of datas is size
00357       // apply increment
00358       this->decPtrData(inc);
00359     }

template<class DATA>
void STK::IArrayBase< DATA >::freePtrData (  )  [inline, protected]

protected function for main ptr memory desallocation.

Definition at line 363 of file STK_IArrayBase.h.

Referenced by STK::IArrayBase< TYPE >::mallocPtrData(), and STK::IArrayBase< TYPE >::~IArrayBase().

00364     {
00365       // nothing to do for ref
00366       // TODO this test should emit a warning if true
00367       if (this->isRef()) return;
00368       // if there is elts
00369       if (ptr_data_)
00370       {
00371         this->incPtrData(first_data_);  // translate
00372         delete [] ptr_data_;   // erase
00373         // set default values
00374         this->setPtrData();
00375         this->setFirstData();
00376         this->setLastData();
00377         this->setSizeData();
00378       }
00379     }

template<class DATA>
void STK::IArrayBase< DATA >::setPtrData ( DATA *const &  q = (DATA*)NULL  )  [inline, private]

Set the adress of the datas : this method is not destined to the end-user.

Parameters:
q the adress to set

Definition at line 386 of file STK_IArrayBase.h.

Referenced by STK::IArrayBase< TYPE >::freePtrData(), STK::IArrayBase< TYPE >::mallocPtrData(), and STK::IArrayBase< TYPE >::swap().

00387     { ptr_data_ = q;}

template<class DATA>
void STK::IArrayBase< DATA >::setFirstData ( Integer  first = 0  )  [inline, private]

Set the index of the first data : this method is not destined to the end-user.

Parameters:
first the index of the first data

Definition at line 393 of file STK_IArrayBase.h.

Referenced by STK::IArrayBase< TYPE >::freePtrData(), STK::IArrayBase< TYPE >::mallocPtrData(), and STK::IArrayBase< TYPE >::swap().

00394     { first_data_ = first;}

template<class DATA>
void STK::IArrayBase< DATA >::setLastData ( Integer  last = -1  )  [inline, private]

Set the index of the last data : this method is not destined to the end-user.

Parameters:
last the index of the last data

Definition at line 400 of file STK_IArrayBase.h.

Referenced by STK::IArrayBase< TYPE >::freePtrData(), STK::IArrayBase< TYPE >::mallocPtrData(), and STK::IArrayBase< TYPE >::swap().

00401     { last_data_ = last;}

template<class DATA>
void STK::IArrayBase< DATA >::setSizeData ( Integer  size = 0  )  [inline, private]

Set the size of the datas : this method is not destined to the end-user.

Parameters:
size the size to set

Definition at line 407 of file STK_IArrayBase.h.

Referenced by STK::IArrayBase< TYPE >::freePtrData(), STK::IArrayBase< TYPE >::mallocPtrData(), and STK::IArrayBase< TYPE >::swap().

00408     { size_data_ = size;}


Member Data Documentation

template<class DATA>
DATA* STK::IArrayBase< DATA >::ptr_data_ [private]

template<class DATA>
Integer STK::IArrayBase< DATA >::first_data_ [private]

template<class DATA>
Integer STK::IArrayBase< DATA >::last_data_ [private]

template<class DATA>
Integer STK::IArrayBase< DATA >::size_data_ [private]


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

Generated on Fri Sep 25 10:31:01 2009 for STK++ by  doxygen 1.5.8