00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #ifndef RESIZEARRAY_H
00017 #define RESIZEARRAY_H
00018
00019 #include "ResizeArrayRaw.h"
00020
00021
00022 template <class Type> class ResizeArrayIter;
00023 template <class Type> class ResizeArrayRaw;
00024
00025 template <class Elem> class ResizeArray {
00026 friend class ResizeArrayIter<Elem>;
00027
00028 protected:
00029 ResizeArrayRaw<Elem> *rep;
00030
00031 public:
00032
00033 typedef Elem* iterator;
00034 iterator begin(void) { return rep->array; }
00035 iterator end(void) { return rep->array + rep->arraySize; }
00036 typedef const Elem* const_iterator;
00037 const_iterator const_begin(void) const { return rep->array; }
00038 const_iterator const_end(void) const { return rep->array + rep->arraySize; }
00039
00040
00041 ResizeArray(void) {
00042 rep = new ResizeArrayRaw<Elem>();
00043 rep->resize(0);
00044 rep->refCount = 1;
00045 }
00046
00047
00048 ResizeArray(int s) {
00049 rep = new ResizeArrayRaw<Elem>();
00050 rep->resize(s);
00051 rep->refCount = 1;
00052 }
00053
00054
00055 ResizeArray(ResizeArray<Elem> &ra) {
00056 rep = ra.rep;
00057 rep->refCount++;
00058 }
00059
00060
00061 ResizeArray(const ResizeArray<Elem>* ra) {
00062 rep = new ResizeArrayRaw<Elem>(*(ra->rep));
00063 rep->refCount = 1;
00064 }
00065
00066
00067 ResizeArray(Elem * * array, int arraySize, int allocSize=0) {
00068 rep = new ResizeArrayRaw<Elem>(array, arraySize, allocSize);
00069 rep->refCount = 1;
00070 }
00071
00072 virtual ~ResizeArray(void) {
00073 if (!--rep->refCount) delete rep;
00074 }
00075
00076
00077 ResizeArray<Elem> & operator= (ResizeArray<Elem> &ra) {
00078 if (rep != NULL && !(--rep->refCount) )
00079 delete rep;
00080 rep = ra.rep;
00081 rep->refCount++;
00082 return (*this);
00083 }
00084
00085
00086 void setParams(int s, float growthFactor) {
00087 rep->setResizeParams(s, growthFactor);
00088 }
00089
00090
00091
00092
00093 void resize(int i) { rep->resize(i); }
00094
00095
00096 void setall(const Elem &elem) {
00097 iterator i = begin();
00098 iterator e = end();
00099 for ( ; i != e; ++i ) *i = elem;
00100 }
00101
00102
00103 int add (const Elem &elem) {
00104 int end=rep->size();
00105 rep->ins(elem, end);
00106 return(end);
00107 }
00108
00109
00110 int del(int index, int num=1) {
00111 return(rep->del(index,num));
00112 }
00113
00114
00115 int insert (const Elem& elem, int index) {
00116 rep->ins(elem,index);
00117 return (index);
00118 }
00119
00120
00121 inline Elem & item(int i) {
00122 i = ( i < 0 ? 0 : i );
00123 if ((i+1) > size())
00124 resize(i+1);
00125 return rep->array[i];
00126 }
00127
00128
00129 inline Elem & operator[](int index) { return rep->array[index]; }
00130 inline const Elem & operator[](int index) const { return rep->array[index]; }
00131
00132
00133 inline int size(void) const { return rep->size(); }
00134
00135
00136 void reduce(void) { rep->reduce(); }
00137
00138 inline int find(const Elem &e) const { return rep->find(e); }
00139
00140
00141
00142
00143 void clear() {
00144 if (!--rep->refCount){
00145 delete rep;
00146 rep = new ResizeArrayRaw<Elem>();
00147 rep->resize(0);
00148 rep->refCount = 1;
00149 }
00150 }
00151 };
00152
00153 #endif