Scippy

SoPlex

Sequential object-oriented simPlex

ClassArray< T > Class Template Reference

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...
 
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 64 of file classarray.h.

Constructor & Destructor Documentation

◆ ClassArray() [1/2]

◆ ClassArray() [2/2]

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

◆ ~ClassArray()

virtual ~ClassArray ( )
virtual

Destructor.

Definition at line 391 of file classarray.h.

References ClassArray< T >::data, soplex::spx_free(), and ClassArray< T >::themax.

Member Function Documentation

◆ append() [1/3]

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.

◆ append() [2/3]

void append ( const T &  t)

Appends element t.

Definition at line 123 of file classarray.h.

References ClassArray< T >::insert(), and ClassArray< T >::thesize.

◆ append() [3/3]

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.

◆ clear()

void clear ( )

Removes all elements.

Definition at line 211 of file classarray.h.

References ClassArray< T >::thesize.

◆ get_const_ptr()

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.

◆ get_ptr()

T * get_ptr ( )

Gets a C pointer to the data.

Definition at line 111 of file classarray.h.

References ClassArray< T >::data.

◆ insert() [1/3]

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

◆ insert() [2/3]

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

◆ insert() [3/3]

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

◆ isConsistent()

bool isConsistent ( ) const

◆ last() [1/2]

T & last ( )

Reference to last element.

Definition at line 97 of file classarray.h.

References ClassArray< T >::data, and ClassArray< T >::thesize.

◆ last() [2/2]

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.

◆ max()

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

◆ operator=()

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

◆ operator[]() [1/2]

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.

◆ operator[]() [2/2]

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.

◆ reMax()

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

◆ remove()

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.

◆ removeLast()

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.

◆ reSize()

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

◆ size()

Member Data Documentation

◆ data

◆ memFactor

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 76 of file classarray.h.

Referenced by ClassArray< T >::isConsistent(), and ClassArray< T >::reSize().

◆ themax

◆ thesize