source: Sophya/trunk/SophyaExt/Blitz/blitz/vector.h@ 3668

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

Creation module DPC/Blitz (blitz 0.4) Reza 09/04/99

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