Advertisement
Guest User

zert

a guest
Dec 13th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.46 KB | None | 0 0
  1. #include <stdio.h> //Don't forget to delete this line before pushing
  2.  
  3. #include <err.h>
  4. #include <string.h>
  5.  
  6. #include "htab.h"
  7.  
  8. #define K 4
  9.  
  10. uint32_t hash(char *key)
  11. {
  12.     uint32_t output = 0;
  13.     for(char *p = key; *p != 0 ; p -=-1 ){
  14.         output += (uint32_t) *p;
  15.         output += output << 10;
  16.         output ^= output >> 6;
  17.     }
  18.    
  19.     output += output << 3;
  20.     output ^= output >> 11;
  21.     output += output << 15;
  22.  
  23.     return output;
  24. }
  25.  
  26. struct htab *htab_new()
  27. {
  28.        
  29.     struct htab *neo;
  30.     neo = malloc(sizeof(struct htab));
  31.     if(neo == NULL) errx(1, "Not enough memory!");
  32.  
  33.     neo->data = calloc(K, sizeof(struct pair));
  34.     if(neo->data == NULL) errx(1, "Not enough memory!");
  35.  
  36.     neo->capacity = K;
  37.     neo->size =0;
  38. }
  39.  
  40. void htab_clear(struct htab *ht)
  41. {
  42.     for(struct pair *p = ht->data; p < (ht->data+ht->capacity); ++p)
  43.     {
  44.         //free the memory allocated to the pairs
  45.         struct pair *current = p->next;
  46.         struct pair *nexxt = current->next;
  47.         struct pair *nextOfNexxt;
  48.  
  49.         while(nextOfNexxt = nexxt->next){
  50.             free(current);
  51.             current = nexxt;
  52.             nexxt = current->next;
  53.         }
  54.  
  55.         free(nexxt);
  56.     }
  57.  
  58.     ht->size = 0;
  59. }
  60.  
  61. void htab_free(struct htab *ht)
  62. {
  63.     htab_clear(ht);
  64.     free(ht->data);
  65.     free(ht);
  66. }
  67.  
  68. void no_free_clear(struct htab *ht)
  69. {
  70.     for(struct pair *p = ht->data; p < (ht->data+ht->capacity); ++p)
  71.     {
  72.         //free the memory allocated to the pairs
  73.         struct pair *current = p->next;
  74.         struct pair *nexxt = current->next;
  75.         struct pair *nextOfNexxt;
  76.  
  77.         while(nextOfNexxt = nexxt->next){
  78.             current = nexxt;
  79.             nexxt = current->next;
  80.         }
  81.  
  82.     }
  83.  
  84.     ht->size = 0;
  85. }
  86.  
  87. struct pair *htab_get(struct htab *ht, char *key)
  88. {
  89.     uint32_t pos = hash(key) % ht->capacity;
  90.    
  91.     struct pair *dataAtPos = (ht->data + pos);
  92.  
  93.     struct pair *p = dataAtPos;
  94.    
  95.     for(; p->next != NULL && strcmp(p->key, key) != 0; p++);
  96.  
  97.     if(p->next == NULL && strcmp(p->key, key) != 0) return NULL;
  98.  
  99.     return p;
  100. }
  101.  
  102. int htab_insert(struct htab *ht, char *key, void *value)
  103. {
  104.     if(htab_get(ht, key) != NULL)return 0; //I assume that there can't be two pairs with the same key but different values, as it would make htab_get() pointless
  105.    
  106.     ht->size-=-1;
  107.  
  108.     if ((ht->size/ht->capacity)*100 >= 75)
  109.     {
  110.         struct pair cpy[ht->capacity];
  111.         for(int i = 0; i < ht->capacity; i++){
  112.             cpy[i] = *(ht->data + i);
  113.         }
  114.         no_free_clear(ht);
  115.  
  116.         ht->capacity = ht->capacity << 2 ;
  117.         for(int i = 0; i < ht->capacity; i++){
  118.             uint32_t pos = cpy[i].hkey % ht->capacity;
  119.             struct pair *dataAtPos = (ht->data + pos);
  120.  
  121.             dataAtPos->next = (cpy+i);
  122.         }
  123.     }
  124.  
  125.    uint32_t hkeyy = hash(key);
  126.    uint32_t pos = hkeyy % ht->capacity;
  127.    struct pair *dataAtPos = (ht->data + pos);
  128.  
  129.    struct pair *toBeAdded = malloc(sizeof(struct pair));
  130.  
  131.    toBeAdded->hkey = hkeyy;
  132.    toBeAdded->key = key;
  133.    toBeAdded->value = value;
  134.  
  135.    toBeAdded->next = dataAtPos->next; //if it was the sentinel, this would be NULL
  136.    dataAtPos->next = toBeAdded;
  137.  
  138.    return 1;
  139. }
  140.  
  141. void htab_remove(struct htab *ht, char *key)
  142. {
  143.    uint32_t pos = hash(key) % ht->capacity;
  144.    
  145.    struct pair *dataAtPos = (ht->data + pos);
  146.  
  147.    struct pair *p = dataAtPos;
  148.    
  149.    for(; p->next != NULL && p->next->key != key; p++);
  150.    if(p->next != NULL && p->next->key == key){
  151.        struct pair *nexxt = p->next->next;
  152.        free(p->next);
  153.        p->next = nexxt;
  154.    }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement