-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhashmap.h
181 lines (165 loc) · 5.07 KB
/
hashmap.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
* Matt McFarland
* KPCB Engineering Fellows Application - October 2015
*
* Hashmap.h contains functions for creating and storing
* values in a fixed size hashmap where the key is a
* string of arbitrary length and the data object is a data reference
* pointer.
*
*/
#ifndef HASHMAP_h
#define HASHMAP_h
/* ------ PUBLIC CONSTANTS ------- */
#define SUCCESS 0
#define FAILURE 1
/* ------ STRUCTURES ------ */
typedef struct HashNode {
int occupied;
char * key;
void * data;
unsigned long hash;
struct HashNode * next;
struct HashNode * prev;
} HashNode;
typedef struct HashMap {
int size;
int count;
HashNode * values;
} HashMap;
/* ------ PUBLIC FUNCTIONS ------ */
/*
* CreateMap returns a preallocated HashMap that
* can contain up to size entries.
*
* ARGUMENTS:
* size - must be an integer >= 1
*
* RETURN VALUE:
* Returns pointer to new HashMap if successful
* Returns NULL on failure
*
*/
HashMap * CreateMap(int size);
/*
* Set will add a key-value pair to the HashMap
* if it does not exist. If the key already exists
* in the HashMap, then the existing data value will
* be overwritten.
*
* Note - NULL keys cannot be set in HashMap. Null
* strings can be a valid key however. (ie. key == NULL
* will return FAILURE, but key == "" will try to insert
* or update the data element.) NULL data values are
* valid data elements to store for a key.
*
* ARGUMENTS:
* map - pointer to valid HashMap to add key-value to
* key - string key to add or update value for (cannot be NULL)
* val - data reference to store with key
*
* RETURN VALUE:
* Returns SUCCESS if key was added/updated
* Returns FAILURE if error occurs
*
*/
int Set(HashMap * map, char * key, void * val);
/*
* Get will retrieve the data element for a specified
* key if that key exists in the given HashMap. If the
* key does not exist in the HashMap, NULL is returned.
*
* Note - If a key has a NULL data value, that NULL will
* be returned by Get. The result of the status variable
* pointed at in the argument will state whether a value
* was successfully retrieved (SUCCESS) or if the key
* could not be found/error occurred. So if Get returns
* NULL and the status variable is SUCCESS, the key exists
* in the HashMap and is NULL. If Get returns NULL and status
* is FAILURE, then the key isn't in the HashMap.
*
* ARGUMENTS:
* map - pointer to valid HashMap to search for key in
* key - string key to find value for
* status_ptr - pointer to variable where SUCCESS or FAILURE is
* stored on return (if NULL, success status won't be saved)
*
* RETURN VALUE:
* Returns data value if key-value exists in HashMap
* Returns NULL if key could not be located
* If data was successfully retrieved status will be
* SUCCESS if a pointer is given
* If data could not be retrieved because key was absent
* or error occured, status will be FAILURE
*
*/
void * Get(HashMap * map, char * key, int * status_ptr);
/*
* Delete will remove the key-value pair from the HashMap
* for a given key if that key exists in the HashMap. If the
* key exists the key-value pair is removed, the data element
* for that key is returned.
*
* Note - If a key has a NULL data value, that NULL will
* be returned by Delete. The result of the status variable
* pointed at in the argument will state whether a value
* was successfully retrieved (SUCCESS) or if the key
* could not be found/error occurred. So if Delete returns
* NULL and the status variable is SUCCESS, the key existed
* in the HashMap and the data was NULL. If Delete returns
* NULL and status is FAILURE, then the key wasn't in the HashMap.
*
* ARGUMENTS:
* map - pointer to valid HashMap to delete entry from
* key - string key to delete
* status_ptr - pointer to variable where SUCCESS or FAILURE
* is stored on return (if NULL, success status won't be saved)
*
* RETURN VALUE:
* Returns data value if key-value existed in the
* HashMap and was deleted
* Returns NULL if key did not exist or data element was NULL
*
*/
void * Delete(HashMap * map, char * key, int * status_ptr);
/*
* GetLoad returns the load factor for the specified
* HashMap (defined as entries currently map divided by
* the size of the HashMap). Load factor is a float value
* between 0 and 1.00.
*
* ARGUMENTS:
* map - pointer to valid HashMap retrieve load from
*
* RETURN VALUE:
* Returns load factor is map exists
* Returns FAILURE if map value array does not exist
*
*/
float GetLoad(HashMap * map);
/*
* DeleteMap will free the space allocated for the map
* if memory has been allocated to it. The size and count
* of the map are also reset to 0.
*
* ARGUMENTS:
* map - pointer to valid HashMap to delete
*
* RETURN VALUE:
* Returns SUCCESS if memory was freed and map was cleared
* Returns FAILURE if map or values array was invalid
*
*/
int DeleteMap(HashMap * map);
/*
* PrintMap prints the HashMap to stdout
*
* ARGUMENTS:
* map - pointer to valid HashMap to print
*
* RETURN VALUE:
* None
*
*/
void PrintMap(HashMap * map);
#endif // HASHMAP_h