00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012 #ifndef MTL_COMPLEXITY_INCLUDE
00013 #define MTL_COMPLEXITY_INCLUDE
00014
00015 #include <boost/mpl/int.hpp>
00016 #include <boost/mpl/max.hpp>
00017 #include <boost/mpl/if.hpp>
00018 #include <boost/mpl/less.hpp>
00019 #include <boost/mpl/less_equal.hpp>
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 namespace mtl {
00032 namespace complexity_classes {
00033
00035
00039 struct cached : boost::mpl::int_<1> {};
00040
00042 struct constant : boost::mpl::int_<2> {};
00043
00045 struct log_n : boost::mpl::int_<4> {};
00046
00048 struct polylog_n : boost::mpl::int_<5> {};
00049
00051 struct linear_cached : boost::mpl::int_<21> {};
00052
00054 struct linear : boost::mpl::int_<22> {};
00055
00057 struct n_log_n : boost::mpl::int_<24> {};
00058
00060 struct n_polylog_n : boost::mpl::int_<25> {};
00061
00062 struct quadratic : boost::mpl::int_<41> {};
00063
00065 struct polynomial : boost::mpl::int_<200> {};
00066
00068 struct infinite : boost::mpl::int_<1000> {};
00069
00071 template <typename X, typename Y>
00072 struct plus : boost::mpl::if_< boost::mpl::less<X, Y>, Y, X> {};
00073
00075 template <typename X, typename Y>
00076 struct min : boost::mpl::if_< boost::mpl::less<X, Y>, X, Y> {};
00077
00078
00079 namespace detail
00080 {
00081
00082
00083
00084 template <typename X, typename Y> struct times
00085 {
00086 typedef polynomial type;
00087 };
00088
00089 template <typename Y> struct times<cached, Y>
00090 {
00091 typedef Y type;
00092 };
00093
00094 template <typename Y> struct times<constant, Y>
00095 {
00096 typedef Y type;
00097 };
00098
00099 template <> struct times<log_n, log_n>
00100 {
00101 typedef polylog_n type;
00102 };
00103
00104 template <> struct times<log_n, polylog_n> : times<log_n, log_n> {};
00105
00106 template <> struct times<log_n, linear_cached>
00107 {
00108 typedef n_log_n type;
00109 };
00110
00111 template <> struct times<log_n, linear> : times<log_n, linear_cached> {};
00112
00113 template <> struct times<log_n, n_log_n>
00114 {
00115 typedef n_polylog_n type;
00116 };
00117
00118 template <> struct times<log_n, n_polylog_n> : times<log_n, n_log_n> {};
00119
00120 template <> struct times<polylog_n, polylog_n>
00121 {
00122 typedef polylog_n type;
00123 };
00124
00125 template <> struct times<polylog_n, linear_cached>
00126 {
00127 typedef n_polylog_n type;
00128 };
00129
00130 template <> struct times<polylog_n, linear> : times<polylog_n, linear_cached> {};
00131
00132 template <> struct times<polylog_n, n_log_n> : times<polylog_n, linear_cached> {};
00133
00134 template <> struct times<polylog_n, n_polylog_n> : times<polylog_n, linear_cached> {};
00135
00136 template <> struct times<linear_cached, linear_cached>
00137 {
00138 typedef quadratic type;
00139 };
00140
00141 template <> struct times<linear_cached, linear> : times<linear_cached, linear_cached> {};
00142
00143 template <> struct times<linear, linear> : times<linear_cached, linear_cached> {};
00144
00145 }
00146
00148
00149
00150 template <typename X, typename Y>
00151 struct times
00152 : boost::mpl::if_<
00153 boost::mpl::less<X, Y>
00154 , detail::times<X, Y>
00155 , detail::times<Y, X>
00156 >::type
00157 {};
00158
00159
00160
00161
00162 template <typename X>
00163 struct times<X, infinite>
00164 {
00165 typedef infinite type;
00166 };
00167
00168 template <typename X> struct times<infinite, X> : times<X, infinite> {};
00169
00170 }}
00171
00172 #define MTL_PRINT_COMPLEXITY(TYPE, STRING) \
00173 inline std::ostream& operator<< (std::ostream& os, mtl::complexity_classes::TYPE) \
00174 { \
00175 return os << STRING; \
00176 }
00177
00178 MTL_PRINT_COMPLEXITY(cached, "cached constant complexity")
00179 MTL_PRINT_COMPLEXITY(constant, "constant complexity")
00180 MTL_PRINT_COMPLEXITY(log_n, "logarithmic complexity")
00181 MTL_PRINT_COMPLEXITY(polylog_n, "poly-logarithmic complexity")
00182 MTL_PRINT_COMPLEXITY(linear_cached, "cached linear complexity")
00183 MTL_PRINT_COMPLEXITY(linear, "linear complexity")
00184 MTL_PRINT_COMPLEXITY(n_log_n, "n log n complexity")
00185 MTL_PRINT_COMPLEXITY(n_polylog_n, "n poly-log n complexity")
00186 MTL_PRINT_COMPLEXITY(quadratic, "quadratic complexity")
00187 MTL_PRINT_COMPLEXITY(polynomial, "polynomial complexity")
00188 MTL_PRINT_COMPLEXITY(infinite, "infinite complexity")
00189
00190
00191
00192 #endif // MTL_COMPLEXITY_INCLUDE