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

// Produce header file phi-numbers.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"


int main(int argc, const char * argv[])
{
    FILE       *input = NULL;
    uint32_t   *point_closures, set;
    uint32_t  **phi_array;
    int         j, k, l, space, spacesize, max_spacesize = 11;
    int      ***phi, **phi_equals;
    int         have_phi[31] = {0};
    KZ        **kz;
    KZenum      key = {0,1,2,3,4,5,6};
    KZenum     *kz_fam = &key;

// Allocate memory for the struct that stores KZ(A) for all A in X.

    kz = (KZ **)malloc(8 * sizeof(KZ *));
    if (kz==0)
    {
        printf("\nCould not allocate space for kz.\n");
        exit(0);
    }

// No need for cardinalities 0 and 1.

    for (spacesize = 0; spacesize < 2; spacesize++) kz[spacesize] = NULL;
    for (spacesize = 2; spacesize < 8; spacesize++) kz[spacesize] = CreateKZ(spacesize);

    phi_array = (uint32_t **)malloc(7 * sizeof(uint32_t *));
    if (phi_array==0)
    {
        printf("\nCould not allocate space for phi_array.\n");
        exit(0);
    }
    phi_equals = (int **)malloc(7 * sizeof(int *));
    if (phi_equals==0)
    {
        printf("\nCould not allocate space for phi_equals.\n");
        exit(0);
    }
    for (j=0; j<7; j++)
    {
        phi_array[j] = (uint32_t *)malloc((one<<max_spacesize) * sizeof(uint32_t));
        if (phi_array[j]==0)
        {
            printf("\nCould not allocate space for phi_array[%d].\n", j);
            exit(0);
        }
        for (k=0; k<(one<<max_spacesize); k++) phi_array[j][k] = 0;
        phi_equals[j] = (int *)malloc(7 * sizeof(int));
        if (phi_equals[j]==0)
        {
            printf("\nCould not allocate space for phi_equals[%d].\n", j);
            exit(0);
        }
        for (k=0; k<7; k++) phi_equals[j][k] = 0;
    }
    
    phi = (int ***)malloc(31 * sizeof(int **));
    if (phi==0)
    {
        printf("\nCould not allocate space for phi.\n");
        exit(0);
    }
    for (j=0; j<31; j++)
    {
        phi[j] = (int **)malloc(7 * sizeof(int *));
        if (phi[j]==0)
        {
            printf("\nCould not allocate space for phi[%d].\n", j);
            exit(0);
        }
        for (k=0; k<7; k++)
        {
            phi[j][k] = (int *)malloc(7 * sizeof(int));
            if (phi[j][k]==0)
            {
                printf("\nCould not allocate space for phi[%d][%d].\n", j, k);
                exit(0);
            }
            for (l=0; l<7; l++) phi[j][k][l] = 0;
        }
    }

// Sets are represented by unsigned 32-bit binary integers with leading zeros.
// Assuming X = {0,1,...,n-1} the element k is in A iff the coefficient of 2^k in the
// binary integer representing A is 1 (i.e., the bits of the binary integer representing
// A are in reverse order compared to X).

    point_closures = (uint32_t *)malloc(spacesize * sizeof(uint32_t));
    if (point_closures==0)
    {
        printf("\nCould not allocate space for point_closures.\n");
        exit(0);
    }

// When we first encounter a set with phi-number n, for all x,y <= 7 we set
//
//     phi[n][x][y] = 1 iff phi_array[x][set] == phi_array[y][set].
//
// We then intersect all phi[] matrices pairwise and compute the phi-numbers of these
// intersections.

    for (spacesize = 2; spacesize < 8; spacesize++)
    {
        switch (spacesize)
        {
            case 2:
                input = fopen("2-spaces.txt", "r");
                if (input==0)
                {
                    printf("\nCould not open file 2-spaces.txt for input.\n");
                    exit(0);
                }
                break;
            case 3:
                input = fopen("3-spaces.txt", "r");
                if (input==0)
                {
                    printf("\nCould not open file 3-spaces.txt for input.\n");
                    exit(0);
                }
                break;
            case 4:
                input = fopen("4-spaces.txt", "r");
                if (input==0)
                {
                    printf("\nCould not open file 4-spaces.txt for input.\n");
                    exit(0);
                }
                break;
            case 5:
                input = fopen("5-spaces.txt", "r");
                if (input==0)
                {
                    printf("\nCould not open file 5-spaces.txt for input.\n");
                    exit(0);
                }
                break;
            case 6:
                input = fopen("6-spaces.txt", "r");
                if (input==0)
                {
                    printf("\nCould not open file 6-spaces.txt for input.\n");
                    exit(0);
                }
                break;
            case 7:
                input = fopen("7-spaces.txt", "r");
                if (input==0)
                {
                    printf("\nCould not open file 7-spaces.txt for input.\n");
                    exit(0);
                }
        }

        space = 0;
        while (space < spaces[spacesize])
        {

// Each line of input has the closure of every singleton.  These can alternatively be
// regarded as smallest open neighborhoods.  Either interpretation yields all
// nonhomeomorphic spaces of cardinality = spacesize, but any given line does not
// in general produce the same space under the two.

// Read the current line of input into the point_closures array.

            for (j=0; j<spacesize; j++) fscanf(input, "%" PRIu32, &point_closures[j]);

// Compute KZ(A) for every A in X.

            ComputeKZ(kz[spacesize],spacesize,point_closures);

// Store KZ(A) in array format.

            CopyKZarray(phi_array,kz[spacesize],spacesize);

// Compute all 0-1 phi-number matrices phi[j] (just once for each j).

            for (set=0; set<(one<<spacesize); set++)
            {
                j = Phi(set,kz[spacesize]);
                if (!have_phi[j])
                {
                    for (k=0; k<7; k++)
                        for (l=0; l<7; l++)
                        {
                            if (phi_array[k][set] == phi_array[l][set]) phi[j][k][l] = 1;
                            else phi[j][k][l] = 0;
                        }
                    have_phi[j] = 1;
                }
            }
            space++;
        }
        fclose(input);
    }

// Print phi-numbers.h

    printf("// phi-numbers.h\n");
    printf("// Copyright (C) 2023 Mark Bowron\n\n");
    printf("// Phi-number matrices.\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 phi_numbers[31][7][7] = {\n");

    for (j=0; j<31; j++)
    {
        printf("{");
        for (k=0; k<6; k++)
        {
            printf("{");
            for (l=0; l<6; l++) printf("%d,", phi[j][k][l]);
            printf("%d},", phi[j][k][6]);
        }
        printf("{");
        for (l=0; l<6; l++) printf("%d,", phi[j][6][l]);
        if (j<30) printf("%d}},\n", phi[j][6][6]);
        else printf("%d}}};\n\n", phi[j][6][6]);
    }

    free(point_closures);
    for (j=0; j<7; j++)
    {
        free(phi_array[j]);
        free(phi_equals[j]);
    }
    free(phi_array);
    free(phi_equals);
    for (j=0; j<31; j++)
    {
        for (k=0; k<7; k++) free(phi[j][k]);
        free(phi[j]);
    }
    free(phi);
    for (spacesize = 2; spacesize < 8; spacesize++) FreeKZ(kz[spacesize]);
    free(kz);
}
