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

// Find all psi-numbers admitted by various spaces.

// 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    <stdbool.h>
#include    <string.h>
#include    "set_operators.h"
#include    "set_families.h"
#include    "set_relations.h"
#include    "collapses.h"
#include    "topologies.h"


int main(int argc, const char * argv[])
{
    uint32_t   *point_closures, set;
    int       **closures, **base, psi_present[71] = {0};
    int         j, k, spacesize, max_spacesize = 11;

// These structs store KFG(A) and KZ(A) for all A in X.

    KFZ        *kfz = CreateKFZ(max_spacesize);

// Sets are represented by unsigned 32-bit integers with leading zeros.

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

    base = (int **)malloc(max_spacesize * sizeof(int *));
    if (base==0)
    {
        printf("\nCould not allocate space for base.\n");
        exit(0);
    }

    closures = (int **)malloc(max_spacesize * sizeof(int *));
    if (closures==0)
    {
        printf("\nCould not allocate space for closures.\n");
        exit(0);
    }

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

// The minimal GE space X with base {{w}, {x,y}, {w,x,y,z}} only admits psi-numbers
// 42,44,62,63,65,67,69,70.

    static const int ge[4][4] = {
                                {1,0,0,0},
                                {0,1,1,0},
                                {0,1,1,0},
                                {1,1,1,1}};

    spacesize = 4;
    for (j=0; j<spacesize; j++) memcpy(base[j],ge[j],sizeof(ge[j]));
    BaseToPointClosures(base,closures,point_closures,spacesize);
    ComputeKFZ(kfz,spacesize,point_closures);
    for (j=0; j<71; j++) psi_present[j] = 0;
    for (set=0; set<(one<<spacesize); set++) psi_present[Psi(set,spacesize,kfz)] = 1;
    printf("Find all psi-numbers admitted by various spaces.\n\nminimal GE space: ");
    for (j=1; j<71; j++)
    {
        if (psi_present[j]) printf("%d ", j);
    }
    printf("\n");

// The minimal KD space X with base {{v}, {w}, {v,w,x}, {y,z}} only admits psi-numbers
// 31,46,48,59,60,62-70.

    static const int kd[5][5] = {
                                {1,0,0,0,0},
                                {0,1,0,0,0},
                                {1,1,1,0,0},
                                {0,0,0,1,1},
                                {0,0,0,1,1}};

    spacesize = 5;
    for (j=0; j<spacesize; j++) memcpy(base[j],kd[j],sizeof(kd[j]));
    BaseToPointClosures(base,closures,point_closures,spacesize);
    ComputeKFZ(kfz,spacesize,point_closures);
    for (j=0; j<71; j++) psi_present[j] = 0;
    for (set=0; set<(one<<spacesize); set++) psi_present[Psi(set,spacesize,kfz)] = 1;
    printf("minimal KD space: ");
    for (j=1; j<71; j++)
    {
        if (psi_present[j]) printf("%d ", j);
    }
    printf("\n");

// The minimal ED space X with base {{x,y}, {x,y,z}} only admits psi-numbers
// 61,65,67,69,70.

    static const int ed[3][3] = {
                                {1,1,0},
                                {1,1,0},
                                {1,1,1}};

    spacesize = 3;
    for (j=0; j<spacesize; j++) memcpy(base[j],ed[j],sizeof(ed[j]));
    BaseToPointClosures(base,closures,point_closures,spacesize);
    ComputeKFZ(kfz,spacesize,point_closures);
    for (j=0; j<71; j++) psi_present[j] = 0;
    for (set=0; set<(one<<spacesize); set++) psi_present[Psi(set,spacesize,kfz)] = 1;
    printf("minimal ED space: ");
    for (j=1; j<71; j++)
    {
        if (psi_present[j]) printf("%d ", j);
    }
    printf("\n");

// The ED space X with base {{w}, {x,y}, {x,y,z}} only admits psi-numbers
// 65,67,69,70.

    static const int edone[4][4] =  {
                                    {1,0,0,0},
                                    {0,1,1,0},
                                    {0,1,1,0},
                                    {0,1,1,1}};

    spacesize = 4;
    for (j=0; j<spacesize; j++) memcpy(base[j],edone[j],sizeof(edone[j]));
    BaseToPointClosures(base,closures,point_closures,spacesize);
    ComputeKFZ(kfz,spacesize,point_closures);
    for (j=0; j<71; j++) psi_present[j] = 0;
    for (set=0; set<(one<<spacesize); set++) psi_present[Psi(set,spacesize,kfz)] = 1;
    printf("ED space sans 61: ");
    for (j=1; j<71; j++)
    {
        if (psi_present[j]) printf("%d ", j);
    }
    printf("\n");

// The minimal OU space X with base {{x}, {y}, {x,y,z}} only admits psi-numbers
// 62,63,65,67,69,70.

    static const int ou[3][3] = {
                                {1,0,0},
                                {0,1,0},
                                {1,1,1}};

    spacesize = 3;
    for (j=0; j<spacesize; j++) memcpy(base[j],ou[j],sizeof(ou[j]));
    BaseToPointClosures(base,closures,point_closures,spacesize);
    ComputeKFZ(kfz,spacesize,point_closures);
    for (j=0; j<71; j++) psi_present[j] = 0;
    for (set=0; set<(one<<spacesize); set++) psi_present[Psi(set,spacesize,kfz)] = 1;
    printf("minimal OU space: ");
    for (j=1; j<71; j++)
    {
        if (psi_present[j]) printf("%d ", j);
    }
    printf("\n");

// The minimal EO space X with base {{x}, {x,y}} only admits psi-numbers
// 65,67,69,70.

    static const int eo[2][2] = {
                                {1,0},
                                {1,1}};

    spacesize = 2;
    for (j=0; j<spacesize; j++) memcpy(base[j],eo[j],sizeof(eo[j]));
    BaseToPointClosures(base,closures,point_closures,spacesize);
    ComputeKFZ(kfz,spacesize,point_closures);
    for (j=0; j<71; j++) psi_present[j] = 0;
    for (set=0; set<(one<<spacesize); set++) psi_present[Psi(set,spacesize,kfz)] = 1;
    printf("minimal EO space: ");
    for (j=1; j<71; j++)
    {
        if (psi_present[j]) printf("%d ", j);
    }
    printf("\n");

// The minimal indiscrete partition space X with base {{x,y}} only admits psi-numbers
// 61,69,70.

    static const int p[2][2] =  {
                                {1,1},
                                {1,1}};

    spacesize = 2;
    for (j=0; j<spacesize; j++) memcpy(base[j],p[j],sizeof(p[j]));
    BaseToPointClosures(base,closures,point_closures,spacesize);
    ComputeKFZ(kfz,spacesize,point_closures);
    for (j=0; j<71; j++) psi_present[j] = 0;
    for (set=0; set<(one<<spacesize); set++) psi_present[Psi(set,spacesize,kfz)] = 1;
    printf("minimal indiscrete partition space: ");
    for (j=1; j<71; j++)
    {
        if (psi_present[j]) printf("%d ", j);
    }
    printf("\n");

// The minimal non-indiscrete partition space X with base {{x},{y,z}} only admits
// psi-numbers 59,60,68,69,70.

    static const int part[3][3] =   {
                                    {1,0,0},
                                    {0,1,1},
                                    {0,1,1}};

    spacesize = 3;
    for (j=0; j<spacesize; j++) memcpy(base[j],part[j],sizeof(part[j]));
    BaseToPointClosures(base,closures,point_closures,spacesize);
    ComputeKFZ(kfz,spacesize,point_closures);
    for (j=0; j<71; j++) psi_present[j] = 0;
    for (set=0; set<(one<<spacesize); set++) psi_present[Psi(set,spacesize,kfz)] = 1;
    printf("minimal non-indiscrete partition space: ");
    for (j=1; j<71; j++)
    {
        if (psi_present[j]) printf("%d ", j);
    }
    printf("\n");

// The minimal discrete space with |X| > 1 only admits psi-numbers 68,69,70.

    static const int d[2][2] =  {
                                {1,0},
                                {0,1}};

    spacesize = 2;
    for (j=0; j<spacesize; j++) memcpy(base[j],d[j],sizeof(d[j]));
    BaseToPointClosures(base,closures,point_closures,spacesize);
    ComputeKFZ(kfz,spacesize,point_closures);
    for (j=0; j<71; j++) psi_present[j] = 0;
    for (set=0; set<(one<<spacesize); set++) psi_present[Psi(set,spacesize,kfz)] = 1;
    printf("minimal discrete space with |X| > 1: ");
    for (j=1; j<71; j++)
    {
        if (psi_present[j]) printf("%d ", j);
    }
    printf("\n\n");

    for (j=0; j<max_spacesize; j++)
    {
        free(base[j]);
        free(closures[j]);
    }
    free(base);
    free(closures);
    free(point_closures);
    FreeKFZ(kfz);
    return EXIT_SUCCESS;
}
