project for 2035
Dependencies: mbed wave_player 4DGL-uLCD-SE MMA8452
hash_table.cpp@1:10330bce85cb, 2020-10-23 (annotated)
- Committer:
- DCchico
- Date:
- Fri Oct 23 16:18:39 2020 -0400
- Revision:
- 1:10330bce85cb
- Child:
- 2:4947d6a82971
shell-code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
DCchico | 1:10330bce85cb | 1 | /* |
DCchico | 1:10330bce85cb | 2 | Student Name: |
DCchico | 1:10330bce85cb | 3 | Date: |
DCchico | 1:10330bce85cb | 4 | |
DCchico | 1:10330bce85cb | 5 | ======================= |
DCchico | 1:10330bce85cb | 6 | ECE 2035 Project 2-1: |
DCchico | 1:10330bce85cb | 7 | ======================= |
DCchico | 1:10330bce85cb | 8 | This file provides definition for the structs and functions declared in the |
DCchico | 1:10330bce85cb | 9 | header file. It also contains helper functions that are not accessible from |
DCchico | 1:10330bce85cb | 10 | outside of the file. |
DCchico | 1:10330bce85cb | 11 | |
DCchico | 1:10330bce85cb | 12 | FOR FULL CREDIT, BE SURE TO TRY MULTIPLE TEST CASES and DOCUMENT YOUR CODE. |
DCchico | 1:10330bce85cb | 13 | |
DCchico | 1:10330bce85cb | 14 | =================================== |
DCchico | 1:10330bce85cb | 15 | Naming conventions in this file: |
DCchico | 1:10330bce85cb | 16 | =================================== |
DCchico | 1:10330bce85cb | 17 | 1. All struct names use camel case where the first letter is capitalized. |
DCchico | 1:10330bce85cb | 18 | e.g. "HashTable", or "HashTableEntry" |
DCchico | 1:10330bce85cb | 19 | |
DCchico | 1:10330bce85cb | 20 | 2. Variable names with a preceding underscore "_" will not be called directly. |
DCchico | 1:10330bce85cb | 21 | e.g. "_HashTable", "_HashTableEntry" |
DCchico | 1:10330bce85cb | 22 | |
DCchico | 1:10330bce85cb | 23 | Recall that in C, we have to type "struct" together with the name of the struct |
DCchico | 1:10330bce85cb | 24 | in order to initialize a new variable. To avoid this, in hash_table.h |
DCchico | 1:10330bce85cb | 25 | we use typedef to provide new "nicknames" for "struct _HashTable" and |
DCchico | 1:10330bce85cb | 26 | "struct _HashTableEntry". As a result, we can create new struct variables |
DCchico | 1:10330bce85cb | 27 | by just using: |
DCchico | 1:10330bce85cb | 28 | - "HashTable myNewTable;" |
DCchico | 1:10330bce85cb | 29 | or |
DCchico | 1:10330bce85cb | 30 | - "HashTableEntry myNewHashTableEntry;" |
DCchico | 1:10330bce85cb | 31 | |
DCchico | 1:10330bce85cb | 32 | The preceding underscore "_" simply provides a distinction between the names |
DCchico | 1:10330bce85cb | 33 | of the actual struct defition and the "nicknames" that we use to initialize |
DCchico | 1:10330bce85cb | 34 | new structs. |
DCchico | 1:10330bce85cb | 35 | [See Hidden Definitions section for more information.] |
DCchico | 1:10330bce85cb | 36 | |
DCchico | 1:10330bce85cb | 37 | 3. Functions, their local variables and arguments are named with camel case, where |
DCchico | 1:10330bce85cb | 38 | the first letter is lower-case. |
DCchico | 1:10330bce85cb | 39 | e.g. "createHashTable" is a function. One of its arguments is "numBuckets". |
DCchico | 1:10330bce85cb | 40 | It also has a local variable called "newTable". |
DCchico | 1:10330bce85cb | 41 | |
DCchico | 1:10330bce85cb | 42 | 4. The name of a struct member is divided by using underscores "_". This serves |
DCchico | 1:10330bce85cb | 43 | as a distinction between function local variables and struct members. |
DCchico | 1:10330bce85cb | 44 | e.g. "num_buckets" is a member of "HashTable". |
DCchico | 1:10330bce85cb | 45 | |
DCchico | 1:10330bce85cb | 46 | */ |
DCchico | 1:10330bce85cb | 47 | |
DCchico | 1:10330bce85cb | 48 | /**************************************************************************** |
DCchico | 1:10330bce85cb | 49 | * Include the Public Interface |
DCchico | 1:10330bce85cb | 50 | * |
DCchico | 1:10330bce85cb | 51 | * By including the public interface at the top of the file, the compiler can |
DCchico | 1:10330bce85cb | 52 | * enforce that the function declarations in the the header are not in |
DCchico | 1:10330bce85cb | 53 | * conflict with the definitions in the file. This is not a guarantee of |
DCchico | 1:10330bce85cb | 54 | * correctness, but it is better than nothing! |
DCchico | 1:10330bce85cb | 55 | ***************************************************************************/ |
DCchico | 1:10330bce85cb | 56 | #include "hash_table.h" |
DCchico | 1:10330bce85cb | 57 | |
DCchico | 1:10330bce85cb | 58 | |
DCchico | 1:10330bce85cb | 59 | /**************************************************************************** |
DCchico | 1:10330bce85cb | 60 | * Include other private dependencies |
DCchico | 1:10330bce85cb | 61 | * |
DCchico | 1:10330bce85cb | 62 | * These other modules are used in the implementation of the hash table module, |
DCchico | 1:10330bce85cb | 63 | * but are not required by users of the hash table. |
DCchico | 1:10330bce85cb | 64 | ***************************************************************************/ |
DCchico | 1:10330bce85cb | 65 | #include <stdlib.h> // For malloc and free |
DCchico | 1:10330bce85cb | 66 | #include <stdio.h> // For printf |
DCchico | 1:10330bce85cb | 67 | |
DCchico | 1:10330bce85cb | 68 | |
DCchico | 1:10330bce85cb | 69 | /**************************************************************************** |
DCchico | 1:10330bce85cb | 70 | * Hidden Definitions |
DCchico | 1:10330bce85cb | 71 | * |
DCchico | 1:10330bce85cb | 72 | * These definitions are not available outside of this file. However, because |
DCchico | 1:10330bce85cb | 73 | * the are forward declared in hash_table.h, the type names are |
DCchico | 1:10330bce85cb | 74 | * available everywhere and user code can hold pointers to these structs. |
DCchico | 1:10330bce85cb | 75 | ***************************************************************************/ |
DCchico | 1:10330bce85cb | 76 | /** |
DCchico | 1:10330bce85cb | 77 | * This structure represents an a hash table. |
DCchico | 1:10330bce85cb | 78 | * Use "HashTable" instead when you are creating a new variable. [See top comments] |
DCchico | 1:10330bce85cb | 79 | */ |
DCchico | 1:10330bce85cb | 80 | struct _HashTable { |
DCchico | 1:10330bce85cb | 81 | /** The array of pointers to the head of a singly linked list, whose nodes |
DCchico | 1:10330bce85cb | 82 | are HashTableEntry objects */ |
DCchico | 1:10330bce85cb | 83 | HashTableEntry** buckets; |
DCchico | 1:10330bce85cb | 84 | |
DCchico | 1:10330bce85cb | 85 | /** The hash function pointer */ |
DCchico | 1:10330bce85cb | 86 | HashFunction hash; |
DCchico | 1:10330bce85cb | 87 | |
DCchico | 1:10330bce85cb | 88 | /** The number of buckets in the hash table */ |
DCchico | 1:10330bce85cb | 89 | unsigned int num_buckets; |
DCchico | 1:10330bce85cb | 90 | }; |
DCchico | 1:10330bce85cb | 91 | |
DCchico | 1:10330bce85cb | 92 | /** |
DCchico | 1:10330bce85cb | 93 | * This structure represents a hash table entry. |
DCchico | 1:10330bce85cb | 94 | * Use "HashTableEntry" instead when you are creating a new variable. [See top comments] |
DCchico | 1:10330bce85cb | 95 | */ |
DCchico | 1:10330bce85cb | 96 | struct _HashTableEntry { |
DCchico | 1:10330bce85cb | 97 | /** The key for the hash table entry */ |
DCchico | 1:10330bce85cb | 98 | unsigned int key; |
DCchico | 1:10330bce85cb | 99 | |
DCchico | 1:10330bce85cb | 100 | /** The value associated with this hash table entry */ |
DCchico | 1:10330bce85cb | 101 | void* value; |
DCchico | 1:10330bce85cb | 102 | |
DCchico | 1:10330bce85cb | 103 | /** |
DCchico | 1:10330bce85cb | 104 | * A pointer pointing to the next hash table entry |
DCchico | 1:10330bce85cb | 105 | * NULL means there is no next entry (i.e. this is the tail) |
DCchico | 1:10330bce85cb | 106 | */ |
DCchico | 1:10330bce85cb | 107 | HashTableEntry* next; |
DCchico | 1:10330bce85cb | 108 | }; |
DCchico | 1:10330bce85cb | 109 | |
DCchico | 1:10330bce85cb | 110 | |
DCchico | 1:10330bce85cb | 111 | /**************************************************************************** |
DCchico | 1:10330bce85cb | 112 | * Private Functions |
DCchico | 1:10330bce85cb | 113 | * |
DCchico | 1:10330bce85cb | 114 | * These functions are not available outside of this file, since they are not |
DCchico | 1:10330bce85cb | 115 | * declared in hash_table.h. |
DCchico | 1:10330bce85cb | 116 | ***************************************************************************/ |
DCchico | 1:10330bce85cb | 117 | /** |
DCchico | 1:10330bce85cb | 118 | * createHashTableEntry |
DCchico | 1:10330bce85cb | 119 | * |
DCchico | 1:10330bce85cb | 120 | * Helper function that creates a hash table entry by allocating memory for it on |
DCchico | 1:10330bce85cb | 121 | * the heap. It initializes the entry with key and value, initialize pointer to |
DCchico | 1:10330bce85cb | 122 | * the next entry as NULL, and return the pointer to this hash table entry. |
DCchico | 1:10330bce85cb | 123 | * |
DCchico | 1:10330bce85cb | 124 | * @param key The key corresponds to the hash table entry |
DCchico | 1:10330bce85cb | 125 | * @param value The value stored in the hash table entry |
DCchico | 1:10330bce85cb | 126 | * @return The pointer to the hash table entry |
DCchico | 1:10330bce85cb | 127 | */ |
DCchico | 1:10330bce85cb | 128 | static HashTableEntry* createHashTableEntry(unsigned int key, void* value) { |
DCchico | 1:10330bce85cb | 129 | |
DCchico | 1:10330bce85cb | 130 | } |
DCchico | 1:10330bce85cb | 131 | |
DCchico | 1:10330bce85cb | 132 | /** |
DCchico | 1:10330bce85cb | 133 | * findItem |
DCchico | 1:10330bce85cb | 134 | * |
DCchico | 1:10330bce85cb | 135 | * Helper function that checks whether there exists the hash table entry that |
DCchico | 1:10330bce85cb | 136 | * contains a specific key. |
DCchico | 1:10330bce85cb | 137 | * |
DCchico | 1:10330bce85cb | 138 | * @param hashTable The pointer to the hash table. |
DCchico | 1:10330bce85cb | 139 | * @param key The key corresponds to the hash table entry |
DCchico | 1:10330bce85cb | 140 | * @return The pointer to the hash table entry, or NULL if key does not exist |
DCchico | 1:10330bce85cb | 141 | */ |
DCchico | 1:10330bce85cb | 142 | static HashTableEntry* findItem(HashTable* hashTable, unsigned int key) { |
DCchico | 1:10330bce85cb | 143 | |
DCchico | 1:10330bce85cb | 144 | } |
DCchico | 1:10330bce85cb | 145 | |
DCchico | 1:10330bce85cb | 146 | /**************************************************************************** |
DCchico | 1:10330bce85cb | 147 | * Public Interface Functions |
DCchico | 1:10330bce85cb | 148 | * |
DCchico | 1:10330bce85cb | 149 | * These functions implement the public interface as specified in the header |
DCchico | 1:10330bce85cb | 150 | * file, and make use of the private functions and hidden definitions in the |
DCchico | 1:10330bce85cb | 151 | * above sections. |
DCchico | 1:10330bce85cb | 152 | ****************************************************************************/ |
DCchico | 1:10330bce85cb | 153 | // The createHashTable is provided for you as a starting point. |
DCchico | 1:10330bce85cb | 154 | HashTable* createHashTable(HashFunction hashFunction, unsigned int numBuckets) { |
DCchico | 1:10330bce85cb | 155 | // The hash table has to contain at least one bucket. Exit gracefully if |
DCchico | 1:10330bce85cb | 156 | // this condition is not met. |
DCchico | 1:10330bce85cb | 157 | if (numBuckets==0) { |
DCchico | 1:10330bce85cb | 158 | printf("Hash table has to contain at least 1 bucket...\n"); |
DCchico | 1:10330bce85cb | 159 | exit(1); |
DCchico | 1:10330bce85cb | 160 | } |
DCchico | 1:10330bce85cb | 161 | |
DCchico | 1:10330bce85cb | 162 | // Allocate memory for the new HashTable struct on heap. |
DCchico | 1:10330bce85cb | 163 | HashTable* newTable = (HashTable*)malloc(sizeof(HashTable)); |
DCchico | 1:10330bce85cb | 164 | |
DCchico | 1:10330bce85cb | 165 | // Initialize the components of the new HashTable struct. |
DCchico | 1:10330bce85cb | 166 | newTable->hash = hashFunction; |
DCchico | 1:10330bce85cb | 167 | newTable->num_buckets = numBuckets; |
DCchico | 1:10330bce85cb | 168 | newTable->buckets = (HashTableEntry**)malloc(numBuckets*sizeof(HashTableEntry*)); |
DCchico | 1:10330bce85cb | 169 | |
DCchico | 1:10330bce85cb | 170 | // As the new buckets are empty, init each bucket as NULL. |
DCchico | 1:10330bce85cb | 171 | unsigned int i; |
DCchico | 1:10330bce85cb | 172 | for (i=0; i<numBuckets; ++i) { |
DCchico | 1:10330bce85cb | 173 | newTable->buckets[i] = NULL; |
DCchico | 1:10330bce85cb | 174 | } |
DCchico | 1:10330bce85cb | 175 | |
DCchico | 1:10330bce85cb | 176 | // Return the new HashTable struct. |
DCchico | 1:10330bce85cb | 177 | return newTable; |
DCchico | 1:10330bce85cb | 178 | } |
DCchico | 1:10330bce85cb | 179 | |
DCchico | 1:10330bce85cb | 180 | void destroyHashTable(HashTable* hashTable) { |
DCchico | 1:10330bce85cb | 181 | |
DCchico | 1:10330bce85cb | 182 | } |
DCchico | 1:10330bce85cb | 183 | |
DCchico | 1:10330bce85cb | 184 | void* insertItem(HashTable* hashTable, unsigned int key, void* value) { |
DCchico | 1:10330bce85cb | 185 | } |
DCchico | 1:10330bce85cb | 186 | |
DCchico | 1:10330bce85cb | 187 | void* getItem(HashTable* hashTable, unsigned int key) { |
DCchico | 1:10330bce85cb | 188 | |
DCchico | 1:10330bce85cb | 189 | } |
DCchico | 1:10330bce85cb | 190 | |
DCchico | 1:10330bce85cb | 191 | void* removeItem(HashTable* hashTable, unsigned int key) { |
DCchico | 1:10330bce85cb | 192 | |
DCchico | 1:10330bce85cb | 193 | } |
DCchico | 1:10330bce85cb | 194 | |
DCchico | 1:10330bce85cb | 195 | void deleteItem(HashTable* hashTable, unsigned int key) { |
DCchico | 1:10330bce85cb | 196 | |
DCchico | 1:10330bce85cb | 197 | } |