![]() |
GemaCoreLib
The GeMA Core library
|
A simple vector for Plain Old Data (POD) types with the control size variable type parameterized to allow for a smaller class size with ControlSize = unsigned int, with smaller vector size limits, or for a bigger class with ControlSize = size_t. Also receives as parameter the default growing strategy applied by its resize procedure and a flag stating if the memory allocation should be done in old C style (realloc / free) or in the new C++ style (operators new[] / delete[]). Using the old style is the default and allows us to, hopefully, benefit from realloc(). That can be an issue when using the takeData() method to transfer data ownership to other classes expecting to be able to release memory with operator delete[]. For those cases, set UseNew to true. More...
#include <gmPODVector.h>
Public Types | |
| typedef T | DataType |
| The stored data type. | |
Public Member Functions | |
| GmPODVector () | |
| Default constructor. Creates an empty vector. | |
| GmPODVector (ControlSize nvalues) | |
| The constructor initializing the vector with the given size. The vector contents is NOT initialized. | |
| ~GmPODVector () | |
| Destructor. | |
| ControlSize | size () const |
| Returns the vector size. | |
| ControlSize | capacity () const |
| Returns the vector allocated capacity. | |
| const T * | data () const |
| Returns a pointer to the data inside the vector. | |
| T * | data () |
| Non-const overload for data() | |
| T * | takeData () |
| For the bold who REALLY knows what he is doing! This is a very specialized function that takes the ownership of the data pointer from the vector, clears the vector, and returns the pointer to the data. The user is RESPONSIBLE for further deallocating the memory with a call to free() if UseNew is false or to delete[] if UseNew is true. | |
| const T & | operator[] (ControlSize index) const |
| Standard indexing operator. | |
| T & | operator[] (ControlSize index) |
| Non-const overload for the standard indexing operator. | |
| bool | append (const T &v) |
| Appends v to the vector end, growing the vector, if needed, using the growth algorithm specified as the Grow template parameter. Returns true on success, false on error. | |
| void | remove (ControlSize index, ControlSize numValues) |
| Removes numValues entries from the vector starting at index. | |
| bool | resize (ControlSize newSize, GmPODGrowT grow=Grow) |
| Resizes the vector to the given size using the specified grow policy. | |
| void | shrink () |
| Shrinks the allocated size to match the vector size if possible. Never fails but might do nothing. | |
| void | clear () |
| Clears the vector deallocating used memory. | |
| void | zero (ControlSize index=0) |
| Clears the vector area by setting to zero all entries starting at the given index. | |
Private Member Functions | |
| Q_DISABLE_COPY (GmPODVector) | |
| T * | PODrealloc (T *ptr, ControlSize news, ControlSize olds) |
| Emulates the standard realloc() semantics in a way that is compatible with the UseNew template parameter. Unlike realloc, the new size parameter (news) is the number of vector entries and the allocated size in bytes should be equal to s * sizeof(T). The olds parameter contains the current size of the vector. Remember that ptr can be NULL for the first allocation. | |
| void | PODfree (T *ptr) |
| Releases memory in a way that is compatible with the UseNew template parameter. | |
Private Attributes | |
| T * | _data |
| The vector. | |
| ControlSize | _size |
| The vector size. | |
| ControlSize | _alloc |
| The allocated space. | |
A simple vector for Plain Old Data (POD) types with the control size variable type parameterized to allow for a smaller class size with ControlSize = unsigned int, with smaller vector size limits, or for a bigger class with ControlSize = size_t. Also receives as parameter the default growing strategy applied by its resize procedure and a flag stating if the memory allocation should be done in old C style (realloc / free) or in the new C++ style (operators new[] / delete[]). Using the old style is the default and allows us to, hopefully, benefit from realloc(). That can be an issue when using the takeData() method to transfer data ownership to other classes expecting to be able to release memory with operator delete[]. For those cases, set UseNew to true.
|
inline |
Resizes the vector to the given size using the specified grow policy.
If newSize is bigger than the current allocated size, the vector will be grown using the policy. New entries are NOT initialized. If new size is smaller than the current size, space will be removed from the vector end. The allocated space never shrinks, and so reducing the size never fails. You can explicitly reduce the allocated size by calling shrink().