/*

Please send corrections, questions and suggestions to:
    Alexander Kasiukov <kasiuka@sunysuffolk.edu>

The routines in the "dynamic.js" do the real work.
The routines in this file are their interfaces, which output results in HTML.

Uses: "common.js", "dynamic.js", "special.js"

*/


//------------------------------ Number Partitions ---------------------------------

function listNumberPartitions( givenNumber )
{
    var myPartitions = generateNumberPartitions( givenNumber );
    return "Direct count gives "
        + countNumberPartitions( givenNumber, 1 ) + " partitions of the number " + givenNumber
        + ". Our enumeration contains " + myPartitions.length
            + " partitions:<br />"
                + listRecursiveArray( myPartitions, [ { 'prefix': '', 'postfix': '', 'separator': '<br />=' },
                        { 'prefix': '', 'postfix': '', 'separator': '+' } ] );
}
//document.write( listNumberPartitions( 10 ) );


//------------------------------ Tied Splicings of linear preorders---------------------------------


function listTiedSplicings( givenNumberA, givenNumberB )
{
    var mySplicings = generateTiedSplicings( givenNumberA, givenNumberB );
    return "<br /><br />All (tied) splicings of a linear preorder with " 
        + givenNumberA + " elements and another one with " + givenNumberB + " elements are:<br />"
        + listRecursiveArray( mySplicings, [ { 'prefix': '<ol>', 'postfix': '</ol>', 'separator': '' },
                        { 'prefix': '<li><b>Possible splicing:</b> ', 'postfix': '</li>', 'separator': ', ' } ] );
}
//document.write( listTiedSplicings( 2, 3 ) );

//------------------------------ Untied Splicings of linear orders---------------------------------

function listUntiedSplicings( givenNumberA, givenNumberB )
{
    var mySplicings = generateUntiedSplicings( givenNumberA, givenNumberB );
    return "<br /><br />There should be " 
                + gammaCombinations( givenNumberA + givenNumberB, givenNumberA ) 
                + " (untied) splicings of a linear order with " 
        + givenNumberA + " elements and another one with " + givenNumberB + " elements.<br />"
        + "We found the following ones:<br />"
        + listRecursiveArray( mySplicings, [ { 'prefix': '<ol>', 'postfix': '</ol>', 'separator': '' },
                        { 'prefix': '<li><b>Possible splicing:</b> ', 'postfix': '</li>', 'separator': ', ' } ] );
}
//document.write( listUntiedSplicings( 2, 3 ) );




//--------------------------- Combinations (and generalizations) --------------------


function listMultiSubsets( givenArray, givenCardinality, givenStorage )
{
    generateMultiSubsets( givenArray, givenCardinality, givenStorage );

    var myGammaCount = gammaCombinations( givenArray.length + givenCardinality - 1, givenCardinality );
    var myName = " multisets ";
    return displayCountAlert( myGammaCount, myName, givenArray, givenCardinality, givenStorage );
}

function listSubsets( givenArray, givenCardinality, givenStorage )
{
    generateSubsets( givenArray, givenCardinality, givenStorage );

    var myGammaCount = gammaCombinations( givenArray.length, givenCardinality );
    var myName = " combinations ";
    return displayCountAlert( myGammaCount, myName, givenArray, givenCardinality, givenStorage );
}

function displayCountAlert( givenGammaCount, givenName, givenArray, givenCardinality, givenStorage )
{
    var myPoolSize = givenArray.length;
    var myCount = givenStorage.count();
    var myDisplayArray = givenArray.join('');
    var myDisplayMultisets = givenStorage.content();

    return( "Found " + myCount + givenName
        + "(the Gamma function gives " + givenGammaCount + ") "
        + "of " + givenCardinality + " elements "
        + "selected from " + myPoolSize + " elements [" + myDisplayArray + "]: "
        + "[" + myDisplayMultisets + "]." );
}




//------------------------------------ Factorial -----------------------------------


// a hideous implementation -- just for verification of permutations routine
function findFactorial( givenNumber )
{
    var myCounter = new Counter();
    var myArray = constructArray( givenNumber, function ( x ) { return x; } );
    buildupPermutations( [], myArray, myCounter.store );
    return "Counting permutations gives " + givenNumber + "! = " + myCounter.content() + ".<br />"
            + "The Gamma function gives " + givenNumber + "! = " + gammaFactorial( givenNumber ) + ".";
}
//document.write( findFactorial( 5 ) );




//--------------------------------  Pascal's Triangle -----------------------------

// a hideous implementation -- just for verification of the combinations routine
function listPascalTriangle( givenDepth )
{
    var myIndent = [];
    for( var i = 0; i < givenDepth; i++ ) myIndent.push( '&nbsp;' );

    var myMessage = '';
    for( var i = 0; i < givenDepth; i++ )
    {
        myMessage += myIndent.join('');
        for( j = 0; j <= i; j++ )
        {
            var myStorage = new ArrayStorage();
            var myArray = constructArray( i, function ( x ) { return x; } );
            buildupSubsets( [], myArray, j, 0, myStorage.store );
            myMessage +=  myStorage.count() + " ";
        }
        myMessage += "<br />";
        myIndent.pop();
    }
    return myMessage;
}
//document.write( listPascalTriangle( 10 ) );





//---------------------------- Permutations and generalizations --------------------------------

function listPermutations( givenArray, givenStorage )
{
    generatePermutations( givenArray, givenStorage );

    // for comparison
    var myGammaCount = gammaFactorial( givenArray.length );

    var myPermutations = givenStorage.content();

    var myCount = givenStorage.count();

    return "If all elements are distinct, we would get (as computed using Gamma function) "
                        + myGammaCount + " permutations. "
                + "For the given sequence " + listRecursiveArray( givenArray ) + ", " 
                    + myCount + " permutations were found: "
                + listRecursiveArray( myPermutations, [ { 'prefix': '<ol>', 'postfix': '</ol>', 'separator': '' },
                        { 'prefix': '<li><b>Permutation:</b> (', 'postfix': ')</li>', 'separator': ', ' },
                            { 'prefix': '', 'postfix': '', 'separator': ', ' } ] );
}
//document.write( listPermutations( [ 0, 1, 2 ], new ArrayOfArraysStorage() ) );

//------------------------------ Set Partitions ---------------------------------

function listSetPartitions( givenString )
{
    var myArray = givenString.split("");
    
    var myPartitions = generateSetPartitions( myArray.slice() );
    return "All partitions of the set { " + myArray.join(', ') + " } are:<br />"
            + listRecursiveArray( myPartitions, [ { 'prefix': '<ol>', 'postfix': '</ol>', 'separator': '' },
                        { 'prefix': '<li><b>Partition:</b>', 'postfix': '</li>', 'separator': ', ' },
                            { 'prefix': '{', 'postfix': '}', 'separator': ', ' } ] );
}
//document.write( listSetPartitions( '123456' ) );


