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 00013 Copy construction and assignment are forbidden to avoid 00014 confusion with old reference-counted implementation. 00015 Use copy() and swap() to explicitly transfer storage. 00016 */ 00017 00018 #ifndef RESIZEARRAY_H 00019 #define RESIZEARRAY_H 00020 00021 #include "ResizeArrayRaw.h" 00022 00023 // Need this juju to use templated friend below 00024 template <class Type> class ResizeArrayIter; 00025 template <class Type> class ResizeArrayRaw; 00026 00027 template <class Elem> class ResizeArray { 00028 friend class ResizeArrayIter<Elem>; 00029 00030 protected: 00031 ResizeArrayRaw<Elem> rep; 00032 00033 public: 00034 // STL style iterators 00035 typedef Elem* iterator; 00036 iterator begin(void) { return rep.array; } 00037 iterator end(void) { return rep.array + rep.arraySize; } 00038 typedef const Elem* const_iterator; 00039 const_iterator const_begin(void) const { return rep.array; } 00040 const_iterator const_end(void) const { return rep.array + rep.arraySize; } 00041 00042 // Various Constructors 00043 ResizeArray(void) { } 00044 00045 // Constructor make ResizeArray of predefined size 00046 ResizeArray(int s) { 00047 rep.resize(s); 00048 } 00049 00050 private: 00051 inline ResizeArray(ResizeArray<Elem> &ra); 00052 00053 inline ResizeArray(const ResizeArray<Elem>* ra); 00054 00055 inline ResizeArray<Elem> & operator= (ResizeArray<Elem> &ra); 00056 00057 public: 00058 // Make copy of ResizeArrayRaw (for use in messages) 00059 void copy(ResizeArray<Elem> &ra) { 00060 rep.copy(ra.rep); 00061 } 00062 00063 // Swap ResizeArrayRaw (for use in messages to avoid copy) 00064 void swap(ResizeArray<Elem> &ra) { 00065 // uses synthesized copy constructor and assignment operator 00066 ResizeArrayRaw<Elem> tmp = rep; 00067 rep = ra.rep; 00068 ra.rep = tmp; 00069 } 00070 00071 // does some other ResizeArray have a handle to our data 00072 bool shared() const { 00073 return 0; 00074 } 00075 00076 // Constructor to take-in pre-existing array 00077 ResizeArray(Elem * * array, int arraySize, int allocSize=0) : 00078 rep(array, arraySize, allocSize) { } 00079 00080 ~ResizeArray(void) { rep.free(); } 00081 00082 // If array is expanded - new elements are default constructed 00083 // if array is reduced, removed elements have ~Elem() run 00084 void resize(int i) { rep.resize(i); } 00085 00086 // destruct elements, free storage, set size to 0 00087 void clear() { rep.clear(); } 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) const { return rep.find(e); } 00133 00134 }; 00135 00136 #endif
1.3.9.1