

/**************************************************************************\

MODULE: vector

SUMMARY:

Template class for dynamic-sized vectors.

The declaration

   Vec<T> v;

creates a zero-length vector.  To grow this vector to length n,
execute

   v.SetLength(n)

This causes space to be allocated for (at least) n elements, and also
causes the delault constructor for T to be called to initialize these
elements.

The current length of a vector is available as v.length().

The i-th vector element (counting from 0) is accessed as v[i].  If the
macro NTL_RANGE_CHECK is defined, code is emitted to test if 0 <= i <
v.length().  This check is not performed by default.

For old-time FORTRAN programmers, the i-th vector element (counting
from 1) is accessed as v(i).

Let n = v.length().  Calling v.SetLength(m) with m <= n sets the
current length of v to m (but does not call any destructors or free
any space).  Calling v.SetLength(m) with m > n will allocate space and
initialize as necessary, but will leave the values of the already
allocated elements unchanged (although their addresses may change).
Initializations are performed using T's default constructor.

v.MaxLength() is the largest value of n for which v.SetLength(n) was invoked,
and is equal to the number of entries that have been initialized.
v.SetMaxLength(n) will allocate space for and initialize up to n elements,
without changing v.length().

When v's destructor is called, all constructed elements will be
destructed, and all space will be relinquished.

Space is managed using malloc, realloc, and free.  When a vector is
grown, a bit more space may be allocated than was requested for
efficiency reasons.

Note that when a vector is grown, the space is reallocated using
realloc, and thus the addresses of vector elements may change,
possibly creating dangling references to vector elements.  One has to
be especially careful of this when using vectors passed as reference
parameters that may alias one another.

Because realloc is used to grow a vector, the objects stored
in a vector should be "relocatable"---that is, they shouldn't care
what their actual address is, which may change over time.
Most reasonable objects satisfy this constraint.

v.allocated() is the number of elements which have been allocated,
which may be more than the number elements initialized.
Note that if n <= v.allocated(), then v.SetLength(n) is guaranteed
not to cause any memory allocation, or movement of objects.

\**************************************************************************/

template<class T>
class Vec {  
public:  

   Vec();  // initially length 0

   Vec(const Vec<T>& a); 
   // copy constructor;  currently, this is implemented by
   // initializing elements using T's defaults constructor and 
   // copying elements from a using T's assigment operator.

   Vec& operator=(const Vec<T>& a);  
   // assignment: performs an element-wise assignment
   // using T's assignment operator.

   ~Vec();  
   // destructor: calls T's destructor for all initialized
   // elements in the vector, and then frees the vector itself
  
   void SetLength(long n);  
   // set current length to n, growing vector if necessary

   long length() const;
   // current length
  
   T& operator[](long i);
   const T& operator[](long i) const;
   // indexing operation, starting from 0.
   // The first version is applied to non-const Vec<T>,
   // and returns a non-const reference to a T, while the second version
   // is applied to a const Vec<T> and returns a const reference to a T.
  
   T& operator()(long i);
   const T& operator()(long i) const;
   // indexing operation, starting from 1
   // The first version is applied to non-const Vec<T>,
   // and returns a non-const reference to a T, while the second version
   // is applied to a const Vec<T> and returns a const reference to a T.
  
   T* elts();
   const T* elts() const;
   // returns address of first vector element (or 0 if no space has
   // been allocated for this vector).  If a vector potentially has
   // length 0, it is safer to write v.elts() instead of &v[0].
   // The first version is applied to non-const Vec<T>,
   // and returns a non-const pointer to a T, while the second version
   // is applied to a const Vec<T> and returns a const reference to a T.


// Alternative access interface 

   const T& get(long i) const; 
   // v.get(i) returns v[i]
 
   void put(long i, const T& a); 
   // v.put(i, a) equivalent to v[i] = q



// Some STL compatibility

   typedef T value_type;
   typedef value_type& reference;
   typedef const value_type& const_reference;
   typedef value_type *iterator;
   typedef const value_type *const_iterator; 

   T* data();
   const T* data() const;
   // v.data() same as v.elts()

   T* begin();
   const T* begin() const;
   // v.begin() same as v.elts()

   T* end();
   const T* end() const;
   // pointer to last element (or NULL)

   T& at(long i);
   const T& at(long i) const;
   // indexing with range checking


// the remaining member functions are a bit esoteric (skip on first
// reading)

   Vec(INIT_SIZE_TYPE, long n);
   // Vec(INIT_SIZE, n) initializes with an intial length of n.

   void kill(); 
   // release space and set to length 0

   void SetMaxLength(long n); 
   // allocates space and initializes up to n elements. Does not change
   // current length

   void FixLength(long n);
   // sets length to n and prohibits all future length changes.
   // FixLength may only be invoked immediately after the default
   // construction or kill.

   // The kill operation is also subsequently prohibited, and swap is
   // allowed on fixed length vectors of the same length.

   // FixLength is provided mainly to implement mat_T, to enforce
   // the restriction that all rows have the same length.

   long fixed() const;
   // test if length has been fixed by FixLength().

   long MaxLength() const;
   // maximum length, i.e., number of allocated and initialized elements

   long allocated() const;
   // the number of objects for which space has been allocated, but not
   // necessarily initialized;  this may be larger than MaxLength().

   T& RawGet(long i);
   const T& RawGet(long i) const;
   // indexing with no range checking

   long position(const T& a) const;
   // returns position of a in the vector, or -1 if it is not there.
   // The search is conducted from position 0 to allocated()-1 the vector, 
   // and an error is raised if the object is found at position MaxLength()
   // or higher (in which case a references an uninitialized object).
   // Note that if NTL_CLEAN_PTR flag is set, this routine takes
   // linear time, and otherwise, it takes constant time.

   long position1(const T& a) const;
   // returns position of a in the vector, or -1 if it is not there.
   // The search is conducted from position 0 to length()-1 of the vector.
   // Note that if NTL_CLEAN_PTR flag is set, this routine takes
   // linear time, and otherwise, it takes constant time.
         
};   


/**************************************************************************\

                       Some utility routines

\**************************************************************************/

   
template<class T>
void swap(Vec<T>& x, Vec<T>& y);
// swaps x & y by swapping pointers

template<class T>
void append(Vec<T>& v, const T& a); 
// appends a to the end of v

template<class T>
void append(Vec<T>& v, const Vec<T>& w);
// appends w to the end of v




/**************************************************************************\

                             Input/Output


The I/O format for a vector v with n elements is:

   [v[0] v[1] ... v[n-1]]

\**************************************************************************/

template<class T>
istream& operator>>(istream&, Vec<T>&);  

template<class T>
ostream& operator<<(ostream&, const Vec<T>&);  



/**************************************************************************\

                              Equality Testing

\**************************************************************************/


template<class T>
long operator==(const Vec<T>& a, const Vec<T>& b);  

template<class T>
long operator!=(const Vec<T>& a, const Vec<T>& b);


/**************************************************************************\

                  Customized Constructors and Destructors
 
Esoteric: skip on first reading

When new elements in a vector need to be constructed, the routine

   void BlockConstruct(T* p, long n);

is called, whose default implementation invokes the default
constructor for T n times.  Likewise, when a vector is destroyed, the
routine

   void BlockDestroy(T* p, long n);

is called, whose default implementation invokes the default destructor
for T n times.

Both of these default implementations can be overridden as follows
by overloading these functions with a custom implementation.
 
For an example of this, see vec_ZZ_p.c.

\**************************************************************************/

