Debian Codegraph @

src/ITGSend/newran/array.h

raw symbols SCIP
1 /// \ingroup newran
2 ///@{
3
4 /// \file array.h
5 /// Simple template array class.
6 /// Based a little on an article by Dag Bruck
7
8 #ifndef ARRAY_H
9 #define ARRAY_H
10
11 #include "myexcept.h"
12
13 template <class T>
14 class GeneralArray;
15
16 template <class T>
17 class Array;
18
19 template <class T>
20 class T_Array;
21
22 template <class T>
23 class Array2;
24
25 template <class T>
26 class T_Array2;
27
28
29
30 // This is a very primitive array class, concerned only
31 // with the allocation of space.
32 // Not available to the user; user classes are derived from this.
33 // It uses templates - so will not run in early versions of C++
34
35 /// The base of my other array classes.
36 /// \internal
37 template <class T>
38 class GeneralArray
39 {
40 protected:
41 T* data;
42 int sz;
43
44 GeneralArray(int size); // constructors
45 GeneralArray();
46 GeneralArray(const GeneralArray<T> &); // copy constructor
47
48 T& El(int); // to access elements
49 const T& El(int) const;
50
51 GeneralArray<T>& operator= (const T &);
52 GeneralArray<T>& operator= (const GeneralArray<T>&);
53 GeneralArray<T> operator+(const GeneralArray<T>&) const;
54
55 void ReSize(int size);
56
57 public:
58 int length() const { return sz; }
59 T* base() { return data; }
60 const T* base() const { return data; }
61 void SortAscending();
62 T Sum() const;
63 T Sum(const Array<bool>& Condition) const;
64 ~GeneralArray(); // destructor
65 };
66
67 template <class T>
68 class GeneralArrayIterator
69 {
70 public:
71 GeneralArrayIterator(GeneralArray<T>& a) : p(a.base()), n(a.length()) {}
72 T* operator++() { return n <= 1 ? p=0 : (--n, ++p); } // prefix
73 T* operator++(int) { return n == 0 ? p=0 : (--n, p++); } // postfix
74 operator T* () { return p; }
75
76 private:
77 T* p;
78 int n;
79 };
80
81 /// Construct array with "size" elements.
82 template <class T>
83 GeneralArray<T>::GeneralArray(int size) : sz(size)
84 {
85 if (sz > 0) { data = new T[sz]; if (data == 0) Throw(Bad_alloc("Array: out of space")); }
86 else data = 0;
87 }
88
89 /// Construct array with no elements.
90 template <class T>
91 GeneralArray<T>::GeneralArray() : sz(0), data(0) {}
92
93 /// Copy constructor
94 template <class T>
95 GeneralArray<T>::GeneralArray(const GeneralArray<T> & X) : sz(X.sz)
96 {
97 data = new T[sz]; if (data == 0) Throw(Bad_alloc("Array: out of space"));
98 int i = sz >> 3; T* d = data; T* x = X.data;
99 // should be using copy constructor
100 while (i--)
101 {
102 *d++ = *x++; *d++ = *x++; *d++ = *x++; *d++ = *x++;
103 *d++ = *x++; *d++ = *x++; *d++ = *x++; *d++ = *x++;
104 }
105 i=sz & 7; while (i--) *d++ = *x++;
106 }
107
108 template <class T>
109 GeneralArray<T>::~GeneralArray()
110 { delete [] data; } // should call individual destructors
111
112 /// Element of an array.
113 template <class T>
114 inline T& GeneralArray<T>::El(int i)
115 { if (!(i >= 0 && i < sz)) Throw(Logic_error("Array: subscript error")); return data[i]; }
116
117
118 /// Element of an array.
119 template <class T>
120 inline const T& GeneralArray<T>::El(int i) const
121 { if (!(i >= 0 && i < sz)) Throw(Logic_error("Array: subscript error")); return data[i]; }
122
123 template <class T>
124 GeneralArray<T>& GeneralArray<T>::operator = (const T & x)
125 {
126 int i = sz >> 3; T* d = data;
127 while (i--)
128 {
129 *d++ = x; *d++ = x; *d++ = x; *d++ = x;
130 *d++ = x; *d++ = x; *d++ = x; *d++ = x;
131 }
132 i=sz & 7; while (i--) *d++ = x;
133 return *this;
134 }
135
136 /// Set array equal to another array.
137 template <class T>
138 GeneralArray<T>& GeneralArray<T>::operator = (const GeneralArray<T>& X)
139 {
140 delete [] data;
141 sz=X.sz; data = new T [sz]; if (data == 0) Throw(Bad_alloc("Array: out of space"));
142 int i = sz >> 3; T* d = data; T* x = X.data;
143 while (i--)
144 {
145 *d++ = *x++; *d++ = *x++; *d++ = *x++; *d++ = *x++;
146 *d++ = *x++; *d++ = *x++; *d++ = *x++; *d++ = *x++;
147 }
148 i=sz & 7; while (i--) *d++ = *x++;
149 return *this;
150 }
151
152 /// Add two arrays.
153 template<class T>
154 GeneralArray<T> GeneralArray<T>::operator+(const GeneralArray<T>& A) const
155 {
156 int l = length(); if (l != A.length()) Throw(Logic_error("Array: incompatible subscripts"));
157 const T* s1=base(); const T* s2=A.base();
158 GeneralArray<T> Sum(l);
159 T* s=Sum.base(); int i = l >> 2;
160 while (i--)
161 {
162 *s++ = *s1++ + *s2++; *s++ = *s1++ + *s2++;
163 *s++ = *s1++ + *s2++; *s++ = *s1++ + *s2++;
164 }
165 i=l & 3; while (i--) *s++ = *s1++ + *s2++;
166 return Sum;
167 }
168
169 /// Resize and array.
170 template <class T>
171 void GeneralArray<T>::ReSize(int size)
172 {
173 delete [] data;
174 sz=size;
175 if (sz) { data = new T [sz]; if (data == 0) Throw(Bad_alloc("Array: out of space")); }
176 else data=0;
177 }
178
179
180 /// One dimensional array.
181 template <class T>
182 class Array : public GeneralArray<T>
183 {
184 protected:
185 int low;
186
187 public:
188 Array(int lb, int ub) : GeneralArray<T>(ub-lb+1), low(lb) {}
189 Array() : GeneralArray<T>(), low(0) {}
190 Array(const Array<T> &X) : GeneralArray<T>(X), low(X.low) {}
191 Array(T_Array<T> &X);
192 ~Array() {}
193
194 T& operator () (int i) { return this->El(i-low); }
195 const T& operator () (int i) const { return this->El(i-low); }
196
197 Array<T>& operator = (const T & X)
198 { GeneralArray<T>::operator=(X); return *this; }
199 Array<T>& operator = (const Array<T>& X)
200 { GeneralArray<T>::operator=(X); low = X.low; return *this; }
201 Array<T>& operator = (T_Array<T>& X);
202
203 void ReSize(int lb, int ub) ///< Resize an array
204 { low=lb; GeneralArray<T>::ReSize(ub-lb+1); }
205
206 int first() const { return low; } ///< LB of index
207 int last() const { return low+this->sz-1; } ///< UB of index
208 T_Array<T> operator+(const Array<T>&) const;
209 T_Array<T> operator+(T_Array<T>&) const;
210 };
211
212 /// Temporary 1 dimensional array.
213 /// \internal
214 /// This is used for the result of a numeric operation. Its memory can be
215 /// reused in a subsequent operation reducing the need to copy data.
216 template <class T>
217 class T_Array : public Array<T>
218 {
219 public:
220 T_Array() {}
221 T_Array(int lb, int ub) : Array<T>(lb, ub) {}
222 T_Array(int lb, int l, T* d) { this->low = lb; this->sz = l; this->data = d; }
223 T_Array(T_Array<T>& ta)
224 { this->low = ta.low; this->sz = ta.sz; ta.sz = 0; this->data = ta.data; ta.data = 0; }
225 T_Array<T> operator+(const Array<T>&);
226 ~T_Array() {}
227 friend class Array<T>;
228 };
229
230
231 /// 2 dimensional array.
232 template <class T>
233 class Array2 : public GeneralArray<T>
234 {
235 private:
236 int low1,low2;
237 int sz1,sz2;
238
239 public:
240 Array2(int lb1, int ub1, int lb2, int ub2);
241 Array2() : GeneralArray<T>(), low1(0), low2(0), sz1(0), sz2(0) {}
242 Array2(const Array2<T> &);
243 ~Array2() {}
244
245 T& operator () (int,int);
246 const T& operator () (int,int) const;
247
248 Array2<T>& operator = (const T & X)
249 { GeneralArray<T>::operator=(X); return *this; }
250 Array2<T>& operator = (const Array2<T>&);
251
252 Array2<T> operator+(const Array2<T>&);
253
254 void ReSize(int lb1, int ub1, int lb2, int ub2);
255
256 int first1() const { return low1; } ///< LB first dimension
257 int last1() const { return low1+sz1-1; } ///< UB first dimension
258 int first2() const { return low2; } ///< LB second dimension
259 int last2() const { return low2+sz2-1; } ///< UB second dimension
260
261 };
262
263 /// Construct 2D array with given bounds.
264 template <class T>
265 Array2<T>::Array2(int lb1, int ub1, int lb2, int ub2)
266 : GeneralArray<T>((ub1-lb1+1)*(ub2-lb2+1)),
267 low1(lb1), low2(lb2), sz1(ub1-lb1+1), sz2(ub2-lb2+1)
268 { if (sz1<=0) Throw(Logic_error("Array: subscript error")); }
269
270 /// Construct 2D array equal to 2D array.
271 template <class T>
272 Array2<T>::Array2(const Array2<T> & X)
273 : GeneralArray<T>(X),
274 low1(X.low1), sz1(X.sz1), low2(X.low2), sz2(X.sz2) {}
275
276 /// Element of 2D array.
277 template <class T>
278 inline T& Array2<T>::operator() (int i, int j)
279 {
280 i -= low1; j -= low2;
281 if (!(j>=0 && j < sz2)) Throw(Logic_error("Array: subscript error"));
282 return this->El(i*sz2+j);
283 }
284
285 /// Element of 2D array.
286 template <class T>
287 inline const T& Array2<T>::operator() (int i, int j) const
288 {
289 i -= low1; j -= low2;
290 if (!(j>=0 && j < sz2)) Throw(Logic_error("Array: subscript error"));
291 return this->El(i*sz2+j);
292 }
293
294 /// Set 2D array equal to another 2D array.
295 template <class T>
296 Array2<T>& Array2<T>::operator= (const Array2<T>& X)
297 {
298 low1 = X.low1; sz1 = X.sz1; low2 = X.low2; sz2 = X.sz2;
299 GeneralArray<T>::operator=(X);
300 return *this;
301 }
302
303 /// Resize two dimensional array.
304 template <class T>
305 void Array2<T>::ReSize(int lb1, int ub1, int lb2, int ub2)
306 {
307 low1=lb1; low2=lb2;
308 if (ub1-lb1+1 < 0) Throw(Logic_error("Array: resize error"));
309 sz1=ub1-lb1+1; sz2=ub2-lb2+1;
310 GeneralArray<T>::ReSize(sz1*sz2);
311 }
312
313 /// Array + array.
314 template<class T>
315 T_Array<T> Array<T>::operator+(const Array<T>& A) const
316 {
317 int lb = first(); if (A.first() != lb) Throw(Logic_error("Array: incompatible subscripts"));
318 int l = this->length(); if (l != A.length()) Throw(Logic_error("Array: incompatible subscripts"));
319 const T* s1=this->base(); const T* s2=A.base();
320 T_Array<T> Sum(lb,l+lb-1);
321 T* s=Sum.base(); int i = l >> 2;
322 while (i--)
323 {
324 *s++ = *s1++ + *s2++; *s++ = *s1++ + *s2++;
325 *s++ = *s1++ + *s2++; *s++ = *s1++ + *s2++;
326 }
327 i=l & 3; while (i--) *s++ = *s1++ + *s2++;
328 return Sum;
329 }
330
331 /// Set array equal to temporary array.
332 template<class T>
333 Array<T>& Array<T>::operator= (T_Array<T>& X)
334 {
335 delete this->data;
336 low = X.low; this->sz = X.sz; X.sz = 0; this->data = X.data; X.data = 0;
337 return *this;
338 }
339
340 /// Construct array equal to temporary array.
341 template<class T>
342 inline Array<T>::Array(T_Array<T>& X) : low(X.low)
343 { this->sz = X.sz; X.sz = 0; this->data = X.data; X.data = 0; }
344
345 /// Temporary array + temporary array.
346 template<class T>
347 T_Array<T> Array<T>::operator+(T_Array<T>& A) const
348 {
349 int lb = this->first(); if (A.first()!=lb) Throw(Logic_error("Array: incompatible subscripts"));
350 int l = this->length(); if (l != A.length()) Throw(Logic_error("Array: incompatible subscripts"));
351 const T* s1=this->base(); T* s=A.base();
352 int i = l >> 2;
353 while (i--) { *s++ += *s1++; *s++ += *s1++; *s++ += *s1++; *s++ += *s1++; }
354 i=l & 3; while (i--) *s++ += *s1++;
355 T_Array<T> Sum(lb, l, A.data);
356 A.sz = 0; A.data = 0;
357 return Sum;
358 }
359
360 /// Temporary array + array.
361 template<class T>
362 T_Array<T> T_Array<T>::operator+(const Array<T>& A)
363 {
364 int lb = this->first(); if (A.first()!=lb) Throw(Logic_error("Array: incompatible subscripts"));
365 int l = this->length(); if (l != A.length()) Throw(Logic_error("Array: incompatible subscripts"));
366 T* s=this->base(); const T* s2=A.base();
367 int i = l >> 2;
368 while (i--) { *s++ += *s2++; *s++ += *s2++; *s++ += *s2++; *s++ += *s2++; }
369 i=l & 3; while (i--) *s++ += *s2++;
370 T_Array<T> Sum(lb, l, this->data);
371 this->sz = 0; this->data = 0;
372 return Sum;
373 }
374
375
376 /// Shell sort - from Sedgewick.
377 template <class T>
378 void GeneralArray<T>::SortAscending()
379 {
380 int N = sz; T* a = data;
381 int h;
382 for (h = 1; h <= N / 9; h = 3*h + 1) {}
383 for (; h > 0; h /= 3) for (int i = h; i < N; ++i)
384 {
385 T v = a[i]; int j = i;
386 while (j>=h && a[j-h] > v) { a[j] = a[j-h]; j-= h; }
387 a[j] = v;
388 }
389 }
390
391 /// Sum of elements in an array.
392 template <class T>
393 T GeneralArray<T>::Sum() const
394 {
395 T* t = data; int n = sz; T s = 0;
396 while (n--) s += *t++;
397 return s;
398 }
399
400 /// Sum of subset of elements in an array.
401 template <class T>
402 T GeneralArray<T>::Sum(const Array<bool>& Condition) const
403 {
404 T* t = data; const bool* b = Condition.base(); int n = sz; T s = 0;
405 while (n--) { if (*b++) s += *t++; else t++; }
406 return s;
407 }
408
409 #endif
410
411 ///@}
412
413