Container template. More...

#include <rcontainer.h>

Inheritance diagram for RContainer< C, bAlloc, bOrder >:
[legend]
Collaboration diagram for RContainer< C, bAlloc, bOrder >:
[legend]

Public Member Functions

 RContainer (size_t m, size_t i=0)
 RContainer (const RContainer< C, true, bOrder > &src)
 RContainer (const RContainer< C, false, bOrder > &src)
size_t GetNb (void) const
size_t GetMaxPos (void) const
size_t GetMaxNb (void) const
size_t GetIncNb (void) const
bool VerifyIndex (size_t idx) const
void VerifyTab (size_t max=0)
void Clear (size_t m=0, size_t i=0)
int Compare (const RContainer< C, bAlloc, bOrder > &) const
void ReOrder (int sortOrder(const void *, const void *), size_t min=0, size_t max=0)
void ReOrder (size_t min=0, size_t max=0)
void Exchange (size_t pos1, size_t pos2)
template<bool a, bool o>
void Transfer (RContainer< C, a, o > &src)
RContaineroperator= (const RContainer< C, true, bOrder > &src)
RContaineroperator= (const RContainer< C, false, bOrder > &src)
template<bool a>
void Copy (const RContainer< C, a, bOrder > &src)
RContaineroperator+= (const RContainer< C, true, true > &src)
RContaineroperator+= (const RContainer< C, true, false > &src)
RContaineroperator+= (const RContainer< C, false, true > &src)
RContaineroperator+= (const RContainer< C, false, false > &src)
template<class TUse >
size_t GetIndex (bool order, const TUse &tag, bool &find, size_t min=0, size_t max=0) const
template<class TUse >
size_t GetIndex (const TUse &tag, bool &find, size_t min=0, size_t max=0) const
template<class TUse >
bool IsIn (const TUse &tag, bool sortkey=bOrder, size_t min=0, size_t max=0) const
const C * operator[] (size_t idx) const
C * operator[] (size_t idx)
const C * GetPtrAt (size_t idx) const
C * GetPtrAt (size_t idx)
template<class TUse >
C * GetPtr (const TUse &tag, bool sortkey=bOrder, size_t min=0, size_t max=0) const
template<class TUse >
C * GetInsertPtr (const TUse &tag, bool sortkey=bOrder, size_t min=0, size_t max=0)
template<class TUse >
C * GetInsertPtrAt (const TUse &tag, size_t pos)
size_t GetTab (const void **tab, size_t min=0, size_t max=0) const
size_t GetTab (void **tab, size_t min=0, size_t max=0)
size_t GetTab (const C **tab, size_t min=0, size_t max=0) const
size_t GetTab (C **tab, size_t min=0, size_t max=0)
template<class TUse >
RContainer< C, false, bOrder > * GetPtrs (const TUse &tag, size_t min=0, size_t max=0) const
void InsertPtrAt (C *ins, size_t pos, bool del=bAlloc)
void InsertPtr (C *ins, bool del=false, size_t min=0, size_t max=0)
void DeletePtrAt (size_t pos, bool shift=true, bool del=bAlloc)
template<class TUse >
void DeletePtr (const TUse &tag, bool sortkey=bOrder, bool del=bAlloc, size_t min=0, size_t max=0)
virtual ~RContainer (void)

Protected Member Functions

template<bool b>
void Create (const RContainer< C, b, bOrder > &src)
template<bool b>
RContainerNormalCopy (const RContainer< C, b, bOrder > &src)
template<bool b>
void DeepCopy (const RContainer< C, b, bOrder > &src)
template<bool b, bool o>
RContainerAdd (const RContainer< C, b, o > &src)

Static Protected Member Functions

static int SortOrder (const void *a, const void *b)

Protected Attributes

C ** Tab
size_t NbPtr
size_t MaxPtr
size_t LastPtr
size_t IncPtr

Detailed Description

template<class C, bool bAlloc, bool bOrder = false>
class R::RContainer< C, bAlloc, bOrder >

Container template.

This class represent a container of elements (class C). This elements are stored in an array of pointers which will be increase when necessary. The container can be responsible for the deallocation of the elements (bAlloc), and the elements can be ordered (bOrder).

Template Parameters:
CThe class of the elements to be contained.
bAllocSpecify if the elements are deallocated by the container.
bOrderSpecify if the elements are ordered in the container.

To make the necessary comparisons, the container uses member functions of the class representing the elements (class C). These functions have the signature:

 int Compare(const TUse& tag) const;
 int Compare(const TUse* tag) const;

The TUse represent a class or a structure used for the comparisons. These functions are working like the strcmp function from the standard C/C++ library. The result returned specifies if the tag precedes (>0), is the same (0) or is after (<0) the element used. The structure used for the Comparisons can represent or not the sort key used when the container must be ordered. The user must specify if the key used for a search is the sorting one or not.

At least, a compare function must be implemented in the class C:

 int Compare(const C&) const;

To iterate through the container, a RCursor can be used. Here is an example of class MyElement that will be contained in the variable c:

 #include <rcontainer.h>
 #include <rcursor.h>
 using namespace R;

 class MyElement
 {
    size_t Id;
 public:
    MyElement(size_t id) : Id(id) {}
    MyElement(const MyElement& e) : Id(e.Id) {}
    void DoSomething(double d) {cout<<d<<endl;}
    int Compare(const MyElement& e) const {return(Id-e.Id);}
    int Compare(const size_t id) const {return(Id-id);}
    int Compare(const char* text) const
    {
       size_t id=atoi(text);
       return(Compare(id));
    }
 };

 int main()
 {
    R::RContainer<MyElement,true,true> c(20,10);

    c.InsertPtr(new MyElement(5));
    if(c.IsIn<char*>("5"))
       cout<<"An element of value 5 is in the container"<<endl;
    c.InsertPtr(new MyElement(10));
    R::RCursor<MyElement> cur(c);
    for(cur.Start();!cur.End();cur.Next())
       cur()->DoSomething(2.3);
 }
Author:
Pascal Francq

Constructor & Destructor Documentation

RContainer ( size_t  m,
size_t  i = 0 
) [explicit]

Constructor of a container.

Parameters:
mThe initial maximal size of the array.
iThe value used when increasing the array. If null value, the size is set to the half the maximal size.
RContainer ( const RContainer< C, true, bOrder > &  src)

Copy constructor of a container.

Parameters:
srcContainer used as source.
RContainer ( const RContainer< C, false, bOrder > &  src)

Copy constructor of a container.

Parameters:
srcContainer used as source.
virtual ~RContainer ( void  ) [virtual]

Destruct the container.


Member Function Documentation

static int SortOrder ( const void *  a,
const void *  b 
) [static, protected]

Generic static template function to sort a container.

void Create ( const RContainer< C, b, bOrder > &  src) [protected]

Create a container from another one. If the pointer to the container is null, an empty container is created.

Template Parameters:
bDetermine if the source container is responsible for the allocation.
Parameters:
srcPointer to the source container.
RContainer& NormalCopy ( const RContainer< C, b, bOrder > &  src) [protected]

Copy a container from another one. If the pointer to the container is null, the container is just emptied.

Template Parameters:
bDetermine if the source container is responsible for the allocation.
Parameters:
srcPointer to the source container.
void DeepCopy ( const RContainer< C, b, bOrder > &  src) [protected]

Deep copy of a container in another one. If the pointer to the container is null, the container is just emptied.

Template Parameters:
bDetermine if the source container is responsible for the allocation.
Parameters:
srcPointer to the source container.
RContainer& Add ( const RContainer< C, b, o > &  src) [protected]

Add a container (if the pointer is not null) from another one.

Template Parameters:
bDetermine if the source container is responsible for the allocation.
oDetermine if the source container is ordered.
Parameters:
srcPointer to the source container.
size_t GetMaxPos ( void  ) const
Returns:
the maximal position occupied by an elements in the container.
size_t GetMaxNb ( void  ) const
Returns:
the maximum number of elements in the container.
size_t GetIncNb ( void  ) const
Returns:
the increment used to resize the container.
bool VerifyIndex ( size_t  idx) const

Verify if an index is a valid one in the container.

Parameters:
idxIndex.
Returns:
true if the index is valid.
void VerifyTab ( size_t  max = 0)

Verify if the container can hold a certain number of elements. If not, the container is extended.

Parameters:
maxMaximal number of elements that must be contained. If null, the method verifies that one element can be added.
void Clear ( size_t  m = 0,
size_t  i = 0 
)

Clear the container and destruct the elements if it is responsible for the deallocation.The container can also be extended.

Parameters:
mNew maximal size of the array. If null, the old size remains.
iNew increasing value. If null, the old value remains.

Reimplemented in RStack< C, bAlloc, bPushTop, bPopTop >, RStack< RPrgInstBlock, false, true, true >, RStack< RString, true, true, true >, and RStack< RPrgScope, true, true, true >.

int Compare ( const RContainer< C, bAlloc, bOrder > &  ) const

Compare method that can be used to construct an unordered container of containers.

void ReOrder ( int   sortOrderconst void *, const void *,
size_t  min = 0,
size_t  max = 0 
)

ReOrder the container.

Parameters:
sortOrderPointer to a (static) function used for the ordering.
minStarting index of the container part concerned.
maxEnding index of the container part concerned.
Warning:
This method must be used with caution, because it can crash the container if:
  1. The container contains null pointers.
  2. The container is ordered and the method does not use the same criterion for the ordering.
void ReOrder ( size_t  min = 0,
size_t  max = 0 
)

ReOrder the container based on the 'Compare' method of the objects contained.

Parameters:
minStarting index of the container part concerned.
maxEnding index of the container part concerned.
Warning:
This method must be used with caution, because it can crash the container if the container contains null pointers.
void Exchange ( size_t  pos1,
size_t  pos2 
)

Exchange two elements in the container. The method does not verify if the exchange is compatible with the ordering of the container.

Parameters:
pos1Position of the first element.
pos2Position of the second element.
void Transfer ( RContainer< C, a, o > &  src)

Transfer a container into another one. The destination container is cleared before.

Template Parameters:
bDetermine if the source container is responsible for the allocation.
oDetermine if the source container is ordered.
Parameters:
srcSource container.
RContainer& operator= ( const RContainer< C, true, bOrder > &  src)

The assignment operator.

Parameters:
srcContainer used as source.
Warning:
If the container is not a responsible for the allocation, the elements of src are just copied and not re-created. Use Copy if you want a "deep" copy of src.
RContainer& operator= ( const RContainer< C, false, bOrder > &  src)

The assignment operator.

Parameters:
srcContainer used as source.
Warning:
If the container is not a responsible for the allocation, the elements of src are just copied and not re-created. Use Copy if you want a "deep" copy of src.
void Copy ( const RContainer< C, a, bOrder > &  src)

Deep copy of a container.

Template Parameters:
aDetermine if the source container is responsible for the allocation.
Parameters:
srcContainer used as source.
RContainer& operator+= ( const RContainer< C, true, true > &  src)

Add the elements of a container. If the source container contains null elements, these elements are not copied.

Parameters:
srcContainer used as source.
RContainer& operator+= ( const RContainer< C, true, false > &  src)

Add the elements of a container. If the source container contains null elements, these elements are not copied.

Parameters:
srcContainer used as source.
RContainer& operator+= ( const RContainer< C, false, true > &  src)

Add the elements of a container. If the source container contains null elements, these elements are not copied.

Parameters:
srcContainer used as source.
RContainer& operator+= ( const RContainer< C, false, false > &  src)

Add the elements of a container. If the source container contains null elements, these elements are not copied.

Parameters:
srcContainer used as source.
size_t GetIndex ( bool  order,
const TUse &  tag,
bool &  find,
size_t  min = 0,
size_t  max = 0 
) const

This function returns the index of an element represented by tag, and it is used when the elements are to be ordered. The search can be limited to a part of the container.

Template Parameters:
TUseThe type of tag, the container uses the Compare(TUse &) member function of the elements.
Parameters:
orderMust the search be done on an ordered basis ?
tagThe tag used.
findIf the element represented by tag exist, find is set to true.
minStarting index of the container part concerned.
maxEnding index of the container part concerned.
Returns:
Returns the index of the element if it exists or the index where is has to inserted.
size_t GetIndex ( const TUse &  tag,
bool &  find,
size_t  min = 0,
size_t  max = 0 
) const

This function returns the index of an element represented by tag, and it is used when the elements are to be ordered. The search can be limited to a part of the container.

Template Parameters:
TUseThe type of tag, the container uses the Compare(TUse &) member function of the elements.
Parameters:
tagThe tag used.
findIf the element represented by tag exist, find is set to true.
minStarting index of the container part concerned.
maxEnding index of the container part concerned.
Returns:
Returns the index of the element if it exists or the index where is has to inserted.
bool IsIn ( const TUse &  tag,
bool  sortkey = bOrder,
size_t  min = 0,
size_t  max = 0 
) const

Look if a certain element is in the container. The search can be limited to a part of the container.

Template Parameters:
TUseThe type of tag, the container uses the Compare(TUse &) member function of the elements.
Parameters:
tagThe tag used.
sortkeyThe tag represents the sorting key. The default value depends if the container is ordered (true) or not (false).
minStarting index of the container's part concerned.
maxEnding index of the container's part concerned.
Returns:
Return true if the element is in the container.
const C* operator[] ( size_t  idx) const

Get a pointer to the ith element in the container (Only read). The operator generates an exception is the index is out of range.

Parameters:
idxIndex of the element to get.
Returns:
Return the pointer.

Reimplemented in RDblHashContainer< C, bAlloc >::Hash, RStack< C, bAlloc, bPushTop, bPopTop >, RStack< RPrgInstBlock, false, true, true >, RStack< RString, true, true, true >, and RStack< RPrgScope, true, true, true >.

C* operator[] ( size_t  idx)

Get a pointer to the ith element in the container (Read/Write). The operator generates an exception is the index is out of range.

Parameters:
idxIndex of the element to get.
Returns:
the pointer.

Reimplemented in RDblHashContainer< C, bAlloc >::Hash, RStack< C, bAlloc, bPushTop, bPopTop >, RStack< RPrgInstBlock, false, true, true >, RStack< RString, true, true, true >, and RStack< RPrgScope, true, true, true >.

const C* GetPtrAt ( size_t  idx) const

Get a pointer of the ith element in the container (Only read).

Parameters:
idxIndex of the element to get.
Returns:
the pointer of null if the index is out of range.
C* GetPtrAt ( size_t  idx)

Get a pointer of the ith element in the container (Read/Write).

Parameters:
idxIndex of the element to get.
Returns:
the pointer of null if the index is out of range.
C* GetPtr ( const TUse &  tag,
bool  sortkey = bOrder,
size_t  min = 0,
size_t  max = 0 
) const

Get a pointer to a certain element in the container.

Template Parameters:
TUseThe type of tag, the container uses the Compare(TUse &) member function of the elements.
Parameters:
tagThe tag used.
sortkeyThe tag represents the sorting key. The default value depends if the container is ordered (true) or not (false).
minStarting index of the container's part concerned.
maxEnding index of the container's part concerned.
Returns:
Return the pointer or 0 if the element is not in the container.
C* GetInsertPtr ( const TUse &  tag,
bool  sortkey = bOrder,
size_t  min = 0,
size_t  max = 0 
)

Get a pointer to a certain element in the container. If the element is not existing, the container creates it by using the constructor with TUse as parameter.

Template Parameters:
TUseThe type of tag, the container uses the Compare(TUse &) member function of the elements.
Parameters:
tagThe tag used.
sortkeyThe tag represents the sorting key. The default value depends if the container is ordered (true) or not (false).
minStarting index of the container's part concerned.
maxEnding index of the container's part concerned.
Returns:
The function returns a pointer to the element of the container.
C* GetInsertPtrAt ( const TUse &  tag,
size_t  pos 
)

Get a pointer to a certain element in the container. If the element is not existing, the container creates it by using the constructor with TUse as parameter.

Template Parameters:
TUseThe type of tag, the container uses the Compare(TUse &) member function of the elements.
Parameters:
tagThe tag used.
posThe position where to insert it.
Returns:
The function returns a pointer to the element of the container.
size_t GetTab ( const void **  tab,
size_t  min = 0,
size_t  max = 0 
) const

Copy the array of pointers into a temporary array. This array must have the right size (Read only). This method can be limited to a part of the container.

Parameters:
tabArray of pointers.
minStarting index of the container's part concerned.
maxEnding index of the container's part concerned.
Returns:
number of elements in the array (including eventually null pointers).
size_t GetTab ( void **  tab,
size_t  min = 0,
size_t  max = 0 
)

Copy the array of pointers into a temporary array. This array must have the right size (Read/Write). This method can be limited to a part of the container.

Parameters:
tabArray of pointers.
minStarting index of the container's part concerned.
maxEnding index of the container's part concerned.
Returns:
number of elements in the array (including eventually null pointers).
size_t GetTab ( const C **  tab,
size_t  min = 0,
size_t  max = 0 
) const

Copy the array of pointers into a temporary array. This array must have the right size (Read only). This method can be limited to a part of the container.

Parameters:
tabArray of pointers.
minStarting index of the container's part concerned.
maxEnding index of the container's part concerned.
Returns:
number of elements in the array (including eventually null pointers).
size_t GetTab ( C **  tab,
size_t  min = 0,
size_t  max = 0 
)

Copy the array of pointers into a temporary array. This array must have the right size (Read/Write). This method can be limited to a part of the container.

Parameters:
tabArray of pointers.
minStarting index of the container's part concerned.
maxEnding index of the container's part concerned.
Returns:
number of elements in the array (including eventually null pointers).
RContainer<C,false,bOrder>* GetPtrs ( const TUse &  tag,
size_t  min = 0,
size_t  max = 0 
) const

This method returns a container of all the elements that are responding to the given criteria. This method can be limited to a part of the container.

Template Parameters:
TUseThe type of tag, the container uses the Compare(TUse &) member function of the elements.
Parameters:
tagThe tag used.
minStarting index of the container's part concerned.
maxEnding index of the container's part concerned.
Returns:
The function returns a pointer to the result container.
void InsertPtrAt ( C *  ins,
size_t  pos,
bool  del = bAlloc 
)

Insert an element at a certain position. Two remarks must be made :

  1. The function verifies not if the index used is compatible with the order in case of the elements treated in ascending order.
  2. By using this function, the user can leave "blanks" in the container. In other words, some "valid" pointers could be null. This situation is not handle by the other functions of the container.
Parameters:
insA pointer to the element to insert.
posThe position where to insert it.
delSpecify if the object that was previously at the position should be deleted (true) or shifted (false).
void InsertPtr ( C *  ins,
bool  del = false,
size_t  min = 0,
size_t  max = 0 
)

Insert an element in the container. If the container is ordered and if the corresponding index is already used, the previously inserted element is removed from the container (and destroy if the container is responsible for the allocation). This method can be limited to a part of the container.

Parameters:
insA pointer to the element to insert.
delSpecify if a similar existing element must be deleted.
minStarting index of the container's part concerned.
maxEnding index of the container's part concerned.
void DeletePtrAt ( size_t  pos,
bool  shift = true,
bool  del = bAlloc 
)

Delete an element from the container at a given position.

Parameters:
posPosition of the element.
shiftSpecify if the the container should be shifted or if the position should be left empty.
delSpecify if the object must deleted or not. By default, the element is destruct if the container is responsible of the deallocation.
void DeletePtr ( const TUse &  tag,
bool  sortkey = bOrder,
bool  del = bAlloc,
size_t  min = 0,
size_t  max = 0 
)

Delete an element from the container. This method can be limited to a part of the container.

Template Parameters:
TUseThe type of tag, the container uses the Compare(TUse &) member function of the elements.
Parameters:
tagThe tag used.
sortkeyThe tag represents the sorting key. The default value depends if the container is ordered (true) or not (false).
delSpecify if the object must deleted or not. By default, the element is destruct if the container is responsible of the deallocation.
minStarting index of the container's part concerned.
maxEnding index of the container's part concerned.

Field Documentation

C** Tab [protected]

The array of pointers for the elements.

size_t NbPtr [protected]

The number of elements in the container.

size_t MaxPtr [protected]

The maximal number of elements that can be hold by the container actually.

size_t LastPtr [protected]

The last position in the array used by an object.

size_t IncPtr [protected]

When the array is increased, this value is used.