Laxkit  0.0.7.1
Public Member Functions | Public Attributes | Protected Attributes
Laxkit::NumStack< T > Class Template Reference

A generic stack for values (like int, double), not pointers. More...

List of all members.

Public Member Functions

 NumStack ()
 Constructor, creates null list, n=max=0, e=NULL, delta=10.
 NumStack (const NumStack &numstack)
 Copy constructor, makes max=numstack.n+delta if numstack.e exists, else max=0.
NumStackoperator= (NumStack &numstack)
 Preserves delta, adjust everything else.
const NumStackoperator= (const NumStack &numstack)
 Preserves delta, adjust everything else.
virtual T & operator[] (int i)
 Should probably remove this function..
virtual void flush ()
 Deletes e, sets e=NULL, sets n and max to 0.
virtual int howmany ()
 Returns how many things are on the stack.
virtual int findindex (T t)
 Find the index (in the range [0,n-1]) corresponding to the value t.
virtual void swap (int i1, int i2)
 Swap the elements with indices i1 and i2.
virtual int push (T nd, int where=-1)
 Push an element onto the stack before index where.
virtual int pushnodup (T nd)
 Pushes an element only if it is not already on the stack.
virtual T pop (int which=-1)
 Pop which.
virtual int remove (int which=-1)
 Removes an element like pop(), but returns 0 if element found, else 1 if not found.
virtual void Delta (int ndelta)
 Set the delta, the size of a chunk of memory to allocate or deallocate.
virtual int Delta ()
 Get the delta, the size of a chunk of memory to allocate or deallocate.
virtual T * extractArray (int *nn=NULL)
 Return the e array, and set e to NULL, max=n=0.
virtual int insertArray (T *a, int nn)
 Flush, then use a as the new array.

Public Attributes

int n
 The number of elements on the stack.
T * e
 The elements of the stack.

Protected Attributes

int delta
 Size of chunks of memory to add or remove from internal array.
int max
 The number of spaces allocated in the internal array.

Detailed Description

template<class T>
class Laxkit::NumStack< T >

A generic stack for values (like int, double), not pointers.

IMPORTANT: Do not use this to stack classes that have special allocations in them!! Internally, this class uses memcpy and memmove to work on its elements. It does not trigger any assingment operator.

The internal array has max elements allocated. When an item is pushed, and the number of actual elements is greater than max, then the array is reallocated with max+delta spaces. If an element is popped, and the number of elements is less than max-2*delta, then the array is reallocated with max-delta spaces.

max is readonly, but you can set and get delta with Delta(int) and Delta().


Member Function Documentation

template<class T>
int Laxkit::NumStack< T >::findindex ( t)
virtual

Find the index (in the range [0,n-1]) corresponding to the value t.

If the element is not in the stack, then -2 is returned. The -2 is used rather than -1 so that one can call pop(findindex(t)) and not pop anything if the element is not there. Otherwise, pop(-1) makes the top of the stack popped.

template<class T>
int Laxkit::NumStack< T >::howmany ( )
inlinevirtual

Returns how many things are on the stack.

This function is not called from within NumStack, so derived classes can make "how many things on the stack" mean something other than how many elements are in e.

References Laxkit::NumStack< T >::n.

template<class T>
int Laxkit::NumStack< T >::insertArray ( T *  a,
int  nn 
)
virtual

Flush, then use a as the new array.

Note that it is assumed that flush sets e=NULL. e is then set to a. No element copying is done, the actual array a is used. Does not alter delta, sets max=nn.

Returns 0 for success, nonzero for error.

template<class T >
T Laxkit::NumStack< T >::pop ( int  which = -1)
virtual
template<class T>
int Laxkit::NumStack< T >::push ( ne,
int  where = -1 
)
virtual
template<class T>
int Laxkit::NumStack< T >::pushnodup ( ne)
virtual

Pushes an element only if it is not already on the stack.

Returns the index if the item was already on the stack. Otherwise -1, which means that the item got pushed.

Todo:
should probably have -2 for error, though currently no errors are caught.

Referenced by LaxInterfaces::GradientInterface::LBDown(), and LaxInterfaces::GradientInterface::SelectPoint().

template<class T >
void Laxkit::NumStack< T >::swap ( int  i1,
int  i2 
)
virtual

Swap the elements with indices i1 and i2.

If i1 or i2 is out of bounds, then substitute the top of the stack for it.


The documentation for this class was generated from the following files:

Mon Feb 17 2014 11:52:59, Laxkit