00001
00021 #ifndef dArray_h
00022 #define dArray_h
00023
00024
00025 #include <assert.h>
00026
00030 template <class T> class cArray
00031 {
00032 T **m_array;
00033 int m_num;
00034 int m_size;
00035 int m_resize;
00036
00037 public:
00041 cArray( int r = 1 )
00042 : m_array( 0 ), m_num( 0 ), m_size( 0 ), m_resize( r )
00043 {
00044 assert( r > 0 );
00045 }
00046
00050 cArray( const cArray &array )
00051 : m_array( 0 ), m_num( 0 ), m_size( 0 ), m_resize( array.m_resize )
00052 {
00053 }
00054
00058 ~cArray()
00059 {
00060 Clear();
00061 }
00062
00066 int Add( T *t )
00067 {
00068 if ( m_num == m_size )
00069 {
00070 T **newa = new T *[m_size+m_resize];
00071
00072 if ( m_num )
00073 memcpy( newa, m_array, sizeof( T * ) * m_num );
00074
00075 delete[] m_array;
00076 m_array = newa;
00077 m_size += m_resize;
00078 }
00079
00080 m_array[m_num++] = t;
00081
00082 return m_num-1;
00083 }
00084
00088 T *Rem( int idx )
00089 {
00090 assert( idx >= 0 && idx < m_num );
00091
00092 T *rv = m_array[idx];
00093 m_num--;
00094
00095 if ( m_num == 0 )
00096 return rv;
00097
00098 int n = m_num/m_resize*m_resize+m_resize-1;
00099
00100 if ( m_size > n )
00101 {
00102 m_size = n;
00103
00104 T **newa = new T *[n];
00105
00106 if ( idx != 0 )
00107 memcpy( newa, m_array, sizeof( T * ) * idx );
00108
00109 if ( idx != m_num )
00110 memcpy( newa+idx, m_array+idx+1, (m_num - idx)*sizeof( T * ) );
00111
00112 delete[] m_array;
00113 m_array = newa;
00114
00115 return rv;
00116 }
00117
00118 if ( idx != m_num )
00119 memmove( m_array+idx, m_array+idx+1, (m_num - idx)*sizeof( T * ) );
00120
00121 return rv;
00122 }
00123
00127 void RemAll()
00128 {
00129 if ( m_array )
00130 {
00131 delete[] m_array;
00132 m_num = 0;
00133 m_array = 0;
00134 m_size = 0;
00135 }
00136 }
00137
00141 T *operator[]( int idx ) const
00142 {
00143 assert( idx >= 0 && idx < m_num );
00144
00145 return m_array[idx];
00146 }
00147
00151 T * & operator[]( int idx )
00152 {
00153 assert( idx >= 0 && idx < m_num );
00154
00155 return m_array[idx];
00156 }
00157
00161 cArray<T> &operator+=( T *t )
00162 {
00163 Add( t );
00164 return *this;
00165 }
00166
00170 cArray<T> &operator-=( T *t )
00171 {
00172 int idx = Find( t );
00173 assert( idx != -1 );
00174
00175 if ( idx != -1 )
00176 Rem( idx );
00177
00178 return *this;
00179 }
00180
00184 int Num() const { return m_num; }
00185
00189 int Find( T *t ) const
00190 {
00191 for( int i = 0; i < m_num; i++ )
00192 if ( m_array[i] == t )
00193 return i;
00194
00195 return -1;
00196 }
00197
00201 void Sort( int (*cmp)( T **t1, T **t2 ) )
00202 {
00203 qsort( m_array, m_num, sizeof( T * ), (int (*)(const void *, const void *) )cmp );
00204 }
00205
00209 int Search( T *key, int (*cmp)( T **t1, T **t2 ), int mmax = -1 ) const
00210 {
00211 int n = m_num;
00212
00213 if ( mmax >= 0 && mmax < m_num )
00214 n = mmax;
00215
00216 T **e = (T **)bsearch( &key, m_array, n, sizeof( T * ), (int (*)(const void *, const void *) )cmp );
00217
00218 if ( e == 0 )
00219 return -1;
00220
00221 int idx = (e - m_array);
00222
00223 assert( idx >= 0 && idx < n );
00224
00225 return idx;
00226 }
00227
00231 void Clear()
00232 {
00233 if ( m_array )
00234 {
00235 for( int i = 0; i < m_num; i++ ) delete m_array[i];
00236
00237 delete[] m_array;
00238 m_num = 0;
00239 m_array = 0;
00240 m_size = 0;
00241 }
00242 }
00243
00247 int Insert( int befor, T *t )
00248 {
00249 assert( befor <= m_num );
00250
00251 if ( befor == -1 || befor == m_num )
00252 return Add( t );
00253
00254 if ( m_num == m_size )
00255 {
00256 T **newa = new T *[m_size+m_resize];
00257
00258 if ( m_num )
00259 memcpy( newa, m_array, sizeof( T * ) * m_num );
00260
00261 delete[] m_array;
00262 m_array = newa;
00263 m_size += m_resize;
00264 }
00265
00266 for( int i = m_num-1; i >= befor; i-- )
00267 m_array[i+1] = m_array[i];
00268
00269 m_num++;
00270 m_array[befor] = t;
00271
00272 return befor;
00273 }
00274
00278 cArray &operator=( const cArray & )
00279 {
00280
00281 Clear();
00282
00283 return *this;
00284 }
00285 };
00286
00287
00288 #endif