Created
March 11, 2018 00:48
-
-
Save SimplyAhmazing/2bee2cc8de631f4a2734560c7ee433de to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <assert.h> | |
#include <stdbool.h> | |
#include <stddef.h> | |
#include <stdio.h> | |
#include <stdlib.h> | |
typedef struct node { | |
int key; | |
int val; | |
struct node *next; | |
} node; | |
typedef struct hashmap { | |
int num_elements; | |
int num_buckets; | |
node *buckets; | |
} hashmap; | |
typedef struct result { | |
bool found; | |
int val; | |
} result; | |
hashmap make_hashmap() { | |
int buckets = 8; | |
hashmap new_map = {0, buckets, calloc(buckets, sizeof(node))}; | |
return new_map; | |
} | |
// false = overwrote, true = inserted | |
bool set(hashmap *map, int key, int val) { | |
node *current_element = &map->buckets[key % map->num_buckets]; | |
node *new_node = malloc(sizeof(node)); | |
new_node->key = key; | |
new_node->val = val; | |
new_node->next = NULL; | |
if (current_element == NULL) { | |
map->buckets[key % map->num_buckets] = *new_node; | |
return true; | |
} | |
while (true) { | |
if (current_element->key == key) { | |
current_element->val = val; | |
return false; | |
} | |
if (current_element->next == NULL) { | |
current_element->next = new_node; | |
return true; | |
} | |
current_element = current_element->next; | |
} | |
} | |
result get(hashmap *map, int key) { | |
result res; | |
node *current_element = &map->buckets[key % map->num_buckets]; | |
if (current_element == NULL) { | |
res.found = false; | |
return res; | |
} | |
while (true) { | |
if (current_element->key == key) { | |
res.found = true; | |
res.val = current_element->val; | |
return res; | |
} | |
if (current_element->next == NULL) { | |
res.found = false; | |
return res; | |
} | |
current_element = current_element->next; | |
} | |
} | |
bool exists(hashmap *map, int key) { | |
return get(map, key).found; | |
} | |
bool test_make_hashmap() { | |
hashmap test_map = make_hashmap(); | |
assert(test_map.num_elements == 0); | |
} | |
bool test_exists() { | |
hashmap test_map = make_hashmap(); | |
assert(!exists(&test_map, 1)); | |
} | |
bool test_set() { | |
hashmap test_map = make_hashmap(); | |
assert(set(&test_map, 1, 8)); | |
assert(!set(&test_map, 1, 10)); | |
assert(exists(&test_map, 1)); | |
assert(!exists(&test_map, 2)); | |
} | |
bool test_get() { | |
hashmap test_map = make_hashmap(); | |
assert(set(&test_map, 1, 8)); | |
assert(set(&test_map, 9, 12)); | |
assert(get(&test_map, 1).val == 8); | |
assert(get(&test_map, 1).found); | |
assert(get(&test_map, 9).found); | |
assert(!get(&test_map, 2).found); | |
} | |
int main() { | |
test_make_hashmap(); | |
test_exists(); | |
test_set(); | |
test_get(); | |
printf("Successfully finished\n"); | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment