-
Notifications
You must be signed in to change notification settings - Fork 4
/
vector.hpp
388 lines (328 loc) · 10.2 KB
/
vector.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#ifndef _SCTL_VECTOR_HPP_
#define _SCTL_VECTOR_HPP_
#include <ostream> // for ostream
#include <initializer_list> // for initializer_list
#include <vector> // for vector
#include "sctl/common.hpp" // for Long, sctl
#include "sctl/iterator.hpp" // for Iterator, ConstIterator
namespace sctl {
// forward declaration
template <class ValueType> Iterator<ValueType> NullIterator();
/**
* A contiguous array of elements. The elements can be accesses with a non-negative index. The vector can be the
* owner of the memory allocated on the heap (automatically aligned to `SCTL_MEM_ALIGN` bytes for SIMD vectorization) or
* it may be constructed from a user provided memory location (using Iterator<ValueType>).
*
* @tparam ValueType Type of the elements stored in the vector.
*/
template <class ValueType> class Vector {
public:
typedef ValueType value_type; /**< Type of the elements stored in the vector. */
typedef ValueType& reference; /**< Reference to an element in the vector. */
typedef const ValueType& const_reference; /**< Const reference to an element in the vector. */
typedef Iterator<ValueType> iterator; /**< Iterator for traversing the vector. */
typedef ConstIterator<ValueType> const_iterator; /**< Const iterator for traversing the vector. */
typedef Long difference_type; /**< Integer type representing the difference between two iterators. */
typedef Long size_type; /**< Integer type representing the size of the vector. */
/**
* Default constructor.
*/
Vector();
/**
* Constructor with dimension and data pointer.
*
* @param dim Dimension of the vector.
* @param data Pointer to the data.
* @param own_data Flag indicating ownership of data.
*/
explicit Vector(Long dim, Iterator<ValueType> data = NullIterator<ValueType>(), bool own_data = true);
/**
* Copy constructor.
*
* @param V Another vector to copy from.
*/
Vector(const Vector& V);
/**
* Constructor from std::vector.
*
* @param V std::vector to construct from.
*/
explicit Vector(const std::vector<ValueType>& V);
/**
* Constructor from initializer list.
*
* @param V Initializer list to construct from.
*/
explicit Vector(std::initializer_list<ValueType> V);
/**
* Destructor.
*/
~Vector();
/**
* Swap the contents of two vectors.
*
* @param v1 Vector to swap with.
*/
void Swap(Vector<ValueType>& v1);
/**
* Reinitialize the vector.
*
* @param dim New dimension of the vector.
* @param data New data pointer.
* @param own_data Flag indicating ownership of new data.
*/
void ReInit(Long dim, Iterator<ValueType> data = NullIterator<ValueType>(), bool own_data = true);
/**
* Write the vector to a file.
*
* @param fname File name to write to.
*/
void Write(const char* fname) const;
/**
* Write the vector to a file with a different data type.
*
* @tparam Type Type of data to write.
* @param fname File name to write to.
*/
template <class Type> void Write(const char* fname) const;
/**
* Read the vector from a file.
*
* @param fname File name to read from.
*/
void Read(const char* fname);
/**
* Read the vector from a file with a different data type.
*
* @tparam Type Type of data to read.
* @param fname File name to read from.
*/
template <class Type> void Read(const char* fname);
/**
* Get the dimension of the vector.
*
* @return Long Dimension of the vector.
*/
Long Dim() const;
//Long Capacity() const;
/**
* Set all elements of the vector to zero.
*/
void SetZero();
/**
* Get an iterator pointing to the beginning of the vector.
*
* @return Iterator<ValueType> Iterator pointing to the beginning of the vector.
*/
Iterator<ValueType> begin();
/**
* Get a const iterator pointing to the beginning of the vector.
*
* @return ConstIterator<ValueType> Const iterator pointing to the beginning of the vector.
*/
ConstIterator<ValueType> begin() const;
/**
* Get an iterator pointing to the end of the vector.
*
* @return Iterator<ValueType> Iterator pointing to the end of the vector.
*/
Iterator<ValueType> end();
/**
* Get a const iterator pointing to the end of the vector.
*
* @return ConstIterator<ValueType> Const iterator pointing to the end of the vector.
*/
ConstIterator<ValueType> end() const;
/**
* Add an element to the end of the vector.
*
* @param x Element to be added.
*/
void PushBack(const ValueType& x);
// Element access
/**
* Access an element of the vector.
*
* @param j Index of the element to access.
* @return ValueType& Reference to the accessed element.
*/
ValueType& operator[](Long j);
/**
* Access a const element of the vector.
*
* @param j Index of the element to access.
* @return const ValueType& Const reference to the accessed element.
*/
const ValueType& operator[](Long j) const;
// Vector-Vector operations
/**
* Assignment operator from std::vector.
*
* @param V std::vector to assign from.
* @return Vector& Reference to the modified vector.
*/
Vector& operator=(const std::vector<ValueType>& V);
/**
* Assignment operator.
*
* @param V Vector to assign from.
* @return Vector& Reference to the modified vector.
*/
Vector& operator=(const Vector& V);
/**
* Addition assignment operator.
*
* @param V Vector to add.
* @return Vector& Reference to the modified vector.
*/
Vector& operator+=(const Vector& V);
/**
* Subtraction assignment operator.
*
* @param V Vector to subtract.
* @return Vector& Reference to the modified vector.
*/
Vector& operator-=(const Vector& V);
/**
* Multiplication assignment operator.
*
* @param V Vector to multiply.
* @return Vector& Reference to the modified vector.
*/
Vector& operator*=(const Vector& V);
/**
* Division assignment operator.
*
* @param V Vector to divide.
* @return Vector& Reference to the modified vector.
*/
Vector& operator/=(const Vector& V);
/**
* Addition operator.
*
* @param V Vector to add.
* @return Vector Resultant vector after addition.
*/
Vector operator+(const Vector& V) const;
/**
* Subtraction operator.
*
* @param V Vector to subtract.
* @return Vector Resultant vector after subtraction.
*/
Vector operator-(const Vector& V) const;
/**
* Multiplication operator.
*
* @param V Vector to multiply.
* @return Vector Resultant vector after multiplication.
*/
Vector operator*(const Vector& V) const;
/**
* Division operator.
*
* @param V Vector to divide.
* @return Vector Resultant vector after division.
*/
Vector operator/(const Vector& V) const;
/**
* Negation operator.
*
* @return Vector Negated vector.
*/
Vector operator-() const ;
// Vector-Scalar operations
/**
* Assignment operator with a scalar.
*
* @tparam VType Type of the scalar.
* @param s Scalar value to assign.
* @return Vector& Reference to the modified vector.
*/
template <class VType> Vector& operator=(VType s);
/**
* Addition assignment operator with a scalar.
*
* @tparam VType Type of the scalar.
* @param s Scalar value to add.
* @return Vector& Reference to the modified vector.
*/
template <class VType> Vector& operator+=(VType s);
/**
* Subtraction assignment operator with a scalar.
*
* @tparam VType Type of the scalar.
* @param s Scalar value to subtract.
* @return Vector& Reference to the modified vector.
*/
template <class VType> Vector& operator-=(VType s);
/**
* Multiplication assignment operator with a scalar.
*
* @tparam VType Type of the scalar.
* @param s Scalar value to multiply.
* @return Vector& Reference to the modified vector.
*/
template <class VType> Vector& operator*=(VType s);
/**
* Division assignment operator with a scalar.
*
* @tparam VType Type of the scalar.
* @param s Scalar value to divide.
* @return Vector& Reference to the modified vector.
*/
template <class VType> Vector& operator/=(VType s);
/**
* Addition operator with a scalar.
*
* @tparam VType Type of the scalar.
* @param s Scalar value to add.
* @return Vector Resultant vector after addition.
*/
template <class VType> Vector operator+(VType s) const;
/**
* Subtraction operator with a scalar.
*
* @tparam VType Type of the scalar.
* @param s Scalar value to subtract.
* @return Vector Resultant vector after subtraction.
*/
template <class VType> Vector operator-(VType s) const;
/**
* Multiplication operator with a scalar.
*
* @tparam VType Type of the scalar.
* @param s Scalar value to multiply.
* @return Vector Resultant vector after multiplication.
*/
template <class VType> Vector operator*(VType s) const;
/**
* Division operator with a scalar.
*
* @tparam VType Type of the scalar.
* @param s Scalar value to divide.
* @return Vector Resultant vector after division.
*/
template <class VType> Vector operator/(VType s) const;
private:
/**
* Initialize the vector.
*
* @param dim Dimension of the vector.
* @param data Pointer to the data.
* @param own_data Flag indicating ownership of data.
*/
void Init(Long dim, Iterator<ValueType> data = NullIterator<ValueType>(), bool own_data = true);
Long dim; /**< Dimension of the vector. */
Long capacity; /**< Capacity of the vector. */
Iterator<ValueType> data_ptr; /**< Pointer to the data. */
bool own_data; /**< Flag indicating ownership of the data. */
};
// Function template declarations for vector-scalar operations...
template <class VType, class ValueType> Vector<ValueType> operator+(VType s, const Vector<ValueType>& V);
template <class VType, class ValueType> Vector<ValueType> operator-(VType s, const Vector<ValueType>& V);
template <class VType, class ValueType> Vector<ValueType> operator*(VType s, const Vector<ValueType>& V);
template <class VType, class ValueType> Vector<ValueType> operator/(VType s, const Vector<ValueType>& V);
template <class ValueType> std::ostream& operator<<(std::ostream& output, const Vector<ValueType>& V);
} // end namespace
#endif // _SCTL_VECTOR_HPP_