Boost GIL


dynamic_at_c.hpp
Go to the documentation of this file.
1 /*
2  Copyright 2005-2007 Adobe Systems Incorporated
3 
4  Use, modification and distribution are subject to the Boost Software License,
5  Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6  http://www.boost.org/LICENSE_1_0.txt).
7 
8  See http://opensource.adobe.com/gil for most recent version including documentation.
9 */
10 
11 /*************************************************************************************************/
12 
13 #ifndef GIL_DYNAMIC_AT_C_HPP
14 #define GIL_DYNAMIC_AT_C_HPP
15 
16 #include <cassert>
17 #include <stdexcept>
18 #include <boost/mpl/at.hpp>
19 #include <boost/mpl/size.hpp>
20 
21 
30 
31 namespace boost { namespace gil {
32 
33 #define GIL_AT_C_VALUE(z, N, text) mpl::at_c<IntTypes,S+N>::type::value,
34 #define GIL_DYNAMIC_AT_C_LIMIT 226 // size of the maximum vector to handle
35 
36 #define GIL_AT_C_LOOKUP(z, NUM, text) \
37  template<std::size_t S> \
38  struct at_c_fn<S,NUM> { \
39  template <typename IntTypes, typename ValueType> inline \
40  static ValueType apply(std::size_t index) { \
41  static ValueType table[] = { \
42  BOOST_PP_REPEAT(NUM, GIL_AT_C_VALUE, BOOST_PP_EMPTY) \
43  }; \
44  return table[index]; \
45  } \
46  };
47 
48 namespace detail {
49  namespace at_c {
50  template <std::size_t START, std::size_t NUM> struct at_c_fn;
51  BOOST_PP_REPEAT(GIL_DYNAMIC_AT_C_LIMIT, GIL_AT_C_LOOKUP, BOOST_PP_EMPTY)
52 
53  template <std::size_t QUOT> struct at_c_impl;
54 
55  template <>
56  struct at_c_impl<0> {
57  template <typename IntTypes, typename ValueType> inline
58  static ValueType apply(std::size_t index) {
59  return at_c_fn<0,mpl::size<IntTypes>::value>::template apply<IntTypes,ValueType>(index);
60  }
61  };
62 
63  template <>
64  struct at_c_impl<1> {
65  template <typename IntTypes, typename ValueType> inline
66  static ValueType apply(std::size_t index) {
67  const std::size_t SIZE=mpl::size<IntTypes>::value;
68  const std::size_t REM = SIZE % GIL_DYNAMIC_AT_C_LIMIT;
69  switch (index / GIL_DYNAMIC_AT_C_LIMIT) {
70  case 0: return at_c_fn<0 ,GIL_DYNAMIC_AT_C_LIMIT-1>::template apply<IntTypes,ValueType>(index);
71  case 1: return at_c_fn<GIL_DYNAMIC_AT_C_LIMIT ,REM >::template apply<IntTypes,ValueType>(index - GIL_DYNAMIC_AT_C_LIMIT);
72  };
73  throw;
74  }
75  };
76 
77  template <>
78  struct at_c_impl<2> {
79  template <typename IntTypes, typename ValueType> inline
80  static ValueType apply(std::size_t index) {
81  const std::size_t SIZE=mpl::size<IntTypes>::value;
82  const std::size_t REM = SIZE % GIL_DYNAMIC_AT_C_LIMIT;
83  switch (index / GIL_DYNAMIC_AT_C_LIMIT) {
84  case 0: return at_c_fn<0 ,GIL_DYNAMIC_AT_C_LIMIT-1>::template apply<IntTypes,ValueType>(index);
85  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);
86  case 2: return at_c_fn<GIL_DYNAMIC_AT_C_LIMIT*2,REM >::template apply<IntTypes,ValueType>(index - GIL_DYNAMIC_AT_C_LIMIT*2);
87  };
88  throw;
89  }
90  };
91 
92  template <>
93  struct at_c_impl<3> {
94  template <typename IntTypes, typename ValueType> inline
95  static ValueType apply(std::size_t index) {
96  const std::size_t SIZE=mpl::size<IntTypes>::value;
97  const std::size_t REM = SIZE % GIL_DYNAMIC_AT_C_LIMIT;
98  switch (index / GIL_DYNAMIC_AT_C_LIMIT) {
99  case 0: return at_c_fn<0 ,GIL_DYNAMIC_AT_C_LIMIT-1>::template apply<IntTypes,ValueType>(index);
100  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);
101  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);
102  case 3: return at_c_fn<GIL_DYNAMIC_AT_C_LIMIT*3,REM >::template apply<IntTypes,ValueType>(index - GIL_DYNAMIC_AT_C_LIMIT*3);
103  };
104  throw;
105  }
106  };
107  }
108 }
109 
116 
117 template <typename IntTypes, typename ValueType> inline
118 ValueType at_c(std::size_t index) {
119  const std::size_t Size=mpl::size<IntTypes>::value;
120  return detail::at_c::at_c_impl<Size/GIL_DYNAMIC_AT_C_LIMIT>::template apply<IntTypes,ValueType>(index);
121 }
122 
123 #undef GIL_AT_C_VALUE
124 #undef GIL_DYNAMIC_AT_C_LIMIT
125 #undef GIL_AT_C_LOOKUP
126 
127 } } // namespace boost::gil
128 
129 #endif