#if !defined  HAVE_SETPART_RGS_SUBSET_LEX_H__
#define       HAVE_SETPART_RGS_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 RGS of Dyck words in subset-lex order.
// Cf. comb/catalan-subset-lex.h for Catalan RGS in subset-lex order.


#define SETPART_RGS_SUBSET_LEX_SC  // whether to allow n<=1
// this gives a small slowdown

class setpart_rgs_subset_lex
// Restricted growth strings (RGS) for set partitions, subset-lex order.
{
public:
    ulong *as_; // digits of the RGS
    ulong *m_;  // maximum + 1 in prefix (only maitained for m[0, 1, ..., tr-1])
    ulong tr_;  // current track
    ulong n_;   // Number of digits in RGS

public:
    setpart_rgs_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;

        m_ = new ulong[n_ + 1 + (n_==0)];  // incl. write-only at right
        m_[n_] = 0;  // arbitrary
        if ( n_==0 )  m_[0] = 1;

        first();
    }

    ~setpart_rgs_subset_lex()
    {
        --as_;
        delete [] as_;
        delete [] m_;
    }

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

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

    bool next()
    {
        ulong j = tr_;
        if ( as_[j] < m_[j] )   // easy case 1: can increment track
        {
#ifdef SETPART_RGS_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
        {
            m_[j1] = m_[j] + 1;
            as_[j1] = +1;
            tr_ = j1;
            return true;
        }

        as_[j] = 0;
        m_[j] = m_[j-1];

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

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

        if ( as_[j] == m_[j] )  m_[j+1] = m_[j];
        --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()
    {
#ifdef SETPART_RGS_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] == m_[j] )  // 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];
        if ( as_[j] == m_[j] )  m_[j+1] = m_[j] + 1;

        const ulong m1 = m_[j+1];
        for (ulong i=j+2; i<n_; ++i)  m_[i] = m1;

        tr_ = n_ - 1;
        m_[tr_] = m1;
        as_[tr_] = m_[tr_];
        return true;
    }

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

#endif  // !defined HAVE_SETPART_RGS_SUBSET_LEX_H__
