Safe arrays of class objects. More...
#include <classarray.h>
Public Member Functions | |
T & | operator[] (int n) |
Reference to n 'th element. More... | |
const T & | operator[] (int n) const |
Reference to n 'th const element. More... | |
T & | last () |
Reference to last element. More... | |
const T & | last () const |
Reference to last const element. More... | |
T * | get_ptr () |
Gets a C pointer to the data. More... | |
const T * | get_const_ptr () const |
Gets a const C pointer to the data. More... | |
void | append (const T &t) |
Appends element t . More... | |
void | append (int n, const T t[]) |
Appends n elements from t . More... | |
void | append (const ClassArray< T > &t) |
Appends all elements from t . More... | |
void | insert (int i, int n) |
Inserts n uninitialized elements before i 'th element. More... | |
void | insert (int i, int n, const T t[]) |
Inserts n elements from t before i 'the element. More... | |
void | insert (int i, const ClassArray< T > &t) |
Inserts all elements from t before i 'th element. More... | |
void | remove (int n=0, int m=1) |
Removes m elements starting at n . More... | |
void | removeLast (int m=1) |
Removes m last elements. More... | |
void | clear () |
Removes all elements. More... | |
int | size () const |
Returns number of elements. More... | |
void | reSize (int newsize) |
Resets size to newsize . More... | |
int | max () const |
Returns maximum number of elements. More... | |
ptrdiff_t | reMax (int newMax=1, int newSize=-1) |
Resets maximum number of elements. More... | |
ClassArray & | operator= (const ClassArray &rhs) |
Assignment operator. More... | |
bool | isConsistent () const |
Consistency check. More... | |
ClassArray (const ClassArray &old) | |
Copy constructor. More... | |
ClassArray (int p_size=0, int p_max=0, double p_fac=1.2) | |
Default constructor. More... | |
virtual | ~ClassArray () |
Destructor. More... | |
Protected Attributes | |
int | thesize |
number of used elements in array data More... | |
int | themax |
the length of array data and More... | |
T * | data |
the array of elements More... | |
double | memFactor |
memory extension factor. More... | |
Safe arrays of class objects.
Class ClassArray provides safe arrays of general C++ objects (in contrast to data objects). The elements of an instance of ClassArray can be accessed just like ordinary C++ array elements by means of the index operator[](). Safety is provided by
-DNDEBUG
Moreover, ClassArrays may easily be extended by inserting or appending elements to the ClassArray or shrunken by removing elements. Method reSize(int n) resets the ClassArrays length to n
thereby possibly appending elements or truncating the ClassArray to the required size.
A ClassArray may be used as arguments for standard C functions requiring pointers through the use of get_ptr() and get_const_ptr().
Internally, a ClassArray object allocates a block of memory that fits up to max() elements, only size() of them are used. This makes extension and shrinking methods perform better.
Definition at line 64 of file classarray.h.
ClassArray | ( | const ClassArray< T > & | old | ) |
Copy constructor.
Definition at line 340 of file classarray.h.
References ClassArray< T >::data, ClassArray< T >::isConsistent(), ClassArray< T >::max(), ClassArray< T >::size(), and soplex::spx_alloc().
|
explicit |
Default constructor.
The constructor allocates a ClassArray containing size
uninitialized elements. The internal array is allocated to have max
nonzeros, and the memory extension factor is set to fac
.
p_size | number of unitialised elements. |
p_max | maximum number of elements the array can hold. |
p_fac | value for memFactor. |
Definition at line 370 of file classarray.h.
References ClassArray< T >::data, ClassArray< T >::isConsistent(), ClassArray< T >::max(), soplex::spx_alloc(), ClassArray< T >::themax, and ClassArray< T >::thesize.
|
virtual |
Destructor.
Definition at line 391 of file classarray.h.
References ClassArray< T >::data, soplex::spx_free(), and ClassArray< T >::themax.
void append | ( | const ClassArray< T > & | t | ) |
Appends all elements from t
.
Definition at line 135 of file classarray.h.
References ClassArray< T >::insert(), and ClassArray< T >::thesize.
void append | ( | const T & | t | ) |
Appends element t
.
Definition at line 123 of file classarray.h.
References ClassArray< T >::insert(), and ClassArray< T >::thesize.
void append | ( | int | n, |
const T | t[] | ||
) |
Appends n
elements from t
.
Definition at line 129 of file classarray.h.
References ClassArray< T >::insert(), and ClassArray< T >::thesize.
void clear | ( | ) |
Removes all elements.
Definition at line 211 of file classarray.h.
References ClassArray< T >::thesize.
const T * get_const_ptr | ( | ) | const |
Gets a const C pointer to the data.
Definition at line 117 of file classarray.h.
References ClassArray< T >::data.
T * get_ptr | ( | ) |
Gets a C pointer to the data.
Definition at line 111 of file classarray.h.
References ClassArray< T >::data.
void insert | ( | int | i, |
const ClassArray< T > & | t | ||
) |
Inserts all elements from t
before i
'th element.
Definition at line 176 of file classarray.h.
References ClassArray< T >::data, ClassArray< T >::insert(), and ClassArray< T >::size().
void insert | ( | int | i, |
int | n | ||
) |
Inserts n
uninitialized elements before i
'th element.
move n
elements in memory from insert position i
to the back
Definition at line 141 of file classarray.h.
References ClassArray< T >::data, ClassArray< T >::reSize(), and ClassArray< T >::thesize.
Referenced by ClassArray< T >::append(), and ClassArray< T >::insert().
void insert | ( | int | i, |
int | n, | ||
const T | t[] | ||
) |
Inserts n
elements from t
before i
'the element.
Definition at line 164 of file classarray.h.
References ClassArray< T >::data, and ClassArray< T >::insert().
bool isConsistent | ( | ) | const |
Consistency check.
Definition at line 324 of file classarray.h.
References ClassArray< T >::data, ClassArray< T >::memFactor, SPX_MSG_INCONSISTENT, ClassArray< T >::themax, and ClassArray< T >::thesize.
Referenced by ClassArray< T >::ClassArray(), and ClassArray< T >::operator=().
T & last | ( | ) |
Reference to last element.
Definition at line 97 of file classarray.h.
References ClassArray< T >::data, and ClassArray< T >::thesize.
const T & last | ( | ) | const |
Reference to last const element.
Definition at line 104 of file classarray.h.
References ClassArray< T >::data, and ClassArray< T >::thesize.
int max | ( | ) | const |
Returns maximum number of elements.
Even though the ClassArray currently holds no more than size() elements, up to max() elements could be added without need to reallocated free store.
Definition at line 243 of file classarray.h.
References ClassArray< T >::themax.
Referenced by ClassArray< T >::ClassArray().
ClassArray & operator= | ( | const ClassArray< T > & | rhs | ) |
Assignment operator.
Definition at line 308 of file classarray.h.
References ClassArray< T >::data, ClassArray< T >::isConsistent(), ClassArray< T >::reSize(), and ClassArray< T >::size().
T & operator[] | ( | int | n | ) |
Reference to n
'th element.
Definition at line 81 of file classarray.h.
References ClassArray< T >::data, and ClassArray< T >::thesize.
const T & operator[] | ( | int | n | ) | const |
Reference to n
'th const element.
Definition at line 89 of file classarray.h.
References ClassArray< T >::data, and ClassArray< T >::thesize.
ptrdiff_t reMax | ( | int | newMax = 1 , |
int | newSize = -1 |
||
) |
Resets maximum number of elements.
The value of max() is reset to newMax
thereby setting size() to newSize
. However, if newSize
has a value <
0
(as the default argument does) size() remains unchanged and max() is set to MIN(size(), newMax). Hence, calling reMax() without the default arguments, will reduce the memory consumption to a minimum. In no instance max() will be set to a value less than 1 (even if specified).
Definition at line 257 of file classarray.h.
References ClassArray< T >::data, ClassArray< T >::size(), soplex::spx_alloc(), soplex::spx_free(), ClassArray< T >::themax, and ClassArray< T >::thesize.
Referenced by ClassArray< T >::reSize().
void remove | ( | int | n = 0 , |
int | m = 1 |
||
) |
Removes m
elements starting at n
.
Definition at line 188 of file classarray.h.
References ClassArray< T >::data, ClassArray< T >::size(), and ClassArray< T >::thesize.
void removeLast | ( | int | m = 1 | ) |
Removes m
last elements.
Definition at line 202 of file classarray.h.
References ClassArray< T >::size(), and ClassArray< T >::thesize.
void reSize | ( | int | newsize | ) |
Resets size to newsize
.
Resizing a ClassArray to less than the previous size, involves discarding its last elements. Resizing to a larger value involves adding uninitialized elements (similar to append()). If neccessary, also memory will be reallocated.
Definition at line 227 of file classarray.h.
References ClassArray< T >::memFactor, ClassArray< T >::reMax(), ClassArray< T >::themax, and ClassArray< T >::thesize.
Referenced by ClassArray< T >::insert(), and ClassArray< T >::operator=().
int size | ( | ) | const |
Returns number of elements.
Definition at line 217 of file classarray.h.
References ClassArray< T >::thesize.
Referenced by ClassArray< T >::ClassArray(), ClassArray< T >::insert(), ClassArray< T >::operator=(), SVSetBase< R >::operator=(), ClassArray< T >::reMax(), ClassArray< T >::remove(), and ClassArray< T >::removeLast().
|
protected |
the array of elements
Definition at line 69 of file classarray.h.
Referenced by ClassArray< T >::ClassArray(), ClassArray< T >::get_const_ptr(), ClassArray< T >::get_ptr(), ClassArray< T >::insert(), ClassArray< T >::isConsistent(), ClassArray< T >::last(), ClassArray< T >::operator=(), ClassArray< T >::operator[](), ClassArray< T >::reMax(), ClassArray< T >::remove(), and ClassArray< T >::~ClassArray().
|
protected |
memory extension factor.
When a ClassArray is reSize()d to more than max() elements, the new value for max() is not just set to the new size but rather to memFactor
* size
. This makes reSizeing perform better in codes where a ClassArray is extended often by a small number of elements only.
Definition at line 76 of file classarray.h.
Referenced by ClassArray< T >::isConsistent(), and ClassArray< T >::reSize().
|
protected |
the length of array data and
Definition at line 68 of file classarray.h.
Referenced by ClassArray< T >::ClassArray(), ClassArray< T >::isConsistent(), ClassArray< T >::max(), ClassArray< T >::reMax(), ClassArray< T >::reSize(), and ClassArray< T >::~ClassArray().
|
protected |
number of used elements in array data
Definition at line 67 of file classarray.h.
Referenced by ClassArray< T >::append(), ClassArray< T >::ClassArray(), ClassArray< T >::clear(), ClassArray< T >::insert(), ClassArray< T >::isConsistent(), ClassArray< T >::last(), ClassArray< T >::operator[](), ClassArray< T >::reMax(), ClassArray< T >::remove(), ClassArray< T >::removeLast(), ClassArray< T >::reSize(), and ClassArray< T >::size().