vdk 2.4.0
vdkarray.h
1 /*
2  * ===========================
3  * VDK Visual Development Kit
4  * Version 0.4
5  * October 1998
6  * ===========================
7  *
8  * Copyright (C) 1998, Mario Motta
9  * Developed by Mario Motta <mmotta@guest.net>
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Library General Public License for more details.
20  *
21  * You should have received a copy of the GNU Library General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
24  * 02111-130
25 */
26 #define NO_DEBUG
27 #ifndef VDKARRAY_H
28 #define VDKARRAY_H
29 //#include <iostream>
30 
31 
32 //================
33 // M_ARRAY CLASS
34 //================
72 template <class T> class VDKArray
73 {
74 
75  private:
76  // friend ostream& operator<<(ostream& os, VDKArray<T>& a);
77  // At() operator view arrays in scientific index notation
78  // A[1...n] instead of A[0..n-1].
79  // (used for heapsort alghoritm)
83  T& At(int ndx)
84  {
85  return start[ndx-1];
86  }
87  protected:
88  int xDim; // number of elements
89  T* start; // pointer to data array
90  public:
95  VDKArray(int n = 0):xDim(n),start(n ? new T[n]: (T*) NULL)
96  {
97  }
101  VDKArray(const VDKArray&);
105  VDKArray& operator=(const VDKArray&);
109  virtual ~VDKArray()
110  {
111  if(start)
112  delete[] start;
113  }
117  int size()
118  {
119  return xDim ;
120  }
124  void resize(int); // change vector size
125 
126  // index operations
138  T& operator[](int ndx)
139  {
140  return start[ndx];
141  }
142  const T& operator[](int ndx) const
143  {
144  return start[ndx];
145  }
146  // sorting routine
158  virtual int operator==(VDKArray<T>& m); // equality operator
159 };
160 
161 
162 //copy inizializer
163 template <class T>
165 {
166  xDim = v.xDim;
167  start = new T[xDim];
168  for(register int i = 0;i < xDim; i++) start[i] = v.start[i];
169 }
170 // resize dinamically
171 template <class T>
172 void VDKArray<T>::resize(int ns)
173 {
174  T* temp = new T[ns];
175  T* pstart = start,*ptemp = temp;
176  // compute the smaller size
177  int s = (ns > xDim) ? xDim : ns;
178  // copy elements into new array
179  for(register int i = 0;i < s; i++) *ptemp++ = *pstart++;
180  // delete old array and update pointer to data
181  delete[] start;
182  start = temp;
183  xDim = ns;
184 }
185 // assignement
186 template <class T>
188 {
189  // avoid v = v;
190  if(this !=&v)
191  {
192  if(start) delete[] start;
193  xDim = v.xDim;
194  start=new T[xDim];
195  for(register int i = 0;i < xDim; i++)
196  start[i] = v.start[i];
197  }
198  return *this;
199 }
200 // equality operator
201 template <class T> int
203 {
204  if(xDim != m.xDim) return 0;
205  register int i;
206  for(i = 0;
207  (i < xDim) &&
208  ((*this)[i] == m[i]); i++) ;
209  return i == xDim ? 1 : 0 ;
210 }
211 // Heap sort routine, ref: W.H.Press et al.
212 // "Numerical recipes in C" 2nd edition
213 // Cambridge University Press, 1992
214 template <class T>
216 {
217  unsigned int n = size();
218  unsigned int i,ir,j,l;
219  T rra;
220  if(n<2) return *this;
221  l = (n >> 1)+1;
222  ir=n;
223  for(;;)
224  {
225  if (l > 1) rra = At(--l);
226  else
227  {
228  rra = At(ir); At(ir) = At(1);
229  if(--ir == 1)
230  {
231  At(1) = rra; break;
232  }
233  }
234  i = l; j = l+l;
235  while(j <= ir)
236  {
237  if(j < ir && At(j) < At(j+1) ) j++;
238  if(rra < At(j))
239  {
240  At(i) = At(j); i = j; j <<= 1;
241  }
242  else j = ir+1;
243  }
244  At(i) = rra;
245  }
246  return *this;
247 }
248 // stream output
249 /*
250  template <class T>
251  ostream& operator<<(ostream& os, VDKArray<T>& a)
252  {
253  for(register int i = 0; i < a.xDim; i++)
254  os << a[i] << ' ';
255  return os;
256  }
257 */
258 #endif
259 
260 
261 
262 
263 
VDKArray::operator==
virtual int operator==(VDKArray< T > &m)
Definition: vdkarray.h:202
VDKArray::operator=
VDKArray & operator=(const VDKArray &)
Definition: vdkarray.h:187
VDKArray::size
int size()
Definition: vdkarray.h:117
VDKArray::~VDKArray
virtual ~VDKArray()
Definition: vdkarray.h:109
VDKArray
provides a templatized array
Definition: vdkarray.h:72
VDKArray::VDKArray
VDKArray(int n=0)
Definition: vdkarray.h:95
VDKArray::resize
void resize(int)
Definition: vdkarray.h:172
VDKArray::At
T & At(int ndx)
Definition: vdkarray.h:83
VDKArray::Heapsort
VDKArray< T > & Heapsort()
Definition: vdkarray.h:215
VDKArray::operator[]
T & operator[](int ndx)
Definition: vdkarray.h:138