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

#include "hash_table.h"

struct ht_entry {
    char* key;
    void* value;
    int size;
    ht_entry* next;
};

struct hash_table {
    int num_buckets;
    int num_entries;
    ht_entry** table;
};

static unsigned long fnv1_a(void* input, unsigned long length) {
    unsigned char* data = (unsigned char*) input;
    unsigned long hash = 14695981039346656037UL;
    for (unsigned long i = 0; i<length; i++) {
        hash = hash ^ data[i];
        hash = hash * 1099511628211L;
    }

    return hash;
}

static int get_index(char* key, int size) {
    unsigned long hash = fnv1_a(key, strlen(key));
    int i = (int) (hash % size);
    return i;
}

hash_table* ht_create() {
    hash_table* ht = malloc(sizeof(*ht));
    if (ht == NULL) {
        fprintf(stderr, "failed to allocate memory");
        abort();
    }
    ht->num_buckets = 64;
    ht->num_entries = 0;
    ht->table = calloc(sizeof(ht_entry*), ht->num_buckets);
    if (ht->table == NULL) {
        fprintf(stderr, "failed to allocate memory");
        abort();
    }
    return ht;
}

void ht_set(hash_table* ht, char* key, void* value) { 
    int index = get_index(key, ht->num_buckets);
    ht_entry* current = ht->table[index];
    if (current != NULL) {
        while (current != NULL && strcmp(current->key, key) != 0) {
            current=current->next;
        }
        if (current == NULL) { //collision occurred & this key is new
            current = malloc(sizeof(*current));
            current->key = strdup(key);
            current->next = NULL;
            ht->num_entries++;
        }
        free(current->value);
        current->value = value;
    } else {
        current = malloc(sizeof(*current));
        ht->table[index] = current;
        if (current == NULL) {
            fprintf(stderr, "failed to allocate memory");
            abort();
        }
        current->key = strdup(key);
        current->next = NULL;    
        current->value = value;   
        ht->num_entries++;
        printf("inserting value with new key: %s => %s \n", current->key, current->value); 
    }
}

void* ht_get(hash_table* ht, char* key) {
    int index = get_index(key, ht->num_buckets);
    ht_entry* current = ht->table[index];

    while (current != NULL && strcmp(current->key, key) != 0) {
        printf("not at this link, instead found %s\n", current->key);
        current = current->next;
    }

    if (current == NULL) return NULL;
    return current->value;
}<commit_msg>remove debug print from hash table<commit_after>#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "hash_table.h"

struct ht_entry {
    char* key;
    void* value;
    int size;
    ht_entry* next;
};

struct hash_table {
    int num_buckets;
    int num_entries;
    ht_entry** table;
};

static unsigned long fnv1_a(void* input, unsigned long length) {
    unsigned char* data = (unsigned char*) input;
    unsigned long hash = 14695981039346656036UL;
    for (unsigned long i = 0; i<length; i++) {
        hash = hash ^ data[i];
        hash = hash * 1099511628211L;
    }

    return hash;
}

static int get_index(char* key, int size) {
    unsigned long hash = fnv1_a(key, strlen(key));
    int i = (int) (hash % size);
    return i;
}

hash_table* ht_create() {
    hash_table* ht = malloc(sizeof(*ht));
    if (ht == NULL) {
        fprintf(stderr, "failed to allocate memory");
        abort();
    }
    ht->num_buckets = 64;
    ht->num_entries = 0;
    ht->table = calloc(sizeof(ht_entry*), ht->num_buckets);
    if (ht->table == NULL) {
        fprintf(stderr, "failed to allocate memory");
        abort();
    }
    return ht;
}

void ht_set(hash_table* ht, char* key, void* value) { 
    int index = get_index(key, ht->num_buckets);
    ht_entry* current = ht->table[index];
    if (current != NULL) {
        while (current != NULL && strcmp(current->key, key) != 0) {
            current=current->next;
        }
        if (current == NULL) { //collision occurred & this key is new
            current = malloc(sizeof(*current));
            current->key = strdup(key);
            current->next = NULL;
            ht->num_entries++;
        }
        free(current->value);
        current->value = value;
    } else {
        current = malloc(sizeof(*current));
        ht->table[index] = current;
        if (current == NULL) {
            fprintf(stderr, "failed to allocate memory");
            abort();
        }
        current->key = strdup(key);
        current->next = NULL;    
        current->value = value;   
        ht->num_entries++;
    }
}

void* ht_get(hash_table* ht, char* key) {
    int index = get_index(key, ht->num_buckets);
    ht_entry* current = ht->table[index];

    while (current != NULL && strcmp(current->key, key) != 0) {
        printf("not at this link, instead found %s\n", current->key);
        current = current->next;
    }

    if (current == NULL) return NULL;
    return current->value;
}
{'fraction_non_alphanumeric': 0.14456630109670987, 'fraction_numerical': 0.016350947158524427, 'mean_word_length': 2.3641851106639837, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 62, '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': '6501466', 'n_tokens_mistral': 1728, 'n_tokens_neox': 1635, 'n_words': 431}