00001 #ifndef NMWR_GB_FUNCTION_ADAPTER_H
00002 #define NMWR_GB_FUNCTION_ADAPTER_H
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "Utility/pre-post-conditions.h"
00020 #include "Utility/ref-ptr.h"
00021
00022 namespace GrAL {
00023
00057 template<class F>
00058 class unary_fct_ref {
00059 private:
00060 const F* f;
00061 public:
00062 typedef typename F::argument_type argument_type;
00063 typedef typename F::result_type result_type;
00064
00065 unary_fct_ref() : f(0) {}
00066 unary_fct_ref(const F& ff) : f(&ff) {}
00067
00068 result_type operator()(const argument_type& x) const {
00069 REQUIRE((f != 0), "No function!\n",1);
00070 return (*f)(x);
00071 }
00072 };
00073
00077 template<class F>
00078 inline unary_fct_ref<F> make_unary_fct_ref(const F& f)
00079 { return unary_fct_ref<F>(f); }
00080
00081
00091 template<class M1, class M2>
00092 class unary_map_composition {
00093 private:
00094 M1 const* m1;
00095 M2 const* m2;
00096
00097 public:
00098 typedef typename M2::argument_type argument_type;
00099 typedef typename M1::result_type result_type;
00100 typedef typename M2::domain_type domain_type;
00101 typedef typename M1::range_type range_type;
00102
00104 unary_map_composition(M1 const& mm1,M2 const& mm2) : m1(&mm1), m2(&mm2) {}
00105
00107 result_type operator()(argument_type const& x) const { return (*m1)((*m2)(x));}
00108
00114 domain_type const& domain() const { return m2->domain();}
00115
00117 range_type const& range() const { return m1->range();}
00118 };
00119
00120
00124 template<class M1, class M2>
00125 inline
00126 unary_map_composition<M1,M2>
00127 compose_map( M1 const& m1, M2 const& m2)
00128 { return unary_map_composition<M1,M2>(m1,m2);}
00129
00130
00131
00132
00133
00139 template<class ARG, class RES>
00140 class constant : public std::unary_function<ARG,RES> {
00141 RES r;
00142 public:
00143 constant() {}
00144 constant(RES rr) : r(rr) {}
00145
00146 RES operator()(ARG const&) const { return r;}
00147 };
00148
00149
00150
00155 template<class F, class ARG = typename F::argument_type, class RES = typename F::result_type>
00156 class map_is_equal {
00157 public:
00158 typedef F mapping_type;
00159 typedef ARG argument_type;
00160 typedef RES map_result_type;
00161 typedef bool result_type;
00162
00163 private:
00164 ref_ptr<mapping_type const> f;
00165 map_result_type res;
00166 public:
00168 map_is_equal() {}
00170 map_is_equal(mapping_type const& ff, map_result_type r) : f(ff), res(r) {}
00172 map_is_equal(ref_ptr<mapping_type const> ff, map_result_type r) : f(ff), res(r) {}
00173
00175 bool operator()(argument_type a) const { return (*f)(a) == res;}
00176 };
00177
00178
00183 template<class P1, class P2>
00184 class and_pred : public std::unary_function<typename P1::argument_type, bool>
00185 {
00186 public:
00187 typedef typename std::unary_function<typename P1::argument_type, bool>::result_type result_type;
00188 typedef typename std::unary_function<typename P1::argument_type, bool>::argument_type argument_type;
00189 private:
00190 P1 p1;
00191 P2 p2;
00192 public:
00193 and_pred() {}
00194 and_pred(P1 pp1, P2 pp2) : p1(pp1), p2(pp2) {}
00195
00196 bool operator()(argument_type a) const { return p1(a) && p2(a);}
00197 };
00198
00203 template<class P1, class P2>
00204 class or_pred : public std::unary_function<typename P1::argument_type, bool>
00205 {
00206 public:
00207 typedef typename std::unary_function<typename P1::argument_type, bool>::result_type result_type;
00208 typedef typename std::unary_function<typename P1::argument_type, bool>::argument_type argument_type;
00209 private:
00210 P1 p1;
00211 P2 p2;
00212 public:
00213 or_pred() {}
00214 or_pred(P1 pp1, P2 pp2) : p1(pp1), p2(pp2) {}
00215
00216 bool operator()(argument_type const& a) const { return p1(a) || p2(a);}
00217 };
00218
00219
00224 template<class P1>
00225 class not_pred : public std::unary_function<typename P1::argument_type, bool>
00226 {
00227 public:
00228 typedef typename std::unary_function<typename P1::argument_type, bool>::result_type result_type;
00229 typedef typename std::unary_function<typename P1::argument_type, bool>::argument_type argument_type;
00230 private:
00231 P1 p1;
00232 public:
00233 not_pred() {}
00234 not_pred(P1 pp1) : p1(pp1) {}
00235
00236 bool operator()(argument_type const& a) const { return ! p1(a);}
00237 };
00238
00239
00248 template<class F1, class ARG1, class RES1, class P2>
00249 inline and_pred<map_is_equal<F1,ARG1,RES1>, P2>
00250 operator&&(map_is_equal<F1,ARG1,RES1> p1, P2 p2)
00251 { return and_pred<map_is_equal<F1,ARG1,RES1>, P2>(p1,p2);}
00252
00261 template<class F1, class ARG1, class RES1, class P2>
00262 inline or_pred<map_is_equal<F1,ARG1,RES1>, P2>
00263 operator||(map_is_equal<F1,ARG1,RES1> p1, P2 p2)
00264 { return or_pred<map_is_equal<F1,ARG1,RES1>, P2>(p1,p2);}
00265
00270 template<class F1, class ARG1, class RES1>
00271 inline not_pred<map_is_equal<F1,ARG1,RES1> >
00272 operator!(map_is_equal<F1,ARG1,RES1> p1)
00273 { return not_pred<map_is_equal<F1,ARG1,RES1> >(p1);}
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290
00291
00292
00293 }
00294
00295 #endif
00296