00001
00016 #ifndef _DLR_COMMON_FUNCTIONAL_H_
00017 #define _DLR_COMMON_FUNCTIONAL_H_
00018
00019 #include <algorithm>
00020 #include <functional>
00021 #include <utility>
00022
00023 namespace dlr {
00024
00025 namespace common {
00026
00032 template <class Functor0, class Functor1, class Functor2>
00033 class BinaryComposeFunctor
00034 : public std::binary_function<typename Functor1::argument_type,
00035 typename Functor2::argument_type,
00036 typename Functor0::result_type>
00037 {
00038 public:
00063 BinaryComposeFunctor(const Functor0& functor0, const Functor1& functor1,
00064 const Functor2& functor2)
00065 : m_functor0(functor0), m_functor1(functor1), m_functor2(functor2) {}
00066
00085 typename Functor0::result_type
00086 operator()(const typename Functor1::argument_type& argument0,
00087 const typename Functor2::argument_type& argument1) {
00088 return m_functor0(m_functor1(argument0), m_functor2(argument1));
00089 }
00090
00091 protected:
00092
00094 Functor0 m_functor0;
00095
00097 Functor1 m_functor1;
00098
00100 Functor1 m_functor2;
00101 };
00102
00103
00109 template <class Functor0, class Functor1>
00110 class ComposeFunctor_1_2
00111 : public std::binary_function<typename Functor1::first_argument_type,
00112 typename Functor1::second_argument_type,
00113 typename Functor0::result_type>
00114 {
00115 public:
00133 ComposeFunctor_1_2(const Functor0& functor0, const Functor1& functor1)
00134 : m_functor0(functor0), m_functor1(functor1) {}
00135
00136
00153 typename Functor0::result_type
00154 operator()(const typename Functor1::first_argument_type& argument0,
00155 const typename Functor1::second_argument_type& argument1) {
00156 return m_functor0(m_functor1(argument0, argument1));
00157 }
00158
00159 protected:
00160
00162 Functor0 m_functor0;
00163
00165 Functor1 m_functor1;
00166 };
00167
00168
00173 template <class Type0, class Type1>
00174 class ExtractFirstFunctor
00175 : public std::unary_function<std::pair<Type0, Type1>, Type0>
00176 {
00177 public:
00181 ExtractFirstFunctor() {}
00182
00183
00192 Type0
00193 operator()(const std::pair<Type0, Type1>& argument) {
00194 return argument.first;
00195 }
00196
00197
00206 Type0&
00207 operator()(std::pair<Type0, Type1>& argument) {
00208 return argument.first;
00209 }
00210 };
00211
00212
00217 template <class Type0, class Type1>
00218 class ExtractSecondFunctor
00219 : public std::unary_function<std::pair<Type0, Type1>, Type1>
00220 {
00221 public:
00225 ExtractSecondFunctor() {}
00226
00227
00236 Type1
00237 operator()(const std::pair<Type0, Type1>& argument) {
00238 return argument.second;
00239 }
00240
00241
00250 Type1&
00251 operator()(std::pair<Type0, Type1>& argument) {
00252 return argument.second;
00253 }
00254 };
00255
00256
00263 template <class Type>
00264 class ApproximatelyEqualFunctor
00265 : public std::binary_function<Type, Type, bool>
00266 {
00267 public:
00278 ApproximatelyEqualFunctor(const Type& epsilon=static_cast<Type>(0))
00279 : m_epsilon(epsilon) {}
00280
00295 inline bool
00296 operator()(const Type& argument0, const Type& argument1) {
00297 Type difference = argument0 - argument1;
00298 return ((difference <= m_epsilon) && (difference >= (-m_epsilon)));
00299 }
00300
00301 protected:
00302
00304 Type m_epsilon;
00305 };
00306
00307
00314 template <class ArgumentType0, class ArgumentType1, class ResultType>
00315 class PointerToBinaryFunctionRA
00316 : public std::binary_function<ArgumentType0, ArgumentType0, ResultType>
00317 {
00318 public:
00321 typedef ResultType (*FunctionPtrType)(const ArgumentType0&,
00322 const ArgumentType1&);
00323
00327 explicit
00328 PointerToBinaryFunctionRA(FunctionPtrType functionPtr)
00329 : m_functionPtr(functionPtr) {}
00330
00340 inline ResultType
00341 operator()(const ArgumentType0& argument0,
00342 const ArgumentType0& argument1) const {
00343 return m_functionPtr(argument0, argument1);
00344 }
00345
00346 private:
00347 FunctionPtrType m_functionPtr;
00348 };
00349
00350
00355 template <class TypeIn, class TypeOut>
00356 struct StaticCastFunctor : public std::unary_function<TypeIn, TypeOut> {
00357
00365 inline TypeOut
00366 operator()(const TypeIn& input) {
00367 return static_cast<TypeOut>(input);
00368 }
00369 };
00370
00371
00376 template <class Functor0, class Functor1>
00377 class UnaryComposeFunctor
00378 : public std::unary_function<typename Functor1::argument_type,
00379 typename Functor0::result_type>
00380 {
00381 public:
00399 UnaryComposeFunctor(const Functor0& functor0, const Functor1& functor1)
00400 : m_functor0(functor0), m_functor1(functor1) {}
00401
00413 typename Functor0::result_type
00414 operator()(const typename Functor1::argument_type& argument) {
00415 return m_functor0(m_functor1(argument));
00416 }
00417
00418 protected:
00419
00421 Functor0 m_functor0;
00422
00424 Functor1 m_functor1;
00425 };
00426
00427
00428
00429
00449 template<class Type>
00450 inline bool
00451 approximatelyEqual(const Type& argument0, const Type& argument1,
00452 const Type& epsilon) {
00453 return (ApproximatelyEqualFunctor<Type>(epsilon))(argument0, argument1);
00454 }
00455
00456
00473 template <class Functor0, class Functor1, class Functor2>
00474 inline BinaryComposeFunctor<Functor0, Functor1, Functor2>
00475 binaryComposeFunctor(const Functor0& functor0,
00476 const Functor1& functor1,
00477 const Functor2& functor2) {
00478 return BinaryComposeFunctor<Functor0, Functor1, Functor2>(
00479 functor0, functor1, functor2);
00480 }
00481
00482
00497 template <class Type>
00498 Type
00499 clip(Type value, Type lowerBound, Type upperBound) {
00500 #ifdef WIN32
00501 return std::_cpp_max(std::_cpp_min(value, upperBound), lowerBound);
00502 #else
00503 return std::max(std::min(value, upperBound), lowerBound);
00504 #endif
00505 }
00506
00507
00521 template <class Functor0, class Functor1>
00522 inline ComposeFunctor_1_2<Functor0, Functor1>
00523 composeFunctor_1_2(const Functor0& functor0,
00524 const Functor1& functor1) {
00525 return ComposeFunctor_1_2<Functor0, Functor1>(functor0, functor1);
00526 }
00527
00528
00542 template <class Functor0, class Functor1>
00543 inline UnaryComposeFunctor<Functor0, Functor1>
00544 unaryComposeFunctor(const Functor0& functor0,
00545 const Functor1& functor1) {
00546 return UnaryComposeFunctor<Functor0, Functor1>(functor0, functor1);
00547 }
00548
00549
00550 }
00551
00552 }
00553
00554
00555
00556
00557 namespace dlr {
00558
00559 using common::BinaryComposeFunctor;
00560 using common::ComposeFunctor_1_2;
00561 using common::ExtractFirstFunctor;
00562 using common::ExtractSecondFunctor;
00563 using common::ApproximatelyEqualFunctor;
00564 using common::PointerToBinaryFunctionRA;
00565 using common::StaticCastFunctor;
00566 using common::UnaryComposeFunctor;
00567 using common::approximatelyEqual;
00568 using common::binaryComposeFunctor;
00569 using common::composeFunctor_1_2;
00570 using common::unaryComposeFunctor;
00571
00572 }
00573
00574
00575
00576 #include <dlrCommon/types.h>
00577
00578 namespace dlr {
00579
00580 namespace common {
00581
00582 template <>
00583 inline bool
00584 ApproximatelyEqualFunctor<bool>::
00585 operator()(const bool& argument0, const bool& argument1) {
00586 return argument0 == argument1;
00587 }
00588
00589
00590 template<>
00591 inline bool
00592 ApproximatelyEqualFunctor<size_t>::
00593 operator()(const size_t& argument0, const size_t& argument1) {
00594 Int64 difference = static_cast<Int64>(argument0) - static_cast<Int64>(argument1);
00595 return ((difference <= static_cast<Int64>(m_epsilon))
00596 && (difference >= (-static_cast<Int64>(m_epsilon))));
00597 }
00598
00599 }
00600
00601 }
00602
00603 #endif // #ifndef _DLR_COMMON_FUNCTIONAL_H_