source: Sophya/trunk/Poubelle/DPC:FitsIOServer/Blitz/blitz/vector.h@ 658

Last change on this file since 658 was 658, checked in by ansari, 26 years ago

no message

File size: 15.5 KB
Line 
1/***************************************************************************
2 * blitz/vector.h Declaration of the Vector<P_numtype> class
3 *
4 * $Id: vector.h,v 1.1.1.1 1999-11-26 16:37:06 ansari Exp $
5 *
6 * Copyright (C) 1997,1998 Todd Veldhuizen <tveldhui@seurat.uwaterloo.ca>
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * Suggestions: blitz-suggest@cybervision.com
19 * Bugs: blitz-bugs@cybervision.com
20 *
21 * For more information, please see the Blitz++ Home Page:
22 * http://seurat.uwaterloo.ca/blitz/
23 *
24 ***************************************************************************
25 * $Log: not supported by cvs2svn $
26 * Revision 1.1.1.1 1999/04/09 17:59:00 ansari
27 * Creation module DPC/Blitz (blitz 0.4) Reza 09/04/99
28 *
29 * Revision 1.8 1998/03/14 00:04:47 tveldhui
30 * 0.2-alpha-05
31 *
32 * Revision 1.7 1997/07/16 14:51:20 tveldhui
33 * Update: Alpha release 0.2 (Arrays)
34 *
35 * Revision 1.6 1997/01/24 14:42:00 tveldhui
36 * Periodic RCS update
37 *
38 * Revision 1.5 1997/01/23 03:28:28 tveldhui
39 * Periodic RCS update
40 *
41 * Revision 1.4 1997/01/13 22:19:58 tveldhui
42 * Periodic RCS update
43 *
44 * Revision 1.3 1996/11/11 17:29:13 tveldhui
45 * Periodic RCS update
46 *
47 * Revision 1.2 1996/10/31 21:06:54 tveldhui
48 * Did away with multiple template parameters.
49 */
50
51/*
52 * KNOWN BUGS
53 *
54 * 1. operator[](Vector<int>) won't match; compiler complains of no
55 * suitable copy constructor for VectorPick<T>
56 * 2. Vector<T>(_bz_VecExpr<E>) constructor generates warning
57 * 3. operator+=,-=,..etc.(Random<D>) won't match; compiler complains of
58 * no suitable copy constructor for _bz_VecExpr(...).
59 */
60
61#ifndef BZ_VECTOR_H
62#define BZ_VECTOR_H
63
64#ifndef BZ_BLITZ_H
65 #include <blitz/blitz.h>
66#endif
67
68#ifndef BZ_MEMBLOCK_H
69 #include <blitz/memblock.h>
70#endif
71
72#ifndef BZ_RANGE_H
73 #include <blitz/range.h>
74#endif
75
76#ifndef BZ_LISTINIT_H
77 #include <blitz/listinit.h>
78#endif
79
80BZ_NAMESPACE(blitz)
81
82// Forward declarations
83template<class P_numtype> class VectorIter;
84template<class P_numtype> class VectorIterConst;
85template<class P_expr> class _bz_VecExpr;
86template<class P_numtype> class VectorPick;
87template<class P_numtype> class Random;
88
89// Declaration of class Vector<P_numtype>
90
91template<class P_numtype>
92class Vector : protected MemoryBlockReference<P_numtype> {
93
94public:
95 //////////////////////////////////////////////
96 // Public Types
97 //////////////////////////////////////////////
98
99 typedef P_numtype T_numtype;
100 typedef Vector<T_numtype> T_vector;
101 typedef VectorIter<T_numtype> T_iterator;
102 typedef VectorIterConst<T_numtype> T_constIterator;
103 typedef VectorPick<T_numtype> T_pick;
104 typedef Vector<int> T_indexVector;
105
106 //////////////////////////////////////////////
107 // Constructors //
108 //////////////////////////////////////////////
109 // Most of the constructors are inlined so that
110 // the setting of the stride_ data member will
111 // be visible to the optimizer.
112
113 Vector()
114 {
115 length_ = 0;
116 stride_ = 0;
117 }
118
119 // This constructor is provided inline because it involves
120 // no memory allocation.
121 Vector(Vector<T_numtype>& vec)
122 : MemoryBlockReference<T_numtype>(vec)
123 {
124 length_ = vec.length_;
125 stride_ = vec.stride_;
126 }
127
128 // Slightly unsafe cast-away-const version
129 Vector(const Vector<T_numtype>& vec)
130 : MemoryBlockReference<T_numtype>
131 (const_cast<Vector<T_numtype>& >(vec))
132 {
133 length_ = vec.length_;
134 stride_ = vec.stride_;
135 }
136
137 _bz_explicit Vector(int length)
138 : MemoryBlockReference<T_numtype>(length)
139 {
140 length_ = length;
141 stride_ = 1;
142 }
143
144 Vector(Vector<T_numtype>& vec, Range r)
145 : MemoryBlockReference<T_numtype>(vec,
146 r.first() * vec.stride())
147 {
148 BZPRECONDITION((r.first() >= 0) && (r.first() < vec.length()));
149 BZPRECONDITION((r.last(vec.length()-1) >= 0)
150 && (r.last(vec.length()-1) < vec.length()));
151 length_ = (r.last(vec.length()-1) - r.first()) / r.stride() + 1;
152 stride_ = vec.stride_ * r.stride();
153 }
154
155 Vector(int length, T_numtype initValue)
156 : MemoryBlockReference<T_numtype>(length)
157 {
158 length_ = length;
159 stride_ = 1;
160 (*this) = initValue;
161 }
162
163 Vector(int length, T_numtype firstValue, T_numtype delta)
164 : MemoryBlockReference<T_numtype>(length)
165 {
166 length_ = length;
167 stride_ = 1;
168 for (int i=0; i < length; ++i)
169 data_[i] = firstValue + i * delta;
170 }
171
172 template<class P_distribution>
173 Vector(int length, Random<P_distribution>& random)
174 : MemoryBlockReference<T_numtype>(length)
175 {
176 length_ = length;
177 stride_ = 1;
178 (*this) = random;
179 }
180
181 template<class P_expr>
182 Vector(_bz_VecExpr<P_expr> expr)
183 : MemoryBlockReference<T_numtype>(expr._bz_suggestLength())
184 {
185 length_ = expr._bz_suggestLength();
186 stride_ = 1;
187 (*this) = expr;
188 }
189
190 // Create a vector view of an already allocated block of memory.
191 // Note that the memory will not be freed when this vector is
192 // destroyed.
193 Vector(int length, T_numtype* _bz_restrict data, int stride = 1)
194 : MemoryBlockReference<T_numtype>(length, data)
195 {
196 length_ = length;
197 stride_ = stride;
198 }
199
200 // Create a vector containing a range of numbers
201 Vector(Range r)
202 : MemoryBlockReference<T_numtype>(r._bz_suggestLength())
203 {
204 length_ = r._bz_suggestLength();
205 stride_ = 1;
206 (*this) = _bz_VecExpr<Range>(r);
207 }
208
209 //////////////////////////////////////////////
210 // Member functions
211 //////////////////////////////////////////////
212
213 // assertUnitStride() is provided as an optimizing trick. When
214 // vectors are constructed outside the function scope, the optimizer
215 // is unaware that they have unit stride. This function sets the
216 // stride to 1 in the local scope so the optimizer can do copy
217 // propagation & dead code elimination. Obviously, you don't
218 // want to use this routine unless you are certain that the
219 // vectors have unit stride.
220 void assertUnitStride()
221 {
222 BZPRECONDITION(stride_ == 1);
223 stride_ = 1;
224 }
225
226 T_iterator begin()
227 { return T_iterator(*this); }
228
229 T_constIterator begin() const
230 { return T_constIterator(*this); }
231
232 T_vector copy() const;
233
234 // T_iterator end();
235 // T_constIterator end() const;
236
237 T_numtype * _bz_restrict data()
238 { return data_; }
239
240 const T_numtype * _bz_restrict data() const
241 { return data_; }
242
243 _bz_bool isUnitStride() const
244 { return stride_ == 1; }
245
246 int length() const
247 { return length_; }
248
249 void makeUnique();
250
251 // int storageSize() const;
252
253 // void storeToBuffer(void* buffer, int bufferLength) const;
254
255 void reference(T_vector&);
256
257 void resize(int length);
258
259 void resizeAndPreserve(int newLength);
260
261 // int restoreFromBuffer(void* buffer, int bufferLength);
262
263 T_vector reverse()
264 { return T_vector(*this,Range(length()-1,0,-1)); }
265
266 int stride() const
267 { return stride_; }
268
269 operator _bz_VecExpr<VectorIterConst<T_numtype> > () const
270 { return _bz_VecExpr<VectorIterConst<T_numtype> >(begin()); }
271
272 /////////////////////////////////////////////
273 // Library-internal member functions
274 // These are undocumented and may change or
275 // disappear in future releases.
276 /////////////////////////////////////////////
277
278 int _bz_suggestLength() const
279 { return length_; }
280
281 _bz_bool _bz_hasFastAccess() const
282 { return stride_ == 1; }
283
284 T_numtype& _bz_fastAccess(int i)
285 { return data_[i]; }
286
287 T_numtype _bz_fastAccess(int i) const
288 { return data_[i]; }
289
290 template<class P_expr, class P_updater>
291 void _bz_assign(P_expr, P_updater);
292
293 _bz_VecExpr<T_constIterator> _bz_asVecExpr() const
294 { return _bz_VecExpr<T_constIterator>(begin()); }
295
296 //////////////////////////////////////////////
297 // Subscripting operators
298 //////////////////////////////////////////////
299
300 // operator()(int) may be used only when the vector has unit
301 // stride. Otherwise, use operator[].
302 T_numtype operator()(int i) const
303 {
304 BZPRECONDITION(i < length_);
305 BZPRECONDITION(stride_ == 1);
306 return data_[i];
307 }
308
309 // operator()(int) may be used only when the vector has unit
310 // stride. Otherwise, use operator[].
311 T_numtype& _bz_restrict operator()(int i)
312 {
313 BZPRECONDITION(i < length_);
314 BZPRECONDITION(stride_ == 1);
315 return data_[i];
316 }
317
318 T_numtype operator[](int i) const
319 {
320 BZPRECONDITION(i < length_);
321 return data_[i * stride_];
322 }
323
324 T_numtype& _bz_restrict operator[](int i)
325 {
326 BZPRECONDITION(i < length_);
327 return data_[i * stride_];
328 }
329
330 T_vector operator()(Range r)
331 {
332 return T_vector(*this, r);
333 }
334
335 T_vector operator[](Range r)
336 {
337 return T_vector(*this, r);
338 }
339
340 T_pick operator()(T_indexVector i)
341 {
342 return T_pick(*this, i);
343 }
344
345 T_pick operator[](T_indexVector i)
346 {
347 return T_pick(*this, i);
348 }
349
350 // T_vector operator()(difference-equation-expression)
351
352 //////////////////////////////////////////////
353 // Assignment operators
354 //////////////////////////////////////////////
355
356 // Scalar operand
357 ListInitializationSwitch<T_vector,T_iterator> operator=(T_numtype x)
358 {
359 return ListInitializationSwitch<T_vector,T_iterator>(*this, x);
360 }
361
362 T_iterator getInitializationIterator()
363 { return begin(); }
364
365 T_vector& initialize(T_numtype);
366 T_vector& operator+=(T_numtype);
367 T_vector& operator-=(T_numtype);
368 T_vector& operator*=(T_numtype);
369 T_vector& operator/=(T_numtype);
370 T_vector& operator%=(T_numtype);
371 T_vector& operator^=(T_numtype);
372 T_vector& operator&=(T_numtype);
373 T_vector& operator|=(T_numtype);
374 T_vector& operator>>=(int);
375 T_vector& operator<<=(int);
376
377 // Vector operand
378
379 template<class P_numtype2> T_vector& operator=(const Vector<P_numtype2> &);
380
381 // Specialization uses memcpy instead of element-by-element cast and
382 // copy
383 // NEEDS_WORK -- KCC won't accept this syntax; standard??
384 // template<> T_vector& operator=(const T_vector&);
385
386 template<class P_numtype2> T_vector& operator+=(const Vector<P_numtype2> &);
387 template<class P_numtype2> T_vector& operator-=(const Vector<P_numtype2> &);
388 template<class P_numtype2> T_vector& operator*=(const Vector<P_numtype2> &);
389 template<class P_numtype2> T_vector& operator/=(const Vector<P_numtype2> &);
390 template<class P_numtype2> T_vector& operator%=(const Vector<P_numtype2> &);
391 template<class P_numtype2> T_vector& operator^=(const Vector<P_numtype2> &);
392 template<class P_numtype2> T_vector& operator&=(const Vector<P_numtype2> &);
393 template<class P_numtype2> T_vector& operator|=(const Vector<P_numtype2> &);
394 template<class P_numtype2> T_vector& operator>>=(const Vector<P_numtype2> &);
395 template<class P_numtype2> T_vector& operator<<=(const Vector<P_numtype2> &);
396
397 // Vector expression operand
398 template<class P_expr> T_vector& operator=(_bz_VecExpr<P_expr>);
399 template<class P_expr> T_vector& operator+=(_bz_VecExpr<P_expr>);
400 template<class P_expr> T_vector& operator-=(_bz_VecExpr<P_expr>);
401 template<class P_expr> T_vector& operator*=(_bz_VecExpr<P_expr>);
402 template<class P_expr> T_vector& operator/=(_bz_VecExpr<P_expr>);
403 template<class P_expr> T_vector& operator%=(_bz_VecExpr<P_expr>);
404 template<class P_expr> T_vector& operator^=(_bz_VecExpr<P_expr>);
405 template<class P_expr> T_vector& operator&=(_bz_VecExpr<P_expr>);
406 template<class P_expr> T_vector& operator|=(_bz_VecExpr<P_expr>);
407 template<class P_expr> T_vector& operator>>=(_bz_VecExpr<P_expr>);
408 template<class P_expr> T_vector& operator<<=(_bz_VecExpr<P_expr>);
409
410 // VectorPick operand
411 template<class P_numtype2>
412 T_vector& operator=(const VectorPick<P_numtype2> &);
413 template<class P_numtype2>
414 T_vector& operator+=(const VectorPick<P_numtype2> &);
415 template<class P_numtype2>
416 T_vector& operator-=(const VectorPick<P_numtype2> &);
417 template<class P_numtype2>
418 T_vector& operator*=(const VectorPick<P_numtype2> &);
419 template<class P_numtype2>
420 T_vector& operator/=(const VectorPick<P_numtype2> &);
421 template<class P_numtype2>
422 T_vector& operator%=(const VectorPick<P_numtype2> &);
423 template<class P_numtype2>
424 T_vector& operator^=(const VectorPick<P_numtype2> &);
425 template<class P_numtype2>
426 T_vector& operator&=(const VectorPick<P_numtype2> &);
427 template<class P_numtype2>
428 T_vector& operator|=(const VectorPick<P_numtype2> &);
429 template<class P_numtype2>
430 T_vector& operator>>=(const VectorPick<P_numtype2> &);
431 template<class P_numtype2>
432 T_vector& operator<<=(const VectorPick<P_numtype2> &);
433
434 // Range operand
435 T_vector& operator=(Range);
436 T_vector& operator+=(Range);
437 T_vector& operator-=(Range);
438 T_vector& operator*=(Range);
439 T_vector& operator/=(Range);
440 T_vector& operator%=(Range);
441 T_vector& operator^=(Range);
442 T_vector& operator&=(Range);
443 T_vector& operator|=(Range);
444 T_vector& operator>>=(Range);
445 T_vector& operator<<=(Range);
446
447 // Random operand
448 template<class P_distribution>
449 T_vector& operator=(Random<P_distribution>& random);
450 template<class P_distribution>
451 T_vector& operator+=(Random<P_distribution>& random);
452 template<class P_distribution>
453 T_vector& operator-=(Random<P_distribution>& random);
454 template<class P_distribution>
455 T_vector& operator*=(Random<P_distribution>& random);
456 template<class P_distribution>
457 T_vector& operator/=(Random<P_distribution>& random);
458 template<class P_distribution>
459 T_vector& operator%=(Random<P_distribution>& random);
460 template<class P_distribution>
461 T_vector& operator^=(Random<P_distribution>& random);
462 template<class P_distribution>
463 T_vector& operator&=(Random<P_distribution>& random);
464 template<class P_distribution>
465 T_vector& operator|=(Random<P_distribution>& random);
466
467 //////////////////////////////////////////////
468 // Unary operators
469 //////////////////////////////////////////////
470
471// T_vector& operator++();
472// void operator++(int);
473// T_vector& operator--();
474// void operator--(int);
475
476private:
477 int length_;
478 int stride_;
479};
480
481// Global I/O functions
482
483template<class P_numtype>
484ostream& operator<<(ostream& os, const Vector<P_numtype>& x);
485
486template<class P_expr>
487ostream& operator<<(ostream& os, _bz_VecExpr<P_expr> expr);
488
489BZ_NAMESPACE_END
490
491#include <blitz/veciter.h> // Iterators
492#include <blitz/vecpick.h> // VectorPick
493#include <blitz/vecexpr.h> // Expression templates
494#include <blitz/vecglobs.h> // Global functions
495#include <blitz/vector.cc> // Member functions
496#include <blitz/vecio.cc> // IO functions
497
498#endif // BZ_VECTOR_H
Note: See TracBrowser for help on using the repository browser.