<commit_before>#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "hashMap.h"



/*the first hashing function you can use*/
int stringHash1(char * str)
{
	int i;
	int r = 0;
	for (i = 0; str[i] != '\0'; i++)
		r += str[i];
	return r;
}

/*the second hashing function you can use*/
int stringHash2(char * str)
{
	int i;
	int r = 0;
	for (i = 0; str[i] != '\0'; i++)
		r += (i+1) * str[i]; /*the difference between stringHash1 and stringHash2 is on this line*/
	return r;
}

/* initialize the supplied hashMap struct*/
void _initMap (struct hashMap * ht, int tableSize)
{
	int index;
	if(ht == NULL)
		return;
	ht->table = (hashLink**)malloc(sizeof(hashLink*) * tableSize);
	ht->tableSize = tableSize;
	ht->count = 0;
	for(index = 0; index < tableSize; index++)
		ht->table[index] = NULL;
}

/* allocate memory and initialize a hash map*/
hashMap *createMap(int tableSize) {
	assert(tableSize > 0);
	hashMap *ht;
	ht = malloc(sizeof(hashMap));
	assert(ht != 0);
	_initMap(ht, tableSize);
	return ht;
}

/*
 Free all memory used by the buckets.
 Note: Before freeing up a hashLink, free the memory occupied by key and value
 */
void _freeMap (struct hashMap * ht)
{  
	for (int i = 0; i < ht->tableSize; i++)
	{
		if (ht->table[i] == NULL)
			continue;

		hashLink* currentLink = ht->table[i];
		hashLink* nextLink = NULL;

		do
		{
			nextLink = currentLink->next;
			free(currentLink->key);
			free(currentLink);
			currentLink = nextLink;
		} while (nextLink != NULL);
	}
	free(ht->table);
	ht->table = NULL;
}

/* Deallocate buckets and the hash map.*/
void deleteMap(hashMap *ht) {
	assert(ht!= 0);
	/* Free all memory used by the buckets */
	_freeMap(ht);
	/* free the hashMap struct */
	free(ht);
}

/* 
Resizes the hash table to be the size newTableSize 
*/
void _setTableSize(struct hashMap * ht, int newTableSize)
{
	hashMap* tmpMap = createMap(newTableSize);

	for (int i = 0; i < ht->tableSize; i++)
	{
		if (ht->table[i] == NULL)
			continue;

		hashLink* currentLink = ht->table[i];

		do
		{
			insertMap(tmpMap, currentLink->key, currentLink->value);
			currentLink = currentLink->next;
		} while (currentLink != NULL);
	}

	_freeMap(ht);
	ht->count = tmpMap->count;
	ht->table = tmpMap->table;
	ht->tableSize = tmpMap->tableSize;

	/* We don't want to free the table here, as it is the new table being used by our hash table */
	tmpMap->table = NULL;
	free(tmpMap);
	
}

/*
 insert the following values into a hashLink, you must create this hashLink but
 only after you confirm that this key does not already exist in the table. For example, you
 cannot have two hashLinks for the word "taco".
 
 if a hashLink already exists in the table for the key provided you should
 replace that hashLink--this requires freeing up the old memory pointed by hashLink->value
 and then pointing hashLink->value to value v.
 
 also, you must monitor the load factor and resize when the load factor is greater than
 or equal LOAD_FACTOR_THRESHOLD (defined in hashMap.h).
 */
void insertMap (struct hashMap * ht, KeyType k, ValueType v)
{  
	int hash;

#if HASHING_FUNCTION == 1
	hash = stringHash1(k) % ht->tableSize;
#elif HASHING_FUNCTION == 2
	hash = stringHash2(k) % ht->tableSize;
#else
#error HASHING_FUNCTION is not valid.
#endif

	hashLink* currentLink = ht->table[hash];

	/* we know we don't really need to check anything if the bucket we fall in hasn't been allocated */
	if (currentLink == NULL)
	{
		ht->table[hash] = malloc(sizeof(hashLink));
		ht->table[hash]->key = malloc(sizeof(char) * (strlen(k) + 1)); strcpy(ht->table[hash]->key, k);
		ht->table[hash]->value = v;
		ht->table[hash]->next = NULL;

		ht->count++;
		return;
	}
	
	else if (strcmp(currentLink->key, k) == 0)
	{
		currentLink->value = v;
		return;
	}
	else
	{
		while (currentLink->next != NULL)
		{
			if (strcmp(currentLink->next->key, k) == 0)
			{
				currentLink->value = v;
				return;
			}

			currentLink = currentLink->next;<commit_msg>Calculate if we need to resize the hash table when we drop things in buckets<commit_after>#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include "hashMap.h"



/*the first hashing function you can use*/
int stringHash1(char * str)
{
	int i;
	int r = 0;
	for (i = 0; str[i] != '\0'; i++)
		r += str[i];
	return r;
}

/*the second hashing function you can use*/
int stringHash2(char * str)
{
	int i;
	int r = 0;
	for (i = 0; str[i] != '\0'; i++)
		r += (i+1) * str[i]; /*the difference between stringHash1 and stringHash2 is on this line*/
	return r;
}

/* initialize the supplied hashMap struct*/
void _initMap (struct hashMap * ht, int tableSize)
{
	int index;
	if(ht == NULL)
		return;
	ht->table = (hashLink**)malloc(sizeof(hashLink*) * tableSize);
	ht->tableSize = tableSize;
	ht->count = 0;
	for(index = 0; index < tableSize; index++)
		ht->table[index] = NULL;
}

/* allocate memory and initialize a hash map*/
hashMap *createMap(int tableSize) {
	assert(tableSize > 0);
	hashMap *ht;
	ht = malloc(sizeof(hashMap));
	assert(ht != 0);
	_initMap(ht, tableSize);
	return ht;
}

/*
 Free all memory used by the buckets.
 Note: Before freeing up a hashLink, free the memory occupied by key and value
 */
void _freeMap (struct hashMap * ht)
{  
	for (int i = 0; i < ht->tableSize; i++)
	{
		if (ht->table[i] == NULL)
			continue;

		hashLink* currentLink = ht->table[i];
		hashLink* nextLink = NULL;

		do
		{
			nextLink = currentLink->next;
			free(currentLink->key);
			free(currentLink);
			currentLink = nextLink;
		} while (nextLink != NULL);
	}
	free(ht->table);
	ht->table = NULL;
}

/* Deallocate buckets and the hash map.*/
void deleteMap(hashMap *ht) {
	assert(ht!= 0);
	/* Free all memory used by the buckets */
	_freeMap(ht);
	/* free the hashMap struct */
	free(ht);
}

/* 
Resizes the hash table to be the size newTableSize 
*/
void _setTableSize(struct hashMap * ht, int newTableSize)
{
	hashMap* tmpMap = createMap(newTableSize);

	for (int i = 0; i < ht->tableSize; i++)
	{
		if (ht->table[i] == NULL)
			continue;

		hashLink* currentLink = ht->table[i];

		do
		{
			insertMap(tmpMap, currentLink->key, currentLink->value);
			currentLink = currentLink->next;
		} while (currentLink != NULL);
	}

	_freeMap(ht);
	ht->count = tmpMap->count;
	ht->table = tmpMap->table;
	ht->tableSize = tmpMap->tableSize;

	/* We don't want to free the table here, as it is the new table being used by our hash table */
	tmpMap->table = NULL;
	free(tmpMap);
	
}

/*
 insert the following values into a hashLink, you must create this hashLink but
 only after you confirm that this key does not already exist in the table. For example, you
 cannot have two hashLinks for the word "taco".
 
 if a hashLink already exists in the table for the key provided you should
 replace that hashLink--this requires freeing up the old memory pointed by hashLink->value
 and then pointing hashLink->value to value v.
 
 also, you must monitor the load factor and resize when the load factor is greater than
 or equal LOAD_FACTOR_THRESHOLD (defined in hashMap.h).
 */
void insertMap (struct hashMap * ht, KeyType k, ValueType v)
{  
	int hash;

#if HASHING_FUNCTION == 1
	hash = stringHash1(k) % ht->tableSize;
#elif HASHING_FUNCTION == 2
	hash = stringHash2(k) % ht->tableSize;
#else
#error HASHING_FUNCTION is not valid.
#endif

	hashLink* currentLink = ht->table[hash];

	/* we know we don't really need to check anything if the bucket we fall in hasn't been allocated */
	if (currentLink == NULL)
	{
		ht->table[hash] = malloc(sizeof(hashLink));
		ht->table[hash]->key = malloc(sizeof(char) * (strlen(k) + 1)); strcpy(ht->table[hash]->key, k);
		ht->table[hash]->value = v;
		ht->table[hash]->next = NULL;

		/* increments the count of objects, calculates the load factor, and compares */
		if (((float)(++(ht->count)) / (float)(ht->tableSize)) > LOAD_FACTOR_THRESHOLD)
			_setTableSize(ht, (ht->tableSize * 2));

		return;
	}
	
	else if (strcmp(currentLink->key, k) == 0)
	{
		currentLink->value = v;
		return;
	}
	else
	{
		while (currentLink->next != NULL)
		{
			if (strcmp(currentLink->next->key, k) == 0)
			{
				currentLink->value = v;
				return;
			}

			currentLink = currentLink->next;
{'fraction_non_alphanumeric': 0.13959173695147292, 'fraction_numerical': 0.0062339567290062336, 'mean_word_length': 3.6541524459613197, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 108, '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': '5732788', 'n_tokens_mistral': 3059, 'n_tokens_neox': 2844, 'n_words': 946}