Imported Upstream version 1.72.0
[platform/upstream/boost.git] / boost / gil / extension / dynamic_image / dynamic_at_c.hpp
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
11 #include <boost/gil/detail/mp11.hpp>
12
13 #include <boost/preprocessor/facilities/empty.hpp>
14 #include <boost/preprocessor/repetition/repeat.hpp>
15
16 #include <stdexcept>
17
18 namespace boost { namespace gil {
19
20 // Constructs for static-to-dynamic integer convesion
21
22 #define GIL_AT_C_VALUE(z, N, text) mp11::mp_at_c<IntTypes, S+N>::value,
23 #define GIL_DYNAMIC_AT_C_LIMIT 226 // size of the maximum vector to handle
24
25 #define GIL_AT_C_LOOKUP(z, NUM, text)                                   \
26     template<std::size_t S>                                             \
27     struct at_c_fn<S,NUM> {                                             \
28     template <typename IntTypes, typename ValueType> inline           \
29         static ValueType apply(std::size_t index) {                    \
30             static ValueType table[] = {                               \
31                 BOOST_PP_REPEAT(NUM, GIL_AT_C_VALUE, BOOST_PP_EMPTY)    \
32             };                                                          \
33             return table[index];                                        \
34         }                                                               \
35     };
36
37 namespace detail {
38     namespace at_c {
39         template <std::size_t START, std::size_t NUM> struct at_c_fn;
40         BOOST_PP_REPEAT(GIL_DYNAMIC_AT_C_LIMIT, GIL_AT_C_LOOKUP, BOOST_PP_EMPTY)
41
42         template <std::size_t QUOT> struct at_c_impl;
43
44         template <>
45         struct at_c_impl<0> {
46             template <typename IntTypes, typename ValueType> inline
47             static ValueType apply(std::size_t index) {
48                 return at_c_fn<0, mp11::mp_size<IntTypes>::value>::template apply<IntTypes,ValueType>(index);
49             }
50         };
51
52         template <>
53         struct at_c_impl<1> {
54             template <typename IntTypes, typename ValueType> inline
55             static ValueType apply(std::size_t index) {
56                 const std::size_t SIZE = mp11::mp_size<IntTypes>::value;
57                 const std::size_t REM = SIZE % GIL_DYNAMIC_AT_C_LIMIT;
58                 switch (index / GIL_DYNAMIC_AT_C_LIMIT) {
59                     case 0: return at_c_fn<0                   ,GIL_DYNAMIC_AT_C_LIMIT-1>::template apply<IntTypes,ValueType>(index);
60                     case 1: return at_c_fn<GIL_DYNAMIC_AT_C_LIMIT  ,REM                 >::template apply<IntTypes,ValueType>(index - GIL_DYNAMIC_AT_C_LIMIT);
61                 };
62                 throw;
63             }
64         };
65
66         template <>
67         struct at_c_impl<2> {
68             template <typename IntTypes, typename ValueType> inline
69             static ValueType apply(std::size_t index) {
70                 const std::size_t SIZE = mp11::mp_size<IntTypes>::value;
71                 const std::size_t REM = SIZE % GIL_DYNAMIC_AT_C_LIMIT;
72                 switch (index / GIL_DYNAMIC_AT_C_LIMIT) {
73                     case 0: return at_c_fn<0                   ,GIL_DYNAMIC_AT_C_LIMIT-1>::template apply<IntTypes,ValueType>(index);
74                     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);
75                     case 2: return at_c_fn<GIL_DYNAMIC_AT_C_LIMIT*2,REM                 >::template apply<IntTypes,ValueType>(index - GIL_DYNAMIC_AT_C_LIMIT*2);
76                 };
77                 throw;
78             }
79         };
80
81         template <>
82         struct at_c_impl<3> {
83             template <typename IntTypes, typename ValueType> inline
84             static ValueType apply(std::size_t index) {
85                 const std::size_t SIZE = mp11::mp_size<IntTypes>::value;
86                 const std::size_t REM = SIZE % GIL_DYNAMIC_AT_C_LIMIT;
87                 switch (index / GIL_DYNAMIC_AT_C_LIMIT) {
88                     case 0: return at_c_fn<0                   ,GIL_DYNAMIC_AT_C_LIMIT-1>::template apply<IntTypes,ValueType>(index);
89                     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);
90                     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);
91                     case 3: return at_c_fn<GIL_DYNAMIC_AT_C_LIMIT*3,REM                 >::template apply<IntTypes,ValueType>(index - GIL_DYNAMIC_AT_C_LIMIT*3);
92                 };
93                 throw;
94             }
95         };
96     }
97 }
98
99 ////////////////////////////////////////////////////////////////////////////////////
100 ///
101 /// \brief Given an Boost.MP11-compatible list and a dynamic index n,
102 /// returns the value of the n-th element.
103 /// It constructs a lookup table at compile time.
104 ///
105 ////////////////////////////////////////////////////////////////////////////////////
106
107 template <typename IntTypes, typename ValueType> inline
108 ValueType at_c(std::size_t index) {
109     const std::size_t Size=mp11::mp_size<IntTypes>::value;
110     return detail::at_c::at_c_impl<Size/GIL_DYNAMIC_AT_C_LIMIT>::template apply<IntTypes,ValueType>(index);
111 }
112
113 #undef GIL_AT_C_VALUE
114 #undef GIL_DYNAMIC_AT_C_LIMIT
115 #undef GIL_AT_C_LOOKUP
116
117 }} // namespace boost::gil
118
119 #endif