golib  0.5
goFixedArray< T > Class Template Reference

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...
 

Detailed Description

template<class T>
class goFixedArray< T >

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.

Todo:
resize() currently does not shrink memory usage when resizing to a smaller size. This should possibly be changed as it may lead to unexpectedly large memory consumption of programs.
Author
Christian Gosch
Examples
karchermean/km.cpp, and sumproduct/sp.cpp.

Constructor & Destructor Documentation

◆ goFixedArray()

template<class T>
goFixedArray< T >::goFixedArray ( goSize_t  size = 1,
goSize_t  reserve = 0,
goSize_t  resize_overhead = 0 
)
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.

Parameters
sizeSize in number elements.
reserveReserved memory in number of elements (default 0)
resize_overheadResize overhead in number of elements (default 0)

Member Function Documentation

◆ fill()

template<class T>
void goFixedArray< T >::fill ( const T &  value)
inline

Fill the array with value.

Parameters
valueThe new value.

◆ flip()

template<class T>
void goFixedArray< T >::flip ( )
inline

Flip the direction of the data.

Actually copies the data.

@TODO: An alternative is to just change the stride and start pointer.

◆ getPtr() [1/2]

template<class T>
T* goFixedArray< T >::getPtr ( )
inline

Get the pointer to the data.

Returns
The pointer to the data.

◆ getPtr() [2/2]

template<class T>
const T* goFixedArray< T >::getPtr ( ) const
inline

Get the pointer to the data.

Returns
The pointer to the data.

◆ getReserved()

template<class T>
goSize_t goFixedArray< T >::getReserved ( ) const
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().

Returns
Number of elements for which memory was allocated.

◆ getResizeOverhead()

template<class T>
goSize_t goFixedArray< T >::getResizeOverhead ( ) const
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.

Returns
Number of overhead elements.

◆ getSize()

template<class T >
goSize_t goFixedArray< T >::getSize ( ) const
inline

Get the size of the array in number of elements.

Note
This is not necessarily equal to the number of allocated elements. The number of actually reserved elements can be retrieved with getReserved().
Returns
Size of the array in number of elements.
Examples
sumproduct/sp.cpp.

◆ resize()

template<class T>
bool goFixedArray< T >::resize ( goSize_t  newSize)
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.

Parameters
newSizeNew size of the array in number of elements.
Returns
True if successful, false otherwise.

◆ setResizeOverhead()

template<class T>
void goFixedArray< T >::setResizeOverhead ( goSize_t  ro)
inline

Set the resize overhead in number of elements.

See also
getResizeOverhead()
Parameters
roThe new resize overhead in number of elements.

◆ setSize()

template<class T>
void goFixedArray< T >::setSize ( goSize_t  newSize,
goSize_t  reserve = 0,
goSize_t  resize_overhead = 0 
)
inline

Set the size of the array, deleting old content.

See also
resize()

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.

Parameters
newSizeNew size in number of elements.
reserveNumber of elements to reserve memory for (see also getReserved()).
resize_overheadNumber of resize overhead elements (see also getResizeOverhead()).
Examples
sumproduct/sp.cpp.

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