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: VectorImpl.h,v 1.31 2007-06-23 15:58:58 opetzold Exp $
narshu 25:143b19c1fb05 22 */
narshu 25:143b19c1fb05 23
narshu 25:143b19c1fb05 24 #ifndef TVMET_VECTOR_IMPL_H
narshu 25:143b19c1fb05 25 #define TVMET_VECTOR_IMPL_H
narshu 25:143b19c1fb05 26
narshu 25:143b19c1fb05 27 #include <iomanip> // setw
narshu 25:143b19c1fb05 28
narshu 25:143b19c1fb05 29 #include <tvmet/Functional.h>
narshu 25:143b19c1fb05 30 #include <tvmet/Io.h>
narshu 25:143b19c1fb05 31
narshu 25:143b19c1fb05 32
narshu 25:143b19c1fb05 33 namespace tvmet {
narshu 25:143b19c1fb05 34
narshu 25:143b19c1fb05 35
narshu 25:143b19c1fb05 36 /*
narshu 25:143b19c1fb05 37 * member operators for i/o
narshu 25:143b19c1fb05 38 */
narshu 25:143b19c1fb05 39 template<class T, std::size_t Sz>
narshu 25:143b19c1fb05 40 std::ostream& Vector<T, Sz>::print_xpr(std::ostream& os, std::size_t l) const
narshu 25:143b19c1fb05 41 {
narshu 25:143b19c1fb05 42 os << IndentLevel(l++) << "Vector[" << ops << "]<"
narshu 25:143b19c1fb05 43 << typeid(T).name() << ", " << Size << ">,"
narshu 25:143b19c1fb05 44 << IndentLevel(--l)
narshu 25:143b19c1fb05 45 << std::endl;
narshu 25:143b19c1fb05 46
narshu 25:143b19c1fb05 47 return os;
narshu 25:143b19c1fb05 48 }
narshu 25:143b19c1fb05 49
narshu 25:143b19c1fb05 50
narshu 25:143b19c1fb05 51 template<class T, std::size_t Sz>
narshu 25:143b19c1fb05 52 std::ostream& Vector<T, Sz>::print_on(std::ostream& os) const
narshu 25:143b19c1fb05 53 {
narshu 25:143b19c1fb05 54 enum {
narshu 25:143b19c1fb05 55 complex_type = NumericTraits<value_type>::is_complex
narshu 25:143b19c1fb05 56 };
narshu 25:143b19c1fb05 57
narshu 25:143b19c1fb05 58 std::streamsize w = IoPrintHelper<Vector>::width(dispatch<complex_type>(), *this);
narshu 25:143b19c1fb05 59
narshu 25:143b19c1fb05 60 os << std::setw(0) << "[\n ";
narshu 25:143b19c1fb05 61 for(std::size_t i = 0; i < (Size - 1); ++i) {
narshu 25:143b19c1fb05 62 os << std::setw(w) << m_data[i] << ", ";
narshu 25:143b19c1fb05 63 }
narshu 25:143b19c1fb05 64 os << std::setw(w) << m_data[Size - 1] << "\n]";
narshu 25:143b19c1fb05 65
narshu 25:143b19c1fb05 66 return os;
narshu 25:143b19c1fb05 67 }
narshu 25:143b19c1fb05 68
narshu 25:143b19c1fb05 69
narshu 25:143b19c1fb05 70 /*
narshu 25:143b19c1fb05 71 * member operators with scalars, per se element wise
narshu 25:143b19c1fb05 72 */
narshu 25:143b19c1fb05 73 #define TVMET_IMPLEMENT_MACRO(NAME, OP) \
narshu 25:143b19c1fb05 74 template<class T, std::size_t Sz> \
narshu 25:143b19c1fb05 75 inline \
narshu 25:143b19c1fb05 76 Vector<T, Sz>& Vector<T, Sz>::operator OP (value_type rhs) { \
narshu 25:143b19c1fb05 77 typedef XprLiteral<value_type> expr_type; \
narshu 25:143b19c1fb05 78 this->M_##NAME(XprVector<expr_type, Size>(expr_type(rhs))); \
narshu 25:143b19c1fb05 79 return *this; \
narshu 25:143b19c1fb05 80 }
narshu 25:143b19c1fb05 81
narshu 25:143b19c1fb05 82 TVMET_IMPLEMENT_MACRO(add_eq, +=)
narshu 25:143b19c1fb05 83 TVMET_IMPLEMENT_MACRO(sub_eq, -=)
narshu 25:143b19c1fb05 84 TVMET_IMPLEMENT_MACRO(mul_eq, *=)
narshu 25:143b19c1fb05 85 TVMET_IMPLEMENT_MACRO(div_eq, /=)
narshu 25:143b19c1fb05 86 #undef TVMET_IMPLEMENT_MACRO
narshu 25:143b19c1fb05 87
narshu 25:143b19c1fb05 88
narshu 25:143b19c1fb05 89 #define TVMET_IMPLEMENT_MACRO(NAME, OP) \
narshu 25:143b19c1fb05 90 template<class T, std::size_t Sz> \
narshu 25:143b19c1fb05 91 inline \
narshu 25:143b19c1fb05 92 Vector<T, Sz>& Vector<T, Sz>::operator OP (std::size_t rhs) { \
narshu 25:143b19c1fb05 93 typedef XprLiteral<value_type> expr_type; \
narshu 25:143b19c1fb05 94 this->M_##NAME(XprVector<expr_type, Size>(expr_type(rhs))); \
narshu 25:143b19c1fb05 95 return *this; \
narshu 25:143b19c1fb05 96 }
narshu 25:143b19c1fb05 97
narshu 25:143b19c1fb05 98 TVMET_IMPLEMENT_MACRO(mod_eq, %=)
narshu 25:143b19c1fb05 99 TVMET_IMPLEMENT_MACRO(xor_eq,^=)
narshu 25:143b19c1fb05 100 TVMET_IMPLEMENT_MACRO(and_eq, &=)
narshu 25:143b19c1fb05 101 TVMET_IMPLEMENT_MACRO(or_eq, |=)
narshu 25:143b19c1fb05 102 TVMET_IMPLEMENT_MACRO(shl_eq, <<=)
narshu 25:143b19c1fb05 103 TVMET_IMPLEMENT_MACRO(shr_eq, >>=)
narshu 25:143b19c1fb05 104 #undef TVMET_IMPLEMENT_MACRO
narshu 25:143b19c1fb05 105
narshu 25:143b19c1fb05 106
narshu 25:143b19c1fb05 107 /*
narshu 25:143b19c1fb05 108 * member functions (operators) with vectors, for use with +=,-= ... <<=
narshu 25:143b19c1fb05 109 */
narshu 25:143b19c1fb05 110 #define TVMET_IMPLEMENT_MACRO(NAME) \
narshu 25:143b19c1fb05 111 template<class T1, std::size_t Sz> \
narshu 25:143b19c1fb05 112 template <class T2> \
narshu 25:143b19c1fb05 113 inline Vector<T1, Sz>& \
narshu 25:143b19c1fb05 114 Vector<T1, Sz>::M_##NAME (const Vector<T2, Size>& rhs) { \
narshu 25:143b19c1fb05 115 this->M_##NAME( XprVector<typename Vector<T2, Size>::ConstReference, Size>(rhs.const_ref()) ); \
narshu 25:143b19c1fb05 116 return *this; \
narshu 25:143b19c1fb05 117 }
narshu 25:143b19c1fb05 118
narshu 25:143b19c1fb05 119 TVMET_IMPLEMENT_MACRO(add_eq)
narshu 25:143b19c1fb05 120 TVMET_IMPLEMENT_MACRO(sub_eq)
narshu 25:143b19c1fb05 121 TVMET_IMPLEMENT_MACRO(mul_eq)
narshu 25:143b19c1fb05 122 TVMET_IMPLEMENT_MACRO(div_eq)
narshu 25:143b19c1fb05 123 TVMET_IMPLEMENT_MACRO(mod_eq)
narshu 25:143b19c1fb05 124 TVMET_IMPLEMENT_MACRO(xor_eq)
narshu 25:143b19c1fb05 125 TVMET_IMPLEMENT_MACRO(and_eq)
narshu 25:143b19c1fb05 126 TVMET_IMPLEMENT_MACRO(or_eq)
narshu 25:143b19c1fb05 127 TVMET_IMPLEMENT_MACRO(shl_eq)
narshu 25:143b19c1fb05 128 TVMET_IMPLEMENT_MACRO(shr_eq)
narshu 25:143b19c1fb05 129 #undef TVMET_IMPLEMENT_MACRO
narshu 25:143b19c1fb05 130
narshu 25:143b19c1fb05 131
narshu 25:143b19c1fb05 132 /*
narshu 25:143b19c1fb05 133 * member functions (operators) with expressions, for use width +=,-= ... <<=
narshu 25:143b19c1fb05 134 */
narshu 25:143b19c1fb05 135 #define TVMET_IMPLEMENT_MACRO(NAME) \
narshu 25:143b19c1fb05 136 template<class T, std::size_t Sz> \
narshu 25:143b19c1fb05 137 template <class E> \
narshu 25:143b19c1fb05 138 inline \
narshu 25:143b19c1fb05 139 Vector<T, Sz>& \
narshu 25:143b19c1fb05 140 Vector<T, Sz>::M_##NAME (const XprVector<E, Size>& rhs) { \
narshu 25:143b19c1fb05 141 rhs.assign_to(*this, Fcnl_##NAME<value_type, typename E::value_type>()); \
narshu 25:143b19c1fb05 142 return *this; \
narshu 25:143b19c1fb05 143 }
narshu 25:143b19c1fb05 144
narshu 25:143b19c1fb05 145 TVMET_IMPLEMENT_MACRO(add_eq)
narshu 25:143b19c1fb05 146 TVMET_IMPLEMENT_MACRO(sub_eq)
narshu 25:143b19c1fb05 147 TVMET_IMPLEMENT_MACRO(mul_eq)
narshu 25:143b19c1fb05 148 TVMET_IMPLEMENT_MACRO(div_eq)
narshu 25:143b19c1fb05 149 TVMET_IMPLEMENT_MACRO(mod_eq)
narshu 25:143b19c1fb05 150 TVMET_IMPLEMENT_MACRO(xor_eq)
narshu 25:143b19c1fb05 151 TVMET_IMPLEMENT_MACRO(and_eq)
narshu 25:143b19c1fb05 152 TVMET_IMPLEMENT_MACRO(or_eq)
narshu 25:143b19c1fb05 153 TVMET_IMPLEMENT_MACRO(shl_eq)
narshu 25:143b19c1fb05 154 TVMET_IMPLEMENT_MACRO(shr_eq)
narshu 25:143b19c1fb05 155 #undef TVMET_IMPLEMENT_MACRO
narshu 25:143b19c1fb05 156
narshu 25:143b19c1fb05 157
narshu 25:143b19c1fb05 158 /*
narshu 25:143b19c1fb05 159 * aliased member functions (operators) with vectors,
narshu 25:143b19c1fb05 160 * for use with +=,-= ... <<=
narshu 25:143b19c1fb05 161 */
narshu 25:143b19c1fb05 162 #define TVMET_IMPLEMENT_MACRO(NAME) \
narshu 25:143b19c1fb05 163 template<class T1, std::size_t Sz> \
narshu 25:143b19c1fb05 164 template <class T2> \
narshu 25:143b19c1fb05 165 inline \
narshu 25:143b19c1fb05 166 Vector<T1, Sz>& \
narshu 25:143b19c1fb05 167 Vector<T1, Sz>::alias_##NAME (const Vector<T2, Size>& rhs) { \
narshu 25:143b19c1fb05 168 this->alias_##NAME( XprVector<typename Vector<T2, Size>::ConstReference, Size>(rhs.const_ref()) ); \
narshu 25:143b19c1fb05 169 return *this; \
narshu 25:143b19c1fb05 170 }
narshu 25:143b19c1fb05 171
narshu 25:143b19c1fb05 172 TVMET_IMPLEMENT_MACRO(assign)
narshu 25:143b19c1fb05 173 TVMET_IMPLEMENT_MACRO(add_eq)
narshu 25:143b19c1fb05 174 TVMET_IMPLEMENT_MACRO(sub_eq)
narshu 25:143b19c1fb05 175 TVMET_IMPLEMENT_MACRO(mul_eq)
narshu 25:143b19c1fb05 176 TVMET_IMPLEMENT_MACRO(div_eq)
narshu 25:143b19c1fb05 177 #undef TVMET_IMPLEMENT_MACRO
narshu 25:143b19c1fb05 178
narshu 25:143b19c1fb05 179
narshu 25:143b19c1fb05 180 /*
narshu 25:143b19c1fb05 181 * aliased member functions (operators) with expressions,
narshu 25:143b19c1fb05 182 * for use width +=,-= ... <<=
narshu 25:143b19c1fb05 183 */
narshu 25:143b19c1fb05 184 #define TVMET_IMPLEMENT_MACRO(NAME) \
narshu 25:143b19c1fb05 185 template<class T, std::size_t Sz> \
narshu 25:143b19c1fb05 186 template <class E> \
narshu 25:143b19c1fb05 187 inline \
narshu 25:143b19c1fb05 188 Vector<T, Sz>& \
narshu 25:143b19c1fb05 189 Vector<T, Sz>::alias_##NAME (const XprVector<E, Size>& rhs) { \
narshu 25:143b19c1fb05 190 typedef Vector<T, Sz> temp_type; \
narshu 25:143b19c1fb05 191 temp_type(rhs).assign_to(*this, Fcnl_##NAME<value_type, typename E::value_type>()); \
narshu 25:143b19c1fb05 192 return *this; \
narshu 25:143b19c1fb05 193 }
narshu 25:143b19c1fb05 194
narshu 25:143b19c1fb05 195 TVMET_IMPLEMENT_MACRO(assign)
narshu 25:143b19c1fb05 196 TVMET_IMPLEMENT_MACRO(add_eq)
narshu 25:143b19c1fb05 197 TVMET_IMPLEMENT_MACRO(sub_eq)
narshu 25:143b19c1fb05 198 TVMET_IMPLEMENT_MACRO(mul_eq)
narshu 25:143b19c1fb05 199 TVMET_IMPLEMENT_MACRO(div_eq)
narshu 25:143b19c1fb05 200 #undef TVMET_IMPLEMENT_MACRO
narshu 25:143b19c1fb05 201
narshu 25:143b19c1fb05 202
narshu 25:143b19c1fb05 203 } // namespace tvmet
narshu 25:143b19c1fb05 204
narshu 25:143b19c1fb05 205 #endif // TVMET_VECTOR_IMPL_H
narshu 25:143b19c1fb05 206
narshu 25:143b19c1fb05 207 // Local Variables:
narshu 25:143b19c1fb05 208 // mode:C++
narshu 25:143b19c1fb05 209 // tab-width:8
narshu 25:143b19c1fb05 210 // End: