
#include "comb/motzkin-path-lex.h"

#include "comb/is-motzkin-path.h"

#include "comb/fact2perm.h"



#include "nextarg.h" // NXARG()
#include "fxttypes.h"
#include "fxtalloca.h"
#include "fxtio.h"
#include "jjassert.h"


//% Motzkin paths in lexicographic order, CAT algorithm.

//#define TIMING // uncomment to disable printing

int
main(int argc, char **argv)
{
    ulong n = 6;
    NXARG(n,"Length of paths (n>=1)");
    jjassert( n>=1);

    motzkin_path_lex M(n);
    const ulong *x = M.data();

    ulong ct = 0;
    ulong j = 0;
    do
    {
//        { bool q = true;
//            for (ulong i=1,z=0; i<=n; ++i)  // only Schroeder paths
//            {
//                if ( x[i] - x[i-1] != 0 )
//                {
//                    if ( (z&1) != 0 )  { q = false; break; }
//                    z = 0;
//                }
//                else  ++z;
//            }
//            if ( ! q )  continue;
//
//            for (ulong i=1; i<n; ++i)  // no peaks at height 1
//            {
//                if ( (x[i]==1) && (x[i-1]==0) && (x[i+1]==0 ) )
//                      { q = false; break; }
//            }
//            if ( ! q )  continue;
//        }

        ++ct;
#ifndef TIMING
        cout << setw(4) << ct << ":  ";
        M.print("    ", true );
        cout << "    " << setw(2) << j;

        cout << "    ";
        for (ulong i=1; i<=n; ++i)
        {
            ulong d = x[i] - x[i-1] + 1;
            cout << " " << "-0+"[d];
//            cout << " " << ")x("[d];
//            cout << " " << "DhU"[d];
//            cout << " " << "\\-/"[d];
        }

        cout << endl;

        jjassert( is_motzkin_path(x, n) );
#endif // TIMING
    }
    while ( (j=M.next()) );

    cout << " ct=" << ct << endl;

    return 0;
}
// -------------------------

/*
Timing: (AMD Phenom II X4 945 3000MHz)

 time ./bin 23
 ct=1129760415
./bin 23  5.10s user 0.00s system 99% cpu 5.104 total
 == > 1129760415/5.10 == 221,521,650 per second

// without "easy case" optimization:
./bin 23  7.00s user 0.00s system 99% cpu 7.003 total

*/

/*
 OEIS:  http://oeis.org/A001006  Motzkin numbers
1, 1, 2, 4, 9, 21, 51, 127, 323, 835, 2188, 5798, 15511, 41835, 113634, ...
*/


/// Emacs:
/// Local Variables:
/// MyRelDir: "demo/comb"
/// makefile-dir: "../../"
/// make-target: "1demo DSRC=demo/comb/motzkin-path-lex-demo.cc"
/// make-target2: "1demo DSRC=demo/comb/motzkin-path-lex-demo.cc DEMOFLAGS=-DTIMING"
/// End:

