Advertisement
Dukales

enumerate indices of multidimentional array

Nov 30th, 2015
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.25 KB | None | 0 0
  1. #include <type_traits>
  2. #include <utility>
  3.  
  4. template< typename F, std::size_t ...indices >
  5. struct enumerator
  6. {
  7.     static constexpr const std::size_t size_ = sizeof...(indices);
  8.     static constexpr const std::size_t count_ = (indices * ...);
  9.  
  10.     template< typename I >
  11.     struct decomposer;
  12.    
  13.     template< std::size_t ...I >
  14.     struct decomposer< std::index_sequence< I... > >
  15.     {
  16.         F & f;
  17.        
  18.         static constexpr const std::size_t indices_[size_] = {indices...};
  19.        
  20.         static
  21.         constexpr
  22.         std::size_t
  23.         order(std::size_t const i)
  24.         {
  25.             std::size_t o = 1;
  26.             for (std::size_t n = i + 1; n < size_; ++n) {
  27.                 o *= indices_[n];
  28.             }
  29.             return o;
  30.         }
  31.        
  32.         static constexpr std::size_t const orders_[size_] = {order(I)...};
  33.        
  34.         static
  35.         constexpr
  36.         std::size_t
  37.         digit(std::size_t c, std::size_t const i)
  38.         {
  39.             for (std::size_t n = 0; n < i; ++n) {
  40.                 c = c % orders_[n];
  41.             }
  42.             return c / orders_[i];
  43.         }
  44.        
  45.         template< std::size_t c >
  46.         constexpr
  47.         bool
  48.         call() const
  49.         {
  50.             return f.template operator () < digit(c, I)... >(); // error here
  51.         }
  52.        
  53.     };
  54.    
  55.     decomposer< std::make_index_sequence< size_ > > decomposer_;
  56.  
  57.     constexpr
  58.     bool
  59.     operator () () const
  60.     {
  61.         return call(std::make_index_sequence< count_ >{});
  62.     }
  63.    
  64.     template< std::size_t ...counter >
  65.     constexpr
  66.     bool
  67.     call(std::index_sequence< counter... >) const
  68.     {
  69.         return (decomposer_.template call< counter >() && ...);
  70.     }
  71. };
  72.  
  73. #include <iterator>
  74. #include <iostream>
  75. #include <initializer_list>
  76. #include <algorithm>
  77.  
  78. #include <cstdlib>
  79.  
  80. struct print
  81. {
  82.     template< std::size_t ...indices >
  83.     constexpr
  84.     bool
  85.     operator () () const
  86.     {
  87.         for (std::size_t const i : {indices...}) {
  88.             std::cout << i << ' ';
  89.         }
  90.         std::cout << std::endl;
  91.         return true;
  92.     }
  93. };
  94.  
  95. int
  96. main()
  97. {
  98.     print const print_{};
  99.     enumerator< print const, 11, 7, 3 >{{print_}}();
  100.     return EXIT_SUCCESS;
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement