ref: d0a49f42e8b88797f63b02c45b00c461f62a70a6
parent: 8a962569e358f39e6a8fa272bed9c1492458e80e
author: LTCHIPS <ltchips994@gmail.com>
date: Thu Apr 26 00:05:22 EDT 2018
Rewrote part of HashTable that was preventing ROTT from running on Ubuntu VM. also fixed casing on include statements for queue
--- a/rott/HashTable.c
+++ b/rott/HashTable.c
@@ -2,7 +2,7 @@
#include <stdio.h>
#include <string.h>
#include "HashTable.h"
-#include "LinkedList.h"
+//#include "LinkedList.h"
#if !defined(ARRAY_SIZE)
#define ARRAY_SIZE(x) (sizeof((x)) / sizeof((x)[0]))
@@ -11,9 +11,12 @@
void InitHashTable(HashTable * hashTable, int initSize)
{
hashTable->totalSize = initSize;
- hashTable->table = malloc(sizeof(LinkedList) * initSize);
- memset(hashTable->table,0, sizeof(LinkedList) * initSize);
+ hashTable->table = calloc(sizeof(listNode), initSize);
+ //hashTable->table = malloc(sizeof(LinkedList) * initSize);
+
+ //memset(hashTable->table,0, sizeof(LinkedList) * initSize);
+ memset(hashTable->table, NULL, sizeof(listNode)*initSize);
}
int HashFunc(HashTable * hashTable, int key)
@@ -25,9 +28,18 @@
{
int index = HashFunc(hashTable,key);
- LinkedList * list = hashTable->table[index];
+ while(hashTable->table[index]->key != key)
+ {
+ //go to next cell
+ ++index;
+
+ //wrap around the table
+ index %= hashTable->totalSize;
+ }
- DeleteWithKey(list, key);
+ //LinkedList * list = hashTable->table[index];
+
+ //DeleteWithKey(list, key);
free(&hashTable->table[index]);
}
@@ -36,9 +48,10 @@
int x = 0;
for (x; x < ARRAY_SIZE(hashTable->table); x++)
{
- if (hashTable->table[x] != 0)
+ if (hashTable->table[x] != NULL)
{
- DestroyList(hashTable->table[x]);
+ //DestroyList(hashTable->table[x]);
+ free(hashTable->table[x]);
}
}
@@ -48,21 +61,24 @@
void Insert(HashTable * hashTable, int key, int item)
{
int index = HashFunc(hashTable,key);
- int found = 0;
- if (hashTable->table[index] != 0)
+
+
+ while(hashTable->table[index] != NULL)
{
- InsertInList(hashTable->table[index], key, item);
+ //go to next cell
+ ++index;
+
+ //wrap around the table
+ index %= hashTable->totalSize;
}
- else
- {
- LinkedList * newList = malloc(sizeof(LinkedList));
-
- InitLinkedList(newList);
-
- InsertInList(newList, key, item);
-
- hashTable->table[index] = newList;
- }
+
+ listNode * newNode = malloc(sizeof(listNode));
+
+ newNode->data = item;
+ newNode->key = key;
+
+ hashTable->table[index] = newNode;
+
}
int Lookup(HashTable * hashTable, int key)
@@ -69,13 +85,29 @@
{
int index = HashFunc(hashTable,key);
- if (hashTable->table[index] == 0)
+ int origIndex = index;
+
+
+ //If it starts off at a NULL spot, it never existed...
+ if (hashTable->table[index] == NULL)
{
- //printf("ERROR: HashTable Lookup lead to a NULL Entry. \n");
return 0;
+ }
+
+ while(hashTable->table[index]->key != key)
+ {
+ //go to next cell
+ ++index;
+
+ //wrap around the table
+ index %= hashTable->totalSize;
- //exit(1);
+ if (index == origIndex)
+ {
+ return 0;
+ }
+
}
- return SearchWithKey(hashTable->table[index], key);
+ return hashTable->table[index]->data;
}
\ No newline at end of file
--- a/rott/HashTable.h
+++ b/rott/HashTable.h
@@ -1,7 +1,7 @@
/*
* File: HashTable.h
- * Author: LTCHIPS
+ * Author: Steven LeVesque
*
* Created on March 13, 2018, 5:10 PM
*/
@@ -9,18 +9,18 @@
#ifndef HASHTABLE_H
#define HASHTABLE_H
+typedef struct listNode
+{
+ int key;
+ int data;
+} listNode;
+
+
typedef struct HashTable
{
int totalSize;
- int * keys;
- int * table;
+ listNode ** table;
} HashTable;
-
-typedef struct Key
-{
- int IndexInTable;
- int IndexInLinkedList;
-} Key;
void InitHashTable(HashTable * hashTable, int initSize);
--- a/rott/Makefile
+++ b/rott/Makefile
@@ -84,7 +84,6 @@
OBJS += winrott.o
OBJS += queue.o
OBJS += HashTable.o
-OBJS += LinkedList.o
AUDIOLIB := audiolib/audiolib.a
--- a/rott/modexlib.c
+++ b/rott/modexlib.c
@@ -42,7 +42,7 @@
#include "rt_net.h" // for GamePaused
#include "myprint.h"
#include "rt_view.h"
-#include "Queue.h"
+#include "queue.h"
#include "lumpy.h"
//#include <SDL2/SDL_image.h>
--- a/rott/queue.c
+++ b/rott/queue.c
@@ -1,7 +1,7 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include "Queue.h"
+#include "queue.h"
//Fetched from https://codereview.stackexchange.com/questions/141238/implementing-a-generic-queue-in-c