Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | Related Pages

vdkarray.h

00001 /* 00002 * =========================== 00003 * VDK Visual Development Kit 00004 * Version 0.4 00005 * October 1998 00006 * =========================== 00007 * 00008 * Copyright (C) 1998, Mario Motta 00009 * Developed by Mario Motta <mmotta@guest.net> 00010 * 00011 * This library is free software; you can redistribute it and/or 00012 * modify it under the terms of the GNU Library General Public 00013 * License as published by the Free Software Foundation; either 00014 * version 2 of the License, or (at your option) any later version. 00015 * 00016 * This library is distributed in the hope that it will be useful, 00017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00019 * Library General Public License for more details. 00020 * 00021 * You should have received a copy of the GNU Library General Public 00022 * License along with this library; if not, write to the Free Software 00023 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 00024 * 02111-130 00025 */ 00026 #define NO_DEBUG 00027 #ifndef VDKARRAY_H 00028 #define VDKARRAY_H 00029 //#include <iostream> 00030 00031 00032 //================ 00033 // M_ARRAY CLASS 00034 //================ 00072 template <class T> class VDKArray 00073 { 00074 00075 private: 00076 // friend ostream& operator<<(ostream& os, VDKArray<T>& a); 00077 // At() operator view arrays in scientific index notation 00078 // A[1...n] instead of A[0..n-1]. 00079 // (used for heapsort alghoritm) 00083 T& At(int ndx) 00084 { 00085 return start[ndx-1]; 00086 } 00087 protected: 00088 int xDim; // number of elements 00089 T* start; // pointer to data array 00090 public: 00095 VDKArray(int n = 0):xDim(n),start(n ? new T[n]: (T*) NULL) 00096 { 00097 } 00101 VDKArray(const VDKArray&); 00105 VDKArray& operator=(const VDKArray&); 00109 virtual ~VDKArray() 00110 { 00111 if(start) 00112 delete[] start; 00113 } 00117 int size() 00118 { 00119 return xDim ; 00120 } 00124 void resize(int); // change vector size 00125 00126 // index operations 00138 T& operator[](int ndx) 00139 { 00140 return start[ndx]; 00141 } 00142 const T& operator[](int ndx) const 00143 { 00144 return start[ndx]; 00145 } 00146 // sorting routine 00154 VDKArray<T>& Heapsort(); 00158 virtual int operator==(VDKArray<T>& m); // equality operator 00159 }; 00160 00161 00162 //copy inizializer 00163 template <class T> 00164 VDKArray<T>::VDKArray(const VDKArray<T> &v) 00165 { 00166 xDim = v.xDim; 00167 start = new T[xDim]; 00168 for(register int i = 0;i < xDim; i++) start[i] = v.start[i]; 00169 } 00170 // resize dinamically 00171 template <class T> 00172 void VDKArray<T>::resize(int ns) 00173 { 00174 T* temp = new T[ns]; 00175 T* pstart = start,*ptemp = temp; 00176 // compute the smaller size 00177 int s = (ns > xDim) ? xDim : ns; 00178 // copy elements into new array 00179 for(register int i = 0;i < s; i++) *ptemp++ = *pstart++; 00180 // delete old array and update pointer to data 00181 delete[] start; 00182 start = temp; 00183 xDim = ns; 00184 } 00185 // assignement 00186 template <class T> 00187 VDKArray<T>& VDKArray<T>::operator=(const VDKArray<T>&v) 00188 { 00189 // avoid v = v; 00190 if(this !=&v) 00191 { 00192 if(start) delete[] start; 00193 xDim = v.xDim; 00194 start=new T[xDim]; 00195 for(register int i = 0;i < xDim; i++) 00196 start[i] = v.start[i]; 00197 } 00198 return *this; 00199 } 00200 // equality operator 00201 template <class T> int 00202 VDKArray<T>::operator==(VDKArray<T>& m) 00203 { 00204 if(xDim != m.xDim) return 0; 00205 register int i; 00206 for(i = 0; 00207 (i < xDim) && 00208 ((*this)[i] == m[i]); i++) ; 00209 return i == xDim ? 1 : 0 ; 00210 } 00211 // Heap sort routine, ref: W.H.Press et al. 00212 // "Numerical recipes in C" 2nd edition 00213 // Cambridge University Press, 1992 00214 template <class T> 00215 VDKArray<T>& VDKArray<T>::Heapsort() 00216 { 00217 unsigned int n = size(); 00218 unsigned int i,ir,j,l; 00219 T rra; 00220 if(n<2) return *this; 00221 l = (n >> 1)+1; 00222 ir=n; 00223 for(;;) 00224 { 00225 if (l > 1) rra = At(--l); 00226 else 00227 { 00228 rra = At(ir); At(ir) = At(1); 00229 if(--ir == 1) 00230 { 00231 At(1) = rra; break; 00232 } 00233 } 00234 i = l; j = l+l; 00235 while(j <= ir) 00236 { 00237 if(j < ir && At(j) < At(j+1) ) j++; 00238 if(rra < At(j)) 00239 { 00240 At(i) = At(j); i = j; j <<= 1; 00241 } 00242 else j = ir+1; 00243 } 00244 At(i) = rra; 00245 } 00246 return *this; 00247 } 00248 // stream output 00249 /* 00250 template <class T> 00251 ostream& operator<<(ostream& os, VDKArray<T>& a) 00252 { 00253 for(register int i = 0; i < a.xDim; i++) 00254 os << a[i] << ' '; 00255 return os; 00256 } 00257 */ 00258 #endif 00259 00260 00261 00262 00263

Generated on Tue Aug 17 12:39:50 2004 for vdk 2.4.0 by doxygen 1.3.7