learning-cc4e

Solution to https://cc4e.com/. If you copy these you're not right in the head.
git clone https://kaka.farm/~git/learning-cc4e
Log | Files | Refs

solution.c (2248B)


      1 struct HashMapEntry* __HashMap_find(struct HashMap* self, char *key, int bucket)
      2 {
      3   /* TODO: Add some code here */
      4   struct HashMapEntry *current_entry;
      5 
      6   for (current_entry = self->__heads[bucket];
      7        current_entry != NULL;
      8        current_entry = current_entry->__next) {
      9     if (strcmp(current_entry->key, key) == 0) {
     10       return current_entry;
     11     }
     12   }
     13 
     14   return NULL;
     15 }
     16 
     17 void __HashMap_put(struct HashMap* self, char *key, int value) {
     18   int bucket;
     19   struct HashMapEntry *old, *new;
     20   char *new_key;
     21   if ( key == NULL ) return;
     22   bucket = getBucket(key, self->__buckets);
     23   old = __HashMap_find(self, key, bucket);
     24   if ( old != NULL ) {
     25     old->value = value;
     26     return;
     27   }
     28   new = malloc(sizeof(*new));
     29   /* TODO: Add some code here */
     30 
     31   new_key = malloc(sizeof(*new_key) * (strlen(key) + 1));
     32   new_key = strcpy(new_key, key);
     33   new->key = new_key;
     34   new->value = value;
     35   new->__next = NULL;
     36 
     37   if (self->__heads[bucket] == NULL) {
     38     new->__prev = NULL;
     39     self->__heads[bucket] = new;
     40   }
     41 
     42   if (self->__tails[bucket] != NULL) {
     43     new->__prev = self->__tails[bucket];
     44     self->__tails[bucket]->__next = new;
     45   }
     46 
     47   self->__tails[bucket] = new;
     48   
     49   self->__count++;
     50 }
     51 
     52 int __HashMap_get(struct HashMap* self, char *key, int def)
     53 {
     54   /* TODO: Add some code here */
     55 
     56   struct HashMapEntry *result = __HashMap_find(self,
     57                                                key,
     58                                                getBucket(key, self->__buckets));
     59 
     60   if (result != NULL) {
     61     return result->value;
     62   }
     63   
     64   return def;
     65 }
     66 
     67 int __HashMap_size(struct HashMap* self)
     68 {
     69   return self->__count;
     70 }
     71 
     72 struct HashMapEntry* __HashMapIter_next(struct HashMapIter* self)
     73 {
     74   /* TODO: Add some code here */
     75 
     76   struct HashMapEntry *result;
     77 
     78   /* Out of the lecture. ~_~ */
     79 
     80   while (self->__current == NULL) {
     81     if (self->__bucket >= self->__map->__buckets) {
     82       /* Avoids counting unnecessarily. */
     83       return NULL;
     84     }
     85     ++self->__bucket;
     86     if (self->__bucket >= self->__map->__buckets) {
     87       return NULL;
     88     }
     89     self->__current = self->__map->__heads[self->__bucket];
     90   }
     91 
     92   result = self->__current;
     93   
     94   if (self->__current != NULL) {
     95     self->__current = self->__current->__next;
     96   }
     97 
     98   return result;
     99 }