// main.c
// Copyright (C) 2023 Mark Bowron

// Count local collapses of KFGZ in spaces of cardinality 11.

// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.


#include    <stdio.h>
#include    <stdlib.h>
#include    <inttypes.h>
#include    <stdbool.h>
#include    <string.h>
#include    "set_operators.h"
#include    "set_families.h"
#include    "set_relations.h"
#include    "collapses.h"
#include    "local_orderings_666.h"


int main(int argc, const char * argv[])
{
    int      ***collapses;
    int         j, k, m = 0, ordering, count = 0, flag = 0;

    collapses = (int ***)malloc(200 * sizeof(int **));
    if (collapses==0)
    {
        printf("\nCould not allocate space for collapses.\n");
        exit(0);
    }
    for (j=0; j<200; j++)
    {
        collapses[j] = (int **)malloc(20 * sizeof(int *));
        if (collapses[j]==0)
        {
            printf("\nCould not allocate space for collapses[%d].\n", j);
            exit(0);
        }
        for (k=0; k<20; k++)
        {
            collapses[j][k] = (int *)malloc(20 * sizeof(int));
            if (collapses[j][k]==0)
            {
                printf("\nCould not allocate space for collapses[%d][%d].\n", j, k);
                exit(0);
            }
            for (m=0; m<20; m++)
            {
                if (m == k) collapses[j][k][m] = 1;
                else collapses[j][k][m] = 0;
            }
        }
    }

    for (ordering=0; ordering<666; ordering++)
    {
        for (j=0; j<20; j++)
            for (k=0; k<20; k++)
            {
                if (k == j) continue;
                collapses[count][j][k] = 0;
            }
        for (j=0; j<19; j++)
            for (k=j+1; k<20; k++)
            {
                if (local_orderings_on_kfgz[ordering][j][k]*local_orderings_on_kfgz[ordering][k][j])
                {
                    collapses[count][j][k] = 1;
                    collapses[count][k][j] = 1;
                }
            }
        if (!count)
        {
            count++;
            continue;
        }
        for (j=0; j<count; j++)
        {
            for (k=0; k<20; k++)
            {
                m = 0;
                while (m < 20)
                {
                    if (collapses[count][k][m] != collapses[j][k][m]) break;
                    m++;
                }
                if (m < 20)
                {
                    flag = 1;
                    break;
                }
            }
            if (flag)
            {
                flag = 0;
                continue;
            }
            break;  // if we get to here it means the collapse is not new
        }
        if (j == count) count++;
    }

    printf("\nNumber of collapses of KFGZ in 11-spaces: %d\n", count);

    for (j=0; j<200; j++)
    {
        for (k=0; k<20; k++) free(collapses[j][k]);
        free(collapses[j]);
    }
    free(collapses);
    printf("\n");
    return EXIT_SUCCESS;
}
