Undertale
Dependencies: mbed wave_player 4DGL-uLCD-SE MMA8452
hash_table.cpp@4:3883ea0930ec, 2019-04-14 (annotated)
- Committer:
- cdong49
- Date:
- Sun Apr 14 17:02:57 2019 +0000
- Revision:
- 4:3883ea0930ec
- Parent:
- 2:3caab2cc0476
FINAL;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rconnorlawson | 0:35660d7952f7 | 1 | /* |
cdong49 | 2:3caab2cc0476 | 2 | Student Name: Chuong Dong |
cdong49 | 2:3caab2cc0476 | 3 | Date: 03/27/2019 |
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) { |
cdong49 | 2:3caab2cc0476 | 129 | /** Create a pointer pointing to an empty hash table entry **/ |
cdong49 | 2:3caab2cc0476 | 130 | HashTableEntry* HTEPointer = (HashTableEntry*)malloc(sizeof(HashTableEntry)); |
cdong49 | 2:3caab2cc0476 | 131 | if(HTEPointer) { // if the pointer is not null -> malloc successfully |
cdong49 | 2:3caab2cc0476 | 132 | HTEPointer->key = key; // assign the key |
cdong49 | 2:3caab2cc0476 | 133 | HTEPointer->value = value;// assign the value |
cdong49 | 2:3caab2cc0476 | 134 | HTEPointer->next = NULL; // set the next pointer pointing to null |
cdong49 | 2:3caab2cc0476 | 135 | return HTEPointer; // return the pointer to the hash table entry |
cdong49 | 2:3caab2cc0476 | 136 | } else { // this pointer is null -> malloc fail |
cdong49 | 2:3caab2cc0476 | 137 | fprintf(stderr, "Can not allocate space for more Hash Table Entry... Try freeing some stuff!\n"); |
cdong49 | 2:3caab2cc0476 | 138 | return NULL; |
cdong49 | 2:3caab2cc0476 | 139 | } |
rconnorlawson | 0:35660d7952f7 | 140 | } |
rconnorlawson | 0:35660d7952f7 | 141 | |
rconnorlawson | 0:35660d7952f7 | 142 | /** |
rconnorlawson | 0:35660d7952f7 | 143 | * findItem |
rconnorlawson | 0:35660d7952f7 | 144 | * |
rconnorlawson | 0:35660d7952f7 | 145 | * Helper function that checks whether there exists the hash table entry that |
rconnorlawson | 0:35660d7952f7 | 146 | * contains a specific key. |
rconnorlawson | 0:35660d7952f7 | 147 | * |
rconnorlawson | 0:35660d7952f7 | 148 | * @param hashTable The pointer to the hash table. |
rconnorlawson | 0:35660d7952f7 | 149 | * @param key The key corresponds to the hash table entry |
rconnorlawson | 0:35660d7952f7 | 150 | * @return The pointer to the hash table entry, or NULL if key does not exist |
rconnorlawson | 0:35660d7952f7 | 151 | */ |
rconnorlawson | 0:35660d7952f7 | 152 | static HashTableEntry* findItem(HashTable* hashTable, unsigned int key) { |
cdong49 | 2:3caab2cc0476 | 153 | if(hashTable) { // check if hashTable is pointing to null or not |
cdong49 | 2:3caab2cc0476 | 154 | /** get the index in buckets from the key **/ |
cdong49 | 2:3caab2cc0476 | 155 | unsigned int index = hashTable->hash(key); |
cdong49 | 2:3caab2cc0476 | 156 | /** create a pointer pointing to the first Hash table entry in the right bucket **/ |
cdong49 | 2:3caab2cc0476 | 157 | HashTableEntry* HTE = hashTable->buckets[index]; |
cdong49 | 2:3caab2cc0476 | 158 | if(HTE) { // if the pointer is not null |
cdong49 | 2:3caab2cc0476 | 159 | while(HTE) { // loop until HTE becomes null |
cdong49 | 2:3caab2cc0476 | 160 | /** if the key in the hash table entry matches the given key **/ |
cdong49 | 2:3caab2cc0476 | 161 | if(HTE->key == key) { |
cdong49 | 2:3caab2cc0476 | 162 | return HTE; // return the pointer to the right entry |
cdong49 | 2:3caab2cc0476 | 163 | } |
cdong49 | 2:3caab2cc0476 | 164 | HTE = HTE->next; // if not found, the pointer point to the next entry |
cdong49 | 2:3caab2cc0476 | 165 | } |
cdong49 | 2:3caab2cc0476 | 166 | } else { // the pointer is null, return null |
cdong49 | 2:3caab2cc0476 | 167 | return NULL; |
cdong49 | 2:3caab2cc0476 | 168 | } |
cdong49 | 2:3caab2cc0476 | 169 | return NULL; // if after the search and still haven't found a match, return null |
cdong49 | 2:3caab2cc0476 | 170 | } else { // hash table pointing to null, so return null |
cdong49 | 2:3caab2cc0476 | 171 | return NULL; |
cdong49 | 2:3caab2cc0476 | 172 | } |
rconnorlawson | 0:35660d7952f7 | 173 | } |
rconnorlawson | 0:35660d7952f7 | 174 | |
rconnorlawson | 0:35660d7952f7 | 175 | /**************************************************************************** |
rconnorlawson | 0:35660d7952f7 | 176 | * Public Interface Functions |
rconnorlawson | 0:35660d7952f7 | 177 | * |
rconnorlawson | 0:35660d7952f7 | 178 | * These functions implement the public interface as specified in the header |
rconnorlawson | 0:35660d7952f7 | 179 | * file, and make use of the private functions and hidden definitions in the |
rconnorlawson | 0:35660d7952f7 | 180 | * above sections. |
rconnorlawson | 0:35660d7952f7 | 181 | ****************************************************************************/ |
rconnorlawson | 0:35660d7952f7 | 182 | // The createHashTable is provided for you as a starting point. |
rconnorlawson | 0:35660d7952f7 | 183 | HashTable* createHashTable(HashFunction hashFunction, unsigned int numBuckets) { |
rconnorlawson | 0:35660d7952f7 | 184 | // The hash table has to contain at least one bucket. Exit gracefully if |
rconnorlawson | 0:35660d7952f7 | 185 | // this condition is not met. |
rconnorlawson | 0:35660d7952f7 | 186 | if (numBuckets==0) { |
rconnorlawson | 0:35660d7952f7 | 187 | printf("Hash table has to contain at least 1 bucket...\n"); |
rconnorlawson | 0:35660d7952f7 | 188 | exit(1); |
rconnorlawson | 0:35660d7952f7 | 189 | } |
rconnorlawson | 0:35660d7952f7 | 190 | |
rconnorlawson | 0:35660d7952f7 | 191 | // Allocate memory for the new HashTable struct on heap. |
rconnorlawson | 0:35660d7952f7 | 192 | HashTable* newTable = (HashTable*)malloc(sizeof(HashTable)); |
rconnorlawson | 0:35660d7952f7 | 193 | |
rconnorlawson | 0:35660d7952f7 | 194 | // Initialize the components of the new HashTable struct. |
rconnorlawson | 0:35660d7952f7 | 195 | newTable->hash = hashFunction; |
rconnorlawson | 0:35660d7952f7 | 196 | newTable->num_buckets = numBuckets; |
rconnorlawson | 0:35660d7952f7 | 197 | newTable->buckets = (HashTableEntry**)malloc(numBuckets*sizeof(HashTableEntry*)); |
rconnorlawson | 0:35660d7952f7 | 198 | |
cdong49 | 2:3caab2cc0476 | 199 | // As the new buckets contain indeterminant values, init each bucket as NULL. |
rconnorlawson | 0:35660d7952f7 | 200 | unsigned int i; |
rconnorlawson | 0:35660d7952f7 | 201 | for (i=0; i<numBuckets; ++i) { |
rconnorlawson | 0:35660d7952f7 | 202 | newTable->buckets[i] = NULL; |
rconnorlawson | 0:35660d7952f7 | 203 | } |
rconnorlawson | 0:35660d7952f7 | 204 | |
rconnorlawson | 0:35660d7952f7 | 205 | // Return the new HashTable struct. |
rconnorlawson | 0:35660d7952f7 | 206 | return newTable; |
rconnorlawson | 0:35660d7952f7 | 207 | } |
rconnorlawson | 0:35660d7952f7 | 208 | |
cdong49 | 2:3caab2cc0476 | 209 | HashTableEntry* destroyHashTableEntry(HashTableEntry* hashTableEntry); // helper function |
cdong49 | 2:3caab2cc0476 | 210 | void destroyHashTable(HashTable* hashTable){ |
cdong49 | 2:3caab2cc0476 | 211 | if (hashTable) { // if the pointer is not null, destroy it |
cdong49 | 2:3caab2cc0476 | 212 | if (hashTable->buckets) { // if the pointer to the buckets is not null, destroy |
cdong49 | 2:3caab2cc0476 | 213 | /** for loop to go through every bucket **/ |
cdong49 | 2:3caab2cc0476 | 214 | for (int i = 0; i < hashTable->num_buckets; i++) { |
cdong49 | 2:3caab2cc0476 | 215 | /** create a pointer pointing to the first HTE of the bucket **/ |
cdong49 | 2:3caab2cc0476 | 216 | HashTableEntry* HTE = hashTable->buckets[i]; |
cdong49 | 2:3caab2cc0476 | 217 | while(HTE){ // loop to destroy until the pointer point to null |
cdong49 | 2:3caab2cc0476 | 218 | // destroy the entry and return the next pointer to nextHTE |
cdong49 | 2:3caab2cc0476 | 219 | free(HTE->value); |
cdong49 | 2:3caab2cc0476 | 220 | HashTableEntry* nextEntry = destroyHashTableEntry(HTE); |
cdong49 | 2:3caab2cc0476 | 221 | HTE = nextEntry; |
cdong49 | 2:3caab2cc0476 | 222 | } |
cdong49 | 2:3caab2cc0476 | 223 | } // done freeing all the buckets |
cdong49 | 2:3caab2cc0476 | 224 | free(hashTable->buckets); // free the array of pointer to the buckets |
cdong49 | 2:3caab2cc0476 | 225 | } |
cdong49 | 2:3caab2cc0476 | 226 | free(hashTable); // finally free the hash table |
cdong49 | 2:3caab2cc0476 | 227 | } // else there is nothing to be done |
rconnorlawson | 0:35660d7952f7 | 228 | } |
rconnorlawson | 0:35660d7952f7 | 229 | |
rconnorlawson | 0:35660d7952f7 | 230 | void* insertItem(HashTable* hashTable, unsigned int key, void* value) { |
cdong49 | 2:3caab2cc0476 | 231 | if(!hashTable) { // if hashTable point to null, return null |
cdong49 | 2:3caab2cc0476 | 232 | return NULL; |
cdong49 | 2:3caab2cc0476 | 233 | } |
cdong49 | 2:3caab2cc0476 | 234 | /** get the index in the buckets from the given key **/ |
cdong49 | 2:3caab2cc0476 | 235 | unsigned int index = hashTable->hash(key); |
cdong49 | 2:3caab2cc0476 | 236 | /** find the entry that has the same key with the function findKey **/ |
cdong49 | 2:3caab2cc0476 | 237 | HashTableEntry* findResult = findItem(hashTable, key); |
cdong49 | 2:3caab2cc0476 | 238 | if(!findResult) { //if findResult is null -> no entry with the key |
cdong49 | 2:3caab2cc0476 | 239 | if(!hashTable->buckets[index]) { // if the bucket is empty |
cdong49 | 2:3caab2cc0476 | 240 | /**insert it directly in the first position in the bucket **/ |
cdong49 | 2:3caab2cc0476 | 241 | HashTableEntry* result = createHashTableEntry(key, value); |
cdong49 | 2:3caab2cc0476 | 242 | hashTable->buckets[index] = result; |
cdong49 | 2:3caab2cc0476 | 243 | return NULL; |
cdong49 | 2:3caab2cc0476 | 244 | } // else we insert it at the end |
cdong49 | 2:3caab2cc0476 | 245 | /** HTE point to the first entry in the bucket **/ |
cdong49 | 2:3caab2cc0476 | 246 | HashTableEntry* HTE = hashTable->buckets[index]; |
cdong49 | 2:3caab2cc0476 | 247 | while(HTE) { // loop until HTE point to null or until return |
cdong49 | 2:3caab2cc0476 | 248 | if(!HTE->next) { // if the next pointer point to null, insert the entry |
cdong49 | 2:3caab2cc0476 | 249 | HTE->next = createHashTableEntry(key, value); |
cdong49 | 2:3caab2cc0476 | 250 | return NULL; |
cdong49 | 2:3caab2cc0476 | 251 | } |
cdong49 | 2:3caab2cc0476 | 252 | // HTE points to the next entry |
cdong49 | 2:3caab2cc0476 | 253 | HTE = HTE->next; |
cdong49 | 2:3caab2cc0476 | 254 | }// if we haven't return after this loop, return null |
cdong49 | 2:3caab2cc0476 | 255 | return NULL; |
cdong49 | 2:3caab2cc0476 | 256 | } // else find result is legit, we just change the value |
cdong49 | 2:3caab2cc0476 | 257 | void* result = findResult->value; // store the old value |
cdong49 | 2:3caab2cc0476 | 258 | findResult->value = value; // update the value in the entry |
cdong49 | 2:3caab2cc0476 | 259 | return result; |
rconnorlawson | 0:35660d7952f7 | 260 | } |
rconnorlawson | 0:35660d7952f7 | 261 | |
rconnorlawson | 0:35660d7952f7 | 262 | void* getItem(HashTable* hashTable, unsigned int key) { |
cdong49 | 2:3caab2cc0476 | 263 | if(!hashTable) { // if hashTable point to null, return null |
cdong49 | 2:3caab2cc0476 | 264 | return NULL; |
cdong49 | 2:3caab2cc0476 | 265 | } |
cdong49 | 2:3caab2cc0476 | 266 | /** item point to the entry with the given key **/ |
cdong49 | 2:3caab2cc0476 | 267 | HashTableEntry* item = findItem(hashTable, key); |
cdong49 | 2:3caab2cc0476 | 268 | if(item) { // if item is not pointing to null |
cdong49 | 2:3caab2cc0476 | 269 | return item->value; // return that entry's value |
cdong49 | 2:3caab2cc0476 | 270 | } else { // else the item is pointing to null, return null |
cdong49 | 2:3caab2cc0476 | 271 | return NULL; |
cdong49 | 2:3caab2cc0476 | 272 | } |
rconnorlawson | 0:35660d7952f7 | 273 | } |
rconnorlawson | 0:35660d7952f7 | 274 | |
rconnorlawson | 0:35660d7952f7 | 275 | void* removeItem(HashTable* hashTable, unsigned int key) { |
cdong49 | 2:3caab2cc0476 | 276 | if(!hashTable) { // if hashTable point to null, return null |
cdong49 | 2:3caab2cc0476 | 277 | return NULL; |
cdong49 | 2:3caab2cc0476 | 278 | } |
cdong49 | 2:3caab2cc0476 | 279 | /** removeHTE points to the entry to be removed **/ |
cdong49 | 2:3caab2cc0476 | 280 | HashTableEntry* removeHTE = findItem(hashTable, key); |
cdong49 | 2:3caab2cc0476 | 281 | if(!removeHTE) { // if removeHTE points to null, return null |
cdong49 | 2:3caab2cc0476 | 282 | return NULL; |
cdong49 | 2:3caab2cc0476 | 283 | } |
cdong49 | 2:3caab2cc0476 | 284 | /** get the index in buckets from the given key **/ |
cdong49 | 2:3caab2cc0476 | 285 | unsigned int index = hashTable->hash(key); |
cdong49 | 2:3caab2cc0476 | 286 | /** HTE points to the first entry in the bucket **/ |
cdong49 | 2:3caab2cc0476 | 287 | HashTableEntry* HTE = hashTable->buckets[index]; |
cdong49 | 2:3caab2cc0476 | 288 | if(HTE == removeHTE) { // if the entry to be removed in also the first entry |
cdong49 | 2:3caab2cc0476 | 289 | void* result = removeHTE->value; // store the old value into result |
cdong49 | 2:3caab2cc0476 | 290 | /** free the entry and store the pointer to next(which is null) into the bucket **/ |
cdong49 | 2:3caab2cc0476 | 291 | hashTable->buckets[index] = destroyHashTableEntry(removeHTE); |
cdong49 | 2:3caab2cc0476 | 292 | return result; // return the old value |
cdong49 | 2:3caab2cc0476 | 293 | } // else we have to look for the entry with the right key |
cdong49 | 2:3caab2cc0476 | 294 | while(HTE) { // loop until HTE points to null |
cdong49 | 2:3caab2cc0476 | 295 | /** if the next pointer of HTE point to the entry to be removed **/ |
cdong49 | 2:3caab2cc0476 | 296 | if(HTE->next == removeHTE) { |
cdong49 | 2:3caab2cc0476 | 297 | void* result = removeHTE->value; // store the old value into result |
cdong49 | 2:3caab2cc0476 | 298 | /** free the entry and store the next pointer to the next value of the deleted entry **/ |
cdong49 | 2:3caab2cc0476 | 299 | removeHTE = destroyHashTableEntry(removeHTE); |
cdong49 | 2:3caab2cc0476 | 300 | return result; |
cdong49 | 2:3caab2cc0476 | 301 | } |
cdong49 | 2:3caab2cc0476 | 302 | HTE = HTE->next; // HTE point to the next entry |
cdong49 | 2:3caab2cc0476 | 303 | }// if after the while loop we haven't return, return null |
cdong49 | 2:3caab2cc0476 | 304 | return NULL; |
rconnorlawson | 0:35660d7952f7 | 305 | } |
rconnorlawson | 0:35660d7952f7 | 306 | |
rconnorlawson | 0:35660d7952f7 | 307 | void deleteItem(HashTable* hashTable, unsigned int key) { |
cdong49 | 2:3caab2cc0476 | 308 | free(removeItem(hashTable, key));//free the value returned after remove the item |
cdong49 | 2:3caab2cc0476 | 309 | } |
rconnorlawson | 0:35660d7952f7 | 310 | |
cdong49 | 2:3caab2cc0476 | 311 | /** |
cdong49 | 2:3caab2cc0476 | 312 | * destroyHashTableEntry |
cdong49 | 2:3caab2cc0476 | 313 | * |
cdong49 | 2:3caab2cc0476 | 314 | * Destroy the hash table entry being passed in. Freeing key, value and next |
cdong49 | 2:3caab2cc0476 | 315 | * Return the next value(HashTableEntry *) |
cdong49 | 2:3caab2cc0476 | 316 | */ |
cdong49 | 2:3caab2cc0476 | 317 | HashTableEntry* destroyHashTableEntry(HashTableEntry* hashTableEntry) { |
cdong49 | 2:3caab2cc0476 | 318 | HashTableEntry* result = hashTableEntry->next; |
cdong49 | 2:3caab2cc0476 | 319 | //free(hashTableEntry->value); |
cdong49 | 2:3caab2cc0476 | 320 | free(hashTableEntry); |
cdong49 | 2:3caab2cc0476 | 321 | return result; |
cdong49 | 2:3caab2cc0476 | 322 | } |