2006-08-30 Thierry GERAUD <theo(a)tegucigalpa.lrde.epita.fr>
* xtd/optraits.hh (xtd_unop_trait, xtd_unop_trait_): New.
(xtd_binop_trait, xtd_binop_trait_): New.
Update.
* xtd/vec.hh: New.
Index: xtd/optraits.hh
===================================================================
--- xtd/optraits.hh (revision 506)
+++ xtd/optraits.hh (working copy)
@@ -198,40 +198,50 @@
+// general
+
+# define xtd_unop_trait(Op, T) typename xtd::internal::get_trait_<Op, T>::ret
+# define xtd_unop_trait_(Op, T) xtd::internal::get_trait_<Op, T>::ret
+# define xtd_binop_trait(Op, L, R) typename xtd::internal::get_trait_<Op, L,
R>::ret
+# define xtd_binop_trait_(Op, L, R) xtd::internal::get_trait_<Op, L,
R>::ret
+
+
// logic
-# define xtd_op_land_trait(L, R) typename xtd::internal::get_trait_<xtd::op_land, L,
R>::ret
-# define xtd_op_lor_trait(L, R) typename xtd::internal::get_trait_<xtd::op_lor, L,
R>::ret
-# define xtd_op_lxor_trait(L, R) typename xtd::internal::get_trait_<xtd::op_lxor, L,
R>::ret
+# define xtd_op_land_trait(L, R) xtd_binop_trait(xtd::op_land, L, R)
+# define xtd_op_lor_trait(L, R) xtd_binop_trait(xtd::op_lor, L, R)
+# define xtd_op_lxor_trait(L, R) xtd_binop_trait(xtd::op_lxor, L, R)
-# define xtd_op_lnot_trait(T) typename xtd::internal::get_trait_<xtd::op_lnot,
T>::ret
+# define xtd_op_lnot_trait(T) xtd_unop_trait(xtd::op_lnot, T)
// cmp
-# define xtd_op_eq_trait(L, R) typename xtd::internal::get_trait_<xtd::op_eq, L,
R>::ret
-# define xtd_op_neq_trait(L, R) typename xtd::internal::get_trait_<xtd::op_neq, L,
R>::ret
-# define xtd_op_less_trait(L, R) typename xtd::internal::get_trait_<xtd::op_less,
L, R>::ret
-# define xtd_op_leq_trait(L, R) typename xtd::internal::get_trait_<xtd::op_leq, L,
R>::ret
-# define xtd_op_greater_trait(L, R) typename
xtd::internal::get_trait_<xtd::op_greater, L, R>::ret
-# define xtd_op_geq_trait(L, R) typename xtd::internal::get_trait_<xtd::op_geq, L,
R>::ret
+# define xtd_op_eq_trait(L, R) xtd_binop_trait(xtd::op_eq, L, R)
+# define xtd_op_neq_trait(L, R) xtd_binop_trait(xtd::op_neq, L, R)
+# define xtd_op_less_trait(L, R) xtd_binop_trait(xtd::op_less, L, R)
+# define xtd_op_leq_trait(L, R) xtd_binop_trait(xtd::op_leq, L, R)
+# define xtd_op_greater_trait(L, R) xtd_binop_trait(xtd::op_greater, L, R)
+# define xtd_op_geq_trait(L, R) xtd_binop_trait(xtd::op_geq, L, R)
// arith
-# define xtd_op_plus_trait(L, R) typename xtd::internal::get_trait_<xtd::op_plus, L,
R>::ret
-# define xtd_op_plus_trait_(L, R) xtd::internal::get_trait_<xtd::op_plus, L,
R>::ret
-# define xtd_op_minus_trait(L, R) typename xtd::internal::get_trait_<xtd::op_minus, L,
R>::ret
-# define xtd_op_minus_trait_(L, R) xtd::internal::get_trait_<xtd::op_minus, L,
R>::ret
-# define xtd_op_mult_trait(L, R) typename xtd::internal::get_trait_<xtd::op_mult, L,
R>::ret
-# define xtd_op_mult_trait_(L, R) xtd::internal::get_trait_<xtd::op_mult, L,
R>::ret
-# define xtd_op_div_trait(L, R) typename xtd::internal::get_trait_<xtd::op_div, L,
R>::ret
-# define xtd_op_div_trait_(L, R) xtd::internal::get_trait_<xtd::op_div, L,
R>::ret
-# define xtd_op_mod_trait(L, R) typename xtd::internal::get_trait_<xtd::op_mod, L,
R>::ret
-# define xtd_op_mod_trait_(L, R) xtd::internal::get_trait_<xtd::op_mod, L,
R>::ret
+# define xtd_op_plus_trait(L, R) xtd_binop_trait(xtd::op_plus, L, R)
+# define xtd_op_minus_trait(L, R) xtd_binop_trait(xtd::op_minus, L, R)
+# define xtd_op_mult_trait(L, R) xtd_binop_trait(xtd::op_mult, L, R)
+# define xtd_op_div_trait(L, R) xtd_binop_trait(xtd::op_div, L, R)
+# define xtd_op_mod_trait(L, R) xtd_binop_trait(xtd::op_mod, L, R)
+# define xtd_op_uminus_trait(T) xtd_unop_trait(xtd::op_uminus, T)
-# define xtd_op_uminus_trait(T) typename xtd::internal::get_trait_<xtd::op_uminus,
T>::ret
+# define xtd_op_plus_trait_(L, R) xtd_binop_trait_(xtd::op_plus, L, R)
+# define xtd_op_minus_trait_(L, R) xtd_binop_trait_(xtd::op_minus, L, R)
+# define xtd_op_mult_trait_(L, R) xtd_binop_trait_(xtd::op_mult, L, R)
+# define xtd_op_div_trait_(L, R) xtd_binop_trait_(xtd::op_div, L, R)
+# define xtd_op_mod_trait_(L, R) xtd_binop_trait_(xtd::op_mod, L, R)
+# define xtd_op_uminus_trait_(T) xtd_unop_trait_(xtd::op_uminus, T)
+
#endif // ! EXTENDED_OPTRAITS_HH
Index: xtd/vec.hh
===================================================================
--- xtd/vec.hh (revision 0)
+++ xtd/vec.hh (revision 0)
@@ -0,0 +1,284 @@
+// Copyright (C) 2006 EPITA Research and Development Laboratory
+//
+// This file is part of the Olena Library. This library is free
+// software; you can redistribute it and/or modify it under the terms
+// of the GNU General Public License version 2 as published by the
+// Free Software Foundation.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+// General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License
+// along with this library; see the file COPYING. If not, write to
+// the Free Software Foundation, 51 Franklin Street, Fifth Floor,
+// Boston, MA 02111-1307, USA.
+//
+// As a special exception, you may use this file as part of a free
+// software library without restriction. Specifically, if other files
+// instantiate templates or use macros or inline functions from this
+// file, or you compile this file and link it with other files to
+// produce an executable, this file does not by itself cause the
+// resulting executable to be covered by the GNU General Public
+// License. This exception does not however invalidate any other
+// reasons why the executable file might be covered by the GNU General
+// Public License.
+
+
+#ifndef EXTENDED_VEC_HH
+# define EXTENDED_VEC_HH
+
+# include <cassert>
+# include <xtd/optraits.hh>
+
+
+// FIXME: Document.
+
+
+namespace xtd
+{
+
+ namespace internal
+ {
+
+ template <unsigned n, typename T>
+ class vec_base_
+ {
+ protected:
+ T data_[n];
+ };
+
+ template <typename T>
+ class vec_base_ <1, T>
+ {
+ public:
+ void set(const T& val0)
+ {
+ data_[0] = val0;
+ }
+ protected:
+ T data_[1];
+ };
+
+ template <typename T>
+ class vec_base_ <2, T>
+ {
+ public:
+ void set(const T& val0, const T& val1)
+ {
+ data_[0] = val0;
+ data_[1] = val1;
+ }
+ protected:
+ T data_[2];
+ };
+
+ template <typename T>
+ class vec_base_ <3, T>
+ {
+ public:
+ void set(const T& val0, const T& val1, const T& val2)
+ {
+ data_[0] = val0;
+ data_[1] = val1;
+ data_[2] = val2;
+ }
+ protected:
+ T data_[3];
+ };
+
+
+ } // end of namespace xtd::internal
+
+
+
+ template <unsigned n, typename T>
+ class vec : public internal::vec_base_<n, T>
+ {
+ typedef internal::vec_base_<n,T> super;
+ using super::data_;
+
+ public:
+
+ typedef T value_type;
+ enum { dim = n };
+
+ vec()
+ {
+ }
+
+ template <typename U>
+ vec(const vec<n, U>& rhs)
+ {
+ for (unsigned i = 0; i < n; ++i)
+ data_[i] = rhs[i];
+ }
+
+ template <typename U>
+ vec& operator=(const vec<n, U>& rhs)
+ {
+ for (unsigned i = 0; i < n; ++i)
+ data_[i] = rhs[i];
+ return *this;
+ }
+
+ const T operator[](unsigned i) const
+ {
+ assert(i < n);
+ return data_[i];
+ }
+
+ T& operator[](unsigned i)
+ {
+ assert(i < n);
+ return data_[i];
+ }
+
+ void set_all(const T& val)
+ {
+ for (unsigned i = 0; i < n; ++i)
+ data_[i] = val;
+ }
+
+ unsigned size() const
+ {
+ return n;
+ }
+
+ // eq
+
+ bool operator==(const vec& rhs) const
+ {
+ for (unsigned i = 0; i < n; ++i)
+ if (data_[i] != rhs[i])
+ return false;
+ return true;
+ }
+
+ bool operator!=(const vec& rhs) const
+ {
+ return not (*this == rhs);
+ }
+
+ // +
+
+ template <typename U>
+ vec& operator+=(const vec<n,U>& rhs)
+ {
+ for (unsigned i = 0; i < n; ++i)
+ data_[i] += rhs[i];
+ return *this;
+ }
+
+ template <typename U>
+ vec<n, xtd_op_plus_trait(T, U)>
+ operator+(const vec<n,U>& rhs) const
+ {
+ vec<n, xtd_op_plus_trait(T, U)> tmp;
+ for (unsigned i = 0; i < n; ++i)
+ tmp[i] = data_[i] + rhs[i];
+ return tmp;
+ }
+
+ // -
+
+ template <typename U>
+ vec& operator-=(const vec<n,U>& rhs)
+ {
+ for (unsigned i = 0; i < n; ++i)
+ data_[i] -= rhs[i];
+ return *this;
+ }
+
+ template <typename U>
+ vec<n, xtd_op_minus_trait(T, U)>
+ operator-(const vec<n,U>& rhs) const
+ {
+ vec<n, xtd_op_plus_trait(T, U)> tmp;
+ for (unsigned i = 0; i < n; ++i)
+ tmp[i] = data_[i] - rhs[i];
+ return tmp;
+ }
+
+ vec<n, xtd_op_uminus_trait(T)>
+ operator-()
+ {
+ vec<n, xtd_op_uminus_trait(T)> tmp;
+ for (unsigned i = 0; i < n; ++i)
+ tmp[i] = - data_[i];
+ return tmp;
+ }
+
+ // *
+
+ template <typename U> void operator*=(const vec<n,U>& rhs);
+ template <typename U> void operator*(const vec<n,U>& rhs) const;
+
+ template <typename S>
+ vec& operator*=(const S& scalar)
+ {
+ for (unsigned i = 0; i < n; ++i)
+ data_[i] *= scalar;
+ return *this;
+ }
+
+ template <typename S>
+ vec<n, xtd_op_mult_trait(T, S)>
+ operator*(const S& scalar) const
+ {
+ vec<n, xtd_op_mult_trait(T, S)> tmp;
+ for (unsigned i = 0; i < n; ++i)
+ tmp[i] = data_[i] * scalar;
+ return tmp;
+ }
+
+ // /
+
+ template <typename U> void operator/=(const vec<n,U>& rhs);
+ template <typename U> void operator/(const vec<n,U>& rhs) const;
+
+ template <typename S>
+ vec& operator/=(const S& scalar)
+ {
+ assert(scalar != 0);
+ for (unsigned i = 0; i < n; ++i)
+ data_[i] /= scalar;
+ return *this;
+ }
+
+ template <typename S>
+ vec<n, xtd_op_div_trait(T, S)>
+ operator/(const S& scalar) const
+ {
+ assert(scalar != 0);
+ vec<n, xtd_op_div_trait(T, S)> tmp;
+ for (unsigned i = 0; i < n; ++i)
+ tmp[i] = data_[i] / scalar;
+ return tmp;
+ }
+
+ };
+
+
+
+ template <typename uop,
+ unsigned n, typename T>
+ struct set_trait_< uop, vec<n,T> >
+ {
+ typedef vec<n, xtd_unop_trait(uop, T)> ret;
+ };
+
+
+ template <typename bop,
+ unsigned n, typename L, typename R>
+ struct set_trait_< bop, vec<n,L>, vec<n,R> >
+ {
+ typedef vec<n, xtd_binop_trait(bop, L, R)> ret;
+ };
+
+
+} // end of namespace xtd
+
+
+#endif // ! EXTENDED_VEC_HH