FIMS  v0.8.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
196 inline reference front() { return this->vec_m.front(); }
197
201 inline const_reference front() const { return this->vec_m.front(); }
202
206 inline reference back() { return this->vec_m.back(); }
207
211 inline const_reference back() const { return this->vec_m.back(); }
212
216 inline pointer data() { return this->vec_m.data(); }
217
221 inline const_pointer data() const { return this->vec_m.data(); }
222
223 // iterators
224
228 inline iterator begin() { return this->vec_m.begin(); }
229
234 inline iterator end() { return this->vec_m.end(); }
235
240 inline reverse_iterator rbegin() { return this->vec_m.rbegin(); }
241
247 inline reverse_iterator rend() { return this->vec_m.rend(); }
248
254 inline const_reverse_iterator rbegin() const { return this->vec_m.rbegin(); }
255
261 inline const_reverse_iterator rend() const { return this->vec_m.rend(); }
262
263 // capacity
264
268 inline bool empty() { return this->vec_m.empty(); }
269
273 inline size_type size() const { return this->vec_m.size(); }
274
278 inline size_type max_size() const { return this->vec_m.max_size(); }
279
283 inline void reserve(size_type cap) { this->vec_m.reserve(cap); }
284
289 inline size_type capacity() { return this->vec_m.capacity(); }
290
294 inline void shrink_to_fit() { this->vec_m.shrink_to_fit(); }
295
296 // modifiers
297
301 inline void clear() { this->vec_m.clear(); }
302
306 inline iterator insert(const_iterator pos, const Type &value) {
307 return this->vec_m.insert(pos, value);
308 }
309
314 const Type &value) {
315 return this->vec_m.insert(pos, count, value);
316 }
317
321 template <class InputIt>
322 iterator insert(const_iterator pos, InputIt first, InputIt last) {
323 return this->vec_m.insert(pos, first, last);
324 }
325
330 iterator insert(const_iterator pos, std::initializer_list<Type> ilist) {
331 return this->vec_m.insert(pos, ilist);
332 }
333
337 template <class... Args>
338 iterator emplace(const_iterator pos, Args &&...args) {
339 return this->vec_m.emplace(pos, std::forward<Args>(args)...);
340 }
341
345 inline iterator erase(iterator pos) { return this->vec_m.erase(pos); }
346
350 inline iterator erase(iterator first, iterator last) {
351 return this->vec_m.erase(first, last);
352 }
353
357 inline void push_back(const Type &&value) { this->vec_m.push_back(value); }
358
362 template <class... Args>
363 void emplace_back(Args &&...args) {
364 this->vec_m.emplace_back(std::forward<Args>(args)...);
365 }
366
370 inline void pop_back() { this->vec_m.pop_back(); }
371
375 inline void resize(size_t s) { this->vec_m.resize(s); }
376
380 inline void swap(Vector &other) { this->vec_m.swap(other.vec_m); }
381
382 // end std::vector functions
383
391 inline operator std::vector<Type>() { return this->vec_m; }
392
393#ifdef TMB_MODEL
394
402 explicit operator tmbutils::vector<Type>() const {
403 tmbutils::vector<Type> ret(this->vec_m.size());
404 for (size_t i = 0; i < this->vec_m.size(); i++) {
405 ret[i] = this->vec_m[i];
406 }
407 return ret;
408 }
409
413 tmbutils::vector<Type> to_tmb() const {
414 tmbutils::vector<Type> ret(this->vec_m.size());
415 for (size_t i = 0; i < this->vec_m.size(); i++) {
416 ret[i] = this->vec_m[i];
417 }
418 return ret;
419 }
420
428 explicit operator tmbutils::vector<Type>() {
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() {
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
447#else
448
457 std::vector<Type> to_std() const { return this->vec_m; }
458
468 std::vector<Type> to_tmb() const { return this->vec_m; }
469#endif
470
476 std::string get_tag() const { return this->tag_m; }
481 void set_tag(const std::string &tag) { this->tag_m = tag; }
482
483 private:
484 std::string tag_m;
485}; // end fims::Vector class
486
490template <class T>
491bool operator==(const fims::Vector<T> &lhs, const fims::Vector<T> &rhs) {
492 return lhs.vec_m == rhs.vec_m;
493}
494
495} // namespace fims
496
504template <typename Type>
505std::ostream &operator<<(std::ostream &out, const fims::Vector<Type> &v) {
506 out << std::fixed << std::setprecision(10);
507 out << "[";
508
509 if (v.size() == 0) {
510 out << "]";
511 return out;
512 }
513 for (size_t i = 0; i < v.size() - 1; i++) {
514 if (v[i] != v[i]) {
515 out << "-999" << ",";
516 } else {
517 out << v[i] << ",";
518 }
519 }
520 if (v[v.size() - 1] != v[v.size() - 1]) {
521 out << "-999]";
522 } else {
523 out << v[v.size() - 1] << "]";
524 }
525 return out;
526}
527
528#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:338
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:278
reverse_iterator rend()
Returns a reverse iterator to the element following the last element of the reversed vector....
Definition fims_vector.hpp:247
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:201
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:283
iterator begin()
Returns an iterator to the first element of the vector.
Definition fims_vector.hpp:228
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:234
void shrink_to_fit()
Reduces memory usage by freeing unused memory.
Definition fims_vector.hpp:294
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:289
void emplace_back(Args &&...args)
Constructs an element in-place at the end.
Definition fims_vector.hpp:363
reference front()
Returns a reference to the first element in the container.
Definition fims_vector.hpp:196
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:476
std::vector< Type >::value_type value_type
Definition fims_vector.hpp:41
void swap(Vector &other)
Swaps the contents.
Definition fims_vector.hpp:380
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:357
iterator insert(const_iterator pos, InputIt first, InputIt last)
Inserts elements from range [first, last) before pos.
Definition fims_vector.hpp:322
bool empty()
Checks whether the container is empty.
Definition fims_vector.hpp:268
iterator erase(iterator pos)
Removes the element at pos.
Definition fims_vector.hpp:345
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:216
reference back()
Returns a reference to the last element in the container.
Definition fims_vector.hpp:206
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:261
void pop_back()
Removes the last element.
Definition fims_vector.hpp:370
void resize(size_t s)
Changes the number of elements stored.
Definition fims_vector.hpp:375
std::vector< Type > to_std() const
Convert fims::Vector to std::vector.
Definition fims_vector.hpp:457
std::vector< Type > to_tmb() const
Convert fims::Vector to TMB vector type.
Definition fims_vector.hpp:468
iterator erase(iterator first, iterator last)
Removes the elements in the range [first, last).
Definition fims_vector.hpp:350
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:254
iterator insert(const_iterator pos, const Type &value)
Inserts value before pos.
Definition fims_vector.hpp:306
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:330
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:240
const_pointer data() const
Returns a constant pointer to the underlying data array.
Definition fims_vector.hpp:221
size_type size() const
Returns the number of elements.
Definition fims_vector.hpp:273
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:481
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:301
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:211
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:491
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:313
std::ostream & operator<<(std::ostream &out, const fims::Vector< Type > &v)
Output for std::ostream& for a vector.
Definition fims_vector.hpp:505
bool operator==(const fims::Vector< T > &lhs, const fims::Vector< T > &rhs)
Comparison operator.
Definition fims_vector.hpp:491