ECE 2035 Homework

Dependencies:   mbed wave_player 4DGL-uLCD-SE MMA8452

Committer:
zkang0729
Date:
Thu Nov 14 17:14:37 2019 +0000
Revision:
4:e3fbc74a3783
Parent:
0:35660d7952f7
Initial Commit;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
rconnorlawson 0:35660d7952f7 1 /*
zkang0729 4:e3fbc74a3783 2 Student Name: Zixuan Kang
zkang0729 4:e3fbc74a3783 3 Date: 2019-10-25
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) {
zkang0729 4:e3fbc74a3783 129 //Allocate memort for the new HashTableEntry struct on heap
zkang0729 4:e3fbc74a3783 130 HashTableEntry* newHashTableEntry = (HashTableEntry*)malloc(sizeof(HashTableEntry));
zkang0729 4:e3fbc74a3783 131
zkang0729 4:e3fbc74a3783 132 //Initialize the components of the new HashTableEntry stuct
zkang0729 4:e3fbc74a3783 133 newHashTableEntry->key = key;
zkang0729 4:e3fbc74a3783 134 newHashTableEntry->value = value;
zkang0729 4:e3fbc74a3783 135 newHashTableEntry->next = NULL;
rconnorlawson 0:35660d7952f7 136
zkang0729 4:e3fbc74a3783 137 //Return the HashTableEntry struct.
zkang0729 4:e3fbc74a3783 138 return newHashTableEntry;
rconnorlawson 0:35660d7952f7 139 }
rconnorlawson 0:35660d7952f7 140
rconnorlawson 0:35660d7952f7 141 /**
rconnorlawson 0:35660d7952f7 142 * findItem
rconnorlawson 0:35660d7952f7 143 *
rconnorlawson 0:35660d7952f7 144 * Helper function that checks whether there exists the hash table entry that
rconnorlawson 0:35660d7952f7 145 * contains a specific key.
rconnorlawson 0:35660d7952f7 146 *
rconnorlawson 0:35660d7952f7 147 * @param hashTable The pointer to the hash table.
rconnorlawson 0:35660d7952f7 148 * @param key The key corresponds to the hash table entry
rconnorlawson 0:35660d7952f7 149 * @return The pointer to the hash table entry, or NULL if key does not exist
rconnorlawson 0:35660d7952f7 150 */
rconnorlawson 0:35660d7952f7 151 static HashTableEntry* findItem(HashTable* hashTable, unsigned int key) {
zkang0729 4:e3fbc74a3783 152 int i;
zkang0729 4:e3fbc74a3783 153 HashTableEntry *thisHashTableEntry;
zkang0729 4:e3fbc74a3783 154 for(i = 0; i < hashTable->num_buckets; i++) { // Go through each linked lsit
zkang0729 4:e3fbc74a3783 155 thisHashTableEntry = hashTable->buckets[i];
zkang0729 4:e3fbc74a3783 156 while (thisHashTableEntry) {
zkang0729 4:e3fbc74a3783 157 if (thisHashTableEntry->key != key) {
zkang0729 4:e3fbc74a3783 158 thisHashTableEntry = thisHashTableEntry->next; // If the keys don't match go to the next one
zkang0729 4:e3fbc74a3783 159 } else {
zkang0729 4:e3fbc74a3783 160 return thisHashTableEntry;
zkang0729 4:e3fbc74a3783 161 }
zkang0729 4:e3fbc74a3783 162 }
zkang0729 4:e3fbc74a3783 163 }
zkang0729 4:e3fbc74a3783 164 return NULL;
rconnorlawson 0:35660d7952f7 165 }
rconnorlawson 0:35660d7952f7 166
rconnorlawson 0:35660d7952f7 167 /****************************************************************************
rconnorlawson 0:35660d7952f7 168 * Public Interface Functions
rconnorlawson 0:35660d7952f7 169 *
rconnorlawson 0:35660d7952f7 170 * These functions implement the public interface as specified in the header
rconnorlawson 0:35660d7952f7 171 * file, and make use of the private functions and hidden definitions in the
rconnorlawson 0:35660d7952f7 172 * above sections.
rconnorlawson 0:35660d7952f7 173 ****************************************************************************/
rconnorlawson 0:35660d7952f7 174 // The createHashTable is provided for you as a starting point.
rconnorlawson 0:35660d7952f7 175 HashTable* createHashTable(HashFunction hashFunction, unsigned int numBuckets) {
rconnorlawson 0:35660d7952f7 176 // The hash table has to contain at least one bucket. Exit gracefully if
rconnorlawson 0:35660d7952f7 177 // this condition is not met.
rconnorlawson 0:35660d7952f7 178 if (numBuckets==0) {
rconnorlawson 0:35660d7952f7 179 printf("Hash table has to contain at least 1 bucket...\n");
rconnorlawson 0:35660d7952f7 180 exit(1);
rconnorlawson 0:35660d7952f7 181 }
rconnorlawson 0:35660d7952f7 182
rconnorlawson 0:35660d7952f7 183 // Allocate memory for the new HashTable struct on heap.
rconnorlawson 0:35660d7952f7 184 HashTable* newTable = (HashTable*)malloc(sizeof(HashTable));
rconnorlawson 0:35660d7952f7 185
rconnorlawson 0:35660d7952f7 186 // Initialize the components of the new HashTable struct.
rconnorlawson 0:35660d7952f7 187 newTable->hash = hashFunction;
rconnorlawson 0:35660d7952f7 188 newTable->num_buckets = numBuckets;
rconnorlawson 0:35660d7952f7 189 newTable->buckets = (HashTableEntry**)malloc(numBuckets*sizeof(HashTableEntry*));
rconnorlawson 0:35660d7952f7 190
zkang0729 4:e3fbc74a3783 191 // As the new buckets contain indeterminant values, init each bucket as NULL.
rconnorlawson 0:35660d7952f7 192 unsigned int i;
rconnorlawson 0:35660d7952f7 193 for (i=0; i<numBuckets; ++i) {
rconnorlawson 0:35660d7952f7 194 newTable->buckets[i] = NULL;
rconnorlawson 0:35660d7952f7 195 }
rconnorlawson 0:35660d7952f7 196
rconnorlawson 0:35660d7952f7 197 // Return the new HashTable struct.
rconnorlawson 0:35660d7952f7 198 return newTable;
rconnorlawson 0:35660d7952f7 199 }
rconnorlawson 0:35660d7952f7 200
rconnorlawson 0:35660d7952f7 201 void destroyHashTable(HashTable* hashTable) {
zkang0729 4:e3fbc74a3783 202 int i;
zkang0729 4:e3fbc74a3783 203 HashTableEntry *t1, *t2;
zkang0729 4:e3fbc74a3783 204 for (i = 0; i < hashTable->num_buckets; i++) {
zkang0729 4:e3fbc74a3783 205 t1 = hashTable->buckets[i];
zkang0729 4:e3fbc74a3783 206 while (t1) {
zkang0729 4:e3fbc74a3783 207 t2 = t1; // Sign it to a temporary variable
zkang0729 4:e3fbc74a3783 208 t1 = t1->next; // Assign the next one to t1
zkang0729 4:e3fbc74a3783 209 free(t2->value); // Free the value
zkang0729 4:e3fbc74a3783 210 free(t2); // Free the node
zkang0729 4:e3fbc74a3783 211 }
zkang0729 4:e3fbc74a3783 212 }
zkang0729 4:e3fbc74a3783 213 free(hashTable->buckets); // Free the bucket
zkang0729 4:e3fbc74a3783 214 free(hashTable); // Free the hashtable
rconnorlawson 0:35660d7952f7 215 }
rconnorlawson 0:35660d7952f7 216
rconnorlawson 0:35660d7952f7 217 void* insertItem(HashTable* hashTable, unsigned int key, void* value) {
zkang0729 4:e3fbc74a3783 218 HashTableEntry *thisHashTableEntry;
zkang0729 4:e3fbc74a3783 219 if ((thisHashTableEntry = findItem(hashTable, key))) {
zkang0729 4:e3fbc74a3783 220 //Key in list
zkang0729 4:e3fbc74a3783 221 void *temp = thisHashTableEntry->value; // Get the initial value of the entry point
zkang0729 4:e3fbc74a3783 222 thisHashTableEntry->value = value;
zkang0729 4:e3fbc74a3783 223 return temp;
zkang0729 4:e3fbc74a3783 224 }
zkang0729 4:e3fbc74a3783 225 //Key not in list
zkang0729 4:e3fbc74a3783 226 thisHashTableEntry = createHashTableEntry(key, value);
zkang0729 4:e3fbc74a3783 227 if (!thisHashTableEntry) return NULL; //Malloc failed
zkang0729 4:e3fbc74a3783 228 thisHashTableEntry->next = hashTable->buckets[hashTable->hash(key)];
zkang0729 4:e3fbc74a3783 229 hashTable->buckets[hashTable->hash(key)] = thisHashTableEntry; // Insert the node at the head of the linked list
zkang0729 4:e3fbc74a3783 230 return thisHashTableEntry->value;
rconnorlawson 0:35660d7952f7 231 }
rconnorlawson 0:35660d7952f7 232
rconnorlawson 0:35660d7952f7 233 void* getItem(HashTable* hashTable, unsigned int key) {
zkang0729 4:e3fbc74a3783 234 if (findItem(hashTable, key) == NULL) {
zkang0729 4:e3fbc74a3783 235 return NULL;
zkang0729 4:e3fbc74a3783 236 } else {
zkang0729 4:e3fbc74a3783 237 return findItem(hashTable, key)->value; // return the value of the elemnt that has the key
zkang0729 4:e3fbc74a3783 238 }
rconnorlawson 0:35660d7952f7 239 }
rconnorlawson 0:35660d7952f7 240
rconnorlawson 0:35660d7952f7 241 void* removeItem(HashTable* hashTable, unsigned int key) {
zkang0729 4:e3fbc74a3783 242 HashTableEntry *thisHashTableEntry = hashTable->buckets[hashTable->hash(key)];
zkang0729 4:e3fbc74a3783 243 HashTableEntry *temp;
zkang0729 4:e3fbc74a3783 244 if (!thisHashTableEntry) return NULL;
zkang0729 4:e3fbc74a3783 245 //If the node is at the head of the linked list
zkang0729 4:e3fbc74a3783 246 if (thisHashTableEntry->key == key) {
zkang0729 4:e3fbc74a3783 247 hashTable->buckets[hashTable->hash(key)] = thisHashTableEntry->next;
zkang0729 4:e3fbc74a3783 248 temp = thisHashTableEntry;
zkang0729 4:e3fbc74a3783 249 free(thisHashTableEntry);
zkang0729 4:e3fbc74a3783 250 return temp->value;
zkang0729 4:e3fbc74a3783 251 }
zkang0729 4:e3fbc74a3783 252 // If the node is not at the head of the linked list
zkang0729 4:e3fbc74a3783 253 while (thisHashTableEntry->next) {
zkang0729 4:e3fbc74a3783 254 if (thisHashTableEntry->next->key == key) {
zkang0729 4:e3fbc74a3783 255 //Split the linked list
zkang0729 4:e3fbc74a3783 256 temp = thisHashTableEntry->next;
zkang0729 4:e3fbc74a3783 257 thisHashTableEntry->next = thisHashTableEntry->next->next;
zkang0729 4:e3fbc74a3783 258 free(thisHashTableEntry->next);
zkang0729 4:e3fbc74a3783 259 return temp->value;
zkang0729 4:e3fbc74a3783 260 }
zkang0729 4:e3fbc74a3783 261 thisHashTableEntry = thisHashTableEntry->next;
zkang0729 4:e3fbc74a3783 262 }
zkang0729 4:e3fbc74a3783 263 return NULL;
rconnorlawson 0:35660d7952f7 264 }
rconnorlawson 0:35660d7952f7 265
rconnorlawson 0:35660d7952f7 266 void deleteItem(HashTable* hashTable, unsigned int key) {
zkang0729 4:e3fbc74a3783 267 free(removeItem(hashTable, key));
zkang0729 4:e3fbc74a3783 268 return;
zkang0729 4:e3fbc74a3783 269 }