aboutsummaryrefslogblamecommitdiffstats
path: root/libhashtable.c
blob: f333c24480663229119c138ffc569366c96f85ea (plain) (tree)
1
2
3
4
5
6
7
8
9


                   
                   

                      

               
      


                                                      
                    







                    

                   
                       
                    
  
       
 

















































                                                                               

 

                                                           
                                             


                                      































                                                                              
                                                         



                                         
                
          
                                                            







                                  


                                      






                                          

 
                                                              








                                                                          

                                      





                                          


                                               


  
                                                           
                


                       
                        


                                        





































                                                                 

                     
                           





                                                                                              

   
          



                                                           
                     
                                     











                                                                              


                                                             


                          



                                                            


























                                                                  
                                                                             
                                                

                                     
                   
 











                                                                                                                           
 












                                                                                                            
  

                 
  
 
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "hashtable.h"

//#define DEBUG

/* _A_
struct entry {//wtf is with my stupid choice of names?
 void *target;//value
 char *original;//key
 struct entry *prev;
 struct entry *next;
};

struct hitem {
 struct entry *ll;
};

struct hashtable {
 unsigned int kl;
 unsigned int size;
 struct hitem **bucket;
 unsigned int *keys;
};
 _B_ */

/*stolen from wikipedia!*/
unsigned int murmur3_32(const char *key, unsigned int len, unsigned int seed) {
        static const uint32_t c1 = 0xcc9e2d51;
        static const uint32_t c2 = 0x1b873593;
        static const uint32_t r1 = 15;
        static const uint32_t r2 = 13;
        static const uint32_t m = 5;
        static const uint32_t n = 0xe6546b64;

        uint32_t hash = seed;

        const int nblocks = len / 4;
        const uint32_t *blocks = (const uint32_t *) key;
        const uint8_t *tail;
        uint32_t k1;
        int i;
        for (i = 0; i < nblocks; i++) {
                uint32_t k = blocks[i];
                k *= c1;
                k = (k << r1) | (k >> (32 - r1));
                k *= c2;

                hash ^= k;
                hash = ((hash << r2) | (hash >> (32 - r2))) * m + n;
        }

        tail = (const uint8_t *) (key + nblocks * 4);
        k1 = 0;

        switch (len & 3) {
        case 3:
                k1 ^= tail[2] << 16;
        case 2:
                k1 ^= tail[1] << 8;
        case 1:
                k1 ^= tail[0];
                k1 *= c1;
                k1 = (k1 << r1) | (k1 >> (32 - r1));
                k1 *= c2;
                hash ^= k1;
        }

        hash ^= len;
        hash ^= (hash >> 16);
        hash *= 0x85ebca6b;
        hash ^= (hash >> 13);
        hash *= 0xc2b2ae35;
        hash ^= (hash >> 16);

        return hash;
}

unsigned int hash(char *key) {
// return 0;//use this like a linked list.*key*strlen(key);
 if(!key) return fprintf(stderr,"piss.\n"),0;
 return murmur3_32(key,strlen(key),0);
}

unsigned int ht_getkeycount(struct hashtable *ht) {
  struct entry *m;
  int key_count=0,i;
  for(i=0;i<ht->kl;i++) //who needs braces? not us. our teeth are straight. :|
    if(ht->bucket[ht->keys[i]])
      for(m=ht->bucket[ht->keys[i]]->ll;m;m=m->next)
        key_count++;
  return key_count;
}

//returns an array of pointers to the keys stored in the hashtable
//free the pointer returned? or maybe we should just pass by reference?
char **ht_getkeys(struct hashtable *ht,unsigned int *len) {
  int i,j;
  //need to count up how many entries are in the hash table?
  //should the hashtable keep track of that info by itself?
  char **a;
  struct entry *m;
  *len=ht_getkeycount(ht);
  a=malloc(sizeof(char *) * *len);
  j=0;
  for(i=0;i<ht->kl;i++) {//for each key...
    if(ht->bucket[ht->keys[i]]) {
      for(m=ht->bucket[ht->keys[i]]->ll;m;m=m->next) {
        a[j]=m->original;
        j++;
      }
    }
  }
  return a;
}

void inittable(struct hashtable *ht,unsigned int tsize) {
 int i;
 ht->bucket=malloc(sizeof(char *)*tsize);
 ht->kl=0;
 ht->keys=malloc(sizeof(int *)*tsize);
 ht->size=tsize;
 if(!ht) {
  fprintf(stderr,"malloc error 6 in hash table.\n");//dafuq?
  return;
 }
 for(i=0;i<tsize;i++) {
  ht->bucket[i]=0;
 }
}

void ll_delete(struct entry *ll) {
 if(!ll) return;
 if(ll->prev) ll->prev->next=ll->next;
 if(ll->next) ll->next->prev=ll->prev;
 free(ll);//all these nodes are malloc()d.
}

void ll_destroy(struct entry *ll) {
 if(ll->next) ll_destroy(ll->next);
 free(ll->original);
 free(ll);
}

//this is to just free the buckets and the linked list structs
void ht_destroy(struct hashtable *ht) {
 int i=0;
 for(i=0;i<ht->kl;i++) {
  ll_destroy(ht->bucket[ht->keys[i]]->ll);
 }
 free(ht->bucket);
}

void ll_freevalues(struct entry *ll) {//only use if you malloc your table.
 if(!ll) return;
 if(ll->next) ll_freevalues(ll->next);
 free(ll->target);