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. 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 55 of file classarray.h.
ClassArray | ( | const ClassArray< T > & | old | ) |
Copy constructor.
Definition at line 331 of file classarray.h.
|
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 361 of file classarray.h.
|
virtual |
Destructor.
Definition at line 382 of file classarray.h.
void append | ( | const T & | t | ) |
Appends element t
.
Definition at line 114 of file classarray.h.
void append | ( | int | n, |
const T | t[] | ||
) |
Appends n
elements from t
.
Definition at line 120 of file classarray.h.
void append | ( | const ClassArray< T > & | t | ) |
Appends all elements from t
.
Definition at line 126 of file classarray.h.
void clear | ( | ) |
Removes all elements.
Definition at line 202 of file classarray.h.
const T* get_const_ptr | ( | ) | const |
Gets a const C pointer to the data.
Definition at line 108 of file classarray.h.
T* get_ptr | ( | ) |
Gets a C pointer to the data.
Definition at line 102 of file classarray.h.
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 132 of file classarray.h.
Referenced by ClassArray< Nonzero< Rational > >::append(), and ClassArray< Nonzero< Rational > >::insert().
void insert | ( | int | i, |
int | n, | ||
const T | t[] | ||
) |
Inserts n
elements from t
before i
'the element.
Definition at line 155 of file classarray.h.
void insert | ( | int | i, |
const ClassArray< T > & | t | ||
) |
Inserts all elements from t
before i
'th element.
Definition at line 167 of file classarray.h.
bool isConsistent | ( | ) | const |
Consistency check.
Definition at line 315 of file classarray.h.
Referenced by ClassArray< Nonzero< Rational > >::ClassArray(), and ClassArray< Nonzero< Rational > >::operator=().
T& last | ( | ) |
Reference to last element.
Definition at line 88 of file classarray.h.
const T& last | ( | ) | const |
Reference to last const element.
Definition at line 95 of file classarray.h.
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 234 of file classarray.h.
Referenced by ClassArray< Nonzero< Rational > >::ClassArray().
ClassArray& operator= | ( | const ClassArray< T > & | rhs | ) |
Assignment operator.
Definition at line 299 of file classarray.h.
T& operator[] | ( | int | n | ) |
Reference to n
'th element.
Definition at line 72 of file classarray.h.
const T& operator[] | ( | int | n | ) | const |
Reference to n
'th const element.
Definition at line 80 of file classarray.h.
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 248 of file classarray.h.
Referenced by ClassArray< Nonzero< Rational > >::reSize().
void remove | ( | int | n = 0 , |
int | m = 1 |
||
) |
Removes m
elements starting at n
.
Definition at line 179 of file classarray.h.
void removeLast | ( | int | m = 1 | ) |
Removes m
last elements.
Definition at line 193 of file classarray.h.
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 218 of file classarray.h.
Referenced by ClassArray< Nonzero< Rational > >::insert(), and ClassArray< Nonzero< Rational > >::operator=().
int size | ( | ) | const |
Returns number of elements.
Definition at line 208 of file classarray.h.
Referenced by ClassArray< Nonzero< Rational > >::ClassArray(), ClassArray< Nonzero< Rational > >::insert(), ClassArray< Nonzero< Rational > >::operator=(), ClassArray< Nonzero< Rational > >::reMax(), ClassArray< Nonzero< Rational > >::remove(), and ClassArray< Nonzero< Rational > >::removeLast().
|
protected |
the array of elements
Definition at line 60 of file classarray.h.
Referenced by ClassArray< Nonzero< Rational > >::ClassArray(), ClassArray< Nonzero< Rational > >::get_const_ptr(), ClassArray< Nonzero< Rational > >::get_ptr(), and ClassArray< Nonzero< Rational > >::operator=().
|
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 67 of file classarray.h.
|
protected |
the length of array data and
Definition at line 59 of file classarray.h.
Referenced by ClassArray< Nonzero< Rational > >::max().
|
protected |
number of used elements in array data
Definition at line 58 of file classarray.h.
Referenced by ClassArray< Nonzero< Rational > >::insert(), and ClassArray< Nonzero< Rational > >::size().