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

// Find all psi-numbers for each GE monoid.

// 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;
    uint32_t   *point_closures, set;
    int         j, k, monoid, spacesize = 8, space = 0;
    int       **monoid_psi;

// Allocate memory for the structs that store KFZ(A) and KZ(A) for all A in X.

    KFZ        *kfz = CreateKFZ(spacesize);
    KZ         *kz = CreateKZ(spacesize);

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

// The input file contains all nonhomeomorphic spaces on 8 points.

    input = fopen("8-spaces.txt", "r");
    if (input==0)
    {
        printf("\nCould not open file 8-spaces.txt.\n");
        exit(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);
    }

    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]);

// We compute KFZ(A) and store the result in kfz.

        ComputeKFZ(kfz,spacesize,point_closures);
        CopyKZfromKFZ(kz,kfz,spacesize);
        monoid = GE(kz,spacesize,point_closures);

// We find every monoid and psi-number combination that occurs.

        for (set=0; set<(one<<spacesize); set++)
        {
            monoid_psi[monoid][Psi(set,spacesize,kfz)] = 1;
        }
        space++;                    // Advances the loop counter.
    }

// For each monoid we print all psi-numbers that occurred.

    printf("\nFor each psi-number below, some 8-space with\n");
    printf("the given GE monoid contains such a subset.\n");

    for (j=1; j<8; j++)
    {
        if (j == 6) continue;
        printf("\n%s:\t", ge_monoid[j]);
        for (k=1; k<71; k++)
            if (monoid_psi[j][k]) printf("%d ", k);
    }
    printf("\n\n");

    fclose(input);
    for (j=0; j<8; j++) free(monoid_psi[j]);
    free(monoid_psi);
    free(point_closures);
    FreeKFZ(kfz);
    FreeKZ(kz);
}
