The C++ template library has a std::vector template which allows for the easy definition of a class which has an auto-expanding vector of some parameter class.
See: http://www.cplusplus.com/reference/vect ... ?kw=vector
The above standard template class has a big bunch of member methods. Maybe more than what are needed for a particular parameter class instantiation. To avoid clutter and overhead, I use my own auto-expanding Vector template class for a number of parameter classes. These classes are for ply-indexed or iteration-indexed items where a fixed length vector would be inappropriate because Symbolic has no fixed MAXPLY or MAXITERATION constant.
For those with an interest, the header:
Code: Select all
typedef unsigned int ui;
#define ZOL(v, limit) for (ui v = 0; v < (limit); v++)
// Auto expanding vector template
template <class T>
class Vector
{
public:
Vector(void) {Allocate(1); Reset();}
~Vector(void) {delete [] storage;}
void Reset(void) {ZOL(index, count) storage[index].Reset();}
T& operator[](const ui index) {Ensure(index + 1); return storage[index];}
private:
void Allocate(const ui newcount) {count = newcount; storage = new T[count];}
void Ensure(const ui mincount) {while (count < mincount) Double();}
void Double(void)
{
const ui oldcount = count;
const T *oldstorage = storage;
Allocate(count * 2);
ZOL(index, oldcount)
{
storage[index] = oldstorage[index];
storage[index + oldcount].Reset();
};
delete [] oldstorage;
}
ui count;
T *storage;
};
The implementation limits unused memory to a maximum of just less than twice the maximum subscript and the number of allocations/copies to (log2 N)^2 where N is the maximum subscript.