Advertisement
KaeruCT

hash table thingy

Dec 13th, 2011
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <assert.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5.  
  6. struct Map {
  7.     int length;
  8.     int allocated;
  9.     char **keys;
  10.     char **values;
  11. };
  12.  
  13. struct Map *Map_create()
  14. {
  15.     struct Map *map = malloc(sizeof(struct Map));
  16.     assert(map != NULL);
  17.    
  18.     map->allocated = 0;
  19.     map->length = 0;
  20.     map->keys = NULL;
  21.     map->values = NULL;
  22.    
  23.     return map;
  24. }
  25.  
  26. char* Map_get(struct Map *map, char *key)
  27. {
  28.     assert(map != NULL);
  29.    
  30.     int i;
  31.    
  32.     for(i = 0; i < map->length; i++){
  33.         if(strcmp(key, map->keys[i]) == 0){
  34.             return map->values[i];
  35.         }
  36.     }
  37.    
  38.     return NULL;
  39. }
  40.  
  41. int Map_get_key_index(struct Map *map, char *key)
  42. {
  43.     assert(map != NULL);
  44.     int i;
  45.    
  46.     for(i = 0; i < map->length; i++){
  47.         if(strcmp(key, map->keys[i]) == 0){
  48.             return i;
  49.         }
  50.     }
  51.    
  52.     return -1;
  53. }
  54.  
  55. int Map_contains_key(struct Map *map, char *key)
  56. {
  57.     assert(map != NULL);
  58.     int i;
  59.    
  60.     for(i = 0; i < map->length; i++){
  61.         if(strcmp(key, map->keys[i]) == 0){
  62.             return 1;
  63.         }
  64.     }
  65.    
  66.     return 0;
  67. }
  68.  
  69. int Map_contains_value(struct Map *map, char *value)
  70. {
  71.     assert(map != NULL);
  72.     int i;
  73.    
  74.     for(i = 0; i < map->length; i++){
  75.         if(strcmp(value, map->values[i]) == 0){
  76.             return 1;
  77.         }
  78.     }
  79.    
  80.     return 0;
  81. }
  82.  
  83. void Map_remove(struct Map *map, char *key)
  84. {
  85.     assert(map != NULL);
  86.     int k = Map_get_key_index(map, key);
  87.     int i = 0;
  88.    
  89.     if(k > -1){
  90.         map->length--;
  91.    
  92.         for(i = k; i < map->length; i++){
  93.             map->keys[i] = map->keys[i+1];
  94.             map->values[i] = map->values[i+1];
  95.         }
  96.     }
  97. }
  98.  
  99. void Map_put(struct Map *map, char *key, char *value)
  100. {
  101.     assert(map != NULL);
  102.     int i = Map_get_key_index(map, key);
  103.     int k;
  104.    
  105.     // will overwrite if key already exists
  106.     if(i > -1){
  107.         k = i;
  108.     }else{
  109.         if(map->length == map->allocated){
  110.        
  111.             if(map->allocated == 0){
  112.                 // begin allocating 2
  113.                 map->allocated = 2;
  114.             }else{
  115.                 // double if more is needed
  116.                 map->allocated *= 2;
  117.             }
  118.            
  119.             void *tmp_keys = realloc(map->keys, map->allocated * sizeof(key));
  120.             void *tmp_values = realloc(map->values, map->allocated * sizeof(value));
  121.            
  122.             map->keys = (char**)tmp_keys;
  123.             map->values = (char**)tmp_values;
  124.         }
  125.         k = map->length;
  126.         map->length = k + 1;
  127.     }
  128.    
  129.     map->keys[k] = key;
  130.     map->values[k] = value;
  131. }
  132.  
  133. void print_key(struct Map *map, char *key)
  134. {
  135.     printf("%s: %s\n", key, Map_get(map, key));
  136. }
  137.  
  138. int main(int arcg, char *argv[])
  139. {
  140.     struct Map *map = Map_create();
  141.     printf("Map length: %d\n", map->length);
  142.    
  143.     Map_put(map, "name", "Andres");
  144.     Map_put(map, "age", "17");
  145.     Map_put(map, "favorite drink", "coffee");
  146.     Map_put(map, "favorite color", "blue");
  147.    
  148.     print_key(map, "name");
  149.     print_key(map, "age");
  150.     print_key(map, "favorite color");
  151.     print_key(map, "favorite drink");
  152.     printf("Map length: %d\n", map->length);
  153.    
  154.     Map_remove(map, "age");
  155.     print_key(map, "age");
  156.     printf("Map length: %d\n", map->length);
  157.    
  158.     Map_put(map, "age", "18");
  159.     print_key(map, "age");
  160.     printf("Map length: %d\n", map->length);
  161.    
  162.     free(map);
  163.     return 0;
  164. }
  165.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement