0

I am trying to figure out a way to generate a sequence of arrays of integers at compile time (using constexpr functions). The sequence is this one:

// first all order 0 numbers
{0, 0, 0},
// then all order 1 numbers (total sum of figures = 1)
{1, 0, 0},
{0, 1, 0},
{0, 0, 1},
// then all order 2 numbers (total sum of figures = 2) in no specific order
{1, 1, 0},
{0, 1, 1},
{1, 0, 1},
{2, 0, 0},
{0, 2, 0},
{0, 0, 2}
// then all order 3 numbers etc... each block in no specific order

Currently I can only write the algorithm to get these arrays in another order

constexpr int M = 5; // maximum figure available, each figure will be from 0 to 5
constexpr int N = (M + 3) * (M + 2) / 2; // total number of arrays to be generated

constexpr std::array<std::array<int, 3>, N> algorithm() {
  std::array<std::array<int, 3>, N> ans;
  int pos = 0;
  for (int i = 0; i <= M; ++i) {
    for (int j = 0; j <= M - i; ++j) {
      for (int k = 0; k <= M - i - j; ++k) {
        ans[pos] = std::array<int, 3>{k, j, i};
        ++pos;
      }
    }
  }
  return ans;
}

This of course gives the arrays in a bad order:

{0, 0, 0},
{1, 0, 0},
{2, 0, 0},
{3, 0, 0},
...

I know this may be impossible to do at compile time, but maybe someone knows any tip for this kind of problems? Thanks for any help.

lucmobz
  • 453
  • 3
  • 8

0 Answers0