00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #ifndef RESIZEARRAY_TEMPLATE_H
00022 #define RESIZEARRAY_TEMPLATE_H
00023
00024 #include <string.h>
00025
00035 template<class T>
00036 class ResizeArray {
00037 private:
00038 T *allocate(size_t n) { return new T[n]; }
00039 void deallocate(T *p) { delete [] p; }
00040
00041 T *data;
00042 int sz;
00043 int currSize;
00044
00045 public:
00050 ResizeArray(int s = 3) {
00051 currSize = 0;
00052 sz = (s > 0 ? s : 10);
00053 data = allocate(sz);
00054 }
00055
00056 ~ResizeArray() {
00057 deallocate(data);
00058 }
00059
00060 int num(void) const { return currSize; }
00061 T& operator[](int N) { return data[N]; }
00062 T const& operator[](int N) const { return data[N]; }
00063
00065 void append(const T& val) {
00066 if (currSize == sz) {
00067 int newsize = (int)((float)sz * 1.3f);
00068
00069
00070
00071
00072 if (newsize == sz)
00073 newsize++;
00074
00075
00076
00077 T *newdata = allocate(newsize);
00078 memcpy(newdata, data, currSize * sizeof(T));
00079 deallocate(data);
00080
00081
00082 data = newdata;
00083 sz = newsize;
00084 }
00085 data[currSize++] = val;
00086 }
00087
00089 void remove(int n) {
00090 if (n < 0 || n >= currSize) return;
00091 for (int i=n; i<currSize-1; i++)
00092 data[i] = data[i+1];
00093 currSize--;
00094 }
00095
00097 void clear() {
00098 currSize = 0;
00099 }
00100
00102 void truncatelastn(int N) {
00103 currSize -= N;
00104 if (currSize < 0)
00105 currSize=0;
00106 }
00107
00110 int find(const T& val) {
00111 int i;
00112
00113 for(i=0; i < currSize; i++) {
00114 if(data[i] == val)
00115 return i;
00116 }
00117
00118 return -1;
00119 }
00120 };
00121
00122 #endif
00123