fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <cmath>
  5. #include <iomanip>
  6.  
  7. /** Range generation,
  8.  * from http://stackoverflow.com/questions/13313980/populate-an-array-using-constexpr-at-compile-time **/
  9. template<unsigned... Is> struct seq{};
  10.  
  11. template<unsigned N, unsigned... Is>
  12. struct gen_seq : gen_seq<N-1, N-1, Is...>{};
  13.  
  14. template<unsigned... Is>
  15. struct gen_seq<0, Is...> : seq<Is...>{};
  16.  
  17. /** A table consisting of indexes and values,
  18.  * which will all be computed at compile-time **/
  19. template<unsigned N>
  20. struct Table
  21. {
  22. unsigned indexes[N];
  23. double values[N];
  24.  
  25. static constexpr unsigned length = N;
  26. };
  27.  
  28.  
  29. template< typename LambdaType, unsigned... Is>
  30. constexpr Table< sizeof...(Is) > TableGenerator(seq<Is...>, LambdaType evalFunc)
  31. {
  32. return {{ Is... }, { evalFunc(Is)... }};
  33. }
  34.  
  35. template<unsigned N, typename LambdaType>
  36. constexpr Table<N> TableGenerator( LambdaType evalFunc )
  37. {
  38. return TableGenerator(gen_seq<N>(), evalFunc);
  39. }
  40.  
  41.  
  42.  
  43. /** Function that computes a value for each index **/
  44. constexpr double myFunc( unsigned idx )
  45. {
  46. return sin(0.2 * idx) + cos(0.5*idx);
  47. }
  48.  
  49.  
  50. int main()
  51. {
  52. constexpr unsigned length = 100;
  53.  
  54. // create compile-time table
  55. constexpr Table<length> table = TableGenerator<length>( myFunc );
  56.  
  57. // print values in vertical form, pretty-looking ;)
  58. const double lineMult = 12;
  59. const double lineOffset = 30;
  60. for(auto v : table.values)
  61. {
  62. const unsigned numSpaces = (unsigned) ( lineOffset + v * lineMult + 0.5 );
  63. std::cout << std::setfill(' ') << std::setw( numSpaces ) << "o" << std::endl;
  64. }
  65.  
  66. std::cout << std::endl;
  67.  
  68. return 0;
  69. }
  70.  
Success #stdin #stdout 0s 16056KB
stdin
Standard input is empty
stdout
                                         o
                                          o
                                        o
                                     o
                                 o
                             o
                            o
                              o
                                 o
                                      o
                                           o
                                               o
                                                 o
                                               o
                                          o
                                   o
                           o
                   o
             o
          o
          o
             o
                  o
                       o
                           o
                             o
                             o
                           o
                       o
                   o
                 o
                o
                   o
                        o
                                o
                                        o
                                              o
                                                   o
                                                    o
                                                   o
                                              o
                                       o
                                 o
                           o
                        o
                       o
                         o
                             o
                                o
                                  o
                                  o
                                o
                           o
                    o
              o
        o
      o
      o
          o
                 o
                        o
                                o
                                      o
                                         o
                                          o
                                        o
                                    o
                                o
                             o
                            o
                              o
                                  o
                                       o
                                            o
                                                o
                                                 o
                                              o
                                         o
                                 o
                         o
                  o
            o
         o
          o
              o
                   o
                        o
                            o
                              o
                             o
                          o
                      o
                   o
                o
                o
                    o
                         o
                                 o
                                         o
                                               o