#if !defined  HAVE_CATALAN_SUBSET_LEX_H__
#define       HAVE_CATALAN_SUBSET_LEX_H__
// This file is part of the FXT library.
// Copyright (C) 2012 Joerg Arndt
// License: GNU General Public License version 3 or later,
// see the file COPYING.txt in the main directory.


#include "fxttypes.h"


// Cf. comb/dyck-rgs-subset-lex.h for Dyck RGS in subset-lex order.
// Cf. comb/catalan-gslex.h for gslex order.


#define CATALAN_SUBSET_LEX_SC  // whether to allow n<=1
// this happens to give a small speedup(!)

class catalan_subset_lex
// Catalan restricted growth strings (RGS), subset-lex order.
{
public:
    ulong *as_; // digits of the RGS: as_[k] <= as[k-1] + 1
    ulong tr_;  // current track
    ulong n_;   // Number of digits (paren pairs)
    char *str_; // paren string

public:
    catalan_subset_lex(ulong n)
    {
        n_ = n;

        as_ = new ulong[n_+ 1 + (n_==0)];  // incl. sentinel
        as_[0] = +1; // sentinel !=0  to catch scan to the left
        ++as_;  // nota bene
        if ( n_==0 )  as_[0] = 0;

        str_ = new char[2*n_+1];  str_[2*n_] = 0;
        first();
    }

    ~catalan_subset_lex()
    {
        --as_;
        delete [] as_;
        delete [] str_;
    }

    void first()
    {
        for (ulong k=0; k<n_; ++k)  as_[k] = 0;
        tr_ = 1;

        // make things work for n <= 1:
        if ( n_ <= 1 )  tr_ = 0;
    }

    bool next()
    {
        ulong j = tr_;
        if ( as_[j] <= as_[j-1] )   // easy case 1: can increment track
        {
#ifdef CATALAN_SUBSET_LEX_SC
            if ( n_ <= 1 )  return false;
#endif
            ++as_[j];
            return true;
        }

        const ulong j1 = j + 1;
        if ( j1 < n_ )  // easy case 2: can attach
        {
            as_[j1] = +1;
            tr_ = j1;
            return true;
        }

        as_[j] = 0;

        // Find nonzero track to the left:
        do  { --j; }  while ( as_[j] == 0 );  // can read sentinel

        if ( (long)j < 0 )  return false;  // current is last

        --as_[j];
        ++j;
        as_[j] = 1;
        tr_ = j;
        return true;
    }

    void last()
    {
        for (ulong k=0; k<n_; ++k)  as_[k] = 0;
        tr_ = n_ - 1;
        // make things work for n <= 1:
        if ( n_==0 )
        {
            tr_ = 0;
            as_[0] = 1;
        }
        if ( n_>=2 )  as_[tr_] = 1;
    }


    bool prev()
    // Loopless algorithm.
    {
#ifdef CATALAN_SUBSET_LEX_SC
        if ( n_<=1 )  return false;
#endif

        ulong j = tr_;
        if ( as_[j] > 1 )   // easy case 1: can decrement track
        {
            --as_[j];
            return true;
        }

        const ulong aj = as_[j];  // zero or one
        as_[j] = 0;
        --j;
        if ( as_[j] == as_[j-1] + 1 )  // easy case 2: move track to the left
        {
            --tr_;
            return true;
        }

        if ( j==0 )  // current or next is last
        {
            if ( aj == 0 )  return false;
            return true;
        }

        ++as_[j];
        tr_ = n_ - 1;
        as_[tr_] = as_[tr_-1] + 1;
        return true;
    }

    const ulong *data()  const  { return as_; }

    const char* str()
    {
        for (ulong k=0; k<2*n_; ++k)  str_[k] = ')';
        for (ulong k=0,j=0; k<n_; ++k,j+=2)  str_[ j-as_[k] ] = '(';
        return str_;
    }
};
// -------------------------

#endif  // !defined HAVE_CATALAN_SUBSET_LEX_H__
