Container template. More...
#include <rcontainer.h>
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) |
| RContainer & | operator= (const RContainer< C, true, bOrder > &src) |
| RContainer & | operator= (const RContainer< C, false, bOrder > &src) |
| template<bool a> | |
| void | Copy (const RContainer< C, a, bOrder > &src) |
| RContainer & | operator+= (const RContainer< C, true, true > &src) |
| RContainer & | operator+= (const RContainer< C, true, false > &src) |
| RContainer & | operator+= (const RContainer< C, false, true > &src) |
| RContainer & | operator+= (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> | |
| RContainer & | NormalCopy (const RContainer< C, b, bOrder > &src) |
| template<bool b> | |
| void | DeepCopy (const RContainer< C, b, bOrder > &src) |
| template<bool b, bool o> | |
| RContainer & | Add (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:
-
C The class of the elements to be contained. bAlloc Specify if the elements are deallocated by the container. bOrder Specify 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:
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); }
Constructor & Destructor Documentation
| RContainer | ( | size_t | m, |
| size_t | i = 0 |
||
| ) | [explicit] |
Constructor of a container.
- Parameters:
-
m The initial maximal size of the array. i The 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:
-
src Container used as source.
| RContainer | ( | const RContainer< C, false, bOrder > & | src | ) |
Copy constructor of a container.
- Parameters:
-
src Container 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:
-
b Determine if the source container is responsible for the allocation.
- Parameters:
-
src Pointer 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:
-
b Determine if the source container is responsible for the allocation.
- Parameters:
-
src Pointer 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:
-
b Determine if the source container is responsible for the allocation.
- Parameters:
-
src Pointer 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:
-
b Determine if the source container is responsible for the allocation. o Determine if the source container is ordered.
- Parameters:
-
src Pointer to the source container.
| size_t GetNb | ( | void | ) | const |
- Returns:
- the number of elements in the container.
Reimplemented in RStack< C, bAlloc, bPushTop, bPopTop >, RStack< RPrgInstBlock, false, true, true >, RStack< RString, true, true, true >, and RStack< RPrgScope, true, true, true >.
| 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:
-
idx Index.
- 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:
-
max Maximal 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:
-
m New maximal size of the array. If null, the old size remains. i New 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:
-
sortOrder Pointer to a (static) function used for the ordering. min Starting index of the container part concerned. max Ending 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.
- 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:
-
min Starting index of the container part concerned. max Ending 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:
-
pos1 Position of the first element. pos2 Position 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:
-
b Determine if the source container is responsible for the allocation. o Determine if the source container is ordered.
- Parameters:
-
src Source container.
| RContainer& operator= | ( | const RContainer< C, true, bOrder > & | src | ) |
The assignment operator.
- Parameters:
-
src Container 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:
-
src Container 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:
-
a Determine if the source container is responsible for the allocation.
- Parameters:
-
src Container 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:
-
src Container 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:
-
src Container 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:
-
src Container 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:
-
src Container 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:
-
TUse The type of tag, the container uses the Compare(TUse &) member function of the elements.
- Parameters:
-
order Must the search be done on an ordered basis ? tag The tag used. find If the element represented by tag exist, find is set to true. min Starting index of the container part concerned. max Ending 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:
-
TUse The type of tag, the container uses the Compare(TUse &) member function of the elements.
- Parameters:
-
tag The tag used. find If the element represented by tag exist, find is set to true. min Starting index of the container part concerned. max Ending 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:
-
TUse The type of tag, the container uses the Compare(TUse &) member function of the elements.
- Parameters:
-
tag The tag used. sortkey The tag represents the sorting key. The default value depends if the container is ordered (true) or not (false). min Starting index of the container's part concerned. max Ending 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:
-
idx Index 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:
-
idx Index 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:
-
idx Index 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:
-
idx Index 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:
-
TUse The type of tag, the container uses the Compare(TUse &) member function of the elements.
- Parameters:
-
tag The tag used. sortkey The tag represents the sorting key. The default value depends if the container is ordered (true) or not (false). min Starting index of the container's part concerned. max Ending 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:
-
TUse The type of tag, the container uses the Compare(TUse &) member function of the elements.
- Parameters:
-
tag The tag used. sortkey The tag represents the sorting key. The default value depends if the container is ordered (true) or not (false). min Starting index of the container's part concerned. max Ending 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:
-
TUse The type of tag, the container uses the Compare(TUse &) member function of the elements.
- Parameters:
-
tag The tag used. pos The 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:
-
tab Array of pointers. min Starting index of the container's part concerned. max Ending 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:
-
tab Array of pointers. min Starting index of the container's part concerned. max Ending 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:
-
tab Array of pointers. min Starting index of the container's part concerned. max Ending 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:
-
tab Array of pointers. min Starting index of the container's part concerned. max Ending 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:
-
TUse The type of tag, the container uses the Compare(TUse &) member function of the elements.
- Parameters:
-
tag The tag used. min Starting index of the container's part concerned. max Ending 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 :
- The function verifies not if the index used is compatible with the order in case of the elements treated in ascending order.
- 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:
-
ins A pointer to the element to insert. pos The position where to insert it. del Specify 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:
-
ins A pointer to the element to insert. del Specify if a similar existing element must be deleted. min Starting index of the container's part concerned. max Ending 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:
-
pos Position of the element. shift Specify if the the container should be shifted or if the position should be left empty. del Specify 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:
-
TUse The type of tag, the container uses the Compare(TUse &) member function of the elements.
- Parameters:
-
tag The tag used. sortkey The tag represents the sorting key. The default value depends if the container is ordered (true) or not (false). del Specify if the object must deleted or not. By default, the element is destruct if the container is responsible of the deallocation. min Starting index of the container's part concerned. max Ending 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.