dynamic_at_c.hpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. //
  2. // Copyright 2005-2007 Adobe Systems Incorporated
  3. //
  4. // Distributed under the Boost Software License, Version 1.0
  5. // See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt
  7. //
  8. #ifndef BOOST_GIL_EXTENSION_DYNAMIC_IMAGE_DYNAMIC_AT_C_HPP
  9. #define BOOST_GIL_EXTENSION_DYNAMIC_IMAGE_DYNAMIC_AT_C_HPP
  10. #include <boost/gil/detail/mp11.hpp>
  11. #include <boost/preprocessor/facilities/empty.hpp>
  12. #include <boost/preprocessor/repetition/repeat.hpp>
  13. #include <stdexcept>
  14. namespace boost { namespace gil {
  15. // Constructs for static-to-dynamic integer convesion
  16. #define GIL_AT_C_VALUE(z, N, text) mp11::mp_at_c<IntTypes, S+N>::value,
  17. #define GIL_DYNAMIC_AT_C_LIMIT 226 // size of the maximum vector to handle
  18. #define GIL_AT_C_LOOKUP(z, NUM, text) \
  19. template<std::size_t S> \
  20. struct at_c_fn<S,NUM> { \
  21. template <typename IntTypes, typename ValueType> inline \
  22. static ValueType apply(std::size_t index) { \
  23. static ValueType table[] = { \
  24. BOOST_PP_REPEAT(NUM, GIL_AT_C_VALUE, BOOST_PP_EMPTY) \
  25. }; \
  26. return table[index]; \
  27. } \
  28. };
  29. namespace detail {
  30. namespace at_c {
  31. template <std::size_t START, std::size_t NUM> struct at_c_fn;
  32. BOOST_PP_REPEAT(GIL_DYNAMIC_AT_C_LIMIT, GIL_AT_C_LOOKUP, BOOST_PP_EMPTY)
  33. template <std::size_t QUOT> struct at_c_impl;
  34. template <>
  35. struct at_c_impl<0> {
  36. template <typename IntTypes, typename ValueType> inline
  37. static ValueType apply(std::size_t index) {
  38. return at_c_fn<0, mp11::mp_size<IntTypes>::value>::template apply<IntTypes,ValueType>(index);
  39. }
  40. };
  41. template <>
  42. struct at_c_impl<1> {
  43. template <typename IntTypes, typename ValueType> inline
  44. static ValueType apply(std::size_t index) {
  45. const std::size_t SIZE = mp11::mp_size<IntTypes>::value;
  46. const std::size_t REM = SIZE % GIL_DYNAMIC_AT_C_LIMIT;
  47. switch (index / GIL_DYNAMIC_AT_C_LIMIT) {
  48. case 0: return at_c_fn<0 ,GIL_DYNAMIC_AT_C_LIMIT-1>::template apply<IntTypes,ValueType>(index);
  49. case 1: return at_c_fn<GIL_DYNAMIC_AT_C_LIMIT ,REM >::template apply<IntTypes,ValueType>(index - GIL_DYNAMIC_AT_C_LIMIT);
  50. };
  51. throw;
  52. }
  53. };
  54. template <>
  55. struct at_c_impl<2> {
  56. template <typename IntTypes, typename ValueType> inline
  57. static ValueType apply(std::size_t index) {
  58. const std::size_t SIZE = mp11::mp_size<IntTypes>::value;
  59. const std::size_t REM = SIZE % GIL_DYNAMIC_AT_C_LIMIT;
  60. switch (index / GIL_DYNAMIC_AT_C_LIMIT) {
  61. case 0: return at_c_fn<0 ,GIL_DYNAMIC_AT_C_LIMIT-1>::template apply<IntTypes,ValueType>(index);
  62. 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);
  63. case 2: return at_c_fn<GIL_DYNAMIC_AT_C_LIMIT*2,REM >::template apply<IntTypes,ValueType>(index - GIL_DYNAMIC_AT_C_LIMIT*2);
  64. };
  65. throw;
  66. }
  67. };
  68. template <>
  69. struct at_c_impl<3> {
  70. template <typename IntTypes, typename ValueType> inline
  71. static ValueType apply(std::size_t index) {
  72. const std::size_t SIZE = mp11::mp_size<IntTypes>::value;
  73. const std::size_t REM = SIZE % GIL_DYNAMIC_AT_C_LIMIT;
  74. switch (index / GIL_DYNAMIC_AT_C_LIMIT) {
  75. case 0: return at_c_fn<0 ,GIL_DYNAMIC_AT_C_LIMIT-1>::template apply<IntTypes,ValueType>(index);
  76. 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);
  77. 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);
  78. case 3: return at_c_fn<GIL_DYNAMIC_AT_C_LIMIT*3,REM >::template apply<IntTypes,ValueType>(index - GIL_DYNAMIC_AT_C_LIMIT*3);
  79. };
  80. throw;
  81. }
  82. };
  83. }
  84. }
  85. ////////////////////////////////////////////////////////////////////////////////////
  86. ///
  87. /// \brief Given an Boost.MP11-compatible list and a dynamic index n,
  88. /// returns the value of the n-th element.
  89. /// It constructs a lookup table at compile time.
  90. ///
  91. ////////////////////////////////////////////////////////////////////////////////////
  92. template <typename IntTypes, typename ValueType> inline
  93. ValueType at_c(std::size_t index) {
  94. const std::size_t Size=mp11::mp_size<IntTypes>::value;
  95. return detail::at_c::at_c_impl<Size/GIL_DYNAMIC_AT_C_LIMIT>::template apply<IntTypes,ValueType>(index);
  96. }
  97. #undef GIL_AT_C_VALUE
  98. #undef GIL_DYNAMIC_AT_C_LIMIT
  99. #undef GIL_AT_C_LOOKUP
  100. }} // namespace boost::gil
  101. #endif