Skip to content

Commit

Permalink
fixed mistakes
Browse files Browse the repository at this point in the history
  • Loading branch information
artemiipatov committed Dec 17, 2021
1 parent 7e1aa18 commit 9430c61
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 46 deletions.
79 changes: 45 additions & 34 deletions hashTable/hashTable/hashTable.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,45 +30,67 @@ HashTable* createHashTable()
return hashTable;
}

void deleteArray(List** array, int arraySize)
void deleteArray(List*** array, int arraySize)
{
if (array == NULL)
if ((*array) == NULL)
{
return;
}
for (int index = 0; index < arraySize; index++)
{
deleteList(&array[index]);
deleteList(&(*array)[index]);
}
free(*array);
(*array) = NULL;
}

void deleteHashTable(HashTable** hashTable)
{
int arraySize = (*hashTable)->arraySize;
for (int index = 0; index < arraySize; index++)
{
deleteList(&((*hashTable)->array[index]));
}
free((*hashTable)->array);
const int arraySize = (*hashTable)->arraySize;
deleteArray(&((*hashTable)->array), (*hashTable)->arraySize);
free(*hashTable);
*hashTable = NULL;
}

int hashFunction(const char word[], int arraySize)
{
const int length = (int)strlen(word);
int hash = 0;
for (int i = 0; i < length; i++)
{
hash = (hash * 3 + word[i] + arraySize) % arraySize;
}
return hash % arraySize;
}

bool resize(HashTable* hashTable);

bool addItemToHashTable(HashTable* hashTable, const char word[])
{
int hash = hashFunction(word, hashTable->arraySize);
++hashTable->numberOfElements;
const int hash = hashFunction(word, hashTable->arraySize);
if (!addItemToList(&(hashTable->array[hash]), word))
{
return false;
}
++hashTable->numberOfElements;
if (getLoadFactor(hashTable) > 1.0)
{
if (!resize(hashTable))
{
return false;
}
}
return true;
}

bool resize(HashTable* hashTable)
{
int oldSize = hashTable->arraySize;
const int oldSize = hashTable->arraySize;
List** newArray = calloc(oldSize * 2, sizeof(List*));
if (newArray == NULL)
{
return false;
}
hashTable->arraySize = oldSize * 2;
List** oldArray = hashTable->array;
hashTable->array = newArray;
Expand All @@ -80,7 +102,7 @@ bool resize(HashTable* hashTable)
Position* position = createPosition();
if (position == NULL)
{
deleteArray(newArray, oldSize * 2);
deleteArray(&newArray, oldSize * 2);
return false;
}
first(oldArray[index], &position);
Expand All @@ -92,15 +114,19 @@ bool resize(HashTable* hashTable)
deletePosition(&position);
}
}
deleteArray(oldArray, oldSize);
deleteArray(&oldArray, oldSize);
hashTable->array = newArray;
hashTable->arraySize = oldSize * 2;
return true;
}

bool parse(HashTable* hashTable, char* fileName)
bool readFile(HashTable* hashTable, const char* fileName)
{
FILE* file = fopen(fileName, "r");
if (file == NULL)
{
return false;
}
while (!feof(file))
{
char word[50] = {'\0'};
Expand All @@ -110,36 +136,21 @@ bool parse(HashTable* hashTable, char* fileName)
fclose(file);
return false;
}
if (getLoadFactor(hashTable) > 1.0)
{
resize(hashTable);
}
}
fclose(file);
return true;
}

int hashFunction(const char word[], int arraySize)
{
int length = (int)strlen(word);
int hash = 0;
for (int i = 0; i < length; i++)
{
hash = (hash * 3 + word[i]) % arraySize;
}
return hash % arraySize;
}

int getCounter(HashTable* hashTable, const char word[])
{
int hash = hashFunction(word, hashTable->arraySize);
const int hash = hashFunction(word, hashTable->arraySize);
List* list = hashTable->array[hash];
return getCounterFromList(list, word);
}

void printHashTable(HashTable* hashTable)
{
int arraySize = hashTable->arraySize;
const int arraySize = hashTable->arraySize;
for (int arrayIndex = 0; arrayIndex < arraySize; arrayIndex++)
{
printList(hashTable->array[arrayIndex]);
Expand All @@ -149,7 +160,7 @@ void printHashTable(HashTable* hashTable)
int getMaxLength(HashTable* hashTable)
{
int maxLength = 0;
int arraySize = hashTable->arraySize;
const int arraySize = hashTable->arraySize;
for (int index = 0; index < arraySize; index++)
{
if (hashTable->array[index] != NULL)
Expand All @@ -167,7 +178,7 @@ int getMaxLength(HashTable* hashTable)
int getAverageLength(HashTable* hashTable)
{
int sumOfLengths = 0;
int arraySize = hashTable->arraySize;
const int arraySize = hashTable->arraySize;
for (int index = 0; index < arraySize; index++)
{
if (hashTable->array[index] != NULL)
Expand Down
5 changes: 1 addition & 4 deletions hashTable/hashTable/hashTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,7 @@ HashTable* createHashTable();
void deleteHashTable(HashTable** hashTable);

// parses file
bool parse(HashTable* hashTable, char* fileName);

// counts hash for given word
int hashFunction(const char word[], int arraySize);
bool readFile(HashTable* hashTable, const char* fileName);

// returns word frequency
int getCounter(HashTable* hashTable, const char word[]);
Expand Down
2 changes: 1 addition & 1 deletion hashTable/hashTable/hashTableTests.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ bool hashTablePassedTests()
{
return false;
}
if (!parse(hashTable, "testData.txt"))
if (!readFile(hashTable, "testData.txt"))
{
deleteHashTable(&hashTable);
return false;
Expand Down
12 changes: 7 additions & 5 deletions hashTable/hashTable/list.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ typedef struct List
{
int length;
ListElement* head;
ListElement* tail;
} List;

typedef struct Position
Expand Down Expand Up @@ -93,12 +92,11 @@ bool addItemToList(List** list, const char* word)
}
(*list)->length++;
(*list)->head = newElement;
(*list)->tail = newElement;
}
else
{
ListElement* currentElement = (*list)->head;
while (currentElement != NULL)
while (currentElement->next != NULL)
{
if (strcmp(currentElement->word, word) == 0)
{
Expand All @@ -107,14 +105,18 @@ bool addItemToList(List** list, const char* word)
}
currentElement = currentElement->next;
}
if (strcmp(currentElement->word, word) == 0)
{
++currentElement->counter;
return true;
}
ListElement* newElement = createElement(word);
if (newElement == NULL)
{
return false;
}
++(*list)->length;
(*list)->tail->next = newElement;
(*list)->tail = (*list)->tail->next;
currentElement->next = newElement;
}
return true;
}
Expand Down
6 changes: 4 additions & 2 deletions hashTable/hashTable/main.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#include <stdio.h>
#include <stdlib.h>
#include <locale.h>
#include "hashTable.h"
#include "hashTableTests.h"

int main()
{
setlocale(LC_ALL, "");
if (!hashTablePassedTests())
{
printf("Tests failed");
Expand All @@ -16,9 +18,9 @@ int main()
printf("Allocation error");
return -1;
}
if (!parse(myHashTable, "data.txt"))
if (!readFile(myHashTable, "data.txt"))
{
printf("An error occured while parsing");
printf("An error occured while reading file");
deleteHashTable(&myHashTable);
return -1;
}
Expand Down

0 comments on commit 9430c61

Please sign in to comment.