STK++ 1.0

STK::IArrayBase< DATA > Class Template Reference

Templated Interface base class for all Arrays classes. More...

#include <STK_IArrayBase.h>

Inheritance diagram for STK::IArrayBase< DATA >:

List of all members.

Public Member Functions

virtual ~IArrayBase ()
 Virtual destructor.
Integer const & firstData () const
 Get the first index of the data.
Integer const & lastData () const
 Get the last index of the data.
Integer const & sizeData () const
 Get the size of the data.
Range const & rangeData () const
 Get the range of the data.
DATA const *const ptrData () const
 Get the main ptr on the data.
DATA const & data (Integer const &pos) const
 Get the const element number pos.

Static Public Member Functions

static Integer evalCapacity (Integer const &m)
 Return n+m, where n is the first number such that m < 2^n.

Protected Member Functions

 IArrayBase (Range const &I=Range())
 Default constructor.
 IArrayBase (const IArrayBase &T, bool ref=false)
 Copy constructor.
 IArrayBase (DATA *q, Range const &I)
 Wrapper constructor.
DATA * ptrData ()
 Get the main ptr on the data.
DATA & data (Integer const &pos)
 Get the element number pos.
void setData (Integer const &pos, DATA const &data=DATA())
 Set the element number pos : this method is not destined to the end-user.
void swap (IArrayBase &T)
 swap this with T.
void shiftPtrData (Integer const &first)
 shift the first index of the data to first.
void mallocPtrData (Integer const &size, Integer const &inc=0)
 protected function for main ptr memory allocation.
void reallocPtrData (Integer const &size, Integer const &inc=0)
 protected function for main ptr memory reallocation.
void freePtrData ()
 protected function for main ptr memory deallocation.
void setPtrData (DATA *p_data=0)
 Set the address of the data : this method is not destined to the end-user.
void setRangeData (Range const &rangeData=Range())
 Set the index of the first data : this method is not destined to the end-user.

Private Member Functions

void incPtrData (Integer const &inc)
 Increment the address of the data : this method is not destined to the end-user.
void decPtrData (Integer const &dec)
 Decrement the address of the data : this method is not destined to the end-user.
void setDefault ()
 Set array members to default values.

Private Attributes

DATA * p_data_
 Main pointer on the data.
Range rangeData_
 Range of the data.

Detailed Description

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

Templated Interface base class for all Arrays classes.

The IArrayBase class is the interface base class of all arrays stored in memory : it manages the main pointer on the data. It derive from the IContainerRef class as an array stored in memory can always be wrapped in some way or be a wrapper of data stored in memory.

The class DATA can be any type of data that can be stored in memory.

Definition at line 58 of file STK_IArrayBase.h.


Constructor & Destructor Documentation

template<class DATA>
STK::IArrayBase< DATA >::IArrayBase ( Range const &  I = Range()) [inline, protected]

Default constructor.

Parameters:
Irange of the data

Definition at line 64 of file STK_IArrayBase.h.

                                          : IContainerRef(false)
                                          , p_data_(0)
                                          , rangeData_(I)
    { mallocPtrData( I.size(), I.first());}
template<class DATA>
STK::IArrayBase< DATA >::IArrayBase ( const IArrayBase< DATA > &  T,
bool  ref = false 
) [inline, protected]

Copy constructor.

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

Definition at line 73 of file STK_IArrayBase.h.

              : IContainerRef(ref)
    {
      if (ref) // set T data
      {
        p_data_   = T.p_data_;
        rangeData_ = T.rangeData_;
      }
      else // Allocate memory in base class
      {
        setDefault();
      }
    }
template<class DATA>
STK::IArrayBase< DATA >::IArrayBase ( DATA *  q,
Range const &  I 
) [inline, protected]

Wrapper constructor.

Parameters:
qptr to the data to wrap
Irange of the data wrapped

Definition at line 91 of file STK_IArrayBase.h.

                                        : IContainerRef(true)
                                        , p_data_(q)
                                        , rangeData_(I)
    { ;}
template<class DATA>
virtual STK::IArrayBase< DATA >::~IArrayBase ( ) [inline, virtual]

Virtual destructor.

Definition at line 98 of file STK_IArrayBase.h.

    { if (!this->isRef()) this->freePtrData(); }

Member Function Documentation

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

Get the last index of the data.

Returns:
the last index of the data

Definition at line 108 of file STK_IArrayBase.h.

Referenced by STK::IArrayBase< TYPE * >::data(), and STK::IArrayBase< TYPE * >::setData().

{ return rangeData_.last();}
template<class DATA>
Integer const& STK::IArrayBase< DATA >::sizeData ( ) const [inline]

Get the size of the data.

Returns:
the size of the data

Definition at line 112 of file STK_IArrayBase.h.

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

{ return rangeData_.size();}
template<class DATA>
Range const& STK::IArrayBase< DATA >::rangeData ( ) const [inline]

Get the range of the data.

Returns:
the range of the data

Definition at line 116 of file STK_IArrayBase.h.

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

{ return rangeData_;}
template<class DATA>
DATA const* const STK::IArrayBase< DATA >::ptrData ( ) const [inline]

Get the main ptr on the data.

a pointer on the data set

Definition at line 120 of file STK_IArrayBase.h.

{ return p_data_;}
template<class DATA>
DATA const& STK::IArrayBase< DATA >::data ( Integer const &  pos) const [inline]
template<class DATA>
static Integer STK::IArrayBase< DATA >::evalCapacity ( Integer const &  m) [inline, static]

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

if m <=0 return 0

Parameters:
mthe size of the container

Definition at line 144 of file STK_IArrayBase.h.

    {
      Integer n = 0;
      Integer b = m;
      for (Integer k=1 ; k <= b; n++, k <<= 1);
      return m+n;
    }
template<class DATA>
DATA* STK::IArrayBase< DATA >::ptrData ( ) [inline, protected]

Get the main ptr on the data.

Definition at line 154 of file STK_IArrayBase.h.

    { return p_data_;}
template<class DATA>
DATA& STK::IArrayBase< DATA >::data ( Integer const &  pos) [inline, protected]

Get the element number pos.

Parameters:
posthe position of the element we get

Definition at line 160 of file STK_IArrayBase.h.

    {
#ifdef STK_BOUNDS_CHECK
      if (pos < firstData())
      { throw std::out_of_range("IArrayBase::data(pos) "
                                "IArrayBase::firstData() > pos");
      }
      if (pos > lastData())
      { throw std::out_of_range("IArrayBase::data(pos) "
                                "IArrayBase::lastData() < pos");
      }
#endif
      return p_data_[pos];
    }
template<class DATA>
void STK::IArrayBase< DATA >::setData ( Integer const &  pos,
DATA const &  data = DATA() 
) [inline, protected]

Set the element number pos : this method is not destined to the end-user.

No check about the given address is performed in this interface class.

Parameters:
posthe position of the element we set
datathe value we set

Definition at line 181 of file STK_IArrayBase.h.

Referenced by STK::IArray1DBase< Range, Range, Array1D< Range > >::moveElt().

   {
#ifdef STK_BOUNDS_CHECK
      if (pos < firstData())
      { throw std::out_of_range("IArrayBase::setData(pos, data) "
                                "IArrayBase::firstData() > pos");
      }
      if (pos > lastData())
      { throw std::out_of_range("IArrayBase::setData(pos, data) "
                                "IArrayBase::lastData() > pos");
      }
#endif
     p_data_[pos] = data;
   }
template<class DATA>
void STK::IArrayBase< DATA >::swap ( IArrayBase< DATA > &  T) [inline, protected]

swap this with T.

Parameters:
Tthe container to swap

Definition at line 199 of file STK_IArrayBase.h.

    {
      // copy main ptr data and range of this
      DATA*   auxMainPtr(p_data_);
      Range   auxRange(rangeData_);
      bool    auxRef(this->isRef());

      // overwrite main ptr data of this
      this->setPtrData(T.p_data_);
      this->setRangeData(T.rangeData_);
      this->setRef(T.isRef());

      // overwrite main PTRCOL of T
      T.setPtrData(auxMainPtr);
      T.setRangeData(auxRange);
      T.setRef(auxRef);
    }
template<class DATA>
void STK::IArrayBase< DATA >::shiftPtrData ( Integer const &  first) [inline, protected]

shift the first index of the data to first.

Parameters:
firstthe index of the first data to set

Definition at line 220 of file STK_IArrayBase.h.

    {
      // check if there is something to do
      if (first == firstData()) return;
      // check for reference
      if (this->isRef())
      { throw std::runtime_error("In IArrayBase::shiftPtrData(first)"
                                 " can't operate on reference.");
      }
      // compute increment
      Integer inc = first - firstData();
      // translate data
      decPtrData(inc);
    }
template<class DATA>
void STK::IArrayBase< DATA >::mallocPtrData ( Integer const &  size,
Integer const &  inc = 0 
) [inline, protected]

protected function for main ptr memory allocation.

Parameters:
sizethe size to reserve in memory
incthe increment to apply to p_data_ after allocation

Definition at line 238 of file STK_IArrayBase.h.

Referenced by STK::IArrayBase< TYPE * >::IArrayBase(), and STK::IArrayBase< TYPE * >::reallocPtrData().

    {
      // check for reference
      if (this->isRef())
      { throw std::runtime_error("In IArrayBase::mallocPtrData(first)"
                                 " can't operate on reference.");
      }
      // delete any memory allocated
      freePtrData();
      // check size
      if (size <= 0)
      {
        // set initial values
        setRangeData(Range(0,size-1));
        // first index is 0
        return;
      }
      // allocate memory
      try
      {
        setPtrData(new DATA[size]);
      }
      catch (std::bad_alloc & error)  // if an alloc error occur
      {
        // initialize to default
        setDefault();
        // and throw an exception
        throw std::runtime_error("IArrayBase::mallocPtrData(size, inc) "
                                 "memory allocation failed.");
      }
      // set initial values
      setRangeData(Range(0,size-1));  // first index is 0
      // apply increment
      decPtrData(inc);
    }
template<class DATA>
void STK::IArrayBase< DATA >::reallocPtrData ( Integer const &  size,
Integer const &  inc = 0 
) [inline, protected]

protected function for main ptr memory reallocation.

If the size requested is greater than the allocated size, the DATA stored are saved and copied using the operator=. the DATA class have to provide this operator.

If the size requested is lesser than the allocated size, only the first elements fitting in the container are copied.

Parameters:
sizethe size to reserve in memory
incthe increment to apply to p_data_ after allocation

Definition at line 286 of file STK_IArrayBase.h.

    {
      // check for reference
      if (this->isRef())
      { throw std::runtime_error("In IArrayBase::reallocPtrData(first)"
                                 " can't operate on reference.");
      }
      // if there is no memory allocated, we use mallocPtrData
      if (!p_data_)
      {
        mallocPtrData(size, inc);
        return;
      }
      // if the new size is empty we can safely remove existing data
      if (size <= 0)
      {
        freePtrData();
        return;
      }
      // reset to zero increment and get data adress
      incPtrData(firstData());
      DATA* p_data = p_data_;
      // allocate memory
      try
      {
        setPtrData(new DATA[size]);
      }
      catch (std::bad_alloc & error)  // if an alloc error occur
      {
        // delete existing data
        delete [] p_data;
        // initialize members to default
        setDefault();
        // and throw an exception
        throw std::runtime_error("IArrayBase::reallocPtrData(size, inc) "
                                 "memory allocation failed.");
      }
      // copy existing data
      Integer nbData = min(size, sizeData());
      for (Integer i=0; i< nbData; i++)
      { p_data_[i] = p_data[i];}
      // delete previous allocated data
      // delete existing data
      delete [] p_data;
      // set initial values
      setRangeData(Range(0,size-1));     // first index is 0
      // apply increment
      decPtrData(inc);
    }
template<class DATA>
void STK::IArrayBase< DATA >::freePtrData ( ) [inline, protected]

protected function for main ptr memory deallocation.

Definition at line 337 of file STK_IArrayBase.h.

Referenced by STK::IArrayBase< TYPE * >::mallocPtrData(), STK::IArray2DBase< TYPE, TYPE *, ArrayHo< TYPE >, Array1D< TYPE >, Container2D >::pushBackByTransfer(), STK::IArrayBase< TYPE * >::reallocPtrData(), and STK::IArrayBase< TYPE * >::~IArrayBase().

    {
      // nothing to do for ref
      if (this->isRef()) return;
      // if there is elts
      if (p_data_)
      {
        incPtrData(firstData());  // translate
        delete [] p_data_;   // erase
        setPtrData();        // set default values
      }
    }
template<class DATA>
void STK::IArrayBase< DATA >::setPtrData ( DATA *  p_data = 0) [inline, protected]
template<class DATA>
void STK::IArrayBase< DATA >::setRangeData ( Range const &  rangeData = Range()) [inline, protected]

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

Parameters:
rangeDatathe range of the data to set

Definition at line 361 of file STK_IArrayBase.h.

Referenced by STK::IArrayBase< TYPE * >::mallocPtrData(), STK::IArrayBase< TYPE * >::reallocPtrData(), STK::IArrayBase< TYPE * >::setDefault(), and STK::IArrayBase< TYPE * >::swap().

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

Increment the address of the data : this method is not destined to the end-user.

Parameters:
incthe increment to apply

Definition at line 369 of file STK_IArrayBase.h.

Referenced by STK::IArrayBase< TYPE * >::freePtrData(), and STK::IArrayBase< TYPE * >::reallocPtrData().

    {
      // translate p_data_ only if there exists data
      if (this->p_data_) { p_data_ += inc;}
      rangeData_.dec(inc);
    }
template<class DATA>
void STK::IArrayBase< DATA >::decPtrData ( Integer const &  dec) [inline, private]

Decrement the address of the data : this method is not destined to the end-user.

Parameters:
decthe increment to apply

Definition at line 380 of file STK_IArrayBase.h.

Referenced by STK::IArrayBase< TYPE * >::mallocPtrData(), STK::IArrayBase< TYPE * >::reallocPtrData(), and STK::IArrayBase< TYPE * >::shiftPtrData().

    {
      // translate p_data_ only if there exists data
      if (this->p_data_) { p_data_  -= dec;}
      rangeData_.inc(dec);
    }
template<class DATA>
void STK::IArrayBase< DATA >::setDefault ( ) [inline, private]

Member Data Documentation


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