URL:
https://svn.lrde.epita.fr/svn/oln/trunk/milena
ChangeLog:
2007-09-17 Simon Nivault <simon.nivault(a)lrde.epita.fr>
Add vector and trait for binary
* sandbox/nivault/binary_arith.hh: New.
* sandbox/nivault/tests/test.cc: Update.
* sandbox/nivault/vec.hh: New.
---
binary_arith.hh | 73 +++++++++++
tests/test.cc | 18 --
vec.hh | 363 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
3 files changed, 441 insertions(+), 13 deletions(-)
Index: trunk/milena/sandbox/nivault/tests/test.cc
===================================================================
--- trunk/milena/sandbox/nivault/tests/test.cc (revision 1116)
+++ trunk/milena/sandbox/nivault/tests/test.cc (revision 1117)
@@ -30,22 +30,14 @@
* \brief Test on mln::median::median_dir.
*/
-#include <median.hh>
-#include <mln/io/load_pgm.hh>
-#include <mln/io/save_pgm.hh>
-#include <mln/level/paste.hh>
-#include <mln/core/win/hline2d.hh>
+#include <vec.hh>
int main()
{
- using namespace mln;
+ using namespace xtd;
- image2d_b< value::int_u8 >
- lena = io::load_pgm("lena.pgm"),
- out = image2d_b< value::int_u8 >(lena.domain());
+ vec<3,int> v_int = mk_vec(3,6,7);
+ vec<3,float> v_f = mk_vec(2.6, 1.9, 5.2);
- win::hline2d win(7);
-
- level::impl::median_dir(lena, win, 0, out);
- io::save_pgm(out, "out.pgm");
+ std::cout << v_int + v_f << std::endl;
}
Index: trunk/milena/sandbox/nivault/binary_arith.hh
===================================================================
--- trunk/milena/sandbox/nivault/binary_arith.hh (revision 0)
+++ trunk/milena/sandbox/nivault/binary_arith.hh (revision 1117)
@@ -0,0 +1,73 @@
+// 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 MLN_CORE_TRAIT_BINARY_ARITH_HH
+# define MLN_CORE_TRAIT_BINARY_ARITH_HH
+
+
+ template <typename T, typename U>
+ struct binary_arith
+ {
+ typedef T ret;
+ };
+
+
+ template <>
+ struct binary_arith<int, float>
+ {
+ typedef float ret;
+ };
+ template <>
+ struct binary_arith<float, int>
+ {
+ typedef float ret;
+ };
+
+ template <>
+ struct binary_arith<int, double>
+ {
+ typedef double ret;
+ };
+ template <>
+ struct binary_arith<double, int>
+ {
+ typedef double ret;
+ };
+
+ template <>
+ struct binary_arith<double, float>
+ {
+ typedef double ret;
+ };
+ template <>
+ struct binary_arith<float, double>
+ {
+ typedef double ret;
+ };
+
+
+#endif // ! MLN_CORE_TRAIT_BINARY_ARITH_HH
Index: trunk/milena/sandbox/nivault/vec.hh
===================================================================
--- trunk/milena/sandbox/nivault/vec.hh (revision 0)
+++ trunk/milena/sandbox/nivault/vec.hh (revision 1117)
@@ -0,0 +1,363 @@
+// 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 XTD_VEC_HH
+# define XTD_VEC_HH
+
+# include <iostream>
+
+# include "binary_arith.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];
+ };
+
+ template <typename T>
+ class vec_base_ <4, T>
+ {
+ public:
+ void set(const T& val0, const T& val1, const T& val2, const T&
val3)
+ {
+ data_[0] = val0;
+ data_[1] = val1;
+ data_[2] = val2;
+ data_[3] = val3;
+ }
+ protected:
+ T data_[4];
+ };
+
+
+ } // 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
+ {
+// precondition(i < dim);
+ return data_[i];
+ }
+
+ T& operator[](unsigned i)
+ {
+// precondition(i < dim);
+ 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
+
+ template <unsigned n, typename T, typename U>
+ bool operator==(const vec<n,T>& lhs, const vec<n,U>& rhs)
+ {
+ for (unsigned i = 0; i < n; ++i)
+ if (lhs[i] != rhs[i])
+ return false;
+ return true;
+ }
+
+ template <unsigned n, typename T, typename U>
+ bool operator!=(const vec<n,T>& lhs, const vec<n,U>& rhs)
+ {
+ return not (lhs == rhs);
+ }
+
+
+ // +
+
+ template <unsigned n, typename T, typename U>
+ vec<n,T>&
+ operator+=(vec<n,T>& lhs, const vec<n,U>& rhs)
+ {
+ for (unsigned i = 0; i < n; ++i)
+ lhs[i] += rhs[i];
+ return lhs;
+ }
+
+ template <unsigned n, typename T, typename U>
+ vec<n, typename binary_arith<T, U>::ret >
+ operator+(const vec<n,T>& lhs, const vec<n,U>& rhs)
+ {
+ vec<n, typename binary_arith<T, U>::ret> tmp;
+ for (unsigned i = 0; i < n; ++i)
+ tmp[i] = lhs[i] + rhs[i];
+ return tmp;
+ }
+
+
+ // -
+
+ template <unsigned n, typename T, typename U>
+ vec<n,T>&
+ operator-=(vec<n,T>& lhs, const vec<n,U>& rhs)
+ {
+ for (unsigned i = 0; i < n; ++i)
+ lhs[i] -= rhs[i];
+ return lhs;
+ }
+
+ template <unsigned n, typename T, typename U>
+ vec<n, typename binary_arith<T, U>::ret>
+ operator-(const vec<n,T>& lhs, const vec<n,U>& rhs)
+ {
+ vec<n, typename binary_arith<T, U>::ret> tmp;
+ for (unsigned i = 0; i < n; ++i)
+ tmp[i] = lhs[i] - rhs[i];
+ return tmp;
+ }
+
+ template <unsigned n, typename T>
+ vec<n, T>
+ operator-(const vec<n,T>& lhs)
+ {
+ vec<n, T> tmp;
+ for (unsigned i = 0; i < n; ++i)
+ tmp[i] = - lhs[i];
+ return tmp;
+ }
+
+
+ // *
+
+ template <unsigned n, typename T, typename S>
+ vec<n,T>&
+ operator*=(vec<n,T>& lhs, const S& scalar)
+ {
+ for (unsigned i = 0; i < n; ++i)
+ lhs[i] *= scalar;
+ return lhs;
+ }
+
+ template <unsigned n, typename T, typename S>
+ vec<n, typename binary_arith<T, S>::ret>
+ operator*(const vec<n,T>& lhs, const S& scalar)
+ {
+ vec<n, typename binary_arith<T, S>::ret> tmp;
+ for (unsigned i = 0; i < n; ++i)
+ tmp[i] = lhs[i] * scalar;
+ return tmp;
+ }
+
+
+ // /
+
+ template <unsigned n, typename T, typename S>
+ vec<n,T>&
+ operator/=(vec<n,T>& lhs, const S& scalar)
+ {
+ precondition(scalar != 0);
+ for (unsigned i = 0; i < n; ++i)
+ lhs[i] /= scalar;
+ return lhs;
+ }
+
+ template <unsigned n, typename T, typename S>
+ vec<n, typename binary_arith<T, S>::ret>
+ operator/(const vec<n,T>& lhs, const S& scalar)
+ {
+ precondition(scalar != 0);
+ vec<n, typename binary_arith<T, S>::ret> tmp;
+ for (unsigned i = 0; i < n; ++i)
+ tmp[i] = lhs[i] / scalar;
+ return tmp;
+ }
+
+
+ // <<
+
+ template <unsigned n, typename T>
+ std::ostream&
+ operator<<(std::ostream& ostr, const vec<n,T>& v)
+ {
+ ostr << '(';
+ for (unsigned i = 0; i < n; ++i)
+ ostr << v[i] << (i == n - 1 ? ")" : ", ");
+ return ostr;
+ }
+
+ template <unsigned n>
+ std::ostream&
+ operator<<(std::ostream& ostr, const vec<n,unsigned char>& v)
+ {
+ ostr << '(';
+ for (unsigned i = 0; i < n; ++i)
+ ostr << (unsigned int)(v[i]) << (i == n - 1 ? ")" : ",
");
+ return ostr;
+ }
+
+ template <unsigned n>
+ std::ostream&
+ operator<<(std::ostream& ostr, const vec<n,signed char>& v)
+ {
+ ostr << '(';
+ for (unsigned i = 0; i < n; ++i)
+ ostr << (signed int)(v[i]) << (i == n - 1 ? ")" : ",
");
+ return ostr;
+ }
+
+
+ /// mk_vec.
+
+ template <typename T>
+ vec<1, T> mk_vec(const T& v_0)
+ {
+ vec<1, T> tmp;
+ tmp[0] = v_0;
+ return tmp;
+ }
+
+ template <typename T>
+ vec<2, T> mk_vec(const T& v_0, const T& v_1)
+ {
+ vec<2, T> tmp;
+ tmp[0] = v_0;
+ tmp[1] = v_1;
+ return tmp;
+ }
+
+ template <typename T>
+ vec<3, T> mk_vec(const T& v_0, const T& v_1, const T& v_2)
+ {
+ vec<3, T> tmp;
+ tmp[0] = v_0;
+ tmp[1] = v_1;
+ tmp[2] = v_2;
+ return tmp;
+ }
+
+ template <typename T>
+ vec<4, T> mk_vec(const T& v_0, const T& v_1, const T& v_2, const
T& v_3)
+ {
+ vec<4, T> tmp;
+ tmp[0] = v_0;
+ tmp[1] = v_1;
+ tmp[2] = v_2;
+ tmp[3] = v_3;
+ return tmp;
+ }
+
+
+} // end of namespace xtd
+
+
+#endif // ! XTD_VEC_HH