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

// Produce header file local_orderings_66.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;
    static const int    k_index[14] = {0,1,2,3,4,5,6,17,18,19,20,21,22,23};

    inequalities = (int ***)malloc(70 * sizeof(int **));
    if (inequalities==0)
    {
        printf("\nCould not allocate space for inequalities.\n");
        exit(0);
    }
    for (j=0; j<70; j++)
    {
        inequalities[j] = (int **)malloc(14 * sizeof(int *));
        if (inequalities[j]==0)
        {
            printf("\nCould not allocate space for inequalities[%d].\n", j);
            exit(0);
        }
        for (k=0; k<14; k++)
        {
            inequalities[j][k] = (int *)malloc(14 * 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<14; m++) inequalities[j][k][m] = 0;
        }
    }

// Initialize inequalities[0] separately as the first of 66 local orderings on K.

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

// Now find the rest.

    for (ordering=1; ordering<496; ordering++)
    {
        for (j=0; j<14; j++)
            for (k=0; k<14; k++)
                inequalities[count][j][k] = local_orderings_on_kf[ordering][k_index[j]][k_index[k]];
        for (m=0; m<count; m++)
        {
            for (j=0; j<14; j++)
            {
                k = 0;
                while (k < 14)
                {
                    if (inequalities[count][j][k] != inequalities[m][j][k]) break;
                    k++;
                }
                if (k < 14)
                {
                    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_66.h

    printf("// local_orderings_66.h\n");
    printf("// Copyright (C) 2023 Mark Bowron\n\n");
    printf("// All local orderings on the Kuratowski monoid.\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_k[66][14][14] = {\n");

    j = 0;
    while (j<count)
    {
        printf("{");
        for (k=0; k<13; k++)
        {
            printf("{");
            for (m=0; m<13; m++) printf("%d,", inequalities[j][k][m]);
            printf("%d},", inequalities[j][k][13]);
        }
        printf("{");
        for (m=0; m<13; m++) printf("%d,", inequalities[j][13][m]);
        if (j<count-1) printf("%d}},\n", inequalities[j][13][13]);
        else printf("%d}}};\n\n", inequalities[j][13][13]);
        j++;
    }

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