Scippy

SoPlex

Sequential object-oriented simPlex

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...
 
ClassArrayoperator= (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...
 

Detailed Description

template<class T>
class soplex::ClassArray< T >

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.

  • automatic memory management in constructor and destructor preventing memory leaks
  • checking of array bounds when accessing elements with the indexing operator[]() when compiled without -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.

See also
Array, Data Objects

Definition at line 55 of file classarray.h.

Constructor & Destructor Documentation

ClassArray ( const ClassArray< T > &  old)

Copy constructor.

Definition at line 328 of file classarray.h.

ClassArray ( int  p_size = 0,
int  p_max = 0,
double  p_fac = 1.2 
)
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.

Parameters
p_sizenumber of unitialised elements.
p_maxmaximum number of elements the array can hold.
p_facvalue for memFactor.

Definition at line 357 of file classarray.h.

virtual ~ClassArray ( )
virtual

Destructor.

Definition at line 378 of file classarray.h.

Member Function Documentation

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< Real > >::append(), and ClassArray< Nonzero< Real > >::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 314 of file classarray.h.

Referenced by ClassArray< Nonzero< Real > >::ClassArray(), and ClassArray< Nonzero< Real > >::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< Real > >::ClassArray().

ClassArray& operator= ( const ClassArray< T > &  rhs)

Assignment operator.

Definition at line 298 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).

Returns
reMax returns the difference in bytes of the new and the old memory block, which can be used to update pointers pointing to elements of the memory block.

Definition at line 248 of file classarray.h.

Referenced by ClassArray< Nonzero< Real > >::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< Real > >::insert(), and ClassArray< Nonzero< Real > >::operator=().

Member Data Documentation

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

int themax
protected

the length of array data and

Definition at line 59 of file classarray.h.

Referenced by ClassArray< Nonzero< Real > >::max().

int thesize
protected

number of used elements in array data

Definition at line 58 of file classarray.h.

Referenced by ClassArray< Nonzero< Real > >::insert(), and ClassArray< Nonzero< Real > >::size().