00001 00007 /* 00008 ResizeArray template 00009 Uses simple contingous array allocation in a hidden manner 00010 so that array object can have items added without limit 00011 Suffers from memory fragmentation during resizing 00012 Fast access, safe and efficient passing of encapsulated array thru 00013 function arguments. 00014 */ 00015 00016 #ifndef RESIZEARRAY_H 00017 #define RESIZEARRAY_H 00018 00019 #include "ResizeArrayRaw.h" 00020 00021 // Need this juju to use templated friend below 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 // STL style iterators 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 // Various Constructors 00041 ResizeArray(void) { 00042 rep = new ResizeArrayRaw<Elem>(); 00043 rep->resize(0); 00044 rep->refCount = 1; 00045 } 00046 00047 // Constructor make ResizeArray of predefined size 00048 ResizeArray(int s) { 00049 rep = new ResizeArrayRaw<Elem>(); 00050 rep->resize(s); 00051 rep->refCount = 1; 00052 } 00053 00054 // Contructor makes ResizeArray which points to same ResizeArrayRaw 00055 ResizeArray(ResizeArray<Elem> &ra) { 00056 rep = ra.rep; 00057 rep->refCount++; 00058 } 00059 00060 // Constructor makes a copy of ResizeArrayRaw 00061 ResizeArray(const ResizeArray<Elem>* ra) { 00062 rep = new ResizeArrayRaw<Elem>(*(ra->rep)); 00063 rep->refCount = 1; 00064 } 00065 00066 // Constructor to take-in pre-existing array 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 // We copy reference to ResizeArrayRaw 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 // If array is expanded - new elements are default constructed 00086 // if array is reduced, removed elements have ~Elem() run 00087 void resize(int i) { rep->resize(i); } 00088 00089 // Set all elements to a given value (like 0). 00090 void setall(const Elem &elem) { 00091 iterator i = begin(); 00092 iterator e = end(); 00093 for ( ; i != e; ++i ) *i = elem; 00094 } 00095 00096 // Add element to end of array 00097 int add (const Elem &elem) { 00098 int end=rep->size(); 00099 rep->ins(elem, end); 00100 return(end); 00101 } 00102 00103 // delete num elements from current index 00104 int del(int index, int num=1) { 00105 return(rep->del(index,num)); 00106 } 00107 00108 // insert element at index 00109 int insert (const Elem& elem, int index) { 00110 rep->ins(elem,index); 00111 return (index); 00112 } 00113 00114 // array member access (can be lvalue) that grows array. 00115 inline Elem & item(int i) { 00116 i = ( i < 0 ? 0 : i ); 00117 if ((i+1) > size()) 00118 resize(i+1); 00119 return rep->array[i]; 00120 } 00121 00122 // array member access (can be lvalue) no checks. 00123 inline Elem & operator[](int index) { return rep->array[index]; } 00124 inline const Elem & operator[](int index) const { return rep->array[index]; } 00125 00126 // returns size of ResizeArray 00127 inline int size(void) const { return rep->size(); } 00128 00129 // reduce storage size 00130 void reduce(void) { rep->reduce(); } 00131 00132 inline int find(const Elem &e) { return rep->find(e); } 00133 }; 00134 00135 #endif
1.3.9.1