Eurobot2012_Primary

Dependencies:   mbed Eurobot_2012_Primary

Committer:
narshu
Date:
Wed Oct 17 22:22:47 2012 +0000
Revision:
26:0995f61cb7b8
Parent:
25:143b19c1fb05
Eurobot 2012 Primary;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
narshu 25:143b19c1fb05 1 /*
narshu 25:143b19c1fb05 2 * Tiny Vector Matrix Library
narshu 25:143b19c1fb05 3 * Dense Vector Matrix Libary of Tiny size using Expression Templates
narshu 25:143b19c1fb05 4 *
narshu 25:143b19c1fb05 5 * Copyright (C) 2001 - 2007 Olaf Petzold <opetzold@users.sourceforge.net>
narshu 25:143b19c1fb05 6 *
narshu 25:143b19c1fb05 7 * This library is free software; you can redistribute it and/or
narshu 25:143b19c1fb05 8 * modify it under the terms of the GNU lesser General Public
narshu 25:143b19c1fb05 9 * License as published by the Free Software Foundation; either
narshu 25:143b19c1fb05 10 * version 2.1 of the License, or (at your option) any later version.
narshu 25:143b19c1fb05 11 *
narshu 25:143b19c1fb05 12 * This library is distributed in the hope that it will be useful,
narshu 25:143b19c1fb05 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
narshu 25:143b19c1fb05 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
narshu 25:143b19c1fb05 15 * lesser General Public License for more details.
narshu 25:143b19c1fb05 16 *
narshu 25:143b19c1fb05 17 * You should have received a copy of the GNU lesser General Public
narshu 25:143b19c1fb05 18 * License along with this library; if not, write to the Free Software
narshu 25:143b19c1fb05 19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
narshu 25:143b19c1fb05 20 *
narshu 25:143b19c1fb05 21 * $Id: Vector.h,v 1.48 2007-06-23 15:58:58 opetzold Exp $
narshu 25:143b19c1fb05 22 */
narshu 25:143b19c1fb05 23
narshu 25:143b19c1fb05 24 #ifndef TVMET_VECTOR_H
narshu 25:143b19c1fb05 25 #define TVMET_VECTOR_H
narshu 25:143b19c1fb05 26
narshu 25:143b19c1fb05 27 #include <iterator> // reverse_iterator
narshu 25:143b19c1fb05 28
narshu 25:143b19c1fb05 29 #include <tvmet/tvmet.h>
narshu 25:143b19c1fb05 30 #include <tvmet/TypePromotion.h>
narshu 25:143b19c1fb05 31 #include <tvmet/CommaInitializer.h>
narshu 25:143b19c1fb05 32 #include <tvmet/RunTimeError.h>
narshu 25:143b19c1fb05 33
narshu 25:143b19c1fb05 34 #include <tvmet/xpr/Vector.h>
narshu 25:143b19c1fb05 35
narshu 25:143b19c1fb05 36 namespace tvmet {
narshu 25:143b19c1fb05 37
narshu 25:143b19c1fb05 38
narshu 25:143b19c1fb05 39 /* forwards */
narshu 25:143b19c1fb05 40 template<class T, std::size_t Sz> class Vector;
narshu 25:143b19c1fb05 41
narshu 25:143b19c1fb05 42
narshu 25:143b19c1fb05 43 /**
narshu 25:143b19c1fb05 44 * \class VectorConstReference Vector.h "tvmet/Vector.h"
narshu 25:143b19c1fb05 45 * \brief Const value iterator for ET
narshu 25:143b19c1fb05 46 */
narshu 25:143b19c1fb05 47 template<class T, std::size_t Sz>
narshu 25:143b19c1fb05 48 class VectorConstReference
narshu 25:143b19c1fb05 49 : public TvmetBase< VectorConstReference<T, Sz> >
narshu 25:143b19c1fb05 50 {
narshu 25:143b19c1fb05 51 public: // types
narshu 25:143b19c1fb05 52 typedef T value_type;
narshu 25:143b19c1fb05 53 typedef T* pointer;
narshu 25:143b19c1fb05 54 typedef const T* const_pointer;
narshu 25:143b19c1fb05 55
narshu 25:143b19c1fb05 56 public:
narshu 25:143b19c1fb05 57 /** Dimensions. */
narshu 25:143b19c1fb05 58 enum {
narshu 25:143b19c1fb05 59 Size = Sz /**< The size of the vector. */
narshu 25:143b19c1fb05 60 };
narshu 25:143b19c1fb05 61
narshu 25:143b19c1fb05 62 public:
narshu 25:143b19c1fb05 63 /** Complexity counter. */
narshu 25:143b19c1fb05 64 enum {
narshu 25:143b19c1fb05 65 ops = Size
narshu 25:143b19c1fb05 66 };
narshu 25:143b19c1fb05 67
narshu 25:143b19c1fb05 68 private:
narshu 25:143b19c1fb05 69 VectorConstReference();
narshu 25:143b19c1fb05 70 VectorConstReference& operator=(const VectorConstReference&);
narshu 25:143b19c1fb05 71
narshu 25:143b19c1fb05 72 public:
narshu 25:143b19c1fb05 73 /** Constructor. */
narshu 25:143b19c1fb05 74 explicit VectorConstReference(const Vector<T, Size>& rhs)
narshu 25:143b19c1fb05 75 : m_data(rhs.data())
narshu 25:143b19c1fb05 76 { }
narshu 25:143b19c1fb05 77
narshu 25:143b19c1fb05 78 /** Constructor by a given memory pointer. */
narshu 25:143b19c1fb05 79 explicit VectorConstReference(const_pointer data)
narshu 25:143b19c1fb05 80 : m_data(data)
narshu 25:143b19c1fb05 81 { }
narshu 25:143b19c1fb05 82
narshu 25:143b19c1fb05 83 public: // access operators
narshu 25:143b19c1fb05 84 /** access by index. */
narshu 25:143b19c1fb05 85 value_type operator()(std::size_t i) const {
narshu 25:143b19c1fb05 86 TVMET_RT_CONDITION(i < Size, "VectorConstReference Bounce Violation")
narshu 25:143b19c1fb05 87 return m_data[i];
narshu 25:143b19c1fb05 88 }
narshu 25:143b19c1fb05 89
narshu 25:143b19c1fb05 90 public: // debugging Xpr parse tree
narshu 25:143b19c1fb05 91 void print_xpr(std::ostream& os, std::size_t l=0) const {
narshu 25:143b19c1fb05 92 os << IndentLevel(l)
narshu 25:143b19c1fb05 93 << "VectorConstReference[O=" << ops << "]<"
narshu 25:143b19c1fb05 94 << "T=" << typeid(T).name() << ">,"
narshu 25:143b19c1fb05 95 << std::endl;
narshu 25:143b19c1fb05 96 }
narshu 25:143b19c1fb05 97
narshu 25:143b19c1fb05 98 private:
narshu 25:143b19c1fb05 99 const_pointer _tvmet_restrict m_data;
narshu 25:143b19c1fb05 100 };
narshu 25:143b19c1fb05 101
narshu 25:143b19c1fb05 102
narshu 25:143b19c1fb05 103 /**
narshu 25:143b19c1fb05 104 * \class Vector Vector.h "tvmet/Vector.h"
narshu 25:143b19c1fb05 105 * \brief Compile time fixed length vector with evaluation on compile time.
narshu 25:143b19c1fb05 106 */
narshu 25:143b19c1fb05 107 template<class T, std::size_t Sz>
narshu 25:143b19c1fb05 108 class Vector
narshu 25:143b19c1fb05 109 {
narshu 25:143b19c1fb05 110 public:
narshu 25:143b19c1fb05 111 /** Data type of the tvmet::Vector. */
narshu 25:143b19c1fb05 112 typedef T value_type;
narshu 25:143b19c1fb05 113
narshu 25:143b19c1fb05 114 /** Reference type of the tvmet::Vector data elements. */
narshu 25:143b19c1fb05 115 typedef T& reference;
narshu 25:143b19c1fb05 116
narshu 25:143b19c1fb05 117 /** const reference type of the tvmet::Vector data elements. */
narshu 25:143b19c1fb05 118 typedef const T& const_reference;
narshu 25:143b19c1fb05 119
narshu 25:143b19c1fb05 120 /** STL iterator interface. */
narshu 25:143b19c1fb05 121 typedef T* iterator;
narshu 25:143b19c1fb05 122
narshu 25:143b19c1fb05 123 /** STL const_iterator interface. */
narshu 25:143b19c1fb05 124 typedef const T* const_iterator;
narshu 25:143b19c1fb05 125
narshu 25:143b19c1fb05 126 /** STL reverse iterator interface. */
narshu 25:143b19c1fb05 127 typedef std::reverse_iterator<iterator> reverse_iterator;
narshu 25:143b19c1fb05 128
narshu 25:143b19c1fb05 129 /** STL const reverse iterator interface. */
narshu 25:143b19c1fb05 130 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
narshu 25:143b19c1fb05 131
narshu 25:143b19c1fb05 132 public:
narshu 25:143b19c1fb05 133 /** Dimensions. */
narshu 25:143b19c1fb05 134 enum {
narshu 25:143b19c1fb05 135 Size = Sz /**< The size of the vector. */
narshu 25:143b19c1fb05 136 };
narshu 25:143b19c1fb05 137
narshu 25:143b19c1fb05 138 public:
narshu 25:143b19c1fb05 139 /** Complexity counter. */
narshu 25:143b19c1fb05 140 enum {
narshu 25:143b19c1fb05 141 ops_assign = Size,
narshu 25:143b19c1fb05 142 ops = ops_assign,
narshu 25:143b19c1fb05 143 use_meta = ops < TVMET_COMPLEXITY_V_ASSIGN_TRIGGER ? true : false
narshu 25:143b19c1fb05 144 };
narshu 25:143b19c1fb05 145
narshu 25:143b19c1fb05 146 public: // STL interface
narshu 25:143b19c1fb05 147 /** STL iterator interface. */
narshu 25:143b19c1fb05 148 iterator begin() { return m_data; }
narshu 25:143b19c1fb05 149
narshu 25:143b19c1fb05 150 /** STL iterator interface. */
narshu 25:143b19c1fb05 151 iterator end() { return m_data + Size; }
narshu 25:143b19c1fb05 152
narshu 25:143b19c1fb05 153 /** STL const_iterator interface. */
narshu 25:143b19c1fb05 154 const_iterator begin() const { return m_data; }
narshu 25:143b19c1fb05 155
narshu 25:143b19c1fb05 156 /** STL const_iterator interface. */
narshu 25:143b19c1fb05 157 const_iterator end() const { return m_data + Size; }
narshu 25:143b19c1fb05 158
narshu 25:143b19c1fb05 159 /** STL reverse iterator interface reverse begin. */
narshu 25:143b19c1fb05 160 reverse_iterator rbegin() { return reverse_iterator( end() ); }
narshu 25:143b19c1fb05 161
narshu 25:143b19c1fb05 162 /** STL const reverse iterator interface reverse begin. */
narshu 25:143b19c1fb05 163 const_reverse_iterator rbegin() const {
narshu 25:143b19c1fb05 164 return const_reverse_iterator( end() );
narshu 25:143b19c1fb05 165 }
narshu 25:143b19c1fb05 166
narshu 25:143b19c1fb05 167 /** STL reverse iterator interface reverse end. */
narshu 25:143b19c1fb05 168 reverse_iterator rend() { return reverse_iterator( begin() ); }
narshu 25:143b19c1fb05 169
narshu 25:143b19c1fb05 170 /** STL const reverse iterator interface reverse end. */
narshu 25:143b19c1fb05 171 const_reverse_iterator rend() const {
narshu 25:143b19c1fb05 172 return const_reverse_iterator( begin() );
narshu 25:143b19c1fb05 173 }
narshu 25:143b19c1fb05 174
narshu 25:143b19c1fb05 175 /** STL vector front element. */
narshu 25:143b19c1fb05 176 value_type front() { return m_data[0]; }
narshu 25:143b19c1fb05 177
narshu 25:143b19c1fb05 178 /** STL vector const front element. */
narshu 25:143b19c1fb05 179 const_reference front() const { return m_data[0]; }
narshu 25:143b19c1fb05 180
narshu 25:143b19c1fb05 181 /** STL vector back element. */
narshu 25:143b19c1fb05 182 value_type back() { return m_data[Size-1]; }
narshu 25:143b19c1fb05 183
narshu 25:143b19c1fb05 184 /** STL vector const back element. */
narshu 25:143b19c1fb05 185 const_reference back() const { return m_data[Size-1]; }
narshu 25:143b19c1fb05 186
narshu 25:143b19c1fb05 187 /** STL vector empty() - returns allways false. */
narshu 25:143b19c1fb05 188 static bool empty() { return false; }
narshu 25:143b19c1fb05 189
narshu 25:143b19c1fb05 190 /** The size of the vector. */
narshu 25:143b19c1fb05 191 static std::size_t size() { return Size; }
narshu 25:143b19c1fb05 192
narshu 25:143b19c1fb05 193 /** STL vector max_size() - returns allways Size. */
narshu 25:143b19c1fb05 194 static std::size_t max_size() { return Size; }
narshu 25:143b19c1fb05 195
narshu 25:143b19c1fb05 196 public:
narshu 25:143b19c1fb05 197 /** Default Destructor */
narshu 25:143b19c1fb05 198 ~Vector() {
narshu 25:143b19c1fb05 199 #if defined(TVMET_DYNAMIC_MEMORY)
narshu 25:143b19c1fb05 200 delete [] m_data;
narshu 25:143b19c1fb05 201 #endif
narshu 25:143b19c1fb05 202 }
narshu 25:143b19c1fb05 203
narshu 25:143b19c1fb05 204 /** Default Constructor. The allocated memory region isn't cleared. If you want
narshu 25:143b19c1fb05 205 a clean use the constructor argument zero. */
narshu 25:143b19c1fb05 206 explicit Vector()
narshu 25:143b19c1fb05 207 #if defined(TVMET_DYNAMIC_MEMORY)
narshu 25:143b19c1fb05 208 : m_data( new value_type[Size] )
narshu 25:143b19c1fb05 209 #endif
narshu 25:143b19c1fb05 210 { }
narshu 25:143b19c1fb05 211
narshu 25:143b19c1fb05 212 /** Copy Constructor, not explicit! */
narshu 25:143b19c1fb05 213 Vector(const Vector& rhs)
narshu 25:143b19c1fb05 214 #if defined(TVMET_DYNAMIC_MEMORY)
narshu 25:143b19c1fb05 215 : m_data( new value_type[Size] )
narshu 25:143b19c1fb05 216 #endif
narshu 25:143b19c1fb05 217 {
narshu 25:143b19c1fb05 218 *this = XprVector<ConstReference, Size>(rhs.const_ref());
narshu 25:143b19c1fb05 219 }
narshu 25:143b19c1fb05 220
narshu 25:143b19c1fb05 221 /**
narshu 25:143b19c1fb05 222 * Constructor with STL iterator interface. The data will be copied into the
narshu 25:143b19c1fb05 223 * vector self, there isn't any stored reference to the array pointer.
narshu 25:143b19c1fb05 224 */
narshu 25:143b19c1fb05 225 template<class InputIterator>
narshu 25:143b19c1fb05 226 explicit Vector(InputIterator first, InputIterator last)
narshu 25:143b19c1fb05 227 #if defined(TVMET_DYNAMIC_MEMORY)
narshu 25:143b19c1fb05 228 : m_data( new value_type[Size] )
narshu 25:143b19c1fb05 229 #endif
narshu 25:143b19c1fb05 230 {
narshu 25:143b19c1fb05 231 TVMET_RT_CONDITION( static_cast<std::size_t>(std::distance(first, last)) <= Size,
narshu 25:143b19c1fb05 232 "InputIterator doesn't fits in size" )
narshu 25:143b19c1fb05 233 std::copy(first, last, m_data);
narshu 25:143b19c1fb05 234 }
narshu 25:143b19c1fb05 235
narshu 25:143b19c1fb05 236 /**
narshu 25:143b19c1fb05 237 * Constructor with STL iterator interface. The data will be copied into the
narshu 25:143b19c1fb05 238 * vector self, there isn't any stored reference to the array pointer.
narshu 25:143b19c1fb05 239 */
narshu 25:143b19c1fb05 240 template<class InputIterator>
narshu 25:143b19c1fb05 241 explicit Vector(InputIterator first, std::size_t sz)
narshu 25:143b19c1fb05 242 #if defined(TVMET_DYNAMIC_MEMORY)
narshu 25:143b19c1fb05 243 : m_data( new value_type[Size] )
narshu 25:143b19c1fb05 244 #endif
narshu 25:143b19c1fb05 245 {
narshu 25:143b19c1fb05 246 TVMET_RT_CONDITION( sz <= Size, "InputIterator doesn't fits in size" )
narshu 25:143b19c1fb05 247 std::copy(first, first + sz, m_data);
narshu 25:143b19c1fb05 248 }
narshu 25:143b19c1fb05 249
narshu 25:143b19c1fb05 250 /** Constructor with initializer for all elements. */
narshu 25:143b19c1fb05 251 explicit Vector(value_type rhs)
narshu 25:143b19c1fb05 252 #if defined(TVMET_DYNAMIC_MEMORY)
narshu 25:143b19c1fb05 253 : m_data( new value_type[Size] )
narshu 25:143b19c1fb05 254 #endif
narshu 25:143b19c1fb05 255 {
narshu 25:143b19c1fb05 256 typedef XprLiteral<value_type> expr_type;
narshu 25:143b19c1fb05 257 *this = XprVector<expr_type, Size>(expr_type(rhs));
narshu 25:143b19c1fb05 258 }
narshu 25:143b19c1fb05 259
narshu 25:143b19c1fb05 260 /** Default Constructor with initializer list. */
narshu 25:143b19c1fb05 261 explicit Vector(value_type x0, value_type x1)
narshu 25:143b19c1fb05 262 #if defined(TVMET_DYNAMIC_MEMORY)
narshu 25:143b19c1fb05 263 : m_data( new value_type[Size] )
narshu 25:143b19c1fb05 264 #endif
narshu 25:143b19c1fb05 265 {
narshu 25:143b19c1fb05 266 TVMET_CT_CONDITION(2 <= Size, ArgumentList_is_too_long)
narshu 25:143b19c1fb05 267 m_data[0] = x0; m_data[1] = x1;
narshu 25:143b19c1fb05 268 }
narshu 25:143b19c1fb05 269
narshu 25:143b19c1fb05 270 /** Default Constructor with initializer list. */
narshu 25:143b19c1fb05 271 explicit Vector(value_type x0, value_type x1, value_type x2)
narshu 25:143b19c1fb05 272 #if defined(TVMET_DYNAMIC_MEMORY)
narshu 25:143b19c1fb05 273 : m_data( new value_type[Size] )
narshu 25:143b19c1fb05 274 #endif
narshu 25:143b19c1fb05 275 {
narshu 25:143b19c1fb05 276 TVMET_CT_CONDITION(3 <= Size, ArgumentList_is_too_long)
narshu 25:143b19c1fb05 277 m_data[0] = x0; m_data[1] = x1; m_data[2] = x2;
narshu 25:143b19c1fb05 278 }
narshu 25:143b19c1fb05 279
narshu 25:143b19c1fb05 280 /** Default Constructor with initializer list. */
narshu 25:143b19c1fb05 281 explicit Vector(value_type x0, value_type x1, value_type x2, value_type x3)
narshu 25:143b19c1fb05 282 #if defined(TVMET_DYNAMIC_MEMORY)
narshu 25:143b19c1fb05 283 : m_data( new value_type[Size] )
narshu 25:143b19c1fb05 284 #endif
narshu 25:143b19c1fb05 285 {
narshu 25:143b19c1fb05 286 TVMET_CT_CONDITION(4 <= Size, ArgumentList_is_too_long)
narshu 25:143b19c1fb05 287 m_data[0] = x0; m_data[1] = x1; m_data[2] = x2; m_data[3] = x3;
narshu 25:143b19c1fb05 288 }
narshu 25:143b19c1fb05 289
narshu 25:143b19c1fb05 290 /** Default Constructor with initializer list. */
narshu 25:143b19c1fb05 291 explicit Vector(value_type x0, value_type x1, value_type x2, value_type x3,
narshu 25:143b19c1fb05 292 value_type x4)
narshu 25:143b19c1fb05 293 #if defined(TVMET_DYNAMIC_MEMORY)
narshu 25:143b19c1fb05 294 : m_data( new value_type[Size] )
narshu 25:143b19c1fb05 295 #endif
narshu 25:143b19c1fb05 296 {
narshu 25:143b19c1fb05 297 TVMET_CT_CONDITION(5 <= Size, ArgumentList_is_too_long)
narshu 25:143b19c1fb05 298 m_data[0] = x0; m_data[1] = x1; m_data[2] = x2; m_data[3] = x3; m_data[4] = x4;
narshu 25:143b19c1fb05 299 }
narshu 25:143b19c1fb05 300
narshu 25:143b19c1fb05 301 /** Default Constructor with initializer list. */
narshu 25:143b19c1fb05 302 explicit Vector(value_type x0, value_type x1, value_type x2, value_type x3,
narshu 25:143b19c1fb05 303 value_type x4, value_type x5)
narshu 25:143b19c1fb05 304 #if defined(TVMET_DYNAMIC_MEMORY)
narshu 25:143b19c1fb05 305 : m_data( new value_type[Size] )
narshu 25:143b19c1fb05 306 #endif
narshu 25:143b19c1fb05 307 {
narshu 25:143b19c1fb05 308 TVMET_CT_CONDITION(6 <= Size, ArgumentList_is_too_long)
narshu 25:143b19c1fb05 309 m_data[0] = x0; m_data[1] = x1; m_data[2] = x2; m_data[3] = x3; m_data[4] = x4;
narshu 25:143b19c1fb05 310 m_data[5] = x5;
narshu 25:143b19c1fb05 311 }
narshu 25:143b19c1fb05 312
narshu 25:143b19c1fb05 313 /** Default Constructor with initializer list. */
narshu 25:143b19c1fb05 314 explicit Vector(value_type x0, value_type x1, value_type x2, value_type x3,
narshu 25:143b19c1fb05 315 value_type x4, value_type x5, value_type x6)
narshu 25:143b19c1fb05 316 #if defined(TVMET_DYNAMIC_MEMORY)
narshu 25:143b19c1fb05 317 : m_data( new value_type[Size] )
narshu 25:143b19c1fb05 318 #endif
narshu 25:143b19c1fb05 319 {
narshu 25:143b19c1fb05 320 TVMET_CT_CONDITION(7 <= Size, ArgumentList_is_too_long)
narshu 25:143b19c1fb05 321 m_data[0] = x0; m_data[1] = x1; m_data[2] = x2; m_data[3] = x3; m_data[4] = x4;
narshu 25:143b19c1fb05 322 m_data[5] = x5; m_data[6] = x6;
narshu 25:143b19c1fb05 323 }
narshu 25:143b19c1fb05 324
narshu 25:143b19c1fb05 325 /** Default Constructor with initializer list. */
narshu 25:143b19c1fb05 326 explicit Vector(value_type x0, value_type x1, value_type x2, value_type x3,
narshu 25:143b19c1fb05 327 value_type x4, value_type x5, value_type x6, value_type x7)
narshu 25:143b19c1fb05 328 #if defined(TVMET_DYNAMIC_MEMORY)
narshu 25:143b19c1fb05 329 : m_data( new value_type[Size] )
narshu 25:143b19c1fb05 330 #endif
narshu 25:143b19c1fb05 331 {
narshu 25:143b19c1fb05 332 TVMET_CT_CONDITION(8 <= Size, ArgumentList_is_too_long)
narshu 25:143b19c1fb05 333 m_data[0] = x0; m_data[1] = x1; m_data[2] = x2; m_data[3] = x3; m_data[4] = x4;
narshu 25:143b19c1fb05 334 m_data[5] = x5; m_data[6] = x6; m_data[7] = x7;
narshu 25:143b19c1fb05 335 }
narshu 25:143b19c1fb05 336
narshu 25:143b19c1fb05 337 /** Default Constructor with initializer list. */
narshu 25:143b19c1fb05 338 explicit Vector(value_type x0, value_type x1, value_type x2, value_type x3,
narshu 25:143b19c1fb05 339 value_type x4, value_type x5, value_type x6, value_type x7,
narshu 25:143b19c1fb05 340 value_type x8)
narshu 25:143b19c1fb05 341 #if defined(TVMET_DYNAMIC_MEMORY)
narshu 25:143b19c1fb05 342 : m_data( new value_type[Size] )
narshu 25:143b19c1fb05 343 #endif
narshu 25:143b19c1fb05 344 {
narshu 25:143b19c1fb05 345 TVMET_CT_CONDITION(9 <= Size, ArgumentList_is_too_long)
narshu 25:143b19c1fb05 346 m_data[0] = x0; m_data[1] = x1; m_data[2] = x2; m_data[3] = x3; m_data[4] = x4;
narshu 25:143b19c1fb05 347 m_data[5] = x5; m_data[6] = x6; m_data[7] = x7; m_data[8] = x8;
narshu 25:143b19c1fb05 348 }
narshu 25:143b19c1fb05 349
narshu 25:143b19c1fb05 350 /** Default Constructor with initializer list. */
narshu 25:143b19c1fb05 351 explicit Vector(value_type x0, value_type x1, value_type x2, value_type x3,
narshu 25:143b19c1fb05 352 value_type x4, value_type x5, value_type x6, value_type x7,
narshu 25:143b19c1fb05 353 value_type x8, value_type x9)
narshu 25:143b19c1fb05 354 #if defined(TVMET_DYNAMIC_MEMORY)
narshu 25:143b19c1fb05 355 : m_data( new value_type[Size] )
narshu 25:143b19c1fb05 356 #endif
narshu 25:143b19c1fb05 357 {
narshu 25:143b19c1fb05 358 TVMET_CT_CONDITION(10 <= Size, ArgumentList_is_too_long)
narshu 25:143b19c1fb05 359 m_data[0] = x0; m_data[1] = x1; m_data[2] = x2; m_data[3] = x3; m_data[4] = x4;
narshu 25:143b19c1fb05 360 m_data[5] = x5; m_data[6] = x6; m_data[7] = x7; m_data[8] = x8; m_data[9] = x9;
narshu 25:143b19c1fb05 361 }
narshu 25:143b19c1fb05 362
narshu 25:143b19c1fb05 363 /** Construct a vector by expression. */
narshu 25:143b19c1fb05 364 template <class E>
narshu 25:143b19c1fb05 365 explicit Vector(const XprVector<E, Size>& e)
narshu 25:143b19c1fb05 366 #if defined(TVMET_DYNAMIC_MEMORY)
narshu 25:143b19c1fb05 367 : m_data( new value_type[Size] )
narshu 25:143b19c1fb05 368 #endif
narshu 25:143b19c1fb05 369 {
narshu 25:143b19c1fb05 370 *this = e;
narshu 25:143b19c1fb05 371 }
narshu 25:143b19c1fb05 372
narshu 25:143b19c1fb05 373 /** Assign a value_type on array, this can be used for a single value
narshu 25:143b19c1fb05 374 or a comma separeted list of values. */
narshu 25:143b19c1fb05 375 CommaInitializer<Vector, Size> operator=(value_type rhs) {
narshu 25:143b19c1fb05 376 return CommaInitializer<Vector, Size>(*this, rhs);
narshu 25:143b19c1fb05 377 }
narshu 25:143b19c1fb05 378
narshu 25:143b19c1fb05 379 public: // access operators
narshu 25:143b19c1fb05 380 value_type* _tvmet_restrict data() { return m_data; }
narshu 25:143b19c1fb05 381 const value_type* _tvmet_restrict data() const { return m_data; }
narshu 25:143b19c1fb05 382
narshu 25:143b19c1fb05 383 public: // index access operators
narshu 25:143b19c1fb05 384 value_type& _tvmet_restrict operator()(std::size_t i) {
narshu 25:143b19c1fb05 385 // Note: g++-2.95.3 does have problems on typedef reference
narshu 25:143b19c1fb05 386 TVMET_RT_CONDITION(i < Size, "Vector Bounce Violation")
narshu 25:143b19c1fb05 387 return m_data[i];
narshu 25:143b19c1fb05 388 }
narshu 25:143b19c1fb05 389
narshu 25:143b19c1fb05 390 value_type operator()(std::size_t i) const {
narshu 25:143b19c1fb05 391 TVMET_RT_CONDITION(i < Size, "Vector Bounce Violation")
narshu 25:143b19c1fb05 392 return m_data[i];
narshu 25:143b19c1fb05 393 }
narshu 25:143b19c1fb05 394
narshu 25:143b19c1fb05 395 value_type& _tvmet_restrict operator[](std::size_t i) {
narshu 25:143b19c1fb05 396 // Note: g++-2.95.3 does have problems on typedef reference
narshu 25:143b19c1fb05 397 return this->operator()(i);
narshu 25:143b19c1fb05 398 }
narshu 25:143b19c1fb05 399
narshu 25:143b19c1fb05 400 value_type operator[](std::size_t i) const {
narshu 25:143b19c1fb05 401 return this->operator()(i);
narshu 25:143b19c1fb05 402 }
narshu 25:143b19c1fb05 403
narshu 25:143b19c1fb05 404 public: // ET interface
narshu 25:143b19c1fb05 405 typedef VectorConstReference<T, Size> ConstReference;
narshu 25:143b19c1fb05 406
narshu 25:143b19c1fb05 407 /** Return a const Reference of the internal data */
narshu 25:143b19c1fb05 408 ConstReference const_ref() const { return ConstReference(*this); }
narshu 25:143b19c1fb05 409
narshu 25:143b19c1fb05 410 /** Return the vector as const expression. */
narshu 25:143b19c1fb05 411 XprVector<ConstReference, Size> as_expr() const {
narshu 25:143b19c1fb05 412 return XprVector<ConstReference, Size>(this->const_ref());
narshu 25:143b19c1fb05 413 }
narshu 25:143b19c1fb05 414
narshu 25:143b19c1fb05 415 private:
narshu 25:143b19c1fb05 416 /** Wrapper for meta assign. */
narshu 25:143b19c1fb05 417 template<class Dest, class Src, class Assign>
narshu 25:143b19c1fb05 418 static inline
narshu 25:143b19c1fb05 419 void do_assign(dispatch<true>, Dest& dest, const Src& src, const Assign& assign_fn) {
narshu 25:143b19c1fb05 420 meta::Vector<Size, 0>::assign(dest, src, assign_fn);
narshu 25:143b19c1fb05 421 }
narshu 25:143b19c1fb05 422
narshu 25:143b19c1fb05 423 /** Wrapper for loop assign. */
narshu 25:143b19c1fb05 424 template<class Dest, class Src, class Assign>
narshu 25:143b19c1fb05 425 static inline
narshu 25:143b19c1fb05 426 void do_assign(dispatch<false>, Dest& dest, const Src& src, const Assign& assign_fn) {
narshu 25:143b19c1fb05 427 loop::Vector<Size>::assign(dest, src, assign_fn);
narshu 25:143b19c1fb05 428 }
narshu 25:143b19c1fb05 429
narshu 25:143b19c1fb05 430 public:
narshu 25:143b19c1fb05 431 /** assign this to a vector expression using the functional assign_fn. */
narshu 25:143b19c1fb05 432 template<class T2, class Assign>
narshu 25:143b19c1fb05 433 void assign_to(Vector<T2, Size>& dest, const Assign& assign_fn) const {
narshu 25:143b19c1fb05 434 do_assign(dispatch<use_meta>(), dest, *this, assign_fn);
narshu 25:143b19c1fb05 435 }
narshu 25:143b19c1fb05 436
narshu 25:143b19c1fb05 437 public: // assign operations
narshu 25:143b19c1fb05 438 /** assign a given Vector element wise to this vector.
narshu 25:143b19c1fb05 439 The operator=(const Vector&) is compiler generated. */
narshu 25:143b19c1fb05 440 template<class T2>
narshu 25:143b19c1fb05 441 Vector& operator=(const Vector<T2, Size>& rhs) {
narshu 25:143b19c1fb05 442 rhs.assign_to(*this, Fcnl_assign<value_type, T2>());
narshu 25:143b19c1fb05 443 return *this;
narshu 25:143b19c1fb05 444 }
narshu 25:143b19c1fb05 445
narshu 25:143b19c1fb05 446 /** assign a given XprVector element wise to this vector. */
narshu 25:143b19c1fb05 447 template<class E>
narshu 25:143b19c1fb05 448 Vector& operator=(const XprVector<E, Size>& rhs) {
narshu 25:143b19c1fb05 449 rhs.assign_to(*this, Fcnl_assign<value_type, typename E::value_type>());
narshu 25:143b19c1fb05 450 return *this;
narshu 25:143b19c1fb05 451 }
narshu 25:143b19c1fb05 452
narshu 25:143b19c1fb05 453 private:
narshu 25:143b19c1fb05 454 template<class Obj, std::size_t LEN> friend class CommaInitializer;
narshu 25:143b19c1fb05 455
narshu 25:143b19c1fb05 456 /** This is a helper for assigning a comma separated initializer
narshu 25:143b19c1fb05 457 list. It's equal to Vector& operator=(value_type) which does
narshu 25:143b19c1fb05 458 replace it. */
narshu 25:143b19c1fb05 459 Vector& assign_value(value_type rhs) {
narshu 25:143b19c1fb05 460 typedef XprLiteral<value_type> expr_type;
narshu 25:143b19c1fb05 461 *this = XprVector<expr_type, Size>(expr_type(rhs));
narshu 25:143b19c1fb05 462 return *this;
narshu 25:143b19c1fb05 463 }
narshu 25:143b19c1fb05 464
narshu 25:143b19c1fb05 465 public: // math operators with scalars
narshu 25:143b19c1fb05 466 // NOTE: this meaning is clear - element wise ops even if not in ns element_wise
narshu 25:143b19c1fb05 467 Vector& operator+=(value_type) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 468 Vector& operator-=(value_type) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 469 Vector& operator*=(value_type) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 470 Vector& operator/=(value_type) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 471
narshu 25:143b19c1fb05 472 Vector& operator%=(std::size_t) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 473 Vector& operator^=(std::size_t) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 474 Vector& operator&=(std::size_t) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 475 Vector& operator|=(std::size_t) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 476 Vector& operator<<=(std::size_t) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 477 Vector& operator>>=(std::size_t) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 478
narshu 25:143b19c1fb05 479 public: // math assign operators with vectors
narshu 25:143b19c1fb05 480 // NOTE: access using the operators in ns element_wise, since that's what is does
narshu 25:143b19c1fb05 481 template <class T2> Vector& M_add_eq(const Vector<T2, Size>&) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 482 template <class T2> Vector& M_sub_eq(const Vector<T2, Size>&) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 483 template <class T2> Vector& M_mul_eq(const Vector<T2, Size>&) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 484 template <class T2> Vector& M_div_eq(const Vector<T2, Size>&) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 485 template <class T2> Vector& M_mod_eq(const Vector<T2, Size>&) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 486 template <class T2> Vector& M_xor_eq(const Vector<T2, Size>&) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 487 template <class T2> Vector& M_and_eq(const Vector<T2, Size>&) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 488 template <class T2> Vector& M_or_eq (const Vector<T2, Size>&) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 489 template <class T2> Vector& M_shl_eq(const Vector<T2, Size>&) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 490 template <class T2> Vector& M_shr_eq(const Vector<T2, Size>&) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 491
narshu 25:143b19c1fb05 492 public: // math operators with expressions
narshu 25:143b19c1fb05 493 // NOTE: access using the operators in ns element_wise, since that's what is does
narshu 25:143b19c1fb05 494 template <class E> Vector& M_add_eq(const XprVector<E, Size>&) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 495 template <class E> Vector& M_sub_eq(const XprVector<E, Size>&) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 496 template <class E> Vector& M_mul_eq(const XprVector<E, Size>&) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 497 template <class E> Vector& M_div_eq(const XprVector<E, Size>&) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 498 template <class E> Vector& M_mod_eq(const XprVector<E, Size>&) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 499 template <class E> Vector& M_xor_eq(const XprVector<E, Size>&) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 500 template <class E> Vector& M_and_eq(const XprVector<E, Size>&) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 501 template <class E> Vector& M_or_eq (const XprVector<E, Size>&) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 502 template <class E> Vector& M_shl_eq(const XprVector<E, Size>&) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 503 template <class E> Vector& M_shr_eq(const XprVector<E, Size>&) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 504
narshu 25:143b19c1fb05 505 public: // aliased math operators with expressions, used with proxy
narshu 25:143b19c1fb05 506 template <class T2> Vector& alias_assign(const Vector<T2, Size>&) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 507 template <class T2> Vector& alias_add_eq(const Vector<T2, Size>&) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 508 template <class T2> Vector& alias_sub_eq(const Vector<T2, Size>&) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 509 template <class T2> Vector& alias_mul_eq(const Vector<T2, Size>&) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 510 template <class T2> Vector& alias_div_eq(const Vector<T2, Size>&) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 511
narshu 25:143b19c1fb05 512 template <class E> Vector& alias_assign(const XprVector<E, Size>&) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 513 template <class E> Vector& alias_add_eq(const XprVector<E, Size>&) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 514 template <class E> Vector& alias_sub_eq(const XprVector<E, Size>&) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 515 template <class E> Vector& alias_mul_eq(const XprVector<E, Size>&) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 516 template <class E> Vector& alias_div_eq(const XprVector<E, Size>&) TVMET_CXX_ALWAYS_INLINE;
narshu 25:143b19c1fb05 517
narshu 25:143b19c1fb05 518 public: // io
narshu 25:143b19c1fb05 519 /** Structure for info printing as Vector<T, Size>. */
narshu 25:143b19c1fb05 520 struct Info : public TvmetBase<Info> {
narshu 25:143b19c1fb05 521 std::ostream& print_xpr(std::ostream& os) const {
narshu 25:143b19c1fb05 522 os << "Vector<T=" << typeid(value_type).name()
narshu 25:143b19c1fb05 523 << ", Sz=" << Size << ">";
narshu 25:143b19c1fb05 524 return os;
narshu 25:143b19c1fb05 525 }
narshu 25:143b19c1fb05 526 };
narshu 25:143b19c1fb05 527
narshu 25:143b19c1fb05 528 /** Get an info object of this vector. */
narshu 25:143b19c1fb05 529 static Info info() { return Info(); }
narshu 25:143b19c1fb05 530
narshu 25:143b19c1fb05 531 /** Member function for expression level printing. */
narshu 25:143b19c1fb05 532 std::ostream& print_xpr(std::ostream& os, std::size_t l=0) const;
narshu 25:143b19c1fb05 533
narshu 25:143b19c1fb05 534 /** Member function for printing internal data. */
narshu 25:143b19c1fb05 535 std::ostream& print_on(std::ostream& os) const;
narshu 25:143b19c1fb05 536
narshu 25:143b19c1fb05 537 private:
narshu 25:143b19c1fb05 538 /** The data of vector self. */
narshu 25:143b19c1fb05 539
narshu 25:143b19c1fb05 540 #if defined(TVMET_DYNAMIC_MEMORY)
narshu 25:143b19c1fb05 541 value_type* m_data;
narshu 25:143b19c1fb05 542 #else
narshu 25:143b19c1fb05 543 value_type m_data[Size];
narshu 25:143b19c1fb05 544 #endif
narshu 25:143b19c1fb05 545 };
narshu 25:143b19c1fb05 546
narshu 25:143b19c1fb05 547
narshu 25:143b19c1fb05 548 } // namespace tvmet
narshu 25:143b19c1fb05 549
narshu 25:143b19c1fb05 550 #include <tvmet/VectorImpl.h>
narshu 25:143b19c1fb05 551 #include <tvmet/VectorFunctions.h>
narshu 25:143b19c1fb05 552 #include <tvmet/VectorBinaryFunctions.h>
narshu 25:143b19c1fb05 553 #include <tvmet/VectorUnaryFunctions.h>
narshu 25:143b19c1fb05 554 #include <tvmet/VectorOperators.h>
narshu 25:143b19c1fb05 555 #include <tvmet/VectorEval.h>
narshu 25:143b19c1fb05 556 #include <tvmet/AliasProxy.h>
narshu 25:143b19c1fb05 557
narshu 25:143b19c1fb05 558 #endif // TVMET_VECTOR_H
narshu 25:143b19c1fb05 559
narshu 25:143b19c1fb05 560 // Local Variables:
narshu 25:143b19c1fb05 561 // mode:C++
narshu 25:143b19c1fb05 562 // tab-width:8
narshu 25:143b19c1fb05 563 // End: