You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+12-10
Original file line number
Diff line number
Diff line change
@@ -16,11 +16,13 @@ For example, if keys A, B, and C all hash to slot 13 (and are added in that orde
16
16
17
17
Continuing this example with A, B, and C, if a fourth element, D is added that hashes to the slot that B occupies, D will see that B is occupying D's intended slot. Since B was placed there arbitrarily, and D belongs is B's slot, B is moved to a different, arbitrary free slot and D is placed in B's old location. B's adjacent linked-list neighbors (A and C) are updated to relect B's new location. If element E is added that also maps to D's slot, then E will be placed in an arbitrary, free slot and attached to D's collision list.
18
18
19
-
If element A is removed, then A is removed from slot 13. However, because A is the head of a collision list for slot 13 (linking B and C to 13) and that collision list isn't empty, a new head element needs to be placed in slot 13 to maintain the integrity of that collision list. So B, the next element in that collision list, is moved from it's arbitrary location and placed in slot 13. B updates it's predecessor with its new location information.
19
+
If element A is removed, then A is removed from slot 13. However, because A is the head of a collision list for slot 13 (linking B and C to 13) and that collision list isn't empty, a new head element needs to be placed in slot 13 to maintain the integrity of that collision list. So B, the next element in that collision list, is moved from it's arbitrary location and placed in slot 13. B updates its neighbor C with its new location information.
20
+
21
+
### Pseudocode
20
22
21
23
To set a key (and map isn't full), look at its intended slot. If that slot is unoccupied, add the new entry there. If that slot is occupied by another entry that maps to the new entry's slot, traverse that pre-existing entry's collided entry list. While traversing, examine each collided node and see if the new entry is already in HashMap. If it is, just update the data value. If the new entry isn't on the collided list, add the new entry to the end of that list (place the new_entry in an arbitrary, free slot and put it on the end of the linked list).
22
24
23
-
To get a data value for a key, examine the slot the key maps to and each entry on that slot's collision list for matching keys. If a match isn't found, return error.
25
+
To get a data value for a key, examine the slot the key maps to and each entry on that slot's collision list for matching keys. If a match isn't found, return failure.
24
26
25
27
To delete an entry, find the slot the specified key should map to. If occupied, traverse the collision list, examining each entry along the way for a match. Once a match is found, remove that entry from the HashMap (fix up linked list pointers). If the entry is the head of a collision list (exists in the slot that key hashes to), then move the first element in the collision list to the deleted entry's location.
26
28
@@ -68,11 +70,11 @@ The following tests are conducted in the TestHashMap executable.
68
70
- GetLoad when HashMap is full and empty
69
71
- DeleteMap is tested
70
72
71
-
SPECIAL CASE - SIZE = 10 -> When the SIZE constant is defined as 10, the collision handling mechanism used in this HashMap become clear. A collided element gets move to a different slot by a new addition on a different slot. When the original element that the second element collided with is removed, the second element is moved back to the head of the collision list (back to first element's slot).
73
+
SPECIAL CASE - SIZE = 10 -> When the SIZE constant is defined as 10, the collision handling mechanism used in this HashMap become clear. Run "TestHashMap verbose" to see representations of the map printed out throughout the collisions handling. A collided element "0" gets moved to a different slot by a new addition "1" on a different slot. When the original element that the second element collided with is removed (""), the second element "0" is moved back to the head of the collision list (back to first element's slot).
72
74
73
-
All of these tests have been conducted in a memory management environment. All memory leaks have been handled.
75
+
All of these tests have been conducted in a memory management environment. All memory leaks have been handled and plugged.
74
76
75
-
To see more verbose testing output, use "TestHashMap verbose" call and uncomment the "#define DEBUG" statement at the top of the hashmap.c file.
77
+
To see more verbose testing output, use "TestHashMap verbose" and uncomment the "#define DEBUG" statement at the top of the hashmap.c file.
76
78
77
79
## Function Descriptions
78
80
@@ -83,12 +85,12 @@ CreateMap returns a preallocated HashMap that can contain up to size entries.
83
85
84
86
ARGUMENTS:
85
87
86
-
- size: must be an integer >= 1
88
+
- size: must be an integer greater than zero
87
89
88
90
RETURN VALUE:
89
91
90
92
- Returns pointer to new HashMap if successful
91
-
- Returns NULL on failure
93
+
- Returns NULL if map could not be created
92
94
93
95
94
96
### Set
@@ -107,7 +109,7 @@ ARGUMENTS:
107
109
RETURN VALUE:
108
110
109
111
- Returns SUCCESS if key was added/updated
110
-
- Returns FAILURE if error occurs
112
+
- Returns FAILURE if key could not be added or updated
111
113
112
114
113
115
### Get
@@ -121,7 +123,7 @@ ARGUMENTS:
121
123
122
124
- map: pointer to valid HashMap to search for key in
123
125
- key: string key to find value for
124
-
- status_ptr - pointer to variables where SUCCESS or FAILURE will be stored on return (if NULL, status won't be saved)
126
+
- status_ptr - pointer to variable where SUCCESS or FAILURE will be stored on return (if NULL, status won't be saved)
125
127
126
128
RETURN VALUE:
127
129
- Returns data value if key-value exists in HashMap
@@ -138,7 +140,7 @@ ARGUMENTS:
138
140
139
141
- map: pointer to valid HashMap to delete entry from
140
142
- key: string key to delete
141
-
- status_ptr - pointer to variables where SUCCESS or FAILURE will be stored on return (if NULL, status won't be saved)
143
+
- status_ptr - pointer to variable where SUCCESS or FAILURE will be stored on return (if NULL, status won't be saved)
0 commit comments