<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 <inform/dist.h>
#include <string.h>

inform_dist* inform_dist_alloc(size_t n)
{
    // if the requested support size is zero, return NULL
    if (n == 0)
    {
        return NULL;
    }
    // allocate the distribution
    inform_dist *dist = malloc(sizeof(inform_dist));
    // if the allocation succeeded
    if (dist != NULL)
    {
        // allocate the underlying histogram
        dist->histogram = calloc(n, sizeof(uint32_t));
        // if the allocation succeeded
        if (dist->histogram != NULL)
        {
            // set the distribution size and counts
            dist->size   = n;
            dist->counts = 0;
        }
        // otherwise free the distribution
        else
        {
            free(dist);
            dist = NULL;
        }
    }
    // return the (potentially NULL) distribution
    return dist;
}

inform_dist* inform_dist_realloc(inform_dist *dist, size_t n)
{
    // if the requested size is zero, return the original distribution
    if (n == 0)
    {
        return dist;
    }
    // if the distribution is not NULL and the requested size is different
    // from the current size
    if (dist != NULL && dist->size != n)
    {
        // realloc the histogram
        uint32_t *histogram = realloc(dist->histogram, n * sizeof(uint32_t));
        // if the reallocation succeeded
        if (histogram != NULL)
        {
            // reset the distribution's histogram to the new histogram
            dist->histogram = histogram;
            // if the histogram was shrunken
            if (n < dist->size)
            {
                // set the new distribution size
                dist->size = n;
                // sum up the counts that are in the smaller support
                dist->counts = 0;
                for (size_t i = 0; i < dist->size; ++i)
                {
                    dist->counts += dist->histogram[i];
                }
            }
            // otherwise
            else
            {
                // zero out all of the newly observable events
                for (size_t i = dist->size; i < n; ++i)
                {
                    dist->histogram[i] = 0;
                }
                // set the size of the distribution
                dist->size = n;
            }
        }
        // otherwise
        else
        {
            // return NULL leaving the original distribution unscathed
            return NULL;
        }
    }
    // if the original distribution was NULL
    else if (dist == NULL)
    {
        // simply allocate the a new distribution of the requested size
        dist = inform_dist_alloc(n);
    }
    // return the (potentially NULL) distribution
    return dist;
}

inform_dist* inform_dist_copy(inform_dist const *src, inform_dist *dest)
{
    // if the source is NULL, return NULL.
    if (src == NULL)
    {
        return NULL;
    }
    // if the destination is NULL or the destination size is not the same
    // as the source
    else if (dest == NULL || src->size != dest->size)
    {
        // reallocate the destination
        inform_dist *redest = inform_dist_realloc(dest, src->size);
        // return NULL if the allocation fails
        if (redest == NULL)
        {
            return NULL;
        }
        // point dest at the new distribution
        dest = redest;
    }
    // don't copy the source to itself
    else if (src == dest)
    {
        return dest;
    }
    // copy the contents of the histogram from the source to the destination
    memcpy(dest->histogram, src->histogram, src->size * sizeof(uint32_t));
    // set the counts appropriately
    dest->counts = src->counts;
    // return the modified destination
    return dest;
}

inform_dist* inform_dist_dup(inform_dist const *dist)
{
    // if the source distribution is NULL, then just return NULL.
    if (dist == NULL)
    {
        return NULL;
    }
    // allocate the new distribution
    inform_dist *dup = inform_dist_alloc(dist->size);
    // if the allocation succeeded
    if (dup != NULL)
    {
        // copy the source distribution to the new distribution
        inform_dist_copy(dist, dup);
    }
    // return the (potentially NULL) distribution
    return dup;
}

inform_dist* inform_dist_create(uint32_t const *data, size_t n)
{
    // if the requested support size is zero, return NULL
    if (n == 0)
    {
        return NULL;
    }
    // allocate the distribution
    inform_dist *dist = malloc(sizeof(inform_dist));
    // if the allocation succeeded
    if (dist != NULL)
    {
        // allocate the underlying histogram
        dist->histogram = calloc(n, sizeof(uint32_t));
        // if the allocation succeeded
        if (dist->histogram != NULL)
        {
            // set the distribution content, size and counts
            memcpy(dist->histogram, data, n*sizeof(uint32_t));
            dist->size   = n;
            dist->counts = 0;<commit_msg>Fix NULL bug in inform_dist_create<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 <inform/dist.h>
#include <string.h>

inform_dist* inform_dist_alloc(size_t n)
{
    // if the requested support size is zero, return NULL
    if (n == 0)
    {
        return NULL;
    }
    // allocate the distribution
    inform_dist *dist = malloc(sizeof(inform_dist));
    // if the allocation succeeded
    if (dist != NULL)
    {
        // allocate the underlying histogram
        dist->histogram = calloc(n, sizeof(uint32_t));
        // if the allocation succeeded
        if (dist->histogram != NULL)
        {
            // set the distribution size and counts
            dist->size   = n;
            dist->counts = 0;
        }
        // otherwise free the distribution
        else
        {
            free(dist);
            dist = NULL;
        }
    }
    // return the (potentially NULL) distribution
    return dist;
}

inform_dist* inform_dist_realloc(inform_dist *dist, size_t n)
{
    // if the requested size is zero, return the original distribution
    if (n == 0)
    {
        return dist;
    }
    // if the distribution is not NULL and the requested size is different
    // from the current size
    if (dist != NULL && dist->size != n)
    {
        // realloc the histogram
        uint32_t *histogram = realloc(dist->histogram, n * sizeof(uint32_t));
        // if the reallocation succeeded
        if (histogram != NULL)
        {
            // reset the distribution's histogram to the new histogram
            dist->histogram = histogram;
            // if the histogram was shrunken
            if (n < dist->size)
            {
                // set the new distribution size
                dist->size = n;
                // sum up the counts that are in the smaller support
                dist->counts = 0;
                for (size_t i = 0; i < dist->size; ++i)
                {
                    dist->counts += dist->histogram[i];
                }
            }
            // otherwise
            else
            {
                // zero out all of the newly observable events
                for (size_t i = dist->size; i < n; ++i)
                {
                    dist->histogram[i] = 0;
                }
                // set the size of the distribution
                dist->size = n;
            }
        }
        // otherwise
        else
        {
            // return NULL leaving the original distribution unscathed
            return NULL;
        }
    }
    // if the original distribution was NULL
    else if (dist == NULL)
    {
        // simply allocate the a new distribution of the requested size
        dist = inform_dist_alloc(n);
    }
    // return the (potentially NULL) distribution
    return dist;
}

inform_dist* inform_dist_copy(inform_dist const *src, inform_dist *dest)
{
    // if the source is NULL, return NULL.
    if (src == NULL)
    {
        return NULL;
    }
    // if the destination is NULL or the destination size is not the same
    // as the source
    else if (dest == NULL || src->size != dest->size)
    {
        // reallocate the destination
        inform_dist *redest = inform_dist_realloc(dest, src->size);
        // return NULL if the allocation fails
        if (redest == NULL)
        {
            return NULL;
        }
        // point dest at the new distribution
        dest = redest;
    }
    // don't copy the source to itself
    else if (src == dest)
    {
        return dest;
    }
    // copy the contents of the histogram from the source to the destination
    memcpy(dest->histogram, src->histogram, src->size * sizeof(uint32_t));
    // set the counts appropriately
    dest->counts = src->counts;
    // return the modified destination
    return dest;
}

inform_dist* inform_dist_dup(inform_dist const *dist)
{
    // if the source distribution is NULL, then just return NULL.
    if (dist == NULL)
    {
        return NULL;
    }
    // allocate the new distribution
    inform_dist *dup = inform_dist_alloc(dist->size);
    // if the allocation succeeded
    if (dup != NULL)
    {
        // copy the source distribution to the new distribution
        inform_dist_copy(dist, dup);
    }
    // return the (potentially NULL) distribution
    return dup;
}

inform_dist* inform_dist_create(uint32_t const *data, size_t n)
{
    // if the requested support size is zero, return NULL
    if (data == NULL || n == 0)
    {
        return NULL;
    }
    // allocate the distribution
    inform_dist *dist = malloc(sizeof(inform_dist));
    // if the allocation succeeded
    if (dist != NULL)
    {
        // allocate the underlying histogram
        dist->histogram = calloc(n, sizeof(uint32_t));
        // if the allocation succeeded
        if (dist->histogram != NULL)
        {
            // set the distribution content, size and counts
            memcpy(dist->histogram, data, n*sizeof(uint32_t));
            dist->size   = n;
            dist->counts = 0;
{'fraction_non_alphanumeric': 0.09296408650552891, 'fraction_numerical': 0.005871415989822879, 'mean_word_length': 1.6817108370506428, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 67, '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': '5844905', 'n_tokens_mistral': 2855, 'n_tokens_neox': 2723, 'n_words': 975}