website

Website contents
git clone git://git.reagancfischer.dev/website.git
Log | Files | Refs

util.c (991B)


      1 /* util.c */
      2 #include <string.h>
      3 #include <stdlib.h>
      4 #include "hash_table.h"
      5 /* String Comparison */
      6 int cmp_string(const void *key1, const void *key2) {
      7   return strcmp((char *)key1, (char *)key2);
      8 }
      9 
     10 /* Hash Function */
     11 unsigned int hash_string(const void *key) {
     12   unsigned long hash = 0, hi = 0;
     13   const char *p = key;
     14   hash = *p;
     15   if (hash != 0 && p[1] != 0) {
     16     hash = (hash << 4) + p[1];
     17     if (p[2] != 0) {
     18       hash = (hash << 4) + p[2];
     19       if (p[3] != 0) {
     20         hash = (hash << 4) + p[3];
     21         if (p[4] != 0) {
     22           hash = (hash << 4) + p[4];
     23           p += 5;
     24           while (*p != 0) {
     25             hash = (hash << 4) + *p++;
     26             hi = hash & 0xf0000000l;
     27             hash ^= hi >> 24;
     28           }
     29           hash &= 0x0fffffffl;
     30         }
     31       }
     32     }
     33   }
     34   return hash;
     35 }
     36 
     37 /* String Destructor */
     38 void dtor_string(void *value, int is_key) {
     39   if (is_key) {
     40     free(value); // Since the key and value are the same, we only need to free once.
     41   }
     42 }
     43 
     44