1 // Larbin 2 // Sebastien Ailleret 3 // 04-02-00 -> 10-12-01 4 5 #ifndef VECTOR_HH 6 #define VECTOR_HH 7 8 #include "types.h" 9 10 #include <assert.h> 11 12 template <class T> 13 class Vector { 14 private: 15 /** This array contain the object */ 16 T **tab; 17 /** Number of object in the array */ 18 uint pos; 19 /** Size of the array */ 20 uint size; 21 22 public: 23 /** Constructor */ 24 Vector (uint size = StdVectSize); 25 /** Destructor */ 26 ~Vector (); 27 /** Re-init this vector : empty it all */ 28 void recycle (); 29 /** add an element to this vector */ 30 void addElement (T *elt); 31 /** give the size of the vector */ getLength()32 inline uint getLength () { return pos; } 33 /** give the array containing the objects */ getTab()34 inline T **getTab() { return tab; } 35 /** get an element of this Vector */ 36 T *operator [] (uint i); 37 }; 38 39 /** Constructor 40 * @param size the initial capacity of the Vector 41 */ 42 template <class T> Vector(uint size)43Vector<T>::Vector (uint size) { 44 this->size = size; 45 pos = 0; 46 tab = new T*[size]; 47 } 48 49 /** Destructor */ 50 template <class T> ~Vector()51Vector<T>::~Vector () { 52 for (uint i=0; i<pos; i++) { 53 delete tab[i]; 54 } 55 delete [] tab; 56 } 57 58 /** Re-init this vector : empty it all */ 59 template <class T> recycle()60void Vector<T>::recycle () { 61 for (uint i=0; i<pos; i++) { 62 delete tab[i]; 63 } 64 pos = 0; 65 } 66 67 /** add an element to this vector */ 68 template <class T> addElement(T * elt)69void Vector<T>::addElement (T *elt) { 70 assert (pos <= size); 71 if (pos == size) { 72 size *= 2; 73 T **tmp = new T*[size]; 74 for (uint i=0; i<pos; i++) { 75 tmp[i] = tab[i]; 76 } 77 delete [] tab; 78 tab = tmp; 79 } 80 tab[pos] = elt; 81 pos++; 82 } 83 84 /** get an element of this Vector */ 85 template <class T> 86 T *Vector<T>::operator [] (uint i) { 87 if (i<pos) { 88 return tab[i]; 89 } else { 90 return NULL; 91 } 92 } 93 94 #endif // VECTOR_HH 95