00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef _ARRAYH_
00014 #define _ARRAYH_
00015
00016
00017
00018
00019
00020
00021 #ifndef _WIN32
00022 #include <unistd.h>
00023 #endif
00024 #include <cassert>
00025
00026
00027 template <class Type>
00028 class Array {
00029
00030 public:
00031
00032
00033 Array(unsigned int size=0) : realsize(size), maxsize(size)
00034 { if (size) array = new Type[size]; else array = (Type *) NULL; }
00035
00036
00037 Array(const Array<Type> &a)
00038 : realsize(a.realsize), maxsize(a.maxsize), array((Type *) NULL)
00039 { array = new Type[a.realsize]; *this = a; }
00040
00041
00042 ~Array() { if (array) delete [] array; }
00043
00044
00045
00046 Array<Type> &operator=(const Array<Type> &a)
00047 {
00048 if (maxsize < a.maxsize) Grow(a.maxsize,0);
00049 assert(a.array != NULL);
00050 for (unsigned int i=0; i<a.maxsize; i++) array[i] = a.array[i];
00051 Grow(a.maxsize);
00052 return *this;
00053 }
00054
00055 Type &operator[](unsigned int idx) const
00056 {
00057 #ifdef ARRAY_BOUNDARY_CHECK
00058 assert((idx >= 0) && (idx < maxsize));
00059 #endif
00060 return array[idx];
00061 }
00062
00063
00064 void Grow(unsigned int newsize, int copy=1)
00065
00066 {
00067 if (realsize >= (unsigned int) newsize) { maxsize = newsize; return; }
00068
00069 Type * newarray;
00070 if (newsize == 0) {
00071 newarray = (Type *) NULL;
00072 }
00073 else {
00074 newarray = new Type[newsize];
00075 assert(newarray != NULL);
00076 }
00077
00078 if (realsize)
00079 {
00080 if (copy) for (unsigned int i=0; i<maxsize; i++) newarray[i] = array[i];
00081 delete [] array;
00082 }
00083
00084 array = newarray;
00085 maxsize = newsize;
00086 realsize = newsize;
00087 }
00088 Array<Type> Union (const Array<Type> & B) const
00089 {
00090 int length=0;
00091 int l1=Size();
00092 int l2=B.Size();
00093 Array<Type> C(l1+l2);
00094 C.Grow(0);
00095
00096 int i = 0;
00097 for (i=0; i<l1; i++)
00098 {
00099 const Type & var=array[i];
00100 if (C.Member(var)==0)
00101 {
00102 length++;
00103 C.Grow(length);
00104 C[length-1]=var;
00105 }
00106 }
00107 for (i=0; i<l2; i++)
00108 {
00109 const Type & var=B[i];
00110 if (C.Member(var)==0)
00111 {
00112 length++;
00113 C.Grow(length);
00114 C[length-1]=var;
00115 }
00116 }
00117 return C;
00118 }
00119
00120 Array<Type> Intersect (const Array<Type> & B) const
00121 {
00122 int length = 0;
00123 int l1 = Size();
00124 int l2 = B.Size();
00125 if (l1 < l2)
00126 {
00127 Array<Type> C(l1);
00128 C.Grow(0);
00129 for (int i=0; i<l1; i++)
00130 {
00131 const Type & var = array[i];
00132 if ((C.Member(var)==0) && (B.Member(var)==1))
00133 {
00134 length++;
00135 C.Grow(length);
00136 C[length-1] = var;
00137 }
00138 }
00139 return C;
00140 }
00141 else
00142 {
00143 Array<Type> C(l2);
00144 C.Grow(0);
00145 for (int i=0; i<l2; i++)
00146 {
00147 const Type & var = B[i];
00148 if ((C.Member(var)==0) && (Member(var)==1))
00149 {
00150 length++;
00151 C.Grow(length);
00152 C[length-1] = var;
00153 }
00154 }
00155 return C;
00156 }
00157 }
00158
00159 int Member (const Type & a) const
00160 {
00161 int l=Size();
00162 for (int i=0; i<l; i++)
00163 if (array[i] == a)
00164 return 1;
00165 return 0;
00166 }
00167
00168 const Type *Get() const { return array; }
00169 Type *Get() { return array; }
00170
00171
00172 unsigned int Size() const { return maxsize; }
00173
00174 public:
00175 int operator==(const Array<Type> &) const
00176 {
00177 assert(0);
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190 return 1;
00191 }
00192
00193
00194 protected:
00195 unsigned int RealSize() const { return realsize; }
00196
00197 private:
00198 unsigned int realsize;
00199 unsigned int maxsize;
00200 Type *array;
00201
00202 };
00203
00204
00205 #endif