<commit_before>// Copyright 2016-2017 ELIFE. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
#include <ginger/vector.h>
#include <inform/pid.h>
#include <inform/dist.h>
#include <inform/utilities.h>
#include <inform/utilities/black_boxing.h>
#include <inform/utilities/encoding.h>
#include <string.h>
#include <math.h>

static void inform_pid_source_free(inform_pid_source *src)
{
    if (src)
    {
        gvector_free(src->name);
        gvector_free(src->above);
        gvector_free(src->below);
        free(src);
    }
}

static inform_pid_source *inform_pid_source_alloc(size_t *name)
{
    inform_pid_source *src = malloc(sizeof(inform_pid_source));
    if (src)
    {
        src->name = gvector_shrink(name);
        src->size = gvector_len(src->name);

        src->below = NULL;

        src->above = gvector_alloc(0, 0, sizeof(inform_pid_source*));
        if (!src->above)
        {
            inform_pid_source_free(src);
            return NULL;
        }

        src->below = gvector_alloc(0, 0, sizeof(inform_pid_source*));
        if (!src->below)
        {
            inform_pid_source_free(src);
            return NULL;
        }

        src->n_above = src->n_below = 0;
        src->size = gvector_len(src->name);

        src->imin = src->pi = src->info = 0.0;
    }
    return src;
}

static inform_pid_source **sources_rec(size_t i, size_t m, size_t *c,
        inform_pid_source **srcs)
{
    if (i < m)
    {
        srcs = sources_rec(i + 1, m, gvector_dup(c), srcs);
    }

    if (i <= m)
    {
        size_t z = 0;
        for (size_t j = 0; j < gvector_len(c); ++j)
        {
            z = i & c[j];
            if (z == i || z == c[j])
            {
                gvector_free(c);
                return srcs;
            }
        }
        gvector_push(c, i);

        size_t *d = gvector_dup(c);
        gvector_push(srcs, inform_pid_source_alloc(c));
        return sources_rec(i + 1, m, d, srcs);
    }
    else
    {
        gvector_free(c);
    }

    return srcs;
}

static inform_pid_source **inform_pid_sources(size_t n)
{
    size_t const m = (1 << n) - 1;
    inform_pid_source **srcs = gvector_alloc(0, 0, sizeof(inform_pid_source*));
    for (size_t i = 1; i <= m; ++i)
    {
        size_t *c = gvector_alloc(1, 1, sizeof(size_t));
        c[0] = i;

        size_t *d = gvector_dup(c);
        gvector_push(srcs, inform_pid_source_alloc(c));
        srcs = sources_rec(i + 1, m, d, srcs);
    }
    srcs = gvector_shrink(srcs);
    return srcs;
}

static bool name_below(size_t const *xs, size_t const *ys)
{
    size_t const m = gvector_len(xs);
    size_t const n = gvector_len(ys);

    bool is_below = true;
    for (size_t const *y = ys; y < ys + n && is_below; ++y)
    {
        bool is_valid = false;
        for (size_t const *x = xs; x < xs + m && !is_valid; ++x)
        {
            if ((*x & *y) == *x)<commit_msg>Simplify name duplication<commit_after>// Copyright 2016-2017 ELIFE. All rights reserved.
// Use of this source code is governed by a MIT
// license that can be found in the LICENSE file.
#include <ginger/vector.h>
#include <inform/pid.h>
#include <inform/dist.h>
#include <inform/utilities.h>
#include <inform/utilities/black_boxing.h>
#include <inform/utilities/encoding.h>
#include <string.h>
#include <math.h>

static void inform_pid_source_free(inform_pid_source *src)
{
    if (src)
    {
        gvector_free(src->name);
        gvector_free(src->above);
        gvector_free(src->below);
        free(src);
    }
}

static inform_pid_source *inform_pid_source_alloc(size_t *name)
{
    inform_pid_source *src = malloc(sizeof(inform_pid_source));
    if (src)
    {
        src->name = gvector_dup(name);
        src->name = gvector_shrink(src->name);
        src->size = gvector_len(src->name);

        src->below = NULL;

        src->above = gvector_alloc(0, 0, sizeof(inform_pid_source*));
        if (!src->above)
        {
            inform_pid_source_free(src);
            return NULL;
        }

        src->below = gvector_alloc(0, 0, sizeof(inform_pid_source*));
        if (!src->below)
        {
            inform_pid_source_free(src);
            return NULL;
        }

        src->n_above = src->n_below = 0;
        src->size = gvector_len(src->name);

        src->imin = src->pi = src->info = 0.0;
    }
    return src;
}

static inform_pid_source **sources_rec(size_t i, size_t m, size_t *c,
        inform_pid_source **srcs)
{
    if (i < m)
    {
        srcs = sources_rec(i + 1, m, gvector_dup(c), srcs);
    }

    if (i <= m)
    {
        size_t z = 0;
        for (size_t j = 0; j < gvector_len(c); ++j)
        {
            z = i & c[j];
            if (z == i || z == c[j])
            {
                gvector_free(c);
                return srcs;
            }
        }
        gvector_push(c, i);
        gvector_push(srcs, inform_pid_source_alloc(c));
        return sources_rec(i + 1, m, c, srcs);
    }
    else
    {
        gvector_free(c);
    }

    return srcs;
}

static inform_pid_source **inform_pid_sources(size_t n)
{
    size_t const m = (1 << n) - 1;
    inform_pid_source **srcs = gvector_alloc(0, 0, sizeof(inform_pid_source*));
    for (size_t i = 1; i <= m; ++i)
    {
        size_t *c = gvector_alloc(1, 1, sizeof(size_t));
        c[0] = i;

        gvector_push(srcs, inform_pid_source_alloc(c));
        srcs = sources_rec(i + 1, m, c, srcs);
    }
    srcs = gvector_shrink(srcs);
    return srcs;
}

static bool name_below(size_t const *xs, size_t const *ys)
{
    size_t const m = gvector_len(xs);
    size_t const n = gvector_len(ys);

    bool is_below = true;
    for (size_t const *y = ys; y < ys + n && is_below; ++y)
    {
        bool is_valid = false;
        for (size_t const *x = xs; x < xs + m && !is_valid; ++x)
        {
            if ((*x & *y) == *x)
{'fraction_non_alphanumeric': 0.13828164717844432, 'fraction_numerical': 0.009489916963226572, 'mean_word_length': 2.1310344827586207, 'pattern_counts': {'":': 0, '<': 35, '<?xml version=': 0, '>': 57, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '553253', 'n_tokens_mistral': 2278, 'n_tokens_neox': 2150, 'n_words': 489}