<commit_before>#include <hash.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

unsigned int simpleHashStr(char * string) {
    unsigned int hashAddress = 0;

    /* sum each letter of the string */
    while (*string != '\0') {
        hashAddress += *string;
        string++;
    }
    return hashAddress;
}

unsigned int sdbmStringHash(char * key) {
    unsigned int hashAddress = 0;
    int i;

    for (i = 0; key[i] != '\0'; ++i) {
        hashAddress = key[i] + (hashAddress << 6) + (hashAddress << 16) - hashAddress;
    }
    return hashAddress;
}

unsigned int djb2StringHash(char * key) {
    unsigned int hashAddress = 5381;
    int i;

    for (i = 0; key[i] != '\0'; ++i) {
        hashAddress = ((hashAddress << 5) + hashAddress) + key[i];
    }
    return hashAddress;
}

HashTable * initTable(int size, unsigned int (*hashFn)(char * key)) {
    HashTable * table = malloc(sizeof(*table));

    if (!table) {
        return NULL;
    }

    table->dataStore = malloc(sizeof(*table->dataStore) * size);

    if (!table->dataStore) {
        free(table);
        return NULL;
    }

    table->numSlots = size;
    table->hashFn = hashFn;

    int i;
    for (i = 0; i < table->numSlots; ++i) {
        table->dataStore[i] = NULL;
    }

    return table;
}

int insert_hash(HashTable * table, char * key, void * data) {
    unsigned int index;

    /* We don't really care if the data itself is NULL because the user might
     * be storing it specifically */
    if (!table || !key) {
        return EXIT_FAILURE;
    }

    index = table->hashFn(key) % table->numSlots;

    Element * elementToInsertAt = makeElement(key, data);
    /* That index will not be NULL if we had a collision.
     * We resolve collisions by chaining.
     * The following ensures that elementToInsertAt is linked in */
    if (table->dataStore[index] == NULL) {
        table->dataStore[index] = elementToInsertAt;
    } else {
        Element * t = table->dataStore[index];
        while (t->next != NULL) {
            t = t->next;
        }
        t->next = elementToInsertAt;
    }
}

void * get_hash(HashTable * table, char * key) {
    unsigned int index;

    /* We don't really care if the data itself is NULL because the user might
     * be storing it specifically */
    if (!table || !key) {
        return NULL;
    }<commit_msg>Improved insert performance<commit_after>#include <hash.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

unsigned int simpleHashStr(char * string) {
    unsigned int hashAddress = 0;

    /* sum each letter of the string */
    while (*string != '\0') {
        hashAddress += *string;
        string++;
    }
    return hashAddress;
}

unsigned int sdbmStringHash(char * key) {
    unsigned int hashAddress = 0;
    int i;

    for (i = 0; key[i] != '\0'; ++i) {
        hashAddress = key[i] + (hashAddress << 6) + (hashAddress << 16) - hashAddress;
    }
    return hashAddress;
}

unsigned int djb2StringHash(char * key) {
    unsigned int hashAddress = 5381;
    int i;

    for (i = 0; key[i] != '\0'; ++i) {
        hashAddress = ((hashAddress << 5) + hashAddress) + key[i];
    }
    return hashAddress;
}

HashTable * initTable(int size, unsigned int (*hashFn)(char * key)) {
    HashTable * table = malloc(sizeof(*table));

    if (!table) {
        return NULL;
    }

    table->dataStore = malloc(sizeof(*table->dataStore) * size);

    if (!table->dataStore) {
        free(table);
        return NULL;
    }

    table->numSlots = size;
    table->hashFn = hashFn;

    int i;
    for (i = 0; i < table->numSlots; ++i) {
        table->dataStore[i] = NULL;
    }

    return table;
}

int insert_hash(HashTable * table, char * key, void * data) {
    unsigned int index;

    /* We don't really care if the data itself is NULL because the user might
     * be storing it specifically */
    if (!table || !key) {
        return EXIT_FAILURE;
    }

    index = table->hashFn(key) % table->numSlots;

    Element * elementToInsertAt = makeElement(key, data);
    elementToInsertAt->next = table->dataStore[index];
    table->dataStore[index] = elementToInsertAt;
}

void * get_hash(HashTable * table, char * key) {
    unsigned int index;

    if (!table || !key) {
        return NULL;
    }
{'fraction_non_alphanumeric': 0.13737421015679852, 'fraction_numerical': 0.007956938918792417, 'mean_word_length': 2.488979591836735, 'pattern_counts': {'":': 0, '<': 25, '<?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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3127032', 'n_tokens_mistral': 1419, 'n_tokens_neox': 1353, 'n_words': 402}