00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef MTL_VECTOR_MAP_VIEW_INCLUDE
00013 #define MTL_VECTOR_MAP_VIEW_INCLUDE
00014
00015 #include <boost/shared_ptr.hpp>
00016 #include <boost/numeric/mtl/utility/category.hpp>
00017 #include <boost/numeric/mtl/utility/range_generator.hpp>
00018 #include <boost/numeric/mtl/utility/property_map.hpp>
00019 #include <boost/numeric/mtl/utility/copy_expression_const_ref_container.hpp>
00020 #include <boost/numeric/mtl/operation/sfunctor.hpp>
00021 #include <boost/numeric/mtl/operation/tfunctor.hpp>
00022 #include <boost/numeric/mtl/operation/conj.hpp>
00023 #include <boost/numeric/mtl/vector/vec_expr.hpp>
00024
00025
00026 namespace mtl { namespace vector { namespace detail {
00027
00028 template <typename, typename> struct map_value;
00029 }}}
00030
00031 namespace mtl { namespace vector {
00032
00033 template <typename Functor, typename Vector>
00034 struct map_view
00035 : public vec_expr< map_view<Functor, Vector> >
00036 {
00037 typedef map_view self;
00038 typedef vec_expr< self > expr_base;
00039 typedef Vector other;
00040
00041 typedef typename Functor::result_type value_type;
00042 typedef typename Functor::result_type const_reference;
00043 typedef typename Vector::size_type size_type;
00044
00045 map_view (const Functor& functor, const other& ref)
00046 : expr_base(*this), functor(functor), ref(ref)
00047 {
00048 ref.delay_assign();
00049 }
00050
00051 map_view (const Functor& functor, boost::shared_ptr<Vector> p)
00052 : expr_base(*this), functor(functor), my_copy(p), ref(*p)
00053 {
00054 ref.delay_assign();
00055 }
00056
00057
00058 friend size_type inline size(const self& v) { return size(v.ref); }
00059 friend size_type inline num_rows(const self& v) { return num_rows(v.ref); }
00060 friend size_type inline num_cols(const self& v) { return num_cols(v.ref); }
00061
00062 size_type stride() const { return ref.stride(); }
00063 const_reference operator() (size_type i) const { return functor(ref(i)); }
00064 const_reference operator[] (size_type i) const { return functor(ref[i]); }
00065 void delay_assign() const {}
00066
00067 template <typename, typename> friend struct detail::map_value;
00068
00069 protected:
00070 boost::shared_ptr<Vector> my_copy;
00071 public:
00072 Functor functor;
00073
00074 typename mtl::traits::copy_expression_const_ref_container<Vector>::type ref;
00075 };
00076
00077
00078
00079
00080
00081
00082 namespace detail {
00083
00084 template <typename Functor, typename Vector>
00085 struct map_value
00086 {
00087 typedef typename Vector::key_type key_type;
00088 typedef typename vector::map_view<Functor, Vector>::value_type value_type;
00089
00090 map_value(vector::map_view<Functor, Vector> const& map_vector)
00091 : map_vector(map_vector), its_value(map_vector.ref)
00092 {}
00093
00094 value_type operator() (key_type const& key) const
00095 {
00096 return map_vector.functor(its_value(key));
00097 }
00098
00099 protected:
00100 vector::map_view<Functor, Vector> const& map_vector;
00101 typename ::mtl::traits::const_value<Vector>::type its_value;
00102 };
00103
00104 }
00105
00106 }}
00107
00108
00109
00110 namespace mtl { namespace traits {
00111
00112
00113
00114
00115
00116 template <typename Functor, typename Vector>
00117 struct index<vector::map_view<Functor, Vector> >
00118 : public index<Vector>
00119 {};
00120
00121 template <typename Functor, typename Vector>
00122 struct const_value<vector::map_view<Functor, Vector> >
00123 {
00124 typedef vector::detail::map_value<Functor, Vector> type;
00125 };
00126
00127
00128
00129
00130
00131
00132
00133 template <typename Tag, typename Functor, typename Vector>
00134 struct range_generator<Tag, vector::map_view<Functor, Vector> >
00135 : public range_generator<Tag, Vector>
00136 {};
00137
00138 }}
00139
00140 namespace mtl { namespace vector {
00141
00142 template <typename Scaling, typename Vector>
00143 struct scaled_view
00144 : public map_view<tfunctor::scale<Scaling, typename Vector::value_type>, Vector>
00145 {
00146 typedef tfunctor::scale<Scaling, typename Vector::value_type> functor_type;
00147 typedef map_view<functor_type, Vector> base;
00148
00149 scaled_view(const Scaling& scaling, const Vector& vector)
00150 : base(functor_type(scaling), vector)
00151 {}
00152
00153 scaled_view(const Scaling& scaling, boost::shared_ptr<Vector> p)
00154 : base(functor_type(scaling), p)
00155 {}
00156 };
00157
00158
00159 template <typename Vector, typename RScaling>
00160 struct rscaled_view
00161 : public map_view<tfunctor::rscale<typename Vector::value_type, RScaling>, Vector>
00162 {
00163 typedef tfunctor::rscale<typename Vector::value_type, RScaling> functor_type;
00164 typedef map_view<functor_type, Vector> base;
00165
00166 rscaled_view(const Vector& vector, const RScaling& rscaling)
00167 : base(functor_type(rscaling), vector)
00168 {}
00169
00170 rscaled_view(boost::shared_ptr<Vector> p, const RScaling& rscaling)
00171 : base(functor_type(rscaling), p)
00172 {}
00173 };
00174
00175
00176
00177 template <typename Vector, typename Divisor>
00178 struct divide_by_view
00179 : public map_view<tfunctor::divide_by<typename Vector::value_type, Divisor>, Vector>
00180 {
00181 typedef tfunctor::divide_by<typename Vector::value_type, Divisor> functor_type;
00182 typedef map_view<functor_type, Vector> base;
00183
00184 divide_by_view(const Vector& vector, const Divisor& div)
00185 : base(functor_type(div), vector)
00186 {}
00187
00188 divide_by_view(boost::shared_ptr<Vector> p, const Divisor& div)
00189 : base(functor_type(div), p)
00190 {}
00191 };
00192
00193
00194 template <typename Vector>
00195 struct conj_view
00196 : public map_view<mtl::sfunctor::conj<typename Vector::value_type>, Vector>
00197 {
00198 typedef mtl::sfunctor::conj<typename Vector::value_type> functor_type;
00199 typedef map_view<functor_type, Vector> base;
00200
00201 conj_view(const Vector& vector)
00202 : base(functor_type(), vector)
00203 {}
00204
00205 conj_view(boost::shared_ptr<Vector> p)
00206 : base(functor_type(), p)
00207 {}
00208 };
00209
00210 template <typename Vector>
00211 struct negate_view
00212 : public map_view<mtl::sfunctor::negate<typename Vector::value_type>, Vector>
00213 {
00214 typedef mtl::sfunctor::negate<typename Vector::value_type> functor_type;
00215 typedef map_view<functor_type, Vector> base;
00216
00217 negate_view(const Vector& vector)
00218 : base(functor_type(), vector)
00219 {}
00220
00221 negate_view(boost::shared_ptr<Vector> p)
00222 : base(functor_type(), p)
00223 {}
00224 };
00225
00226
00227
00228 }}
00229
00230
00231 #endif // MTL_VECTOR_MAP_VIEW_INCLUDE