<commit_before>/* Copyright 2011-2012 <NAME> <<EMAIL>>.
 * Licensed under the 2-clause BSD license, see LICENSE. */


#include <stdlib.h>
#include <string.h>

#include "vector.h"


static int vector_resize (struct vector *v, size_t size)
{
    if (size > 0) {
        void * tmp = malloc(size*sizeof(void *));
        if (tmp==NULL) return 0;

        memcpy(tmp, v->list, v->elements*sizeof(void*));
        free(v->list);
        v->list = tmp;
    }

    return size;
}


int vector_add (struct vector *v, void *element)
{
    /* If list too small, expand */
    if(v->elements == v->size) {
        if(!vector_resize(v, 2*v->size)) return 0;
    }

    v->list[v->elements] = element;
    v->elements += 1;

    return v->elements;
}


int vector_add_vector (struct vector *v1, struct vector *v2)
{
    unsigned int i;
    for (i=0; i<v2->elements; ++i) {
        if (vector_add(v1, vector_get(v2, i)))
            return 0;
    }

    return v1->elements;
}


struct vector * vector_init ()
{
    return vector_init_size(VECTOR_DEFAULT_SIZE);
}


struct vector * vector_init_size (size_t size)
{
    struct vector *v = (struct vector *) malloc(sizeof(struct vector));
    if (v==NULL) return NULL;

    v->list = malloc(size*sizeof(void *));

    v->size = size;
    v->elements = 0;

    return v;
}


struct vector * vector_create ()
{
    return vector_create_size(VECTOR_DEFAULT_SIZE);
}


struct vector * vector_create_size (size_t size)
{
    struct vector *v = (struct vector *) malloc(sizeof(struct vector));
    if (v==NULL) return NULL;

    v->list = malloc(size*sizeof(void *));

    v->size = size;
    v->elements = 0;

    return v;
}<commit_msg>Remove initialization code duplication<commit_after>/* Copyright 2011-2012 <NAME> <<EMAIL>>.
 * Licensed under the 2-clause BSD license, see LICENSE. */


#include <stdlib.h>
#include <string.h>

#include "vector.h"


static int vector_resize (struct vector *v, size_t size)
{
    if (size > 0) {
        void * tmp = malloc(size*sizeof(void *));
        if (tmp==NULL) return 0;

        memcpy(tmp, v->list, v->elements*sizeof(void*));
        free(v->list);
        v->list = tmp;
    }

    return size;
}


int vector_add (struct vector *v, void *element)
{
    /* If list too small, expand */
    if(v->elements == v->size) {
        if(!vector_resize(v, 2*v->size)) return 0;
    }

    v->list[v->elements] = element;
    v->elements += 1;

    return v->elements;
}


int vector_add_vector (struct vector *v1, struct vector *v2)
{
    unsigned int i;
    for (i=0; i<v2->elements; ++i) {
        if (vector_add(v1, vector_get(v2, i)))
            return 0;
    }

    return v1->elements;
}


struct vector * vector_init ()
{
    return vector_init_size(VECTOR_DEFAULT_SIZE);
}


struct vector * vector_init_size (size_t size)
{
    struct vector *v = (struct vector *) malloc(sizeof(struct vector));
    if (v==NULL) return NULL;

    v->list = malloc(size*sizeof(void *));

    v->size = size;
    v->elements = 0;

    return v;
}


struct vector * vector_create ()
{
    return vector_init_size(VECTOR_DEFAULT_SIZE);
}


struct vector * vector_create_size (size_t size)
{
    return vector_init_size(size);
}
{'fraction_non_alphanumeric': 0.1490429871352369, 'fraction_numerical': 0.014747411358644493, 'mean_word_length': 2.87363304981774, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 50, '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': '708030', 'n_tokens_mistral': 1167, 'n_tokens_neox': 1092, 'n_words': 286}