FIMS  v0.10.0
Loading...
Searching...
No Matches
fims_vector.hpp
Go to the documentation of this file.
1
8#ifndef FIMS_VECTOR_HPP
9#define FIMS_VECTOR_HPP
10
11#include "../interface/interface.hpp"
12#include <ostream>
13#include <iomanip>
14
15namespace fims {
16
26template <typename Type>
27class Vector {
28 std::vector<Type> vec_m;
33 template <typename T>
34 friend bool operator==(const fims::Vector<T> &lhs,
35 const fims::Vector<T> &rhs);
36
37 public:
38 // Member Types
39
40 typedef
41 typename std::vector<Type>::value_type value_type;
42 typedef typename std::vector<Type>::allocator_type
44 typedef typename std::vector<Type>::size_type size_type;
45 typedef typename std::vector<Type>::difference_type
47 typedef typename std::vector<Type>::reference
49 typedef typename std::vector<Type>::const_reference
51 typedef typename std::vector<Type>::pointer pointer;
52 typedef typename std::vector<Type>::const_pointer
54 typedef typename std::vector<Type>::iterator iterator;
55 typedef typename std::vector<Type>::const_iterator
57 typedef typename std::vector<Type>::reverse_iterator
59 typedef typename std::vector<Type>::const_reverse_iterator
62 // Constructors
63
67 Vector() {}
68
73 Vector(size_t size, const Type &value = Type()) {
74 this->vec_m.resize(size, value);
75 }
76
80 Vector(const Vector<Type> &other) {
81 this->vec_m.resize(other.size());
82 for (size_t i = 0; i < this->vec_m.size(); i++) {
83 this->vec_m[i] = other[i];
84 }
85 }
86
96 Vector &operator=(const Vector &other) {
97 if (this != &other) {
98 // clean up existing
99 this->~Vector();
100 // copy construct into *this
101 new (this) Vector(other);
102 }
103 return *this;
104 }
105
109 Vector(const std::vector<Type> &other) { this->vec_m = other; }
110
111 // TMB specific constructor
112#ifdef TMB_MODEL
113
117 Vector(const tmbutils::vector<Type> &other) {
118 this->vec_m.resize(other.size());
119 for (size_t i = 0; i < this->vec_m.size(); i++) {
120 this->vec_m[i] = other[i];
121 }
122 }
123
124#endif
125
129 Vector(std::initializer_list<Type> init) {
130 this->vec_m = std::vector<Type>(init);
131 }
132
143 inline Type &operator[](size_t pos) {
144 if (pos >= this->size()) {
145 throw std::invalid_argument("fims::Vector out of bounds");
146 }
147 return this->vec_m[pos];
148 }
149
154 inline const Type &operator[](size_t n) const {
155 if (n >= this->size()) {
156 throw std::invalid_argument("fims::Vector out of bounds");
157 }
158 return this->vec_m[n];
159 }
160
165 inline Type &at(size_t n) { return this->vec_m.at(n); }
166
171 inline const Type &at(size_t n) const { return this->vec_m.at(n); }
172
182 inline Type &get_force_scalar(size_t pos) {
183 if (this->size() == 1 && pos > 0) {
184 return this->at(0);
185 } else if (this->size() > 1 && pos >= this->size()) {
186 throw std::invalid_argument(
187 "fims::Vector index out of bounds, check parameter sizes of input.");
188 } else {
189 return this->at(pos);
190 }
191 }
192
207 inline Type &get_force_scalar_wrap(size_t pos) {
208 if (this->size() == 1 && pos > 0) {
209 return this->at(0);
210 } else if (this->size() > 1 && pos >= this->size()) {
211 size_t remain = pos % this->size();
212 // this only works if both pos and this->size() are integers
213 return this->at(remain);
214 } else {
215 return this->at(pos);
216 }
217 }
218
222 inline reference front() { return this->vec_m.front(); }
223
227 inline const_reference front() const { return this->vec_m.front(); }
228
232 inline reference back() { return this->vec_m.back(); }
233
237 inline const_reference back() const { return this->vec_m.back(); }
238
242 inline pointer data() { return this->vec_m.data(); }
243
247 inline const_pointer data() const { return this->vec_m.data(); }
248
249 // iterators
250
254 inline iterator begin() { return this->vec_m.begin(); }
255
260 inline iterator end() { return this->vec_m.end(); }
261
266 inline reverse_iterator rbegin() { return this->vec_m.rbegin(); }
267
273 inline reverse_iterator rend() { return this->vec_m.rend(); }
274
280 inline const_reverse_iterator rbegin() const { return this->vec_m.rbegin(); }
281
287 inline const_reverse_iterator rend() const { return this->vec_m.rend(); }
288
289 // capacity
290
294 inline bool empty() { return this->vec_m.empty(); }
295
299 inline size_type size() const { return this->vec_m.size(); }
300
304 inline size_type max_size() const { return this->vec_m.max_size(); }
305
309 inline void reserve(size_type cap) { this->vec_m.reserve(cap); }
310
315 inline size_type capacity() { return this->vec_m.capacity(); }
316
320 inline void shrink_to_fit() { this->vec_m.shrink_to_fit(); }
321
322 // modifiers
323
327 inline void clear() { this->vec_m.clear(); }
328
332 inline iterator insert(const_iterator pos, const Type &value) {
333 return this->vec_m.insert(pos, value);
334 }
335
340 const Type &value) {
341 return this->vec_m.insert(pos, count, value);
342 }
343
347 template <class InputIt>
348 iterator insert(const_iterator pos, InputIt first, InputIt last) {
349 return this->vec_m.insert(pos, first, last);
350 }
351
356 iterator insert(const_iterator pos, std::initializer_list<Type> ilist) {
357 return this->vec_m.insert(pos, ilist);
358 }
359
363 template <class... Args>
364 iterator emplace(const_iterator pos, Args &&...args) {
365 return this->vec_m.emplace(pos, std::forward<Args>(args)...);
366 }
367
371 inline iterator erase(iterator pos) { return this->vec_m.erase(pos); }
372
376 inline iterator erase(iterator first, iterator last) {
377 return this->vec_m.erase(first, last);
378 }
379
383 inline void push_back(const Type &&value) { this->vec_m.push_back(value); }
384
388 template <class... Args>
389 void emplace_back(Args &&...args) {
390 this->vec_m.emplace_back(std::forward<Args>(args)...);
391 }
392
396 inline void pop_back() { this->vec_m.pop_back(); }
397
401 inline void resize(size_t s) { this->vec_m.resize(s); }
402
406 inline void swap(Vector &other) { this->vec_m.swap(other.vec_m); }
407
408 // end std::vector functions
409
417 inline operator std::vector<Type>() { return this->vec_m; }
418
419#ifdef TMB_MODEL
420
428 explicit operator tmbutils::vector<Type>() const {
429 tmbutils::vector<Type> ret(this->vec_m.size());
430 for (size_t i = 0; i < this->vec_m.size(); i++) {
431 ret[i] = this->vec_m[i];
432 }
433 return ret;
434 }
435
439 tmbutils::vector<Type> to_tmb() const {
440 tmbutils::vector<Type> ret(this->vec_m.size());
441 for (size_t i = 0; i < this->vec_m.size(); i++) {
442 ret[i] = this->vec_m[i];
443 }
444 return ret;
445 }
446
454 explicit operator tmbutils::vector<Type>() {
455 tmbutils::vector<Type> ret(this->vec_m.size());
456 for (size_t i = 0; i < this->vec_m.size(); i++) {
457 ret[i] = this->vec_m[i];
458 }
459 return ret;
460 }
461
465 tmbutils::vector<Type> to_tmb() {
466 tmbutils::vector<Type> ret(this->vec_m.size());
467 for (size_t i = 0; i < this->vec_m.size(); i++) {
468 ret[i] = this->vec_m[i];
469 }
470 return ret;
471 }
472
473#else
474
483 std::vector<Type> to_std() const { return this->vec_m; }
484
494 std::vector<Type> to_tmb() const { return this->vec_m; }
495#endif
496
502 std::string get_tag() const { return this->tag_m; }
507 void set_tag(const std::string &tag) { this->tag_m = tag; }
508
509 private:
510 std::string tag_m;
511}; // end fims::Vector class
512
516template <class T>
517bool operator==(const fims::Vector<T> &lhs, const fims::Vector<T> &rhs) {
518 return lhs.vec_m == rhs.vec_m;
519}
520
521} // namespace fims
522
530template <typename Type>
531std::ostream &operator<<(std::ostream &out, const fims::Vector<Type> &v) {
532 out << std::fixed << std::setprecision(10);
533 out << "[";
534
535 if (v.size() == 0) {
536 out << "]";
537 return out;
538 }
539 for (size_t i = 0; i < v.size() - 1; i++) {
540 if (v[i] != v[i]) {
541 out << "-999" << ",";
542 } else {
543 out << v[i] << ",";
544 }
545 }
546 if (v[v.size() - 1] != v[v.size() - 1]) {
547 out << "-999]";
548 } else {
549 out << v[v.size() - 1] << "]";
550 }
551 return out;
552}
553
554#endif
Definition fims_vector.hpp:27
std::vector< Type >::pointer pointer
Definition fims_vector.hpp:51
iterator emplace(const_iterator pos, Args &&...args)
Constructs element in-place.
Definition fims_vector.hpp:364
std::vector< Type >::const_iterator const_iterator
Definition fims_vector.hpp:56
size_type max_size() const
Returns the maximum possible number of elements.
Definition fims_vector.hpp:304
reverse_iterator rend()
Returns a reverse iterator to the element following the last element of the reversed vector....
Definition fims_vector.hpp:273
const Type & at(size_t n) const
Returns a constant reference to the element at specified location pos. Bounds checking is performed.
Definition fims_vector.hpp:171
const_reference front() const
Returns a constant reference to the first element in the container.
Definition fims_vector.hpp:227
std::vector< Type >::const_reverse_iterator const_reverse_iterator
Definition fims_vector.hpp:60
Vector(const Vector< Type > &other)
Copy constructor.
Definition fims_vector.hpp:80
void reserve(size_type cap)
Reserves storage.
Definition fims_vector.hpp:309
iterator begin()
Returns an iterator to the first element of the vector.
Definition fims_vector.hpp:254
std::vector< Type >::reference reference
Definition fims_vector.hpp:48
iterator end()
Returns an iterator to the element following the last element of the vector.
Definition fims_vector.hpp:260
void shrink_to_fit()
Reduces memory usage by freeing unused memory.
Definition fims_vector.hpp:320
Vector(std::initializer_list< Type > init)
Initialization constructor from std::initializer_list<Type> type.
Definition fims_vector.hpp:129
size_type capacity()
Returns the number of elements that can be held in currently allocated storage.
Definition fims_vector.hpp:315
void emplace_back(Args &&...args)
Constructs an element in-place at the end.
Definition fims_vector.hpp:389
reference front()
Returns a reference to the first element in the container.
Definition fims_vector.hpp:222
std::string get_tag() const
Gets the tag for the vector. A tag can represent anything and is not used internally by FIMS.
Definition fims_vector.hpp:502
std::vector< Type >::value_type value_type
Definition fims_vector.hpp:41
void swap(Vector &other)
Swaps the contents.
Definition fims_vector.hpp:406
Type & at(size_t n)
Returns a reference to the element at specified location pos. Bounds checking is performed.
Definition fims_vector.hpp:165
void push_back(const Type &&value)
Adds an element to the end.
Definition fims_vector.hpp:383
iterator insert(const_iterator pos, InputIt first, InputIt last)
Inserts elements from range [first, last) before pos.
Definition fims_vector.hpp:348
bool empty()
Checks whether the container is empty.
Definition fims_vector.hpp:294
iterator erase(iterator pos)
Removes the element at pos.
Definition fims_vector.hpp:371
Type & get_force_scalar(size_t pos)
If this vector is size 1 and pos is greater than zero, the first index is returned....
Definition fims_vector.hpp:182
std::vector< Type >::const_reference const_reference
Definition fims_vector.hpp:50
Vector(size_t size, const Type &value=Type())
Constructs a Vector of length "size" and sets the elements with the value from input "value".
Definition fims_vector.hpp:73
pointer data()
Returns a pointer to the underlying data array.
Definition fims_vector.hpp:242
reference back()
Returns a reference to the last element in the container.
Definition fims_vector.hpp:232
const Type & operator[](size_t n) const
Returns a constant reference to the element at specified location pos. No bounds checking is performe...
Definition fims_vector.hpp:154
const_reverse_iterator rend() const
Returns a constant reverse iterator to the element following the last element of the reversed vector....
Definition fims_vector.hpp:287
void pop_back()
Removes the last element.
Definition fims_vector.hpp:396
void resize(size_t s)
Changes the number of elements stored.
Definition fims_vector.hpp:401
std::vector< Type > to_std() const
Convert fims::Vector to std::vector.
Definition fims_vector.hpp:483
Type & get_force_scalar_wrap(size_t pos)
If this vector is size 1 and pos is greater than zero, the first index is returned....
Definition fims_vector.hpp:207
std::vector< Type > to_tmb() const
Convert fims::Vector to TMB vector type.
Definition fims_vector.hpp:494
iterator erase(iterator first, iterator last)
Removes the elements in the range [first, last).
Definition fims_vector.hpp:376
const_reverse_iterator rbegin() const
Returns a constant reverse iterator to the first element of the reversed vector. It corresponds to th...
Definition fims_vector.hpp:280
iterator insert(const_iterator pos, const Type &value)
Inserts value before pos.
Definition fims_vector.hpp:332
std::vector< Type >::allocator_type allocator_type
Definition fims_vector.hpp:43
iterator insert(const_iterator pos, std::initializer_list< Type > ilist)
Inserts elements from initializer list ilist before pos.
Definition fims_vector.hpp:356
reverse_iterator rbegin()
Returns a reverse iterator to the first element of the reversed vector. It corresponds to the last el...
Definition fims_vector.hpp:266
const_pointer data() const
Returns a constant pointer to the underlying data array.
Definition fims_vector.hpp:247
size_type size() const
Returns the number of elements.
Definition fims_vector.hpp:299
Vector()
Definition fims_vector.hpp:67
Type & operator[](size_t pos)
Returns a reference to the element at specified location pos. No bounds checking is performed.
Definition fims_vector.hpp:143
void set_tag(const std::string &tag)
Sets the tag for the vector. A tag can be set to any string value and is not used internally by FIMS.
Definition fims_vector.hpp:507
std::vector< Type >::difference_type difference_type
Definition fims_vector.hpp:46
Vector & operator=(const Vector &other)
Assignment operator for fims::Vector.
Definition fims_vector.hpp:96
std::vector< Type >::const_pointer const_pointer
Definition fims_vector.hpp:53
std::vector< Type >::iterator iterator
Definition fims_vector.hpp:54
Vector(const std::vector< Type > &other)
Initialization constructor from std::vector<Type> type.
Definition fims_vector.hpp:109
void clear()
Clears the contents.
Definition fims_vector.hpp:327
std::vector< Type >::size_type size_type
Definition fims_vector.hpp:44
const_reference back() const
Returns a constant reference to the last element in the container.
Definition fims_vector.hpp:237
friend bool operator==(const fims::Vector< T > &lhs, const fims::Vector< T > &rhs)
friend comparison operator. Allows the operator to see private members of fims::Vector<Type>.
Definition fims_vector.hpp:517
std::vector< Type >::reverse_iterator reverse_iterator
Definition fims_vector.hpp:58
iterator insert(const_iterator pos, size_type count, const Type &value)
Inserts count copies of the value before pos.
Definition fims_vector.hpp:339
std::ostream & operator<<(std::ostream &out, const fims::Vector< Type > &v)
Output for std::ostream& for a vector.
Definition fims_vector.hpp:531
bool operator==(const fims::Vector< T > &lhs, const fims::Vector< T > &rhs)
Comparison operator.
Definition fims_vector.hpp:517