00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef MATH_OPERATORS_INCLUDE
00014 #define MATH_OPERATORS_INCLUDE
00015
00016 #include <functional>
00017
00018 #ifndef MATH_DEFAULT_FUNCTORS_WITH_CONCEPTS
00019 namespace math {
00020
00021 template <typename Element>
00022 struct add : public std::binary_function<Element, Element, Element>
00023 {
00024 Element operator() (const Element& x, const Element& y)
00025 {
00026 return x + y;
00027 }
00028 };
00029
00030
00031
00032 template <typename A1, typename A2, typename R>
00033 struct heterogeneous_add
00034 : public std::binary_function<A1, A2, R>
00035 {
00036 R operator() (const A1& x, const A2& y)
00037 {
00038 return x + y;
00039 }
00040 };
00041
00042
00043
00044 template <> struct add<char> : heterogeneous_add<char, char, int> {};
00045 template <> struct add<short> : heterogeneous_add<short, short, int> {};
00046 template <> struct add<unsigned char> : heterogeneous_add<unsigned char, unsigned char, unsigned int> {};
00047 template <> struct add<unsigned short> : heterogeneous_add<unsigned short, unsigned short, unsigned int> {};
00048
00049
00050 template <typename Element>
00051 struct mult : public std::binary_function<Element, Element, Element>
00052 {
00053 Element operator() (const Element& x, const Element& y)
00054 {
00055 return x * y;
00056 }
00057 };
00058
00059
00060 template <typename A1, typename A2, typename R>
00061 struct heterogeneous_mult
00062 : public std::binary_function<A1, A2, R>
00063 {
00064 R operator() (const A1& x, const A2& y)
00065 {
00066 return x * y;
00067 }
00068 };
00069
00070
00071
00072 template <> struct mult<char> : heterogeneous_mult<char, char, int> {};
00073 template <> struct mult<short> : heterogeneous_mult<short, short, int> {};
00074 template <> struct mult<unsigned char> : heterogeneous_mult<unsigned char, unsigned char, unsigned int> {};
00075 template <> struct mult<unsigned short> : heterogeneous_mult<unsigned short, unsigned short, unsigned int> {};
00076
00077 #else
00078
00079
00080
00081 template <typename Element>
00082 requires std::HasPlus<Element>
00083 struct add : public std::binary_function<Element, Element, result_type>
00084 {
00085 result_type operator() (const Element& x, const Element& y)
00086 {
00087 return x + y;
00088 }
00089 };
00090
00091 template <typename Element>
00092 requires std::HasMultiply<Element>
00093 struct mult : public std::binary_function<Element, Element, result_type>
00094 {
00095 result_type operator() (const Element& x, const Element& y)
00096 {
00097 return x * y;
00098 }
00099 };
00100
00101
00102
00103
00104 #endif // MATH_DEFAULT_FUNCTORS_WITH_CONCEPTS
00105
00106 template <typename Element>
00107 struct min : public std::binary_function<Element, Element, Element>
00108 {
00109 Element operator() (const Element& x, const Element& y)
00110 {
00111 return x <= y ? x : y;
00112 }
00113 };
00114
00115
00116 template <typename Element>
00117 struct max : public std::binary_function<Element, Element, Element>
00118 {
00119 Element operator() (const Element& x, const Element& y)
00120 {
00121 return x >= y ? x : y;
00122 }
00123 };
00124
00125 template <typename Element>
00126 struct bitwise_and : public std::binary_function<Element, Element, Element>
00127 {
00128 Element operator() (const Element& x, const Element& y)
00129 {
00130 return x & y;
00131 }
00132 };
00133
00134 template <typename Element>
00135 struct bitwise_or : public std::binary_function<Element, Element, Element>
00136 {
00137 Element operator() (const Element& x, const Element& y)
00138 {
00139 return x | y;
00140 }
00141 };
00142
00143 template <typename Element>
00144 struct bitwise_xor : public std::binary_function<Element, Element, Element>
00145 {
00146 Element operator() (const Element& x, const Element& y)
00147 {
00148 return x ^ y;
00149 }
00150 };
00151
00152
00153 }
00154
00155 #endif // MATH_OPERATORS_INCLUDE