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

// Produce header file local_orderings_274.h

// 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_496.h"


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

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

// Initialize inequalities[0] separately as the first of 274 local orderings on KFZ.

    for (j=0; j<17; j++)
        for (k=0; k<17; k++)
            inequalities[count][j][k] = local_orderings_on_kf[0][j][k];
    count++;

// Now find the rest.

    for (ordering=1; ordering<496; ordering++)
    {
        for (j=0; j<17; j++)
            for (k=0; k<17; k++)
                inequalities[count][j][k] = local_orderings_on_kf[ordering][j][k];
        for (m=0; m<count; m++)
        {
            for (j=0; j<17; j++)
            {
                k = 0;
                while (k < 17)
                {
                    if (inequalities[count][j][k] != inequalities[m][j][k]) break;
                    k++;
                }
                if (k < 17)
                {
                    flag = 1;
                    break;
                }
            }
            if (flag)
            {
                flag = 0;
                continue;
            }
            break;  // if we get to here it means the local ordering is not new
        }
        if (m == count) count++;
    }

// Print local_orderings_274.h

    printf("// local_orderings_274.h\n");
    printf("// Copyright (C) 2023 Mark Bowron\n\n");
    printf("// All local orderings on the monoid KFZ.\n\n");
    printf("// This program is free software: you can redistribute it and/or modify\n");
    printf("// it under the terms of the GNU General Public License as published by\n");
    printf("// the Free Software Foundation, either version 3 of the License, or\n");
    printf("// (at your option) any later version.\n\n");
    printf("// This program is distributed in the hope that it will be useful,\n");
    printf("// but WITHOUT ANY WARRANTY; without even the implied warranty of\n");
    printf("// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n");
    printf("// GNU General Public License for more details.\n\n");
    printf("static const int local_orderings_on_kfz[274][17][17] = {\n");

    if (count == 274)
    {
        j = 0;
        while (j<count)
        {
            printf("{");
            for (k=0; k<16; k++)
            {
                printf("{");
                for (m=0; m<16; m++) printf("%d,", inequalities[j][k][m]);
                printf("%d},", inequalities[j][k][16]);
            }
            printf("{");
            for (m=0; m<16; m++) printf("%d,", inequalities[j][16][m]);
            if (j<count-1) printf("%d}},\n", inequalities[j][16][16]);
            else printf("%d}}};\n\n", inequalities[j][16][16]);
            j++;
        }
    }
    else printf("There are actually %d local orderings on KFZ\n", count);

    for (j=0; j<300; j++)
    {
        for (k=0; k<17; k++) free(inequalities[j][k]);
        free(inequalities[j]);
    }
    free(inequalities);
    printf("\n\n");
    return EXIT_SUCCESS;
}
