angelbeats's blog

By angelbeats, 2 months ago, In English
portable version
 
 
 
 
  • Vote: I like it
  • +10
  • Vote: I do not like it

»
2 months ago, # |
  Vote: I like it -7 Vote: I do not like it

Where did I go wrong in life ?

»
2 months ago, # |
  Vote: I like it +8 Vote: I do not like it

What's the point?

  • »
    »
    2 months ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Changing the code from REP(i, a) to FOR(i, 1, a) is annoying

»
2 months ago, # |
  Vote: I like it +13 Vote: I do not like it

one loop for everything

Code
#define FOR(i, a, b, s)     for(ll i = (a); i < (b); i += (s))
#define ROF(i, a, b, s)     for(ll i = (a); i > (b); i += (s))
#define FOR1(b)             FOR(_, 0, b, 1)
#define FOR2(i, b)          FOR(i, 0, b, 1)
#define FOR3(i, a, b)       FOR(i, a, b, 1)
#define FOR4(i, a, b, s)    FOR(i, a, b, s)
#define ROF1(i, b)          ROF(i, b, -1, -1)
#define ROF2(i, a, b)       ROF(i, a, b, -1)
#define ROF3(i, a, b, s)    ROF(i, a, b, s)
#define GETC(a, b, c, d, e, ...) e
#define FORC(...)           GETC(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)
#define ROFC(...)           GETC(__VA_ARGS__, ROF3, ROF2, ROF1)
#define loop(...)           FORC(__VA_ARGS__)(__VA_ARGS__)
#define rloop(...)          ROFC(__VA_ARGS__)(__VA_ARGS__)

void solve(int &T)
{
    loop(5) {
        cout << _ << " ";
    }
    cout << endl;

    loop(j, 5) {
        cout << j << " ";
    }
    cout << endl;
    
    loop(j, 5, 10) {
        cout << j << " ";
    }
    cout << endl;

    loop(j, 5, 10, 2) {
        cout << j << " ";
    }
    cout << endl;

    rloop(k, 5, -1) {
        cout << k << " ";
    }
    cout << endl;

    rloop(k, 10, -1, -3) {
        cout << k << " ";
    }
    cout << endl;

    loop(i, 10, 5) {
        assert(0);
    }
    rloop(i, 10, 10) {
        assert(0);
    }

    loop(i, 4, 10, 0) {
        cout << i << " ";
        i++;
    }
    cout << endl;

    rloop(i, 10, 4, 0) {
        cout << i << " ";
        i--;
    }
    cout << endl;

    rloop(i, 10) {
        cout << i << " ";
    }
    cout << endl;
}
»
2 months ago, # |
  Vote: I like it +27 Vote: I do not like it

Beware of side effects. rep(i, get_min(), get_max()) will call the functions several times.

»
2 months ago, # |
  Vote: I like it +49 Vote: I do not like it
Solution