00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #ifndef GIL_DYNAMIC_AT_C_HPP
00014 #define GIL_DYNAMIC_AT_C_HPP
00015
00016 #include "../../gil_config.hpp"
00017 #include <cassert>
00018 #include <stdexcept>
00019 #include <boost/mpl/at.hpp>
00020 #include <boost/mpl/size.hpp>
00021
00022
00031
00032 namespace boost { namespace gil {
00033
00034 #define GIL_AT_C_VALUE(z, N, text) mpl::at_c<IntTypes,S+N>::type::value,
00035 #define GIL_DYNAMIC_AT_C_LIMIT 226 // size of the maximum vector to handle
00036
00037 #define GIL_AT_C_LOOKUP(z, NUM, text) \
00038 template<std::size_t S> \
00039 struct at_c_fn<S,NUM> { \
00040 template <typename IntTypes, typename ValueType> inline \
00041 static ValueType apply(std::size_t index) { \
00042 static ValueType table[] = { \
00043 BOOST_PP_REPEAT(NUM, GIL_AT_C_VALUE, BOOST_PP_EMPTY) \
00044 }; \
00045 return table[index]; \
00046 } \
00047 };
00048
00049 namespace detail {
00050 namespace at_c {
00051 template <std::size_t START, std::size_t NUM> struct at_c_fn;
00052 BOOST_PP_REPEAT(GIL_DYNAMIC_AT_C_LIMIT, GIL_AT_C_LOOKUP, BOOST_PP_EMPTY)
00053
00054 template <std::size_t QUOT> struct at_c_impl;
00055
00056 template <>
00057 struct at_c_impl<0> {
00058 template <typename IntTypes, typename ValueType> inline
00059 static ValueType apply(std::size_t index) {
00060 return at_c_fn<0,mpl::size<IntTypes>::value>::template apply<IntTypes,ValueType>(index);
00061 }
00062 };
00063
00064 template <>
00065 struct at_c_impl<1> {
00066 template <typename IntTypes, typename ValueType> inline
00067 static ValueType apply(std::size_t index) {
00068 const std::size_t SIZE=mpl::size<IntTypes>::value;
00069 const std::size_t REM = SIZE % GIL_DYNAMIC_AT_C_LIMIT;
00070 switch (index / GIL_DYNAMIC_AT_C_LIMIT) {
00071 case 0: return at_c_fn<0 ,GIL_DYNAMIC_AT_C_LIMIT-1>::template apply<IntTypes,ValueType>(index);
00072 case 1: return at_c_fn<GIL_DYNAMIC_AT_C_LIMIT ,REM >::template apply<IntTypes,ValueType>(index - GIL_DYNAMIC_AT_C_LIMIT);
00073 };
00074 throw;
00075 }
00076 };
00077
00078 template <>
00079 struct at_c_impl<2> {
00080 template <typename IntTypes, typename ValueType> inline
00081 static ValueType apply(std::size_t index) {
00082 const std::size_t SIZE=mpl::size<IntTypes>::value;
00083 const std::size_t REM = SIZE % GIL_DYNAMIC_AT_C_LIMIT;
00084 switch (index / GIL_DYNAMIC_AT_C_LIMIT) {
00085 case 0: return at_c_fn<0 ,GIL_DYNAMIC_AT_C_LIMIT-1>::template apply<IntTypes,ValueType>(index);
00086 case 1: return at_c_fn<GIL_DYNAMIC_AT_C_LIMIT ,GIL_DYNAMIC_AT_C_LIMIT-1>::template apply<IntTypes,ValueType>(index - GIL_DYNAMIC_AT_C_LIMIT);
00087 case 2: return at_c_fn<GIL_DYNAMIC_AT_C_LIMIT*2,REM >::template apply<IntTypes,ValueType>(index - GIL_DYNAMIC_AT_C_LIMIT*2);
00088 };
00089 throw;
00090 }
00091 };
00092
00093 template <>
00094 struct at_c_impl<3> {
00095 template <typename IntTypes, typename ValueType> inline
00096 static ValueType apply(std::size_t index) {
00097 const std::size_t SIZE=mpl::size<IntTypes>::value;
00098 const std::size_t REM = SIZE % GIL_DYNAMIC_AT_C_LIMIT;
00099 switch (index / GIL_DYNAMIC_AT_C_LIMIT) {
00100 case 0: return at_c_fn<0 ,GIL_DYNAMIC_AT_C_LIMIT-1>::template apply<IntTypes,ValueType>(index);
00101 case 1: return at_c_fn<GIL_DYNAMIC_AT_C_LIMIT ,GIL_DYNAMIC_AT_C_LIMIT-1>::template apply<IntTypes,ValueType>(index - GIL_DYNAMIC_AT_C_LIMIT);
00102 case 2: return at_c_fn<GIL_DYNAMIC_AT_C_LIMIT*2,GIL_DYNAMIC_AT_C_LIMIT-1>::template apply<IntTypes,ValueType>(index - GIL_DYNAMIC_AT_C_LIMIT*2);
00103 case 3: return at_c_fn<GIL_DYNAMIC_AT_C_LIMIT*3,REM >::template apply<IntTypes,ValueType>(index - GIL_DYNAMIC_AT_C_LIMIT*3);
00104 };
00105 throw;
00106 }
00107 };
00108 }
00109 }
00110
00117
00118 template <typename IntTypes, typename ValueType> inline
00119 ValueType at_c(std::size_t index) {
00120 const std::size_t Size=mpl::size<IntTypes>::value;
00121 return detail::at_c::at_c_impl<Size/GIL_DYNAMIC_AT_C_LIMIT>::template apply<IntTypes,ValueType>(index);
00122 }
00123
00124 #undef GIL_AT_C_VALUE
00125 #undef GIL_DYNAMIC_AT_C_LIMIT
00126 #undef GIL_AT_C_LOOKUP
00127
00128 } }
00129
00130 #endif