mp_with_index.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. #ifndef BOOST_MP11_DETAIL_MP_WITH_INDEX_HPP_INCLUDED
  2. #define BOOST_MP11_DETAIL_MP_WITH_INDEX_HPP_INCLUDED
  3. // Copyright 2017 Peter Dimov.
  4. //
  5. // Distributed under the Boost Software License, Version 1.0.
  6. //
  7. // See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt
  9. #include <boost/mp11/integral.hpp>
  10. #include <boost/mp11/detail/config.hpp>
  11. #include <type_traits>
  12. #include <utility>
  13. #include <cassert>
  14. #if defined( BOOST_MP11_HAS_CXX14_CONSTEXPR )
  15. # define BOOST_MP11_CONSTEXPR14 constexpr
  16. #else
  17. # define BOOST_MP11_CONSTEXPR14
  18. #endif
  19. #if defined( __GNUC__ ) || defined( __clang__ )
  20. # define BOOST_MP11_UNREACHABLE_DEFAULT default: __builtin_unreachable();
  21. #elif defined( _MSC_VER )
  22. # define BOOST_MP11_UNREACHABLE_DEFAULT default: __assume(false);
  23. #else
  24. # define BOOST_MP11_UNREACHABLE_DEFAULT
  25. #endif
  26. namespace boost
  27. {
  28. namespace mp11
  29. {
  30. namespace detail
  31. {
  32. template<std::size_t N> struct mp_with_index_impl_
  33. {
  34. template<std::size_t K, class F> static BOOST_MP11_CONSTEXPR14 decltype(std::declval<F>()(std::declval<mp_size_t<0>>())) call( std::size_t i, F && f )
  35. {
  36. if( i < N / 2 )
  37. {
  38. return mp_with_index_impl_<N/2>::template call<K>( i, std::forward<F>(f) );
  39. }
  40. else
  41. {
  42. return mp_with_index_impl_<N-N/2>::template call<K+N/2>( i - N/2, std::forward<F>(f) );
  43. }
  44. }
  45. };
  46. template<> struct mp_with_index_impl_<0>
  47. {
  48. };
  49. template<> struct mp_with_index_impl_<1>
  50. {
  51. template<std::size_t K, class F> static BOOST_MP11_CONSTEXPR14 decltype(std::declval<F>()(std::declval<mp_size_t<0>>())) call( std::size_t /*i*/, F && f )
  52. {
  53. return std::forward<F>(f)( mp_size_t<K+0>() );
  54. }
  55. };
  56. template<> struct mp_with_index_impl_<2>
  57. {
  58. template<std::size_t K, class F> static BOOST_MP11_CONSTEXPR14 decltype(std::declval<F>()(std::declval<mp_size_t<0>>())) call( std::size_t i, F && f )
  59. {
  60. switch( i )
  61. {
  62. BOOST_MP11_UNREACHABLE_DEFAULT
  63. case 0: return std::forward<F>(f)( mp_size_t<K+0>() );
  64. case 1: return std::forward<F>(f)( mp_size_t<K+1>() );
  65. }
  66. }
  67. };
  68. template<> struct mp_with_index_impl_<3>
  69. {
  70. template<std::size_t K, class F> static BOOST_MP11_CONSTEXPR14 decltype(std::declval<F>()(std::declval<mp_size_t<0>>())) call( std::size_t i, F && f )
  71. {
  72. switch( i )
  73. {
  74. BOOST_MP11_UNREACHABLE_DEFAULT
  75. case 0: return std::forward<F>(f)( mp_size_t<K+0>() );
  76. case 1: return std::forward<F>(f)( mp_size_t<K+1>() );
  77. case 2: return std::forward<F>(f)( mp_size_t<K+2>() );
  78. }
  79. }
  80. };
  81. template<> struct mp_with_index_impl_<4>
  82. {
  83. template<std::size_t K, class F> static BOOST_MP11_CONSTEXPR14 decltype(std::declval<F>()(std::declval<mp_size_t<0>>())) call( std::size_t i, F && f )
  84. {
  85. switch( i )
  86. {
  87. BOOST_MP11_UNREACHABLE_DEFAULT
  88. case 0: return std::forward<F>(f)( mp_size_t<K+0>() );
  89. case 1: return std::forward<F>(f)( mp_size_t<K+1>() );
  90. case 2: return std::forward<F>(f)( mp_size_t<K+2>() );
  91. case 3: return std::forward<F>(f)( mp_size_t<K+3>() );
  92. }
  93. }
  94. };
  95. template<> struct mp_with_index_impl_<5>
  96. {
  97. template<std::size_t K, class F> static BOOST_MP11_CONSTEXPR14 decltype(std::declval<F>()(std::declval<mp_size_t<0>>())) call( std::size_t i, F && f )
  98. {
  99. switch( i )
  100. {
  101. BOOST_MP11_UNREACHABLE_DEFAULT
  102. case 0: return std::forward<F>(f)( mp_size_t<K+0>() );
  103. case 1: return std::forward<F>(f)( mp_size_t<K+1>() );
  104. case 2: return std::forward<F>(f)( mp_size_t<K+2>() );
  105. case 3: return std::forward<F>(f)( mp_size_t<K+3>() );
  106. case 4: return std::forward<F>(f)( mp_size_t<K+4>() );
  107. }
  108. }
  109. };
  110. template<> struct mp_with_index_impl_<6>
  111. {
  112. template<std::size_t K, class F> static BOOST_MP11_CONSTEXPR14 decltype(std::declval<F>()(std::declval<mp_size_t<0>>())) call( std::size_t i, F && f )
  113. {
  114. switch( i )
  115. {
  116. BOOST_MP11_UNREACHABLE_DEFAULT
  117. case 0: return std::forward<F>(f)( mp_size_t<K+0>() );
  118. case 1: return std::forward<F>(f)( mp_size_t<K+1>() );
  119. case 2: return std::forward<F>(f)( mp_size_t<K+2>() );
  120. case 3: return std::forward<F>(f)( mp_size_t<K+3>() );
  121. case 4: return std::forward<F>(f)( mp_size_t<K+4>() );
  122. case 5: return std::forward<F>(f)( mp_size_t<K+5>() );
  123. }
  124. }
  125. };
  126. template<> struct mp_with_index_impl_<7>
  127. {
  128. template<std::size_t K, class F> static BOOST_MP11_CONSTEXPR14 decltype(std::declval<F>()(std::declval<mp_size_t<0>>())) call( std::size_t i, F && f )
  129. {
  130. switch( i )
  131. {
  132. BOOST_MP11_UNREACHABLE_DEFAULT
  133. case 0: return std::forward<F>(f)( mp_size_t<K+0>() );
  134. case 1: return std::forward<F>(f)( mp_size_t<K+1>() );
  135. case 2: return std::forward<F>(f)( mp_size_t<K+2>() );
  136. case 3: return std::forward<F>(f)( mp_size_t<K+3>() );
  137. case 4: return std::forward<F>(f)( mp_size_t<K+4>() );
  138. case 5: return std::forward<F>(f)( mp_size_t<K+5>() );
  139. case 6: return std::forward<F>(f)( mp_size_t<K+6>() );
  140. }
  141. }
  142. };
  143. template<> struct mp_with_index_impl_<8>
  144. {
  145. template<std::size_t K, class F> static BOOST_MP11_CONSTEXPR14 decltype(std::declval<F>()(std::declval<mp_size_t<0>>())) call( std::size_t i, F && f )
  146. {
  147. switch( i )
  148. {
  149. BOOST_MP11_UNREACHABLE_DEFAULT
  150. case 0: return std::forward<F>(f)( mp_size_t<K+0>() );
  151. case 1: return std::forward<F>(f)( mp_size_t<K+1>() );
  152. case 2: return std::forward<F>(f)( mp_size_t<K+2>() );
  153. case 3: return std::forward<F>(f)( mp_size_t<K+3>() );
  154. case 4: return std::forward<F>(f)( mp_size_t<K+4>() );
  155. case 5: return std::forward<F>(f)( mp_size_t<K+5>() );
  156. case 6: return std::forward<F>(f)( mp_size_t<K+6>() );
  157. case 7: return std::forward<F>(f)( mp_size_t<K+7>() );
  158. }
  159. }
  160. };
  161. template<> struct mp_with_index_impl_<9>
  162. {
  163. template<std::size_t K, class F> static BOOST_MP11_CONSTEXPR14 decltype(std::declval<F>()(std::declval<mp_size_t<0>>())) call( std::size_t i, F && f )
  164. {
  165. switch( i )
  166. {
  167. BOOST_MP11_UNREACHABLE_DEFAULT
  168. case 0: return std::forward<F>(f)( mp_size_t<K+0>() );
  169. case 1: return std::forward<F>(f)( mp_size_t<K+1>() );
  170. case 2: return std::forward<F>(f)( mp_size_t<K+2>() );
  171. case 3: return std::forward<F>(f)( mp_size_t<K+3>() );
  172. case 4: return std::forward<F>(f)( mp_size_t<K+4>() );
  173. case 5: return std::forward<F>(f)( mp_size_t<K+5>() );
  174. case 6: return std::forward<F>(f)( mp_size_t<K+6>() );
  175. case 7: return std::forward<F>(f)( mp_size_t<K+7>() );
  176. case 8: return std::forward<F>(f)( mp_size_t<K+8>() );
  177. }
  178. }
  179. };
  180. template<> struct mp_with_index_impl_<10>
  181. {
  182. template<std::size_t K, class F> static BOOST_MP11_CONSTEXPR14 decltype(std::declval<F>()(std::declval<mp_size_t<0>>())) call( std::size_t i, F && f )
  183. {
  184. switch( i )
  185. {
  186. BOOST_MP11_UNREACHABLE_DEFAULT
  187. case 0: return std::forward<F>(f)( mp_size_t<K+0>() );
  188. case 1: return std::forward<F>(f)( mp_size_t<K+1>() );
  189. case 2: return std::forward<F>(f)( mp_size_t<K+2>() );
  190. case 3: return std::forward<F>(f)( mp_size_t<K+3>() );
  191. case 4: return std::forward<F>(f)( mp_size_t<K+4>() );
  192. case 5: return std::forward<F>(f)( mp_size_t<K+5>() );
  193. case 6: return std::forward<F>(f)( mp_size_t<K+6>() );
  194. case 7: return std::forward<F>(f)( mp_size_t<K+7>() );
  195. case 8: return std::forward<F>(f)( mp_size_t<K+8>() );
  196. case 9: return std::forward<F>(f)( mp_size_t<K+9>() );
  197. }
  198. }
  199. };
  200. template<> struct mp_with_index_impl_<11>
  201. {
  202. template<std::size_t K, class F> static BOOST_MP11_CONSTEXPR14 decltype(std::declval<F>()(std::declval<mp_size_t<0>>())) call( std::size_t i, F && f )
  203. {
  204. switch( i )
  205. {
  206. BOOST_MP11_UNREACHABLE_DEFAULT
  207. case 0: return std::forward<F>(f)( mp_size_t<K+0>() );
  208. case 1: return std::forward<F>(f)( mp_size_t<K+1>() );
  209. case 2: return std::forward<F>(f)( mp_size_t<K+2>() );
  210. case 3: return std::forward<F>(f)( mp_size_t<K+3>() );
  211. case 4: return std::forward<F>(f)( mp_size_t<K+4>() );
  212. case 5: return std::forward<F>(f)( mp_size_t<K+5>() );
  213. case 6: return std::forward<F>(f)( mp_size_t<K+6>() );
  214. case 7: return std::forward<F>(f)( mp_size_t<K+7>() );
  215. case 8: return std::forward<F>(f)( mp_size_t<K+8>() );
  216. case 9: return std::forward<F>(f)( mp_size_t<K+9>() );
  217. case 10: return std::forward<F>(f)( mp_size_t<K+10>() );
  218. }
  219. }
  220. };
  221. template<> struct mp_with_index_impl_<12>
  222. {
  223. template<std::size_t K, class F> static BOOST_MP11_CONSTEXPR14 decltype(std::declval<F>()(std::declval<mp_size_t<0>>())) call( std::size_t i, F && f )
  224. {
  225. switch( i )
  226. {
  227. BOOST_MP11_UNREACHABLE_DEFAULT
  228. case 0: return std::forward<F>(f)( mp_size_t<K+0>() );
  229. case 1: return std::forward<F>(f)( mp_size_t<K+1>() );
  230. case 2: return std::forward<F>(f)( mp_size_t<K+2>() );
  231. case 3: return std::forward<F>(f)( mp_size_t<K+3>() );
  232. case 4: return std::forward<F>(f)( mp_size_t<K+4>() );
  233. case 5: return std::forward<F>(f)( mp_size_t<K+5>() );
  234. case 6: return std::forward<F>(f)( mp_size_t<K+6>() );
  235. case 7: return std::forward<F>(f)( mp_size_t<K+7>() );
  236. case 8: return std::forward<F>(f)( mp_size_t<K+8>() );
  237. case 9: return std::forward<F>(f)( mp_size_t<K+9>() );
  238. case 10: return std::forward<F>(f)( mp_size_t<K+10>() );
  239. case 11: return std::forward<F>(f)( mp_size_t<K+11>() );
  240. }
  241. }
  242. };
  243. template<> struct mp_with_index_impl_<13>
  244. {
  245. template<std::size_t K, class F> static BOOST_MP11_CONSTEXPR14 decltype(std::declval<F>()(std::declval<mp_size_t<0>>())) call( std::size_t i, F && f )
  246. {
  247. switch( i )
  248. {
  249. BOOST_MP11_UNREACHABLE_DEFAULT
  250. case 0: return std::forward<F>(f)( mp_size_t<K+0>() );
  251. case 1: return std::forward<F>(f)( mp_size_t<K+1>() );
  252. case 2: return std::forward<F>(f)( mp_size_t<K+2>() );
  253. case 3: return std::forward<F>(f)( mp_size_t<K+3>() );
  254. case 4: return std::forward<F>(f)( mp_size_t<K+4>() );
  255. case 5: return std::forward<F>(f)( mp_size_t<K+5>() );
  256. case 6: return std::forward<F>(f)( mp_size_t<K+6>() );
  257. case 7: return std::forward<F>(f)( mp_size_t<K+7>() );
  258. case 8: return std::forward<F>(f)( mp_size_t<K+8>() );
  259. case 9: return std::forward<F>(f)( mp_size_t<K+9>() );
  260. case 10: return std::forward<F>(f)( mp_size_t<K+10>() );
  261. case 11: return std::forward<F>(f)( mp_size_t<K+11>() );
  262. case 12: return std::forward<F>(f)( mp_size_t<K+12>() );
  263. }
  264. }
  265. };
  266. template<> struct mp_with_index_impl_<14>
  267. {
  268. template<std::size_t K, class F> static BOOST_MP11_CONSTEXPR14 decltype(std::declval<F>()(std::declval<mp_size_t<0>>())) call( std::size_t i, F && f )
  269. {
  270. switch( i )
  271. {
  272. BOOST_MP11_UNREACHABLE_DEFAULT
  273. case 0: return std::forward<F>(f)( mp_size_t<K+0>() );
  274. case 1: return std::forward<F>(f)( mp_size_t<K+1>() );
  275. case 2: return std::forward<F>(f)( mp_size_t<K+2>() );
  276. case 3: return std::forward<F>(f)( mp_size_t<K+3>() );
  277. case 4: return std::forward<F>(f)( mp_size_t<K+4>() );
  278. case 5: return std::forward<F>(f)( mp_size_t<K+5>() );
  279. case 6: return std::forward<F>(f)( mp_size_t<K+6>() );
  280. case 7: return std::forward<F>(f)( mp_size_t<K+7>() );
  281. case 8: return std::forward<F>(f)( mp_size_t<K+8>() );
  282. case 9: return std::forward<F>(f)( mp_size_t<K+9>() );
  283. case 10: return std::forward<F>(f)( mp_size_t<K+10>() );
  284. case 11: return std::forward<F>(f)( mp_size_t<K+11>() );
  285. case 12: return std::forward<F>(f)( mp_size_t<K+12>() );
  286. case 13: return std::forward<F>(f)( mp_size_t<K+13>() );
  287. }
  288. }
  289. };
  290. template<> struct mp_with_index_impl_<15>
  291. {
  292. template<std::size_t K, class F> static BOOST_MP11_CONSTEXPR14 decltype(std::declval<F>()(std::declval<mp_size_t<0>>())) call( std::size_t i, F && f )
  293. {
  294. switch( i )
  295. {
  296. BOOST_MP11_UNREACHABLE_DEFAULT
  297. case 0: return std::forward<F>(f)( mp_size_t<K+0>() );
  298. case 1: return std::forward<F>(f)( mp_size_t<K+1>() );
  299. case 2: return std::forward<F>(f)( mp_size_t<K+2>() );
  300. case 3: return std::forward<F>(f)( mp_size_t<K+3>() );
  301. case 4: return std::forward<F>(f)( mp_size_t<K+4>() );
  302. case 5: return std::forward<F>(f)( mp_size_t<K+5>() );
  303. case 6: return std::forward<F>(f)( mp_size_t<K+6>() );
  304. case 7: return std::forward<F>(f)( mp_size_t<K+7>() );
  305. case 8: return std::forward<F>(f)( mp_size_t<K+8>() );
  306. case 9: return std::forward<F>(f)( mp_size_t<K+9>() );
  307. case 10: return std::forward<F>(f)( mp_size_t<K+10>() );
  308. case 11: return std::forward<F>(f)( mp_size_t<K+11>() );
  309. case 12: return std::forward<F>(f)( mp_size_t<K+12>() );
  310. case 13: return std::forward<F>(f)( mp_size_t<K+13>() );
  311. case 14: return std::forward<F>(f)( mp_size_t<K+14>() );
  312. }
  313. }
  314. };
  315. template<> struct mp_with_index_impl_<16>
  316. {
  317. template<std::size_t K, class F> static BOOST_MP11_CONSTEXPR14 decltype(std::declval<F>()(std::declval<mp_size_t<0>>())) call( std::size_t i, F && f )
  318. {
  319. switch( i )
  320. {
  321. BOOST_MP11_UNREACHABLE_DEFAULT
  322. case 0: return std::forward<F>(f)( mp_size_t<K+0>() );
  323. case 1: return std::forward<F>(f)( mp_size_t<K+1>() );
  324. case 2: return std::forward<F>(f)( mp_size_t<K+2>() );
  325. case 3: return std::forward<F>(f)( mp_size_t<K+3>() );
  326. case 4: return std::forward<F>(f)( mp_size_t<K+4>() );
  327. case 5: return std::forward<F>(f)( mp_size_t<K+5>() );
  328. case 6: return std::forward<F>(f)( mp_size_t<K+6>() );
  329. case 7: return std::forward<F>(f)( mp_size_t<K+7>() );
  330. case 8: return std::forward<F>(f)( mp_size_t<K+8>() );
  331. case 9: return std::forward<F>(f)( mp_size_t<K+9>() );
  332. case 10: return std::forward<F>(f)( mp_size_t<K+10>() );
  333. case 11: return std::forward<F>(f)( mp_size_t<K+11>() );
  334. case 12: return std::forward<F>(f)( mp_size_t<K+12>() );
  335. case 13: return std::forward<F>(f)( mp_size_t<K+13>() );
  336. case 14: return std::forward<F>(f)( mp_size_t<K+14>() );
  337. case 15: return std::forward<F>(f)( mp_size_t<K+15>() );
  338. }
  339. }
  340. };
  341. } // namespace detail
  342. template<std::size_t N, class F> inline BOOST_MP11_CONSTEXPR14 decltype(std::declval<F>()(std::declval<mp_size_t<0>>())) mp_with_index( std::size_t i, F && f )
  343. {
  344. assert( i < N );
  345. return detail::mp_with_index_impl_<N>::template call<0>( i, std::forward<F>(f) );
  346. }
  347. template<class N, class F> inline BOOST_MP11_CONSTEXPR14 decltype(std::declval<F>()(std::declval<mp_size_t<0>>())) mp_with_index( std::size_t i, F && f )
  348. {
  349. return mp_with_index<std::size_t{N::value}>( i, std::forward<F>(f) );
  350. }
  351. #undef BOOST_MP11_CONSTEXPR14
  352. #undef BOOST_MP11_UNREACHABLE_DEFAULT
  353. } // namespace mp11
  354. } // namespace boost
  355. #endif // #ifndef BOOST_MP11_DETAIL_MP_WITH_INDEX_HPP_INCLUDED