Created
August 5, 2020 22:05
-
-
Save samflores/f50ded18ff78a43441aee37275a41f96 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 <stdio.h> | |
#include <stdlib.h> | |
typedef struct node { | |
char key; | |
int val; | |
struct node *next; | |
} node_t; | |
typedef struct { | |
int size; | |
node_t **list; | |
} hash_map_t; | |
typedef struct { | |
char *keys; | |
int *vals; | |
int size; | |
int sum; | |
} result_t; | |
int _map_hash(hash_map_t *t, int key) { | |
return key % t->size; | |
} | |
node_t *_map_lookup_node(hash_map_t *t, char key) { | |
node_t *list = t->list[_map_hash(t, key)]; | |
node_t *node = list; | |
while (node) { | |
if (node->key == key) | |
return node; | |
node = node->next; | |
} | |
return NULL; | |
} | |
hash_map_t *map_create(int size) { | |
hash_map_t *t = (hash_map_t *)malloc(sizeof(hash_map_t)); | |
t->size = size; | |
t->list = (node_t **)malloc(sizeof(node_t *) * size); | |
return t; | |
} | |
int map_get(hash_map_t *t, char key) { | |
node_t *node = _map_lookup_node(t, key); | |
if (node) | |
return node->val; | |
return 0; | |
} | |
void map_put(hash_map_t *t, char key, int val) { | |
int pos = _map_hash(t, key); | |
node_t *list = t->list[pos]; | |
node_t *old_node = _map_lookup_node(t, key); | |
if (old_node) { | |
old_node->val = val; | |
} | |
node_t *new_node = (node_t *)malloc(sizeof(node_t)); | |
new_node->key = key; | |
new_node->val = val; | |
new_node->next = list; | |
t->list[pos] = new_node; | |
} | |
#define BUFFER_SIZE 10 | |
result_t *weird_func(int len, hash_map_t **input) { | |
result_t *result = (result_t *)malloc(sizeof(result_t)); | |
result->keys = (char *)malloc(sizeof(char) * BUFFER_SIZE); | |
result->vals = (int *)malloc(sizeof(int) * BUFFER_SIZE); | |
result->size = 0; | |
result->sum = 0; | |
for (int i = 0; i < len; i++) { | |
hash_map_t *t = input[i]; | |
for (int j = 0; j < t->size; j++) { | |
node_t *list = t->list[j]; | |
node_t *node = list; | |
while (node) { | |
result->keys[result->size] = node->key; | |
result->vals[result->size++] = node->val; | |
result->sum += node->val; | |
node = node->next; | |
} | |
} | |
} | |
return result; | |
} | |
#define INPUT_SIZE 3 | |
int main() { | |
hash_map_t *map_a = map_create(5); | |
map_put(map_a, 'a', 10); | |
hash_map_t *map_b = map_create(5); | |
map_put(map_b, 'b', 20); | |
hash_map_t *map_c = map_create(5); | |
map_put(map_c, 'c', 30); | |
map_put(map_c, 'd', 40); | |
hash_map_t* input [] = (hash_map_t* []){ map_a, map_b, map_c }; | |
result_t *result = weird_func(INPUT_SIZE, input); | |
printf("keys: "); | |
for (int i = 0; i < result->size; i++) | |
printf(" %c ", result->keys[i]); | |
printf("\nvalues: "); | |
for (int i = 0; i < result->size; i++) | |
printf(" %d ", result->vals[i]); | |
printf("\nsum: "); | |
printf(" %d ", result->sum); | |
printf("\n"); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment