12#ifndef NDARRAY_Vector_h_INCLUDED
13#define NDARRAY_Vector_h_INCLUDED
17#include <boost/type_traits/is_arithmetic.hpp>
18#include <boost/iterator/reverse_iterator.hpp>
19#include <boost/utility/enable_if.hpp>
20#include <boost/mpl/int.hpp>
21#include <boost/preprocessor/repetition/repeat.hpp>
22#include <boost/preprocessor/repetition/repeat_from_to.hpp>
23#include <boost/preprocessor/repetition/enum.hpp>
31#define NDARRAY_MAKE_VECTOR_MAX 8
33#define NDARRAY_MAKE_VECTOR_ARG_SPEC(Z,I,DATA) T v ## I
34#define NDARRAY_MAKE_VECTOR_SET_SPEC(Z,I,DATA) r[I] = v ## I;
36#define NDARRAY_MAKE_VECTOR_SPEC(Z,N,DATA) \
37 template <typename T> \
38 inline Vector<T,N> makeVector( \
39 BOOST_PP_ENUM(N,NDARRAY_MAKE_VECTOR_ARG_SPEC,unused) \
42 BOOST_PP_REPEAT(N,NDARRAY_MAKE_VECTOR_SET_SPEC,unused) \
52template <typename T, bool isArithmetic=boost::is_arithmetic<T>::value>
54 static T get() {
return T(); }
59 static T get() {
return T(0); }
86 typedef T & Reference;
87 typedef T
const & ConstReference;
89 typedef T
const * ConstIterator;
91 typedef Value value_type;
92 typedef Iterator iterator;
93 typedef ConstIterator const_iterator;
94 typedef Reference reference;
95 typedef ConstReference const_reference;
96 typedef boost::reverse_iterator<T*> reverse_iterator;
97 typedef boost::reverse_iterator<const T*> const_reverse_iterator;
99 typedef int difference_type;
100 typedef int size_type;
103 typedef boost::mpl::int_<N> ND;
105 size_type
size()
const {
return N; }
109 bool empty()
const {
return N==0; }
113 const_iterator
begin()
const {
return elems; }
115 iterator
end() {
return elems+N; }
117 const_iterator
end()
const {
return elems+N; }
119 reverse_iterator
rbegin() {
return reverse_iterator(
end()); }
121 const_reverse_iterator
rbegin()
const {
return const_reverse_iterator(
end()); }
123 reverse_iterator
rend() {
return reverse_iterator(
begin()); }
125 const_reverse_iterator
rend()
const {
return const_reverse_iterator(
begin()); }
128 reference
front() {
return *elems; }
130 reference
back() {
return *(elems+N-1); }
132 const_reference
front()
const {
return *elems; }
134 const_reference
back()
const {
return *(elems+N-1); }
142 template <
int Start,
int Stop>
145 std::copy(
begin() + Start,
begin()+Stop, r.begin());
166 std::copy(obj.
begin(), obj.
end(), std::ostream_iterator<T>(os,
","));
187 template <
typename U>
203 return !(*
this == other);
209 for (ConstIterator i =
begin(); i !=
end(); ++i) r += (*i);
216 for (ConstIterator i =
begin(); i !=
end(); ++i) r *= (*i);
228 template <
typename U>
231 for (
int i = 0; i < N; ++i) {
232 r[i] =
static_cast<U
>((*this)[i]);
239 template <
typename U>
240 typename boost::enable_if<boost::is_convertible<U,T>,
Vector&>::type
242 typename Vector<U,N>::ConstIterator j = other.
begin();
243 for (Iterator i =
begin(); i !=
end(); ++i, ++j) (*i) = (*j);
247 template <
typename U>
248 typename boost::enable_if<boost::is_convertible<U,T>,
Vector&>::type
250 for (Iterator i =
begin(); i !=
end(); ++i) (*i) = scalar;
255 template <
typename U>
256 typename boost::enable_if<boost::is_convertible<U,T>,
Vector&>::type
258 typename Vector<U,N>::ConstIterator j = other.
begin();
259 for (Iterator i =
begin(); i !=
end(); ++i, ++j) (*i) += (*j);
263 template <
typename U>
264 typename boost::enable_if<boost::is_convertible<U,T>,
Vector&>::type
266 for (Iterator i =
begin(); i !=
end(); ++i) (*i) += scalar;
271 template <
typename U>
272 typename boost::enable_if<boost::is_convertible<U,T>,
Vector&>::type
274 typename Vector<U,N>::ConstIterator j = other.
begin();
275 for (Iterator i =
begin(); i !=
end(); ++i, ++j) (*i) -= (*j);
279 template <
typename U>
280 typename boost::enable_if<boost::is_convertible<U,T>,
Vector&>::type
282 for (Iterator i =
begin(); i !=
end(); ++i) (*i) -= scalar;
287 template <
typename U>
288 typename boost::enable_if<boost::is_convertible<U,T>,
Vector&>::type
290 typename Vector<U,N>::ConstIterator j = other.
begin();
291 for (Iterator i =
begin(); i !=
end(); ++i, ++j) (*i) *= (*j);
295 template <
typename U>
296 typename boost::enable_if<boost::is_convertible<U,T>,
Vector&>::type
298 for (Iterator i =
begin(); i !=
end(); ++i) (*i) *= scalar;
303 template <
typename U>
304 typename boost::enable_if<boost::is_convertible<U,T>,
Vector&>::type
306 typename Vector<U,N>::ConstIterator j = other.
begin();
307 for (Iterator i =
begin(); i !=
end(); ++i, ++j) (*i) /= (*j);
311 template <
typename U>
312 typename boost::enable_if<boost::is_convertible<U,T>,
Vector&>::type
314 for (Iterator i =
begin(); i !=
end(); ++i) (*i) /= scalar;
319 template <
typename U>
320 typename boost::enable_if<boost::is_convertible<U,T>,
Vector&>::type
322 typename Vector<U,N>::ConstIterator j = other.
begin();
323 for (Iterator i =
begin(); i !=
end(); ++i, ++j) (*i) %= (*j);
327 template <
typename U>
328 typename boost::enable_if<boost::is_convertible<U,T>,
Vector&>::type
330 for (Iterator i =
begin(); i !=
end(); ++i) (*i) %= scalar;
335 template <
typename U>
336 typename boost::enable_if<boost::is_convertible<U,T>,
Vector&>::type
338 typename Vector<U,N>::ConstIterator j = other.
begin();
339 for (Iterator i =
begin(); i !=
end(); ++i, ++j) (*i) &= (*j);
343 template <
typename U>
344 typename boost::enable_if<boost::is_convertible<U,T>,
Vector&>::type
346 for (Iterator i =
begin(); i !=
end(); ++i) (*i) &= scalar;
351 template <
typename U>
352 typename boost::enable_if<boost::is_convertible<U,T>,
Vector&>::type
354 typename Vector<U,N>::ConstIterator j = other.
begin();
355 for (Iterator i =
begin(); i !=
end(); ++i, ++j) (*i) ^= (*j);
359 template <
typename U>
360 typename boost::enable_if<boost::is_convertible<U,T>,
Vector&>::type
362 for (Iterator i =
begin(); i !=
end(); ++i) (*i) ^= scalar;
367 template <
typename U>
368 typename boost::enable_if<boost::is_convertible<U,T>,
Vector&>::type
370 typename Vector<U,N>::ConstIterator j = other.
begin();
371 for (Iterator i =
begin(); i !=
end(); ++i, ++j) (*i) |= (*j);
375 template <
typename U>
376 typename boost::enable_if<boost::is_convertible<U,T>,
Vector&>::type
378 for (Iterator i =
begin(); i !=
end(); ++i) (*i) |= scalar;
383 template <
typename U>
384 typename boost::enable_if<boost::is_convertible<U,T>,
Vector&>::type
386 typename Vector<U,N>::ConstIterator j = other.
begin();
387 for (Iterator i =
begin(); i !=
end(); ++i, ++j) (*i) <<= (*j);
391 template <
typename U>
392 typename boost::enable_if<boost::is_convertible<U,T>,
Vector&>::type
394 for (Iterator i =
begin(); i !=
end(); ++i) (*i) <<= scalar;
399 template <
typename U>
400 typename boost::enable_if<boost::is_convertible<U,T>,
Vector&>::type
402 typename Vector<U,N>::ConstIterator j = other.
begin();
403 for (Iterator i =
begin(); i !=
end(); ++i, ++j) (*i) >>= (*j);
407 template <
typename U>
408 typename boost::enable_if<boost::is_convertible<U,T>,
Vector&>::type
410 for (Iterator i =
begin(); i !=
end(); ++i) (*i) >>= scalar;
423 typedef T & Reference;
424 typedef T
const & ConstReference;
425 typedef T * Iterator;
426 typedef T
const * ConstIterator;
428 typedef Value value_type;
429 typedef Iterator iterator;
430 typedef ConstIterator const_iterator;
431 typedef Reference reference;
432 typedef ConstReference const_reference;
433 typedef boost::reverse_iterator<T*> reverse_iterator;
434 typedef boost::reverse_iterator<const T*> const_reverse_iterator;
436 typedef int difference_type;
437 typedef int size_type;
440 typedef boost::mpl::int_<0> ND;
442 size_type
size()
const {
return 0; }
448 const_iterator
begin()
const {
return 0; }
450 iterator
end() {
return 0; }
452 const_iterator
end()
const {
return 0; }
454 reverse_iterator
rbegin() {
return reverse_iterator(); }
456 const_reverse_iterator
rbegin()
const {
return const_reverse_iterator(); }
458 reverse_iterator
rend() {
return reverse_iterator(); }
460 const_reverse_iterator
rend()
const {
return const_reverse_iterator(); }
463 reference
front() { NDARRAY_ASSERT(
false);
return 0; }
465 reference
back() {
return NDARRAY_ASSERT(
false);
return 0; }
467 const_reference
front()
const { NDARRAY_ASSERT(
false);
return 0; }
469 const_reference
back()
const { NDARRAY_ASSERT(
false);
return 0; }
472 reference
operator[](
int i) { NDARRAY_ASSERT(
false);
return 0; }
474 const_reference
operator[](
int i)
const { NDARRAY_ASSERT(
false);
return 0; }
477 template <
int Start,
int Stop>
479 return Vector<T,Stop-Start>();
508 template <
typename U>
518 T
sum()
const {
return 0; }
527 template <
typename U>
534template <
typename T,
int N,
int M>
543template <
typename T,
int N,
typename U>
544inline typename boost::enable_if<boost::is_convertible<U,T>,Vector<T,N+1> >::type
553template <
typename T,
int N,
typename U>
554inline typename boost::enable_if<boost::is_convertible<U,T>,Vector<T,N+1> >::type
563BOOST_PP_REPEAT_FROM_TO(1, NDARRAY_MAKE_VECTOR_MAX, NDARRAY_MAKE_VECTOR_SPEC, unused)
570template <
typename T,
int N>
571Vector<T,N> makeVector(T v1, T v2, ..., T vN);
575template <
typename T,
int N>
578 for (
typename Vector<T,N>::Iterator i = r.
begin(); i != r.
end(); ++i) (*i) = ~(*i);
583template <
typename T,
int N>
586 for (
typename Vector<T,N>::Iterator i = r.
begin(); i != r.
end(); ++i) (*i) = !(*i);
592 template <
typename T,
typename U,
int N>
593 Vector<typename Promote<T,U>::Type,N>
599 template <
typename T,
typename U,
int N>
600 Vector<typename Promote<T,U>::Type,N>
606 template <
typename T,
typename U,
int N>
607 Vector<typename Promote<T,U>::Type,N>
614 template <
typename T,
typename U,
int N>
615 Vector<typename Promote<T,U>::Type,N>
621 template <
typename T,
typename U,
int N>
622 Vector<typename Promote<T,U>::Type,N>
628 template <
typename T,
typename U,
int N>
629 Vector<typename Promote<T,U>::Type,N>
636 template <
typename T,
typename U,
int N>
637 Vector<typename Promote<T,U>::Type,N>
643 template <
typename T,
typename U,
int N>
644 Vector<typename Promote<T,U>::Type,N>
650 template <
typename T,
typename U,
int N>
651 Vector<typename Promote<T,U>::Type,N>
658 template <
typename T,
typename U,
int N>
659 Vector<typename Promote<T,U>::Type,N>
665 template <
typename T,
typename U,
int N>
666 Vector<typename Promote<T,U>::Type,N>
672 template <
typename T,
typename U,
int N>
673 Vector<typename Promote<T,U>::Type,N>
680 template <
typename T,
typename U,
int N>
681 Vector<typename Promote<T,U>::Type,N>
687 template <
typename T,
typename U,
int N>
688 Vector<typename Promote<T,U>::Type,N>
694 template <
typename T,
typename U,
int N>
695 Vector<typename Promote<T,U>::Type,N>
702 template <
typename T,
typename U,
int N>
703 Vector<typename Promote<T,U>::Type,N>
709 template <
typename T,
typename U,
int N>
710 Vector<typename Promote<T,U>::Type,N>
716 template <
typename T,
typename U,
int N>
717 Vector<typename Promote<T,U>::Type,N>
724 template <
typename T,
typename U,
int N>
725 Vector<typename Promote<T,U>::Type,N>
731 template <
typename T,
typename U,
int N>
732 Vector<typename Promote<T,U>::Type,N>
738 template <
typename T,
typename U,
int N>
739 Vector<typename Promote<T,U>::Type,N>
746 template <
typename T,
typename U,
int N>
747 Vector<typename Promote<T,U>::Type,N>
753 template <
typename T,
typename U,
int N>
754 Vector<typename Promote<T,U>::Type,N>
760 template <
typename T,
typename U,
int N>
761 Vector<typename Promote<T,U>::Type,N>
768 template <
typename T,
typename U,
int N>
769 Vector<typename Promote<T,U>::Type,N>
770 operator <<(Vector<T,N>
const & a, Vector<U,N>
const & b) {
771 Vector<typename Promote<T,U>::Type,N> r(a);
775 template <
typename T,
typename U,
int N>
776 Vector<typename Promote<T,U>::Type,N>
777 operator <<(Vector<T,N>
const & a, U b) {
778 Vector<typename Promote<T,U>::Type,N> r(a);
782 template <
typename T,
typename U,
int N>
783 Vector<typename Promote<T,U>::Type,N>
784 operator <<(U a, Vector<T,N>
const & b) {
785 Vector<typename Promote<T,U>::Type,N> r(a);
790 template <
typename T,
typename U,
int N>
791 Vector<typename Promote<T,U>::Type,N>
797 template <
typename T,
typename U,
int N>
798 Vector<typename Promote<T,U>::Type,N>
804 template <
typename T,
typename U,
int N>
805 Vector<typename Promote<T,U>::Type,N>
Vector< T, N > operator!(Vector< T, N > const &vector)
Unary negation for Vector.
Definition Vector.h:584
Vector< T, N+M > concatenate(Vector< T, N > const &a, Vector< T, M > const &b)
Concatenate two Vectors into a single long Vector.
Definition Vector.h:535
Forward declarations and default template parameters for ndarray.
size_type max_size() const
Return the size of the Vector.
Definition Vector.h:443
reverse_iterator rbegin()
Return a reverse_iterator to the beginning of the reversed Vector.
Definition Vector.h:454
Vector< U, 0 > cast() const
Cast the vector element-wise to another type.
Definition Vector.h:528
Vector reverse() const
Return a Vector with the elements reversed.
Definition Vector.h:524
const_reverse_iterator rbegin() const
Return a const_reverse_iterator to the beginning of the reversed Vector.
Definition Vector.h:456
const_reverse_iterator rend() const
Return a const_reverse_iterator to the end of the reversed Vector.
Definition Vector.h:460
Vector< T, M > first() const
Create a new Vector from the first M elements of this.
Definition Vector.h:483
bool empty() const
Return true if size() == 0.
Definition Vector.h:444
reference operator[](int i)
Return a reference to the element with the given index.
Definition Vector.h:472
Vector()
Default constructor.
Definition Vector.h:502
size_type size() const
Return the size of the Vector.
Definition Vector.h:442
iterator begin()
Return an iterator to the beginning of the Vector.
Definition Vector.h:446
T product() const
Return the product of all elements.
Definition Vector.h:521
friend std::ostream & operator<<(std::ostream &os, Vector< T, 0 > const &obj)
Stream output.
Definition Vector.h:493
const_reference back() const
Return a const_reference to the last element.
Definition Vector.h:469
const_reference operator[](int i) const
Return a const_reference to the element with the given index.
Definition Vector.h:474
reference front()
Return a reference to the first element.
Definition Vector.h:463
T sum() const
Return the sum of all elements.
Definition Vector.h:518
const_iterator end() const
Return a const_iterator to the end of the Vector.
Definition Vector.h:452
Vector(T scalar)
Construct with copies of a scalar.
Definition Vector.h:505
reverse_iterator rend()
Return a reverse_iterator to the end of the reversed Vector.
Definition Vector.h:458
Vector(Vector< U, 0 > const &other)
Converting copy constructor.
Definition Vector.h:509
Vector< T, M > last() const
Create a new Vector from the last M elements of this.
Definition Vector.h:488
const_reference front() const
Return a const_reference to the first element.
Definition Vector.h:467
bool operator!=(Vector const &other) const
Return false if any elements of other are not equal to the elements of this.
Definition Vector.h:515
const_iterator begin() const
Return a const_iterator to the beginning of the Vector.
Definition Vector.h:448
Vector< T, Stop-Start > getRange() const
Create a new Vector that is a subset of this.
Definition Vector.h:478
reference back()
Return a reference to the last element.
Definition Vector.h:465
iterator end()
Return an iterator to the end of the Vector.
Definition Vector.h:450
bool operator==(Vector const &other) const
Return true if elements of other are equal to the elements of this.
Definition Vector.h:512
A fixed-size 1D array class.
Definition Vector.h:82
Vector(T scalar)
Construct with copies of a scalar.
Definition Vector.h:182
boost::enable_if< boost::is_convertible< U, T >, Vector & >::type operator+=(Vector< U, N > const &other)
Augmented += assignment from another vector.
Definition Vector.h:257
Vector< T, M > first() const
Create a new Vector from the first M elements of this.
Definition Vector.h:150
reverse_iterator rend()
Return a reverse_iterator to the end of the reversed Vector.
Definition Vector.h:123
friend std::ostream & operator<<(std::ostream &os, Vector< T, N > const &obj)
Stream output.
Definition Vector.h:164
boost::enable_if< boost::is_convertible< U, T >, Vector & >::type operator/=(Vector< U, N > const &other)
Augmented /= assignment from another vector.
Definition Vector.h:305
Vector()
Default constructor.
Definition Vector.h:175
T product() const
Return the product of all elements.
Definition Vector.h:214
boost::enable_if< boost::is_convertible< U, T >, Vector & >::type operator|=(Vector< U, N > const &other)
Augmented |= assignment from another vector.
Definition Vector.h:369
boost::enable_if< boost::is_convertible< U, T >, Vector & >::type operator%=(Vector< U, N > const &other)
Augmented %= assignment from another vector.
Definition Vector.h:321
T sum() const
Return the sum of all elements.
Definition Vector.h:207
iterator begin()
Return an iterator to the beginning of the Vector.
Definition Vector.h:111
const_reverse_iterator rbegin() const
Return a const_reverse_iterator to the beginning of the reversed Vector.
Definition Vector.h:121
boost::enable_if< boost::is_convertible< U, T >, Vector & >::type operator*=(Vector< U, N > const &other)
Augmented *= assignment from another vector.
Definition Vector.h:289
boost::enable_if< boost::is_convertible< U, T >, Vector & >::type operator&=(Vector< U, N > const &other)
Augmented &= assignment from another vector.
Definition Vector.h:337
Vector< T, Stop-Start > getRange() const
Create a new Vector that is a subset of this.
Definition Vector.h:143
const_reference operator[](int i) const
Return a const_reference to the element with the given index.
Definition Vector.h:139
Vector(Vector< U, N > const &other)
Converting copy constructor.
Definition Vector.h:188
boost::enable_if< boost::is_convertible< U, T >, Vector & >::type operator<<=(Vector< U, N > const &other)
Augmented <<= assignment from another vector.
Definition Vector.h:385
bool operator!=(Vector const &other) const
Return false if any elements of other are not equal to the elements of this.
Definition Vector.h:202
reference operator[](int i)
Return a reference to the element with the given index.
Definition Vector.h:137
const_iterator begin() const
Return a const_iterator to the beginning of the Vector.
Definition Vector.h:113
boost::enable_if< boost::is_convertible< U, T >, Vector & >::type operator>>=(Vector< U, N > const &other)
Augmented >>= assignment from another vector.
Definition Vector.h:401
Vector reverse() const
Return a Vector with the elements reversed.
Definition Vector.h:221
Vector< T, M > last() const
Create a new Vector from the last M elements of this.
Definition Vector.h:157
const_reference back() const
Return a const_reference to the last element.
Definition Vector.h:134
size_type size() const
Return the size of the Vector.
Definition Vector.h:105
const_iterator end() const
Return a const_iterator to the end of the Vector.
Definition Vector.h:117
size_type max_size() const
Return the size of the Vector.
Definition Vector.h:107
const_reverse_iterator rend() const
Return a const_reverse_iterator to the end of the reversed Vector.
Definition Vector.h:125
reverse_iterator rbegin()
Return a reverse_iterator to the beginning of the reversed Vector.
Definition Vector.h:119
reference back()
Return a reference to the last element.
Definition Vector.h:130
bool operator==(Vector const &other) const
Return true if elements of other are equal to the elements of this.
Definition Vector.h:197
iterator end()
Return an iterator to the end of the Vector.
Definition Vector.h:115
boost::enable_if< boost::is_convertible< U, T >, Vector & >::type operator=(Vector< U, N > const &other)
Augmented = assignment from another vector.
Definition Vector.h:241
boost::enable_if< boost::is_convertible< U, T >, Vector & >::type operator^=(Vector< U, N > const &other)
Augmented ^= assignment from another vector.
Definition Vector.h:353
Vector< U, N > cast() const
Cast the vector element-wise to another type.
Definition Vector.h:229
boost::enable_if< boost::is_convertible< U, T >, Vector & >::type operator-=(Vector< U, N > const &other)
Augmented -= assignment from another vector.
Definition Vector.h:273
reference front()
Return a reference to the first element.
Definition Vector.h:128
const_reference front() const
Return a const_reference to the first element.
Definition Vector.h:132