STK++ 1.0

STK::List1D< TYPE > Class Template Reference

Templated One dimensional Horizontal List. More...

#include <STK_List1D.h>

Inheritance diagram for STK::List1D< TYPE >:

List of all members.

Public Member Functions

 List1D (Range const &I=Range())
 Default constructor : beg_ =1 and end_ =0.
 List1D (Range const &I, TYPE const &v)
 Misc constructor, initialization with a constant.
 List1D (const List1D< TYPE > &T)
 Copy constructor.
virtual ~List1D ()
 virtual dtor.
TYPE & elt (Integer const &pos)
 access to one element.
TYPE const & elt (Integer const &pos) const
 access to one element const.
List1D< TYPE > elt (Range const &J) const
 access to many elements.
void clear ()
 Clear the object.
void shift (Integer const &beg=1)
 New first index for the object.
void pushBack (Integer const &n=1)
 Add n Elts to the container.
void insert (Range const &I, TYPE const &v)
 Insert element v in the range I of the List1D.
void insertElt (Integer const &pos, Integer const &n=1)
 Insert n elts at the position pos of the container.
void popBack (Integer const &n=1)
 Delete last elts of the container.
void erase (Integer const &pos, Integer const &n=1)
 Delete n elts at the pos index to the container.
void swap (Integer const &j1, Integer const &j2)
 Swapping the j1th column and the j2th column.
List1D< TYPE > & operator= (const List1D< TYPE > &T)
 operator = : overwrite the Array1D with t.
List1D< TYPE > & operator= (TYPE const &v)
 operator= : set the container to a constant value.

Protected Member Functions

 List1D (CellHo< TYPE > *const &p_first, CellHo< TYPE > *const &p_last, Range const &J)
 constructor by reference, ref_=1.
void initialize (Range const &I)
 Protected function for initialization.
void freeMem ()
 Protected function for deallocation.
void setDefault ()
 Protected function for setting members to default.

Protected Attributes

CellHo< TYPE > * p_first_
 First Element of the List.
CellHo< TYPE > * p_last_
 Last Element of the List.

Private Member Functions

void moveCurrLeft () const
 Private function for moving the current position : it's very low level manipulation functions, and no check is done at this level.
void moveCurrRight () const
 move Current position to right
void moveCurr (Integer const &pos) const
 Move the current position to pos.

Private Attributes

Integer currentPosition_
 Current position of pointer p_current_ in the List1D.
CellHo< TYPE > * p_current_
 Current position pointed in the List1D.

Detailed Description

template<class TYPE>
class STK::List1D< TYPE >

Templated One dimensional Horizontal List.

A List1D is an implementation of the Interface ITContainer1D for list.

Definition at line 52 of file STK_List1D.h.


Constructor & Destructor Documentation

template<class TYPE>
STK::List1D< TYPE >::List1D ( Range const &  I = Range()) [inline]

Default constructor : beg_ =1 and end_ =0.

Parameters:
Irange of the container

Definition at line 63 of file STK_List1D.h.

          : ITContainer1D<TYPE, List1D>(I)
          , IContainerRef(false)
    { initialize(I); }
template<class TYPE>
STK::List1D< TYPE >::List1D ( Range const &  I,
TYPE const &  v 
) [inline]

Misc constructor, initialization with a constant.

Parameters:
Irange of the container
vinitial value of the container

Definition at line 72 of file STK_List1D.h.

          : ITContainer1D<TYPE, List1D >(I)
          , IContainerRef(false)
    { initialize(I);

      CellHo<TYPE>* p1  = p_first_;
      for ( Integer j=this->first(); j<=this->last(); j++)
      { (*p1) = v;             // overwrite the value of the current cell
        p1    = p1->getRight();   // Goto Right place
      }
    }
template<class TYPE>
STK::List1D< TYPE >::List1D ( const List1D< TYPE > &  T) [inline]

Copy constructor.

Parameters:
Tthe container to copy

Definition at line 87 of file STK_List1D.h.

          : ITContainer1D<TYPE, List1D>(T)
          , IContainerRef(false)
    {
      // initialize container
      initialize(T.range());
      // copy the container
      CellHo<TYPE>* p1  = p_first_;
      CellHo<TYPE>* pt1 = T.p_first_;

      for (Integer j=T.first(); j<=T.last(); j++)
      { (*p1) = pt1->data();   // write the value of the current cell
        p1    = p1->getRight();   // Goto Right
        pt1   = pt1->getRight();  // Goto Right
      }
    }
template<class TYPE>
STK::List1D< TYPE >::List1D ( CellHo< TYPE > *const &  p_first,
CellHo< TYPE > *const &  p_last,
Range const &  J 
) [inline, protected]

constructor by reference, ref_=1.

This constructor does not copy physically the elements contained in the Container. The List1D is wrapped by a reference List1D reduced to the range J.

Parameters:
p_firstthe first cell of the container to wrap
p_lastthe last cell of the container to wrap
Jrange of the data to wrap

Definition at line 114 of file STK_List1D.h.

          : ITContainer1D<TYPE, List1D>(J)
          , IContainerRef(true)
          , p_first_(p_first)
          , p_last_(p_last)
    {
      // Current position
      currentPosition_  = this->first();
      p_current_ = p_first;
    }
template<class TYPE>
virtual STK::List1D< TYPE >::~List1D ( ) [inline, virtual]

virtual dtor.

Definition at line 130 of file STK_List1D.h.

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

Member Function Documentation

template<class TYPE>
TYPE& STK::List1D< TYPE >::elt ( Integer const &  pos) [inline]

access to one element.

Parameters:
posindex of the element
Returns:
a reference on the element pos

Reimplemented from STK::ITContainer1D< TYPE, List1D< TYPE > >.

Reimplemented in STK::DataFrame.

Definition at line 136 of file STK_List1D.h.

Referenced by STK::Category::tool().

    {
      moveCurr(pos);
      return (p_current_->data());
    }
template<class TYPE>
TYPE const& STK::List1D< TYPE >::elt ( Integer const &  pos) const [inline]

access to one element const.

Parameters:
posindex of the const element
Returns:
a constant reference on the element pos

Reimplemented from STK::ITContainer1D< TYPE, List1D< TYPE > >.

Reimplemented in STK::DataFrame.

Definition at line 146 of file STK_List1D.h.

    {
      moveCurr(pos);
      return (p_current_->data());
    }
template<class TYPE>
List1D<TYPE> STK::List1D< TYPE >::elt ( Range const &  J) const [inline]

access to many elements.

Parameters:
Jthe range of the elements
Returns:
a list with a reference to the elements in the given range

Reimplemented from STK::ITContainer1D< TYPE, List1D< TYPE > >.

Definition at line 156 of file STK_List1D.h.

    {
#ifdef STK_BOUNDS_CHECK
      if ((J.first()<this->first()))
      { throw std::out_of_range("List1D::elt(J) "
                           "J.first()<this->first()");
      }
      if ((J.last()>this->last()))
      { throw std::out_of_range("List1D::elt(J) "
                           "J.last()>this->last()");
      }
#endif
      // get J.first() cell adress
      moveCurr(J.first());
      CellHo<TYPE>* p_first = p_current_;
      // get J.last() cell adress
      moveCurr(J.last());
      CellHo<TYPE>* p_last = p_current_;
      // return the reference
      return List1D<TYPE>(p_first, p_last, J);
    }
template<class TYPE>
void STK::List1D< TYPE >::clear ( ) [inline, virtual]

Clear the object.

Memory is liberated and the range of the Container is set to 0:-1.

Implements STK::IContainer1D.

Reimplemented in STK::DataFrame.

Definition at line 181 of file STK_List1D.h.

    {
      if (this->isRef()) return;   // Nothing to do for ref
      freeMem();        // Free mem
      this->setRange(); // Set to default the dimension
    }
template<class TYPE>
void STK::List1D< TYPE >::shift ( Integer const &  beg = 1) [inline, virtual]

New first index for the object.

Parameters:
begnew first index of the Container.

Implements STK::IContainer1D.

Reimplemented in STK::DataFrame.

Definition at line 191 of file STK_List1D.h.

    {
      if (this->first() == beg) return;
#ifdef STK_DEBUG
      // is this structure just a pointer?
      if (this->isRef())
      { throw std::runtime_error("List1D::shift(pos, n) "
                            "can't operate on references.");
      }
#endif
      //compute increment
      Integer inc = beg - this->first();
      this->incRange(inc);  // update this->range_()
      currentPosition_ += inc;         // update current position
    }
template<class TYPE>
void STK::List1D< TYPE >::pushBack ( Integer const &  n = 1) [inline, virtual]

Add n Elts to the container.

Parameters:
nnumber of elements to add

Implements STK::IContainer1D.

Definition at line 210 of file STK_List1D.h.

    {
      // if n==0 nothing to do
      if (n <= 0) return;
#ifdef STK_DEBUG
      // is this structure just a pointer?
      if (this->isRef())
      { throw std::runtime_error("List1D::pushBack(pos, n) "
                            "can't operate on references.");
      }
#endif
      // If the container is empty : create it
      if (this->empty())
      {
        initialize(Range(this->first(), this->first()+ n -1));
      }
      else  // else adjust the beginning and the sizes
      {
        CellHo<TYPE> *p1, *p2;            // Auxiliary cells;
        try
        { p1 = new CellHo<TYPE>(p_last_);} // Create the end+1 cell
        catch (std::bad_alloc & error)   // if an alloc error is catched
        { // throw the exception
          throw std::runtime_error("List1D::pushBack(n) "
                              "memory allocation failed.");
        }
        // if no error is intercepted
        p_last_->setRight(p1);             // Set the right ending cell
        for (Integer j=2; j<=n; j++)    // main loop for the other cells
        { try
          { p2 = new CellHo<TYPE>(p1);}  // try to allocate memory
          catch (std::bad_alloc & error) // if an alloc error occur
          {
            while ( p1 != p_last_)         // for all cells allocated
            { p2 = p1->getLeft();        // get the cell left
              delete p1;                 // delete the curent cell
              p1 = p2;                   // iterate
            }
            // set the original right side of cend
            p_last_->setRight(p_first_);
            // and throw an exception
            throw std::runtime_error("List1D::pushBack(n) "
                                "memory allocation failed.");
          } // end catch
          // if no error is intercepted
          p1->setRight(p2);  // Set the right cell of the current cell
          p1 = p2;           // Set the current cell to the the next cell
        }
        p1->setRight(p_first_);    // the last cell point on the first cell
        p_first_->setLeft(p1);     // the first cell point on the last cell
        p_last_ = p1;             // the last cell adress
        this->incLast(n); // Update size of the container
      }
    }
template<class TYPE>
void STK::List1D< TYPE >::insert ( Range const &  I,
TYPE const &  v 
) [inline]

Insert element v in the range I of the List1D.

Parameters:
Irange of the index where to insert elements
vthe value tu insert

Reimplemented from STK::ITContainer1D< TYPE, List1D< TYPE > >.

Definition at line 269 of file STK_List1D.h.

Referenced by STK::List1D< IVariable * >::insert().

    { ITContainer1D<TYPE, List1D<TYPE> >::insert(I, v);}
template<class TYPE>
void STK::List1D< TYPE >::insertElt ( Integer const &  pos,
Integer const &  n = 1 
) [inline, virtual]

Insert n elts at the position pos of the container.

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

Implements STK::IContainer1D.

Definition at line 276 of file STK_List1D.h.

    {
      // if n<=0 nothing to do
      if (n <= 0) return;
      // is this structure just a pointer?
      if (this->isRef())
      { throw std::runtime_error("List1D::insertElt(pos, n) "
                                 "can't operate on references.");
      }
#ifdef STK_BOUNDS_CHECK
      // check indices
      if (this->first() > pos)
      { throw std::out_of_range("List1D::insertElt(pos, n) "
                                "this->first() > pos");
      }
      if (this->last()+1 < pos)
      { throw std::out_of_range("List1D::insertElt(pos, n) "
                                "this->last()+1 < pos");
      }
#endif
      // Move the current position to j
      moveCurr(pos);
      CellHo<TYPE> *p0 = p_current_->getLeft(); // Get the j-1 cell
      CellHo<TYPE> *p1 = p0;                    // Auxiliary cell;
      // main loop for the other cells
      for (Integer j1=1; j1<=n; j1++)
      {
        CellHo<TYPE> *p2;        // Auxiliary cell;
        try
        { p2 = new CellHo<TYPE>(p1);}  // try to allocate memory
        catch (std::bad_alloc & error) // if an alloc error occur
        { while ( p1 != p0)            // for all cells allocated
          { p2 = p1;                   // get the cell left
            delete p1;                 // delete the curent cell
            p1 = p2->getLeft();        // iterate
          }
          p0->setRight(p_current_);
          // and throw an exception
          throw std::runtime_error("List1D::insert(j, n) "
                              "memory allocation failed.");
        } // catch block
        // if no error is intercepted
        p1->setRight(p2);  // Set the right cell of the current cell
        p1 = p2;           // iterate
      }
      p1->setRight(p_current_);     // the last cell point on the first cell
      p_current_->setLeft(p1);      // the first cell point on the last cell
      if ( pos==this->first() )     // if the beginning was modified
      { p_first_ = p0->getRight();} // set new beginning
      this->incLast(n);             // Update the size of the container
      currentPosition_ +=n;         // Update the current position
    }
template<class TYPE>
void STK::List1D< TYPE >::popBack ( Integer const &  n = 1) [inline, virtual]

Delete last elts of the container.

Parameters:
nnumber of elts to delete

Implements STK::IContainer1D.

Definition at line 332 of file STK_List1D.h.

    {
      // if n<=0 nothing to do
      if (n <= 0) return;
      // is this structure just a pointer?
      if (this->isRef())
      { throw std::runtime_error("List1D::popBack() "
                                 "can't operate on references.");
      }
#ifdef STK_BOUNDS_CHECK
      // if there is elts to erase
      if (this->size()<n)
      { throw std::out_of_range("List1D::popBack(n) "
                                "this->size() < n");
      }
#endif
      // erase elts with pos = end -n +1
      erase(this->last() - n +1, n);
    }
template<class TYPE>
void STK::List1D< TYPE >::erase ( Integer const &  pos,
Integer const &  n = 1 
) [inline, virtual]

Delete n elts at the pos index to the container.

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

Implements STK::IContainer1D.

Definition at line 356 of file STK_List1D.h.

Referenced by STK::List1D< IVariable * >::popBack(), and STK::Category::removeTool().

    {
      // if n==0 nothing to do
      if (n<=0) return;
      // is this structure just a pointer?
      if (this->isRef())
      { throw std::runtime_error("List1D::erase(pos, n) "
                                 "can't operate on references.");
      }
#ifdef STK_BOUNDS_CHECK
      // check bounds
      if (this->first() > pos)
      { throw std::out_of_range("List1D::erase(pos, n) "
                           "this->first() > pos");
      }
      if (this->last() < pos)
      { throw std::out_of_range("List1D::erase(pos, n) "
                           "this->last() < pos");
      }
      if (this->last() < pos+n-1)
      { throw std::out_of_range("List1D::erase(pos, n) "
                           "this->last() < pos+n-1");
      }
#endif
      stk_cout << "erase(pos,n)::pos =" << pos << "\n";
      stk_cout << "erase(pos,n)::n=" << n << "\n";
      stk_cout << "Moving current to pos\n";
      // Move the current position to pos
      moveCurr(pos);
      stk_cout << "p_current_->data() =" << p_current_->data() << "\n";
      CellHo<TYPE>* p2 = p_current_;  // get pos-th cell
      stk_cout << "Moving current to left\n";
      moveCurrLeft();                // set current to (pos-1)th position
      stk_cout << "p_current_->data() =" << p_current_->data() << "\n";
      // delete n cells
      for (Integer l=1; l<=n; l++)
      { CellHo<TYPE>* p3 = p2->getRight();  // get right cell in p3
        delete p2;                          // delete current cell
        p2 = p3;                            // next
      }
      // If the last column have been erased update p_last_
      if (pos+n-1 == this->last()) { p_last_ = p_current_;}
      // Update the dimension of the container
      this->decLast(n);
      // If we have erased all cols
      if (this->size() == 0)
      { setDefault();}
      else
      { p2->setLeft(p_current_);        // p2 is the j+n cell
        p_current_->setRight(p2);       // p_current_ is on j-1 cell
        // If the first column has been erased
        if (pos == this->first())
        { p_first_  = p2;   // Set the new beg cell
          p_current_ = p2;   // p_current_
          currentPosition_++;       // and current position
        }
      }
    }
template<class TYPE>
void STK::List1D< TYPE >::swap ( Integer const &  j1,
Integer const &  j2 
) [inline]

Swapping the j1th column and the j2th column.

Parameters:
j1index of the first element to swap
j2index of the second element to swap

Reimplemented from STK::ITContainer1D< TYPE, List1D< TYPE > >.

Definition at line 419 of file STK_List1D.h.

    {
#ifdef STK_BOUNDS_CHECK
      if (j1<this->first())
      { throw std::out_of_range("List1D::swap(j1, j2) "
                           "j1<this->first()");
      }
      if (j1>this->last())
      { throw std::out_of_range("List1D::swap(j1, j2) "
                           "j1>this->last()");
      }
      if (j2<this->first())
      { throw std::out_of_range("List1D::swap(j1, j2) "
                           "j2<this->first()");
      }
      if (j2>this->last())
      { throw std::out_of_range("List1D::swap(j1, j2) "
                           "j2>this->last()");
      }
#endif
      // get j1th value in aux
      moveCurr(j1);
      CellHo<TYPE> *p1 = p_current_;
      TYPE aux = p1->data();
      // set j2th value in j1th position
      moveCurr(j2);
      (*p1) = p_current_->data();
      // set j2th value to aux
      (*p_current_) = aux;
    }
template<class TYPE>
List1D<TYPE>& STK::List1D< TYPE >::operator= ( const List1D< TYPE > &  T) [inline]

operator = : overwrite the Array1D with t.

We resize the object if this and T does not have the same size but if they have the same size, we don't modify the range of the object.

Parameters:
Tthe container to copy

Definition at line 456 of file STK_List1D.h.

    {
      // We have to resize if this and T have not the same size
      // but if they have the same size, we don't scale the index
      if (this->size()!=T.size()) { this->resize(T.range());}

      /* copy without ovelapping.                                     */
      if (this->first() < T.first())
      { CellHo<TYPE> *p1 = p_first_, *pt1 = T.p_first_;
        for (Integer j=1; j<=this->size(); j++)
        { (*p1) = pt1->data();   // overwrite the value
          p1    = p1->getRight();   // Goto Right for this
          pt1   = pt1->getRight();  // Goto Right for T
        }
      }
      else
      { CellHo<TYPE> *p1 = p_last_, *pt1 = T.p_last_;
        for (Integer j=this->size(); j>=1; j--)
        { (*p1) = pt1->data();   // overwrite the value
          p1    = p1->getLeft();    // Goto Left for this
          pt1   = pt1->getLeft();   // Goto Left for T
        }
      }
      return *this;
    }
template<class TYPE>
List1D<TYPE>& STK::List1D< TYPE >::operator= ( TYPE const &  v) [inline]

operator= : set the container to a constant value.

Parameters:
vthe value to set

Definition at line 485 of file STK_List1D.h.

    {
      CellHo<TYPE>* p1 = p_first_;
      for (Integer j=1; j<=this->size(); j++)
      { p1->setData(v);      // overwrite the value of the current cell
        p1    = p1->getRight(); // Goto Right
      }
      return *this;
    }
template<class TYPE>
void STK::List1D< TYPE >::initialize ( Range const &  I) [inline, protected]

Protected function for initialization.

Definition at line 497 of file STK_List1D.h.

Referenced by STK::List1D< IVariable * >::List1D(), and STK::List1D< IVariable * >::pushBack().

    {
       // set new dimensions
      this->setRange(I);
      if (this->empty())
      {
        setDefault(); return;
      }
      // Allocate memory for the cells
      CellHo<TYPE> *p1, *p2;        // Auxiliary pointer for cells

      p1 = new CellHo<TYPE>();        // pointer on the first cell
      p_first_ = p1;                     // set the first cell
      // main loop for the other cells
      for (Integer j=this->first()+1; j<=this->last(); j++)
      { try
        { p2 = new CellHo<TYPE>(p1);}        // try to allocate memory
        catch (std::bad_alloc & error)       // if an alloc error occur
        { while (p1 != (CellHo<TYPE>*)NULL)  // for all cells allocated
          { p2 = p1->getLeft();              // get the cell left
            delete p1;                       // delete the cell
            p1 = p2;                         // and iterate
          }
          // set default
          setDefault();
          this->setRange();
          // and throw the exception
          throw std::runtime_error("List1D::initialize(beg, end) "
                              "memory allocation failed.");
        }
        // if no error is catched
        p1->setRight(p2);      // Set the right cell
        p1 = p2;               // and iterate
      }
      p_last_ = p1;              // Set the last cell
      p_last_->setRight(p_first_);  // the last cell point on the first cell
      p_first_->setLeft(p_last_);   // the first cell point on the last cell

      currentPosition_  = this->first();    // current position is first position
      p_current_ = p_first_;            // Current cell is first cell
    }
template<class TYPE>
void STK::List1D< TYPE >::freeMem ( ) [inline, protected]

Protected function for deallocation.

Reimplemented in STK::DataFrame.

Definition at line 540 of file STK_List1D.h.

Referenced by STK::List1D< IVariable * >::clear(), and STK::List1D< IVariable * >::~List1D().

    {
      if (this->isRef()) return;   // Nothing to do for ref
      CellHo<TYPE> *p2, *p1 =p_first_;   // Auxiliary pointers for cells
      // for all cells
      for (Integer j=this->first(); j<=this->last(); j++)
      { p2 = p1->getRight();               // get the right cell
        delete p1;                         // delete the curent cell
        p1 = p2;                           // and iterate
      }
      setDefault();
    }
template<class TYPE>
void STK::List1D< TYPE >::setDefault ( ) [inline, protected]

Protected function for setting members to default.

Definition at line 555 of file STK_List1D.h.

Referenced by STK::List1D< IVariable * >::erase(), STK::List1D< IVariable * >::freeMem(), and STK::List1D< IVariable * >::initialize().

    { p_first_  = (CellHo<TYPE>*)NULL;
      p_last_  = (CellHo<TYPE>*)NULL;
      p_current_ = (CellHo<TYPE>*)NULL;
      currentPosition_  = this->first();
    }
template<class TYPE>
void STK::List1D< TYPE >::moveCurrLeft ( ) const [inline, private]

Private function for moving the current position : it's very low level manipulation functions, and no check is done at this level.

Note that it is constant functions, as the object itself is not modified, the modified members are mutable. Move Current position to left

Definition at line 582 of file STK_List1D.h.

Referenced by STK::List1D< IVariable * >::erase(), and STK::List1D< IVariable * >::moveCurr().

    {
      p_current_ = p_current_->getLeft();
      currentPosition_--;
    }
template<class TYPE>
void STK::List1D< TYPE >::moveCurrRight ( ) const [inline, private]

move Current position to right

Definition at line 589 of file STK_List1D.h.

Referenced by STK::List1D< IVariable * >::moveCurr().

    {
      p_current_ = p_current_->getRight();
      currentPosition_++;
    }
template<class TYPE>
void STK::List1D< TYPE >::moveCurr ( Integer const &  pos) const [inline, private]

Move the current position to pos.

Parameters:
posthe position to move

Definition at line 597 of file STK_List1D.h.

Referenced by STK::List1D< IVariable * >::elt(), STK::List1D< IVariable * >::erase(), STK::List1D< IVariable * >::insertElt(), and STK::List1D< IVariable * >::swap().

    {
      // if j is greater than the current
      if (pos>currentPosition_)
      {
        if ((pos-currentPosition_) <= (this->last()-pos))    // if j is near the current
          for( ;currentPosition_!=pos; ) moveCurrRight(); // move to right
        else                                 // else we are near the end
          for( currentPosition_ = this->last(), p_current_ = p_last_
             ; currentPosition_!=pos
             ; )
             moveCurrLeft();
      }
      else  // else j is less than the current
      {
        if ((currentPosition_-pos) <= (pos-this->first()))  // if j is near the current
          for( ;currentPosition_!=pos; ) moveCurrLeft();  // move to left
        else                                 // else we are near the beg
          for( currentPosition_ = this->first(), p_current_ = p_first_
             ; currentPosition_!=pos
             ; )
             moveCurrRight();
      }
    }

Member Data Documentation


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