1 #ifndef CPPAD_CG_ARRAY_VIEW_INCLUDED 2 #define CPPAD_CG_ARRAY_VIEW_INCLUDED 32 using value_type = Type;
33 using pointer = value_type*;
34 using const_pointer =
const value_type*;
35 using reference = value_type&;
36 using const_reference =
const Type&;
37 using iterator = value_type*;
38 using const_iterator =
const value_type*;
39 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
40 using reverse_iterator = std::reverse_iterator<iterator>;
41 using size_type = size_t;
42 using difference_type = ptrdiff_t;
71 CPPAD_ASSERT_KNOWN(array !=
nullptr || n == 0,
"ArrayView: null array with a non-zero size");
83 _length(vector.
size()) {
95 _length(vector.
size()) {
104 template<std::
size_t S>
117 _data(array.
size() > 0 ? &array[0] : nullptr),
118 _length(array.
size()) {
127 template<
class TT = Type>
129 typename std::enable_if<std::is_const<TT>::value>::type* = 0) :
131 _length(array.
size()) {
141 template<
class TT = Type>
142 inline ArrayView(
const std::vector<
typename std::remove_const<value_type>::type>&
vector,
143 typename std::enable_if<std::is_const<TT>::value>::type* = 0) :
155 template<
class TT = Type>
157 typename std::enable_if<std::is_const<TT>::value>::type* = 0) :
168 template<std::
size_t S,
class TT = Type>
169 inline ArrayView(
const std::array<
typename std::remove_const<value_type>::type, S>& array,
170 typename std::enable_if<std::is_const<TT>::value>::type* = 0) :
181 template<
class TT = Type>
182 inline ArrayView(
const std::valarray<
typename std::remove_const<value_type>::type>& array,
183 typename std::enable_if<std::is_const<TT>::value>::type* = 0) :
184 _data(array.
size() > 0 ? &array[0] : nullptr),
185 _length(array.
size()) {
202 inline size_t size() const noexcept {
213 inline bool empty()
const noexcept {
220 inline pointer
data() noexcept {
227 inline const_pointer
data() const noexcept {
231 inline void fill(
const value_type& u) {
232 std::fill_n(begin(),
size(), u);
239 CPPADCG_ASSERT_KNOWN(n <=
size(),
"ArrayView::head() size must be equal to or greater than the array size");
247 CPPADCG_ASSERT_KNOWN(n <=
size(),
"ArrayView::head() size must be equal to or greater than the array size");
255 CPPADCG_ASSERT_KNOWN(n <=
size(),
"ArrayView::tail() size must be equal to or greater than the array size");
263 CPPADCG_ASSERT_KNOWN(n <=
size(),
"ArrayView::tail() size must be equal to or greater than the array size");
272 CPPADCG_ASSERT_KNOWN(start <
size(),
"ArrayView::segment() start index must be lower than the array size");
273 CPPADCG_ASSERT_KNOWN(start + n <=
size(),
"ArrayView::segment() the new segment will end after the end of this array");
282 CPPADCG_ASSERT_KNOWN(start <
size(),
"ArrayView::segment() start index must be lower than the array size");
283 CPPADCG_ASSERT_KNOWN(start + n <=
size(),
"ArrayView::segment() the new segment will end after the end of this array");
287 inline void swap(
ArrayView& other) noexcept {
288 std::swap(other._data, _data);
289 std::swap(other._length, _length);
293 inline iterator begin() noexcept {
294 return iterator(
data());
297 inline const_iterator begin()
const noexcept {
298 return const_iterator(
data());
301 inline iterator end() noexcept {
305 inline const_iterator end()
const noexcept {
306 return const_iterator(
data() +
size());
309 inline reverse_iterator rbegin() noexcept {
310 return reverse_iterator(end());
313 inline const_reverse_iterator rbegin()
const noexcept {
314 return const_reverse_iterator(end());
317 inline reverse_iterator rend() noexcept {
318 return reverse_iterator(begin());
321 inline const_reverse_iterator rend()
const noexcept {
322 return const_reverse_iterator(begin());
325 inline const_iterator cbegin()
const noexcept {
326 return const_iterator(
data());
329 inline const_iterator cend()
const noexcept {
330 return const_iterator(
data() +
size());
333 inline const_reverse_iterator crbegin()
const noexcept {
334 return const_reverse_iterator(end());
337 inline const_reverse_iterator crend()
const noexcept {
338 return const_reverse_iterator(begin());
342 inline reference operator[](size_type i) {
343 CPPADCG_ASSERT_KNOWN(i <
size(),
"ArrayView::operator[] index is equal to or greater than the array size");
348 CPPADCG_ASSERT_KNOWN(i <
size(),
"ArrayView::operator[] index is equal to or greater than the array size");
352 inline reference at(size_type i) {
354 throw CGException(
"ArrayView::at() index ", i,
" is equal to or greater than the array size ",
size());
360 throw CGException(
"ArrayView::at() index ", i,
" is equal to or greater than the array size ",
size());
364 inline reference front() {
365 CPPADCG_ASSERT_KNOWN(!empty(),
"ArrayView: cannot call front for an empty array");
370 CPPADCG_ASSERT_KNOWN(!empty(),
"ArrayView: cannot call front for an empty array");
374 inline reference back() {
375 CPPADCG_ASSERT_KNOWN(!empty(),
"ArrayView: cannot call back for an empty array");
380 CPPADCG_ASSERT_KNOWN(!empty(),
"ArrayView: cannot call back for an empty array");
381 return _data[
size() - 1];
390 throw CGException(
"ArrayView: assigning an array with different size: the left hand side array has the size ",
size(),
391 " while the right hand side array has the size ", x.
size(),
".");
393 for(
size_t i = 0; i < _length; ++i) {
394 _data[i] = x._data[i];
400 template <
typename TT =
const Type,
401 typename =
typename std::enable_if<!std::is_same<Type, TT>::value && std::is_assignable<Type&, TT&>::value>::type >
404 throw CGException(
"ArrayView: assigning an array with different size: the left hand side array has the size ",
size(),
405 " while the right hand side array has the size ", x.
size(),
".");
407 const auto* dd = x.
data();
408 for (
size_t i = 0; i < _length; ++i) {
415 inline ArrayView& operator=(
const std::vector<Type>& x) {
416 if (x.size() !=
size())
417 throw CGException(
"ArrayView: assigning an array with different size: the left hand side array has the size ",
size(),
418 " while the right hand side array has the size ", x.size(),
".");
420 for(
size_t i = 0; i < _length; ++i) {
428 if (x.size() !=
size())
429 throw CGException(
"ArrayView: assigning an array with different size: the left hand side array has the size ",
size(),
430 " while the right hand side array has the size ", x.size(),
".");
432 for(
size_t i = 0; i < _length; ++i) {
449 inline std::ostream& operator<<(std::ostream& os,
452 size_t n = array.
size();
size_type max_size() const noexcept
ArrayView< value_type > tail(size_t n)
ArrayView(pointer array, size_type n)
virtual ~ArrayView()=default
ArrayView(const std::array< typename std::remove_const< value_type >::type, S > &array, typename std::enable_if< std::is_const< TT >::value >::type *=0)
ArrayView< const value_type > head(size_t n) const
ArrayView(std::vector< value_type > &vector)
ArrayView(const CppAD::vector< typename std::remove_const< value_type >::type > &vector, typename std::enable_if< std::is_const< TT >::value >::type *=0)
ArrayView(std::array< value_type, S > &array)
const_pointer data() const noexcept
ArrayView(const std::vector< typename std::remove_const< value_type >::type > &vector, typename std::enable_if< std::is_const< TT >::value >::type *=0)
ArrayView< const value_type > segment(size_t start, size_t n) const
ArrayView< value_type > segment(size_t start, size_t n)
ArrayView(CppAD::vector< value_type > &vector)
ArrayView(const ArrayView< typename std::remove_const< value_type >::type > &array, typename std::enable_if< std::is_const< TT >::value >::type *=0)
ArrayView(const std::valarray< typename std::remove_const< value_type >::type > &array, typename std::enable_if< std::is_const< TT >::value >::type *=0)
size_t size() const noexcept
ArrayView(std::valarray< value_type > &array)
ArrayView< const value_type > tail(size_t n) const
ArrayView< value_type > head(size_t n)