Created
August 29, 2013 06:24
-
-
Save mmitou/6374767 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 <stdint.h> | |
| #include <stdlib.h> | |
| const uint64_t MAP_ARRAY_LENGTH = 100; | |
| struct map_node { | |
| uint64_t state; | |
| void *value; | |
| struct map_node *next; | |
| }; | |
| struct map_node** new_map() { | |
| struct map_node **array = (struct map_node**)calloc(sizeof(struct map_node*), MAP_ARRAY_LENGTH); | |
| return array; | |
| } | |
| int free_map_list(struct map_node *list) { | |
| struct map_node *p; | |
| for(p = list;p != NULL;) { | |
| struct map_node *next = p -> next; | |
| free(p); | |
| p = next; | |
| } | |
| return 0; | |
| } | |
| int free_map(struct map_node **array) { | |
| int i = 0; | |
| for(;i < MAP_ARRAY_LENGTH; ++i) { | |
| if(array[i] != NULL) { | |
| free_map_list(array[i]); | |
| } | |
| } | |
| return 0; | |
| } | |
| struct map_node* map_node_last(struct map_node *list) | |
| { | |
| struct map_node *p = NULL; | |
| for(p = list; p->next != NULL; p = p->next){} | |
| return p; | |
| } | |
| struct map_node* map_node_get(struct map_node *list, uint64_t state) { | |
| struct map_node *p; | |
| for(p = list; p != NULL; p = p -> next) { | |
| if(p -> state == state) { | |
| return p; | |
| } | |
| } | |
| return NULL; | |
| } | |
| uint64_t state(const char *str) { | |
| uint64_t i; | |
| uint64_t sum; | |
| for(i = 0, sum = 0;str[i] != '\0'; ++i, sum += (uint64_t)str[i] * i){} | |
| return sum; | |
| } | |
| struct map_node **map_add(struct map_node **l, const char *keyStr, void *value) { | |
| uint64_t s = state(keyStr); | |
| uint64_t hash = s % MAP_ARRAY_LENGTH; | |
| struct map_node *node = (struct map_node*)malloc(sizeof(struct map_node)); | |
| node->state = s; | |
| node->value = value; | |
| node->next = NULL; | |
| if(l[hash] == NULL) { | |
| l[hash] = node; | |
| } else { | |
| map_node_last(l[hash])->next = node; | |
| } | |
| return l; | |
| } | |
| void *map_get(struct map_node **l, const char *keyStr) { | |
| uint64_t s = state(keyStr); | |
| uint64_t hash = s % MAP_ARRAY_LENGTH; | |
| if(l[hash] == NULL) { | |
| return NULL; | |
| } else { | |
| return map_node_get(l[hash], s)->value; | |
| } | |
| } | |
| int main(int argc, char **argv) { | |
| const char *p = "/home/mitou/foo"; | |
| const char *q = "/home/mitou/bar"; | |
| int x = 123; | |
| int y = 456; | |
| int *r; | |
| struct map_node **map = new_map(); | |
| map_add(map, p, &x); | |
| map_add(map, q, &y); | |
| r = (int *)map_get(map, p); | |
| printf("%d == %d\n", x, *r); | |
| r = (int *)map_get(map, q); | |
| printf("%d == %d\n", y, *r); | |
| free_map(map); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment