Game For ECE 2035

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

Committer:
nasiromar
Date:
Fri Nov 19 22:03:25 2021 +0000
Revision:
6:c9695079521d
Parent:
0:35660d7952f7
Basics

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rconnorlawson 0:35660d7952f7 1 /*
nasiromar 6:c9695079521d 2 Student Name: Nasir Christian
nasiromar 6:c9695079521d 3 Date: November 9
rconnorlawson 0:35660d7952f7 4
rconnorlawson 0:35660d7952f7 5 =======================
rconnorlawson 0:35660d7952f7 6 ECE 2035 Project 2-1:
rconnorlawson 0:35660d7952f7 7 =======================
rconnorlawson 0:35660d7952f7 8 This file provides definition for the structs and functions declared in the
rconnorlawson 0:35660d7952f7 9 header file. It also contains helper functions that are not accessible from
rconnorlawson 0:35660d7952f7 10 outside of the file.
rconnorlawson 0:35660d7952f7 11
rconnorlawson 0:35660d7952f7 12 FOR FULL CREDIT, BE SURE TO TRY MULTIPLE TEST CASES and DOCUMENT YOUR CODE.
rconnorlawson 0:35660d7952f7 13
rconnorlawson 0:35660d7952f7 14 ===================================
rconnorlawson 0:35660d7952f7 15 Naming conventions in this file:
rconnorlawson 0:35660d7952f7 16 ===================================
rconnorlawson 0:35660d7952f7 17 1. All struct names use camel case where the first letter is capitalized.
rconnorlawson 0:35660d7952f7 18 e.g. "HashTable", or "HashTableEntry"
rconnorlawson 0:35660d7952f7 19
rconnorlawson 0:35660d7952f7 20 2. Variable names with a preceding underscore "_" will not be called directly.
rconnorlawson 0:35660d7952f7 21 e.g. "_HashTable", "_HashTableEntry"
rconnorlawson 0:35660d7952f7 22
rconnorlawson 0:35660d7952f7 23 Recall that in C, we have to type "struct" together with the name of the struct
rconnorlawson 0:35660d7952f7 24 in order to initialize a new variable. To avoid this, in hash_table.h
rconnorlawson 0:35660d7952f7 25 we use typedef to provide new "nicknames" for "struct _HashTable" and
rconnorlawson 0:35660d7952f7 26 "struct _HashTableEntry". As a result, we can create new struct variables
rconnorlawson 0:35660d7952f7 27 by just using:
rconnorlawson 0:35660d7952f7 28 - "HashTable myNewTable;"
rconnorlawson 0:35660d7952f7 29 or
rconnorlawson 0:35660d7952f7 30 - "HashTableEntry myNewHashTableEntry;"
rconnorlawson 0:35660d7952f7 31
rconnorlawson 0:35660d7952f7 32 The preceding underscore "_" simply provides a distinction between the names
rconnorlawson 0:35660d7952f7 33 of the actual struct defition and the "nicknames" that we use to initialize
rconnorlawson 0:35660d7952f7 34 new structs.
rconnorlawson 0:35660d7952f7 35 [See Hidden Definitions section for more information.]
rconnorlawson 0:35660d7952f7 36
rconnorlawson 0:35660d7952f7 37 3. Functions, their local variables and arguments are named with camel case, where
rconnorlawson 0:35660d7952f7 38 the first letter is lower-case.
rconnorlawson 0:35660d7952f7 39 e.g. "createHashTable" is a function. One of its arguments is "numBuckets".
rconnorlawson 0:35660d7952f7 40 It also has a local variable called "newTable".
rconnorlawson 0:35660d7952f7 41
rconnorlawson 0:35660d7952f7 42 4. The name of a struct member is divided by using underscores "_". This serves
rconnorlawson 0:35660d7952f7 43 as a distinction between function local variables and struct members.
rconnorlawson 0:35660d7952f7 44 e.g. "num_buckets" is a member of "HashTable".
rconnorlawson 0:35660d7952f7 45
rconnorlawson 0:35660d7952f7 46 */
rconnorlawson 0:35660d7952f7 47
rconnorlawson 0:35660d7952f7 48 /****************************************************************************
rconnorlawson 0:35660d7952f7 49 * Include the Public Interface
rconnorlawson 0:35660d7952f7 50 *
rconnorlawson 0:35660d7952f7 51 * By including the public interface at the top of the file, the compiler can
rconnorlawson 0:35660d7952f7 52 * enforce that the function declarations in the the header are not in
rconnorlawson 0:35660d7952f7 53 * conflict with the definitions in the file. This is not a guarantee of
rconnorlawson 0:35660d7952f7 54 * correctness, but it is better than nothing!
rconnorlawson 0:35660d7952f7 55 ***************************************************************************/
rconnorlawson 0:35660d7952f7 56 #include "hash_table.h"
rconnorlawson 0:35660d7952f7 57
rconnorlawson 0:35660d7952f7 58
rconnorlawson 0:35660d7952f7 59 /****************************************************************************
rconnorlawson 0:35660d7952f7 60 * Include other private dependencies
rconnorlawson 0:35660d7952f7 61 *
rconnorlawson 0:35660d7952f7 62 * These other modules are used in the implementation of the hash table module,
rconnorlawson 0:35660d7952f7 63 * but are not required by users of the hash table.
rconnorlawson 0:35660d7952f7 64 ***************************************************************************/
rconnorlawson 0:35660d7952f7 65 #include <stdlib.h> // For malloc and free
rconnorlawson 0:35660d7952f7 66 #include <stdio.h> // For printf
rconnorlawson 0:35660d7952f7 67
rconnorlawson 0:35660d7952f7 68
rconnorlawson 0:35660d7952f7 69 /****************************************************************************
rconnorlawson 0:35660d7952f7 70 * Hidden Definitions
rconnorlawson 0:35660d7952f7 71 *
rconnorlawson 0:35660d7952f7 72 * These definitions are not available outside of this file. However, because
rconnorlawson 0:35660d7952f7 73 * the are forward declared in hash_table.h, the type names are
rconnorlawson 0:35660d7952f7 74 * available everywhere and user code can hold pointers to these structs.
rconnorlawson 0:35660d7952f7 75 ***************************************************************************/
rconnorlawson 0:35660d7952f7 76 /**
rconnorlawson 0:35660d7952f7 77 * This structure represents an a hash table.
rconnorlawson 0:35660d7952f7 78 * Use "HashTable" instead when you are creating a new variable. [See top comments]
rconnorlawson 0:35660d7952f7 79 */
rconnorlawson 0:35660d7952f7 80 struct _HashTable {
rconnorlawson 0:35660d7952f7 81 /** The array of pointers to the head of a singly linked list, whose nodes
rconnorlawson 0:35660d7952f7 82 are HashTableEntry objects */
rconnorlawson 0:35660d7952f7 83 HashTableEntry** buckets;
rconnorlawson 0:35660d7952f7 84
rconnorlawson 0:35660d7952f7 85 /** The hash function pointer */
rconnorlawson 0:35660d7952f7 86 HashFunction hash;
rconnorlawson 0:35660d7952f7 87
rconnorlawson 0:35660d7952f7 88 /** The number of buckets in the hash table */
rconnorlawson 0:35660d7952f7 89 unsigned int num_buckets;
rconnorlawson 0:35660d7952f7 90 };
rconnorlawson 0:35660d7952f7 91
rconnorlawson 0:35660d7952f7 92 /**
rconnorlawson 0:35660d7952f7 93 * This structure represents a hash table entry.
rconnorlawson 0:35660d7952f7 94 * Use "HashTableEntry" instead when you are creating a new variable. [See top comments]
rconnorlawson 0:35660d7952f7 95 */
rconnorlawson 0:35660d7952f7 96 struct _HashTableEntry {
rconnorlawson 0:35660d7952f7 97 /** The key for the hash table entry */
rconnorlawson 0:35660d7952f7 98 unsigned int key;
rconnorlawson 0:35660d7952f7 99
rconnorlawson 0:35660d7952f7 100 /** The value associated with this hash table entry */
rconnorlawson 0:35660d7952f7 101 void* value;
rconnorlawson 0:35660d7952f7 102
rconnorlawson 0:35660d7952f7 103 /**
rconnorlawson 0:35660d7952f7 104 * A pointer pointing to the next hash table entry
rconnorlawson 0:35660d7952f7 105 * NULL means there is no next entry (i.e. this is the tail)
rconnorlawson 0:35660d7952f7 106 */
rconnorlawson 0:35660d7952f7 107 HashTableEntry* next;
rconnorlawson 0:35660d7952f7 108 };
rconnorlawson 0:35660d7952f7 109
rconnorlawson 0:35660d7952f7 110
rconnorlawson 0:35660d7952f7 111 /****************************************************************************
rconnorlawson 0:35660d7952f7 112 * Private Functions
rconnorlawson 0:35660d7952f7 113 *
rconnorlawson 0:35660d7952f7 114 * These functions are not available outside of this file, since they are not
rconnorlawson 0:35660d7952f7 115 * declared in hash_table.h.
rconnorlawson 0:35660d7952f7 116 ***************************************************************************/
rconnorlawson 0:35660d7952f7 117 /**
rconnorlawson 0:35660d7952f7 118 * createHashTableEntry
rconnorlawson 0:35660d7952f7 119 *
rconnorlawson 0:35660d7952f7 120 * Helper function that creates a hash table entry by allocating memory for it on
rconnorlawson 0:35660d7952f7 121 * the heap. It initializes the entry with key and value, initialize pointer to
rconnorlawson 0:35660d7952f7 122 * the next entry as NULL, and return the pointer to this hash table entry.
rconnorlawson 0:35660d7952f7 123 *
rconnorlawson 0:35660d7952f7 124 * @param key The key corresponds to the hash table entry
rconnorlawson 0:35660d7952f7 125 * @param value The value stored in the hash table entry
rconnorlawson 0:35660d7952f7 126 * @return The pointer to the hash table entry
rconnorlawson 0:35660d7952f7 127 */
rconnorlawson 0:35660d7952f7 128 static HashTableEntry* createHashTableEntry(unsigned int key, void* value) {
nasiromar 6:c9695079521d 129 HashTableEntry* newEntry = (HashTableEntry*)malloc(sizeof(HashTableEntry));
nasiromar 6:c9695079521d 130 newEntry->value = value;
nasiromar 6:c9695079521d 131 newEntry->key = key;
nasiromar 6:c9695079521d 132 newEntry->next = NULL;
rconnorlawson 0:35660d7952f7 133
nasiromar 6:c9695079521d 134 return newEntry;
rconnorlawson 0:35660d7952f7 135 }
rconnorlawson 0:35660d7952f7 136
rconnorlawson 0:35660d7952f7 137 /**
rconnorlawson 0:35660d7952f7 138 * findItem
rconnorlawson 0:35660d7952f7 139 *
rconnorlawson 0:35660d7952f7 140 * Helper function that checks whether there exists the hash table entry that
rconnorlawson 0:35660d7952f7 141 * contains a specific key.
rconnorlawson 0:35660d7952f7 142 *
rconnorlawson 0:35660d7952f7 143 * @param hashTable The pointer to the hash table.
rconnorlawson 0:35660d7952f7 144 * @param key The key corresponds to the hash table entry
rconnorlawson 0:35660d7952f7 145 * @return The pointer to the hash table entry, or NULL if key does not exist
rconnorlawson 0:35660d7952f7 146 */
rconnorlawson 0:35660d7952f7 147 static HashTableEntry* findItem(HashTable* hashTable, unsigned int key) {
nasiromar 6:c9695079521d 148 /*Checking if hashtable is empty or not*/
nasiromar 6:c9695079521d 149 if (hashTable->num_buckets == 0) {
nasiromar 6:c9695079521d 150 return NULL;
nasiromar 6:c9695079521d 151 }
nasiromar 6:c9695079521d 152 unsigned int index = hashTable->hash(key);
nasiromar 6:c9695079521d 153 HashTableEntry* temp = hashTable->buckets[index];
nasiromar 6:c9695079521d 154
nasiromar 6:c9695079521d 155 while (temp){
nasiromar 6:c9695079521d 156 if (temp->key == key){
nasiromar 6:c9695079521d 157 return temp;
nasiromar 6:c9695079521d 158 }
nasiromar 6:c9695079521d 159 temp = temp->next;
nasiromar 6:c9695079521d 160 }
nasiromar 6:c9695079521d 161 return NULL;
nasiromar 6:c9695079521d 162 }
nasiromar 6:c9695079521d 163
nasiromar 6:c9695079521d 164
nasiromar 6:c9695079521d 165
nasiromar 6:c9695079521d 166
rconnorlawson 0:35660d7952f7 167
rconnorlawson 0:35660d7952f7 168
rconnorlawson 0:35660d7952f7 169 /****************************************************************************
rconnorlawson 0:35660d7952f7 170 * Public Interface Functions
rconnorlawson 0:35660d7952f7 171 *
rconnorlawson 0:35660d7952f7 172 * These functions implement the public interface as specified in the header
rconnorlawson 0:35660d7952f7 173 * file, and make use of the private functions and hidden definitions in the
rconnorlawson 0:35660d7952f7 174 * above sections.
rconnorlawson 0:35660d7952f7 175 ****************************************************************************/
rconnorlawson 0:35660d7952f7 176 // The createHashTable is provided for you as a starting point.
rconnorlawson 0:35660d7952f7 177 HashTable* createHashTable(HashFunction hashFunction, unsigned int numBuckets) {
rconnorlawson 0:35660d7952f7 178 // The hash table has to contain at least one bucket. Exit gracefully if
rconnorlawson 0:35660d7952f7 179 // this condition is not met.
rconnorlawson 0:35660d7952f7 180 if (numBuckets==0) {
rconnorlawson 0:35660d7952f7 181 printf("Hash table has to contain at least 1 bucket...\n");
rconnorlawson 0:35660d7952f7 182 exit(1);
rconnorlawson 0:35660d7952f7 183 }
rconnorlawson 0:35660d7952f7 184
rconnorlawson 0:35660d7952f7 185 // Allocate memory for the new HashTable struct on heap.
rconnorlawson 0:35660d7952f7 186 HashTable* newTable = (HashTable*)malloc(sizeof(HashTable));
rconnorlawson 0:35660d7952f7 187
rconnorlawson 0:35660d7952f7 188 // Initialize the components of the new HashTable struct.
rconnorlawson 0:35660d7952f7 189 newTable->hash = hashFunction;
rconnorlawson 0:35660d7952f7 190 newTable->num_buckets = numBuckets;
rconnorlawson 0:35660d7952f7 191 newTable->buckets = (HashTableEntry**)malloc(numBuckets*sizeof(HashTableEntry*));
rconnorlawson 0:35660d7952f7 192
nasiromar 6:c9695079521d 193 // As the new buckets contain indeterminant values, init each bucket as NULL.
rconnorlawson 0:35660d7952f7 194 unsigned int i;
rconnorlawson 0:35660d7952f7 195 for (i=0; i<numBuckets; ++i) {
rconnorlawson 0:35660d7952f7 196 newTable->buckets[i] = NULL;
rconnorlawson 0:35660d7952f7 197 }
rconnorlawson 0:35660d7952f7 198
rconnorlawson 0:35660d7952f7 199 // Return the new HashTable struct.
rconnorlawson 0:35660d7952f7 200 return newTable;
rconnorlawson 0:35660d7952f7 201 }
rconnorlawson 0:35660d7952f7 202
rconnorlawson 0:35660d7952f7 203 void destroyHashTable(HashTable* hashTable) {
nasiromar 6:c9695079521d 204
nasiromar 6:c9695079521d 205 HashTableEntry* temp;
rconnorlawson 0:35660d7952f7 206
nasiromar 6:c9695079521d 207 if (hashTable->num_buckets == 0) {
nasiromar 6:c9695079521d 208 printf("Hash table has to contain at least 1 bucket...\n");
nasiromar 6:c9695079521d 209 exit(1);
nasiromar 6:c9695079521d 210 }
nasiromar 6:c9695079521d 211 unsigned int i;
nasiromar 6:c9695079521d 212 for ( i = 0; i < hashTable->num_buckets; i++){
nasiromar 6:c9695079521d 213 HashTableEntry* point = hashTable->buckets[i];
nasiromar 6:c9695079521d 214 while(point != NULL){
nasiromar 6:c9695079521d 215 free(point->value);
nasiromar 6:c9695079521d 216 temp = point->next;
nasiromar 6:c9695079521d 217 free(point);
nasiromar 6:c9695079521d 218 point = temp;
nasiromar 6:c9695079521d 219 }
nasiromar 6:c9695079521d 220 }
nasiromar 6:c9695079521d 221 free(hashTable->buckets);
nasiromar 6:c9695079521d 222 free(hashTable);
rconnorlawson 0:35660d7952f7 223 }
rconnorlawson 0:35660d7952f7 224
rconnorlawson 0:35660d7952f7 225 void* insertItem(HashTable* hashTable, unsigned int key, void* value) {
nasiromar 6:c9695079521d 226 void* oldval;
nasiromar 6:c9695079521d 227 HashTableEntry* entry = findItem(hashTable,key);
nasiromar 6:c9695079521d 228 if (entry && (entry->key == key)){
nasiromar 6:c9695079521d 229 if(entry->value == value){
nasiromar 6:c9695079521d 230 return NULL;
nasiromar 6:c9695079521d 231 }
nasiromar 6:c9695079521d 232 oldval = entry->value;
nasiromar 6:c9695079521d 233 entry->value = value;
nasiromar 6:c9695079521d 234 return oldval;
nasiromar 6:c9695079521d 235 }
nasiromar 6:c9695079521d 236 else{
nasiromar 6:c9695079521d 237 HashTableEntry* newentry = createHashTableEntry(key,value);
nasiromar 6:c9695079521d 238 newentry->next = hashTable->buckets[hashTable->hash(key)];
nasiromar 6:c9695079521d 239 hashTable->buckets[hashTable->hash(key)] = newentry;
rconnorlawson 0:35660d7952f7 240
nasiromar 6:c9695079521d 241 return NULL;
nasiromar 6:c9695079521d 242 }
rconnorlawson 0:35660d7952f7 243 }
rconnorlawson 0:35660d7952f7 244
rconnorlawson 0:35660d7952f7 245 void* getItem(HashTable* hashTable, unsigned int key) {
rconnorlawson 0:35660d7952f7 246
nasiromar 6:c9695079521d 247 if (hashTable->num_buckets == 0) {
nasiromar 6:c9695079521d 248 return NULL;
nasiromar 6:c9695079521d 249 }
nasiromar 6:c9695079521d 250
nasiromar 6:c9695079521d 251 HashTableEntry* me = findItem(hashTable,key);
nasiromar 6:c9695079521d 252 if (me != NULL){
nasiromar 6:c9695079521d 253 return me->value;
nasiromar 6:c9695079521d 254 }
nasiromar 6:c9695079521d 255 else{
nasiromar 6:c9695079521d 256 return NULL;
nasiromar 6:c9695079521d 257 }
rconnorlawson 0:35660d7952f7 258 }
rconnorlawson 0:35660d7952f7 259
rconnorlawson 0:35660d7952f7 260 void* removeItem(HashTable* hashTable, unsigned int key) {
nasiromar 6:c9695079521d 261 int keySpot = hashTable->hash(key);
nasiromar 6:c9695079521d 262 HashTableEntry* itemRemove = hashTable->buckets[keySpot];
rconnorlawson 0:35660d7952f7 263
nasiromar 6:c9695079521d 264 if(itemRemove && ((itemRemove->key) == key)){
nasiromar 6:c9695079521d 265 void* oldVal = itemRemove->value;
nasiromar 6:c9695079521d 266 hashTable->buckets[keySpot] = itemRemove->next;
nasiromar 6:c9695079521d 267 free(itemRemove);
nasiromar 6:c9695079521d 268 return oldVal;
nasiromar 6:c9695079521d 269 }
nasiromar 6:c9695079521d 270
nasiromar 6:c9695079521d 271 if (itemRemove){
nasiromar 6:c9695079521d 272 HashTableEntry* tracker = itemRemove;
nasiromar 6:c9695079521d 273
nasiromar 6:c9695079521d 274 while(itemRemove->next){
nasiromar 6:c9695079521d 275 if (((itemRemove->next)->key) == key)
nasiromar 6:c9695079521d 276 {
nasiromar 6:c9695079521d 277 void* old_Val = (itemRemove->next)->value;
nasiromar 6:c9695079521d 278 tracker = itemRemove->next;
nasiromar 6:c9695079521d 279 itemRemove->next = (tracker->next);
nasiromar 6:c9695079521d 280 free(tracker);
nasiromar 6:c9695079521d 281 return old_Val;
nasiromar 6:c9695079521d 282 }
nasiromar 6:c9695079521d 283 itemRemove = itemRemove->next;
nasiromar 6:c9695079521d 284 }
nasiromar 6:c9695079521d 285 }
nasiromar 6:c9695079521d 286 return NULL;
rconnorlawson 0:35660d7952f7 287 }
nasiromar 6:c9695079521d 288
nasiromar 6:c9695079521d 289
rconnorlawson 0:35660d7952f7 290
rconnorlawson 0:35660d7952f7 291 void deleteItem(HashTable* hashTable, unsigned int key) {
nasiromar 6:c9695079521d 292 /* Remove the item from the Hash Table and then free the vlaue returned*/
nasiromar 6:c9695079521d 293 void* remove = removeItem(hashTable,key);
nasiromar 6:c9695079521d 294 free(remove);
nasiromar 6:c9695079521d 295 }