- 3.0.2 core module.
DiscreteArray.h
Go to the documentation of this file.
1 /**********************************************************************************************************************
2 This file is part of the Control Toolbox (https://github.com/ethz-adrl/control-toolbox), copyright by ETH Zurich.
3 Licensed under the BSD-2 license (see LICENSE file in main directory)
4 **********************************************************************************************************************/
5 
6 #pragma once
7 
8 #include <vector>
9 #include <iostream>
10 
11 namespace ct {
12 namespace core {
13 
15 
21 template <class T, class Alloc = Eigen::aligned_allocator<T>>
22 class DiscreteArray : private std::vector<T, Alloc>
23 { // private since we only want to expose selective functionality
24 public:
25  EIGEN_MAKE_ALIGNED_OPERATOR_NEW
26 
27  typedef T value_type;
28  typedef std::vector<T, Alloc> Base;
29  typedef typename std::vector<T, Alloc>::iterator iterator;
30  typedef typename std::vector<T, Alloc>::const_iterator const_iterator;
31 
33  DiscreteArray() : Base(Alloc()){};
34 
36  DiscreteArray(const DiscreteArray&& other) : Base(static_cast<const Base&&>(other)) {}
38  DiscreteArray(const DiscreteArray& other) : Base(static_cast<const Base&>(other)){};
39 
41 
46  DiscreteArray(int n, const T& value = T()) : Base(n, value){};
47 
49 
54  DiscreteArray(iterator first, iterator last) : Base(first, last){};
55 
57 
62  DiscreteArray(const_iterator first, const_iterator last) : Base(first, last){};
63 
65  virtual ~DiscreteArray(){};
66 
67  using Base::operator[];
68  using Base::at;
69  using Base::back;
70  using Base::begin;
71  using Base::clear;
72  using Base::end;
73  using Base::front;
74  using Base::pop_back;
75  using Base::push_back;
76  using Base::reserve;
77  using Base::resize;
78  using Base::size;
79 
81 
84  void swap(DiscreteArray& other) { Base::swap(static_cast<Base&>(other)); }
87  {
88  // Check for self-assignment!
89  if (this == &rhs)
90  return *this;
91 
92  Base::operator=(static_cast<const Base&>(rhs));
93 
94  return *this;
95  }
96 
99  {
100  if (this->size() != rhs.size())
101  throw std::runtime_error("DiscreteArray.h (operator +): lhs.size() != rhs.size()");
102 
103  DiscreteArray<T, Alloc> result(this->size());
104 
105  std::transform(this->begin(), this->end(), rhs.begin(), result.begin(), std::plus<T>());
106  return result;
107  }
108 
111  {
112  if (this->size() != rhs.size())
113  throw std::runtime_error("DiscreteArray.h (operator -): lhs.size() != rhs.size()");
114 
115  DiscreteArray<T, Alloc> result(this->size());
116 
117  std::transform(this->begin(), this->end(), rhs.begin(), result.begin(), std::minus<T>());
118  return result;
119  }
120 
123  {
124  if (this->size() != rhs.size())
125  throw std::runtime_error("DiscreteArray.h (operator +=): lhs.size() != rhs.size()");
126 
127  std::transform(this->begin(), this->end(), rhs.begin(), this->begin(), std::plus<T>());
128  return *this;
129  }
130 
133  {
134  if (this->size() != rhs.size())
135  throw std::runtime_error("DiscreteArray.h (operator -=): lhs.size() != rhs.size()");
136 
137  std::transform(this->begin(), this->end(), rhs.begin(), this->begin(), std::minus<T>());
138  return *this;
139  }
140 
142  template <typename SCALAR>
143  inline DiscreteArray<T, Alloc> operator*(const SCALAR& scalar) const
144  {
145  DiscreteArray<T, Alloc> result(this->size());
146  std::transform(this->begin(), this->end(), result.begin(), [scalar](T arg) { return arg * scalar; });
147  return result;
148  }
149 
151  template <typename SCALAR>
152  inline DiscreteArray<T, Alloc> operator/(const SCALAR& scalar) const
153  {
154  DiscreteArray<T, Alloc> result(this->size());
155  std::transform(this->begin(), this->end(), result.begin(), [scalar](T arg) { return arg / scalar; });
156  return result;
157  }
158 
160  Base& toImplementation() { return *this; }
162  const Base& toImplementation() const { return *this; }
164  void eraseFront(const size_t N) { this->erase(this->begin(), this->begin() + N); }
166  void setConstant(const T& data) { std::fill(this->begin(), this->end(), data); }
168  void addOffset(const T& offset)
169  {
170  std::for_each(this->begin(), this->end(), [&](T& val) { val += offset; });
171  }
172 };
173 
174 } /* namespace core */
175 } /* namespace ct */
virtual ~DiscreteArray()
destructor
Definition: DiscreteArray.h:65
An discrete array (vector) of a particular data type.
Definition: DiscreteArray.h:22
DiscreteArray(const DiscreteArray &&other)
move constructor
Definition: DiscreteArray.h:36
DiscreteArray(iterator first, iterator last)
copy constructor
Definition: DiscreteArray.h:54
void setConstant(const T &data)
sets all elements to a constant.
Definition: DiscreteArray.h:166
DiscreteArray< T, Alloc > & operator-=(const DiscreteArray< T, Alloc > &rhs)
overload -= operator
Definition: DiscreteArray.h:132
void eraseFront(const size_t N)
erase an element from the front
Definition: DiscreteArray.h:164
std::vector< T, Alloc >::iterator iterator
iterator
Definition: DiscreteArray.h:29
DiscreteArray(const DiscreteArray &other)
copy constructor
Definition: DiscreteArray.h:38
const Base & toImplementation() const
returns the underlying std::vector
Definition: DiscreteArray.h:162
DiscreteArray(int n, const T &value=T())
resize constructor
Definition: DiscreteArray.h:46
DiscreteArray< T, Alloc > operator*(const SCALAR &scalar) const
overload * operator
Definition: DiscreteArray.h:143
CppAD::AD< CppAD::cg::CG< double > > SCALAR
constexpr size_t n
Definition: MatrixInversionTest.cpp:14
std::vector< T, Alloc >::const_iterator const_iterator
const iterator
Definition: DiscreteArray.h:30
DiscreteArray< T, Alloc > operator/(const SCALAR &scalar) const
overload / operator
Definition: DiscreteArray.h:152
DiscreteArray()
default constructor
Definition: DiscreteArray.h:33
std::vector< T, Alloc > Base
base class (std::vector)
Definition: DiscreteArray.h:28
DiscreteArray< T, Alloc > operator+(const DiscreteArray< T, Alloc > &rhs) const
overload + operator in order to be able to directly sum up two arrays
Definition: DiscreteArray.h:98
DiscreteArray(const_iterator first, const_iterator last)
copy constructor
Definition: DiscreteArray.h:62
DiscreteArray< T, Alloc > & operator=(const DiscreteArray< T, Alloc > &rhs)
Definition: DiscreteArray.h:86
Base & toImplementation()
returns the underlying std::vector
Definition: DiscreteArray.h:160
void addOffset(const T &offset)
add an offset to each element
Definition: DiscreteArray.h:168
EIGEN_MAKE_ALIGNED_OPERATOR_NEW typedef T value_type
data type
Definition: DiscreteArray.h:27
DiscreteArray< T, Alloc > & operator+=(const DiscreteArray< T, Alloc > &rhs)
overload += operator
Definition: DiscreteArray.h:122
void swap(DiscreteArray &other)
swaps the content of two arrays
Definition: DiscreteArray.h:84
DiscreteArray< T, Alloc > operator-(const DiscreteArray< T, Alloc > &rhs) const
overload - operator in order to be able to directly take the difference between two arrays ...
Definition: DiscreteArray.h:110