Skip to content

Instantly share code, notes, and snippets.

@kuniyoshi
Last active February 19, 2016 12:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kuniyoshi/8a784c36012f9dd30725 to your computer and use it in GitHub Desktop.
Save kuniyoshi/8a784c36012f9dd30725 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
int get_size(int depth)
{
int r = 0;
switch (depth)
{
case 0: r = 2; break;
case 1: r = 2; break;
case 2: r = 3; break;
}
return r;
}
void fill_until(vector< string >* v, int depth)
{
vector< int > depths;
depths.reserve(depth);
for (int i = 0; i < depth; ++i)
{
depths.push_back(get_size(i));
}
vector< int > positions;
positions.reserve(depth);
positions.push_back(1);
int base = 1;
for (int i = 0; i < depth; ++i)
{
base = base * get_size(i);
positions.push_back(base);
}
vector< int > current;
current.reserve(depth);
for (int i = 0; i < depth; ++i)
{
current.push_back(0);
}
for (int i = 0; i < positions[positions.size() - 1]; ++i)
{
for (int j = 0; j < depth; ++j)
{
current[j] = (i / positions[j]) % depths[j];
}
ostringstream oss;
for (int j = 0; j < depth; ++j)
{
oss << current[j];
oss << ", ";
}
v->push_back(oss.str());
}
}
int main()
{
const int depth = 3;
vector< string > v;
fill_until(&v, depth);
for (vector< string >::iterator it = v.begin(); it != v.end(); ++it)
{
cerr << *it << endl;
}
}
@kuniyoshi
Copy link
Author

0, 0, 0, 
1, 0, 0, 
0, 1, 0, 
1, 1, 0, 
0, 2, 1, 
1, 2, 1, 
0, 3, 1, 
1, 3, 1, 
0, 0, 2, 
1, 0, 2, 
0, 1, 2, 
1, 1, 2, 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment