00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef MTL_ASSIGN_EACH_NONZERO_INCLUDE
00013 #define MTL_ASSIGN_EACH_NONZERO_INCLUDE
00014
00015 #include <boost/numeric/mtl/utility/tag.hpp>
00016 #include <boost/numeric/mtl/utility/range_generator.hpp>
00017
00018
00019 namespace mtl {
00020
00021
00022 template <typename Vector, typename Functor>
00023 inline void assign_each_nonzero(Vector& v, const Functor& f, tag::vector)
00024 {
00025 typedef typename traits::range_generator<tag::iter::nz, Vector>::type iterator;
00026 for (iterator i= begin<tag::iter::nz>(v), iend= end<tag::iter::nz>(v); i != iend; ++i)
00027 *i= f(*i);
00028 }
00029
00030
00031 template <typename Matrix, typename Functor>
00032 inline void assign_each_nonzero(Matrix& m, const Functor& f, tag::matrix)
00033 {
00034 typename traits::value<Matrix>::type value(m);
00035
00036 typedef typename traits::range_generator<tag::major, Matrix>::type cursor_type;
00037 typedef typename traits::range_generator<tag::nz, cursor_type>::type icursor_type;
00038
00039 for (cursor_type cursor = begin<tag::major>(m), cend = end<tag::major>(m); cursor != cend; ++cursor)
00040 for (icursor_type icursor = begin<tag::nz>(cursor), icend = end<tag::nz>(cursor);
00041 icursor != icend; ++icursor)
00042 {
00043
00044
00045 typename Collection<Matrix>::value_type tmp= value(*icursor);
00046 value(*icursor, f(tmp));
00047 }
00048
00049 #if 0
00050
00051
00052 typedef typename traits::range_generator<tag::major, Matrix>::type cursor_type;
00053 typedef typename traits::range_generator<tag::iter::nz, cursor_type>::type iterator;
00054
00055 for (cursor_type cursor = begin<tag::major>(m), cend = end<tag::major>(m); cursor != cend; ++cursor)
00056 for (iterator i= begin<tag::iter::nz>(cursor), iend= end<tag::iter::nz>(cursor); i != iend; ++i)
00057 *i= f(*i);
00058 #endif
00059 }
00060
00061
00063 template <typename Collection, typename Functor>
00064 inline void assign_each_nonzero(Collection& c, const Functor& f)
00065 {
00066 assign_each_nonzero(c, f, typename traits::category<Collection>::type());
00067 }
00068
00069 }
00070
00071 #endif // MTL_ASSIGN_EACH_NONZERO_INCLUDE