<commit_before>#include "interference.h"

ljit_interference_graph *_ljit_interference_graph_new(int size)
{
    ljit_interference_graph *ig = NULL;

    if ((ig = malloc(sizeof (ljit_interference_graph))) == NULL)
        return NULL;

    if ((ig->graph = calloc(1, size * sizeof (_ljit_liveness_info *))) == NULL)
    {
        free(ig);
        return NULL;
    }

    ig->size = size;

    return ig;
}

void _ljit_interference_graph_free(ljit_interference_graph *ig)
{
    for (int i = 0; i < ig->size; ++i)
        _ljit_liveness_info_free(ig->graph[i]);

    free(ig->graph);
    free(ig);
}

int _ljit_interference_graph_edge_exists(ljit_interference_graph *ig,
                                         unsigned short v1,
                                         unsigned short v2)
{
    _ljit_liveness_info *tmp = ig->graph[v1];

    while (tmp)
    {
        if (tmp->elt == v2)
            return 1;

        tmp = tmp->next;
    }

    return 0;
}

void _ljit_interference_graph_add_edge(ljit_interference_graph *ig,
                                       unsigned short v1,
                                       unsigned short v2)
{
    /* If the edge exists nothing to do */
    if (_ljit_interference_graph_edge_exists(ig, v1, v2))
        return;

    _ljit_liveness_info *e1 = _ljit_liveness_info_new(v1);
    _ljit_liveness_info *e2 = _ljit_liveness_info_new(v2);


    ig->graph[v1] = _ljit_liveness_add_head(ig->graph[v1], e2);
    ig->graph[v2] = _ljit_liveness_add_head(ig->graph[v2], e1);
}

ljit_interference_graph *
_ljit_interference_graph_build_from_out(_ljit_liveness_info **out,
                                        int graph_size)
{
    ljit_interference_graph *ig = NULL;
    _ljit_liveness_info *tmp = NULL;

    if ((ig = _ljit_interference_graph_new(graph_size)) == NULL)
        return NULL;

    /* Run through every data of the out data */
    for (int i = 0; i < graph_size; ++i)
    {
        tmp = out[i];

        while (tmp)
        {
            /*
            **  If the value is the same as the current one don't create a loop
            **  edge
            */
            if (tmp->elt == i)
            {
                tmp = tmp->next;
                continue;
            }

            /* Add the edge to the interference graph */
            _ljit_interference_graph_add_edge(ig, i, tmp->elt);

            tmp = tmp->next;
        }
    }

    return ig;
}

void _ljit_interference_graph_debug(ljit_interference_graph *ig, char *file)
{
    FILE *f = NULL;
    _ljit_liveness_info *tmp = NULL;<commit_msg>[CORE] Correct inteference graph building function<commit_after>#include "interference.h"

ljit_interference_graph *_ljit_interference_graph_new(int size)
{
    ljit_interference_graph *ig = NULL;

    if ((ig = malloc(sizeof (ljit_interference_graph))) == NULL)
        return NULL;

    if ((ig->graph = calloc(1, size * sizeof (_ljit_liveness_info *))) == NULL)
    {
        free(ig);
        return NULL;
    }

    ig->size = size;

    return ig;
}

void _ljit_interference_graph_free(ljit_interference_graph *ig)
{
    for (int i = 0; i < ig->size; ++i)
        _ljit_liveness_info_free(ig->graph[i]);

    free(ig->graph);
    free(ig);
}

int _ljit_interference_graph_edge_exists(ljit_interference_graph *ig,
                                         unsigned short v1,
                                         unsigned short v2)
{
    _ljit_liveness_info *tmp = ig->graph[v1];

    while (tmp)
    {
        if (tmp->elt == v2)
            return 1;

        tmp = tmp->next;
    }

    return 0;
}

void _ljit_interference_graph_add_edge(ljit_interference_graph *ig,
                                       unsigned short v1,
                                       unsigned short v2)
{
    /* If the edge exists nothing to do */
    if (_ljit_interference_graph_edge_exists(ig, v1, v2))
        return;

    _ljit_liveness_info *e1 = _ljit_liveness_info_new(v1);
    _ljit_liveness_info *e2 = _ljit_liveness_info_new(v2);


    ig->graph[v1] = _ljit_liveness_add_head(ig->graph[v1], e2);
    ig->graph[v2] = _ljit_liveness_add_head(ig->graph[v2], e1);
}

ljit_interference_graph *
_ljit_interference_graph_build_from_out(_ljit_liveness_info **out,
                                        int graph_size)
{
    ljit_interference_graph *ig = NULL;
    _ljit_liveness_info *tmp = NULL;
    _ljit_liveness_info *ref = NULL;

    if ((ig = _ljit_interference_graph_new(graph_size)) == NULL)
        return NULL;

    /* Run through every data of the out data */
    for (int i = 0; i < graph_size; ++i)
    {
        ref = out[i];

        while (ref)
        {
            tmp = out[i];

            while (tmp)
            {
                /*
                 **  If the value is the same as the current one don't create a loop
                 **  edge
                 */
                if (tmp->elt == ref->elt)
                {
                    tmp = tmp->next;
                    continue;
                }

                /* Add the edge to the interference graph */
                _ljit_interference_graph_add_edge(ig, ref->elt, tmp->elt);

                tmp = tmp->next;
            }

            ref = ref->next;
        }
    }

    return ig;
}

void _ljit_interference_graph_debug(ljit_interference_graph *ig, char *file)
{
    FILE *f = NULL;
    _ljit_liveness_info *tmp = NULL;
{'fraction_non_alphanumeric': 0.10212053571428571, 'fraction_numerical': 0.00855654761904762, 'mean_word_length': 1.8479872881355932, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 38, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 16, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7284963', 'n_tokens_mistral': 1816, 'n_tokens_neox': 1743, 'n_words': 373}