golib
0.5
|
Array class. More...
#include <gofixedarray.h>
Public Member Functions | |
goFixedArray (goSize_t size=1, goSize_t reserve=0, goSize_t resize_overhead=0) | |
Constructor. More... | |
goFixedArray (const goFixedArray< T > &other) | |
goFixedArray (T *ptr, goSize_t size, goIndex_t stride) | |
goFixedArray< T > & | operator= (const goFixedArray< T > &other) |
void | setData (T *ptr, goSize_t size, goIndex_t stride) |
void | setPtr (T *ptr) |
void | ref (goFixedArray< T > &target, goSize_t startIndex=0, goSize_t size=0) |
T & | operator() (goIndex_t i) |
T & | operator[] (goIndex_t i) |
const T & | operator() (goIndex_t i) const |
const T & | operator[] (goIndex_t i) const |
goSize_t | getSize () const |
Get the size of the array in number of elements. More... | |
goSize_t | size () const |
Same as getSize(). | |
goSize_t | getReserved () const |
Get the number of actually allocated elements. More... | |
goSize_t | getResizeOverhead () const |
Get the number of resize overhead elements. More... | |
goIndex_t | getStride () const |
void | setResizeOverhead (goSize_t ro) |
Set the resize overhead in number of elements. More... | |
void | setSize (goSize_t newSize, goSize_t reserve=0, goSize_t resize_overhead=0) |
Set the size of the array, deleting old content. More... | |
bool | resize (goSize_t newSize) |
Proper resize with copying. More... | |
bool | operator== (const goFixedArray< T > &other) const |
bool | operator!= (const goFixedArray< T > &other) const |
T * | getPtr () |
Get the pointer to the data. More... | |
const T * | getPtr () const |
Get the pointer to the data. More... | |
void | fill (const T &value) |
Fill the array with value . More... | |
void | flip () |
Flip the direction of the data. More... | |
Array class.
This array can be used as a replacement for simple C++ arrays. As opposed to goArray<>, it can be used with any data type that offers new, delete, and = operators.
goArray uses malloc and can resize memory using the C library functions. goFixedArray also supports resize, but copies data when resizing and the size is larger than the number of reserved elements.
To resize an array, use the resize()
method. It copies the old content to the new array, if the array must be reallocated. It is only reallocated if the new size is larger than the reserved size. Use the setSize()
method to allocate newly without copying.
|
inlineexplicit |
Constructor.
Makes an array of size size
, with reserved memory for reserve
elements in total and resize_overhead
overhead elements (see getReserved() and getResizeOverhead()). If reserve is lower than size, size
elements are reserved.
size | Size in number elements. |
reserve | Reserved memory in number of elements (default 0) |
resize_overhead | Resize overhead in number of elements (default 0) |
|
inline |
Fill the array with value
.
value | The new value. |
|
inline |
Flip the direction of the data.
Actually copies the data.
@TODO: An alternative is to just change the stride and start pointer.
|
inline |
Get the pointer to the data.
|
inline |
Get the pointer to the data.
|
inline |
Get the number of actually allocated elements.
Get the number of elements for which memory was allocated. The size of the array is always lower than or equal to the number of reserved elements.
This is done in order to allow for quicker appending of new elements in situations where that is necessary.
This value can be set by the constructor or by setSize()
.
|
inline |
Get the number of resize overhead elements.
The "resize overhead" is a number of elements that is used in case of a resize()
if the new size is larger than the number of reserved elements (see getReserved()
). In that case, memory for newSize
+ getResizeOverhead()
elements will be allocated while the size will be newSize
(see resize()
).
The default is 0.
|
inline |
Get the size of the array in number of elements.
|
inline |
Proper resize with copying.
New space will be available at the end of the array.
If newSize
is larger than the current reserved space as returned by getReserved(), new memory with size newSize
+ getResizeOverhead()
will be allocated, the old content will be copied and the size is set to newSize
, reserved is set to newSize
+ getResizeOverhead()
.
If newSize
is lower than getReserved()
- 2 * getResizeOverhead()
, the array is also reallocated to newSize
+ getResizeOverhead()
and the contents are copied.
In all other cases, the size is simply adjusted.
newSize | New size of the array in number of elements. |
|
inline |
Set the resize overhead in number of elements.
ro | The new resize overhead in number of elements. |
|
inline |
Set the size of the array, deleting old content.
If the size is differing from the old size of the array, the array is deleted and new memory is allocated. Contents are lost. Use resize()
for a resize that copies the content automatically.
newSize | New size in number of elements. |
reserve | Number of elements to reserve memory for (see also getReserved() ). |
resize_overhead | Number of resize overhead elements (see also getResizeOverhead() ). |