Advertisement
ifinox

Untitled

Aug 8th, 2017
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.48 KB | None | 0 0
  1. #include <stdbool.h>
  2. #include <ctype.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <string.h>
  6. #include <stdint.h>
  7. //#include "dictionary.h"
  8.  
  9.  
  10. #define Buckets 5000
  11. #define HashSeed 154625
  12.  
  13. // Linked lists
  14. typedef struct linkedList
  15. {
  16.     const char * val;
  17.     struct linkedList * n;
  18. } linkedList;
  19.  
  20. // Linked strings functions
  21.  
  22. linkedList * add(const char * str, linkedList * first)
  23. {
  24.     linkedList * new = malloc(sizeof(linkedList));
  25.     new->val = str;
  26.     new->n = first;
  27.     return new;
  28. }
  29.  
  30. void linkedPrint(linkedList * first)
  31. {
  32.     linkedList * wsk = first;
  33.     while(wsk != NULL)
  34.     {
  35.         printf("%s\n", wsk->val);
  36.         wsk = wsk->n;
  37.     }
  38. }
  39.  
  40. /////////////////////////
  41.  
  42. uint32_t murmurHash(const char * key, uint32_t len);
  43. linkedList * dictionary[Buckets];
  44. int WORDS = 0;
  45.  
  46. unsigned int size(void)
  47. {
  48.     return WORDS;
  49. }
  50.  
  51. bool unload(void)
  52. {
  53.     for(int i = 0; i<Buckets; i++)
  54.         if (dictionary[i] != NULL)
  55.         {
  56.  
  57.         }
  58.     if (WORDS != 0) // something wrong happend
  59.         return false;
  60.     return true;
  61. }
  62.  
  63. bool check(const char * word)
  64. {
  65.     char wordSmall[strlen(word) + 1];
  66.     int i = 0;
  67.     while(word[i] != '\0')
  68.     {
  69.         wordSmall[i] = tolower(word[i]);
  70.         i++;
  71.     }
  72.     wordSmall[i] = '\0';
  73.  
  74.     int HASH = murmurHash(wordSmall, strlen(wordSmall)) % Buckets;
  75. }
  76.  
  77. bool load(const char *dictionary)
  78. {    
  79.     FILE * file;
  80.     file = fopen (dictionary , "r");
  81.     if (file == NULL)
  82.         return false;
  83.    
  84.     while (true)
  85.     {
  86.         if (feof(file))
  87.             break;
  88.         char * str = malloc(sizeof(char)*46);
  89.         if (str == NULL)
  90.             return false;
  91.         fscanf(file, "%s", str);
  92.  
  93.         int bucket = murmurHash(str, strlen(str)) % Buckets;
  94.         dictionary[bucket] = add("GZ", dictionary[bucket]);
  95.         WORDS++;
  96.     }
  97.        
  98.    
  99.     fclose (file);
  100.     printf("LOADED %i WORDS\n", WORDS);
  101.     return true;
  102. }
  103.  
  104.  
  105.  
  106.  
  107. linkedList * listy[2];
  108. int main()
  109. {
  110.     char * test = malloc(sizeof(char) * 3);
  111.     test[0] ='h';
  112.     test[1] = 'i';
  113.     test[2] = '\0';
  114.     linkedList * lista = NULL;
  115.     int tableValid = 0;
  116.     for(int i = 0; i<2; i++){
  117.     dictionary[i] = add("Hej", dictionary[i]);
  118.     listy[i] = add("Marcin", listy[i]);
  119.     listy[i] = add(test, listy[i]);
  120.     }
  121.     for(int i = 0; i<2; i++)
  122.     linkedPrint(listy[i]);
  123.     return 0;
  124. }
  125.  
  126. uint32_t murmurHash (const char *key, uint32_t len) {
  127.     uint32_t c1 = 0xcc9e2d51;
  128.     uint32_t c2 = 0x1b873593;
  129.     uint32_t r1 = 15;
  130.     uint32_t r2 = 13;
  131.     uint32_t m = 5;
  132.     uint32_t n = 0xe6546b64;
  133.     uint32_t h = 0;
  134.     uint32_t k = 0;
  135.     uint8_t *d = (uint8_t *) key;
  136.     const uint32_t *chunks = NULL;
  137.     const uint8_t *tail = NULL;
  138.     int i = 0;
  139.     int l = len / 4;
  140.     h = HashSeed;
  141.     chunks = (const uint32_t *) (d + l * 4);
  142.     tail = (const uint8_t *) (d + l * 4);
  143.     for (i = -l; i != 0; ++i) {
  144.         k = chunks[i];
  145.         k *= c1;
  146.         k = (k << r1) | (k >> (32 - r1));
  147.         k *= c2;
  148.         h ^= k;
  149.         h = (h << r2) | (h >> (32 - r2));
  150.         h = h * m + n;
  151.     }
  152.     k = 0;
  153.     switch (len & 3) {
  154.         case 3: k ^= (tail[2] << 16);
  155.         case 2: k ^= (tail[1] << 8);
  156.         case 1:
  157.         k ^= tail[0];
  158.         k *= c1;
  159.         k = (k << r1) | (k >> (32 - r1));
  160.         k *= c2;
  161.         h ^= k;
  162.     }
  163.     h ^= len;
  164.     h ^= (h >> 16);
  165.     h *= 0x85ebca6b;
  166.     h ^= (h >> 13);
  167.     h *= 0xc2b2ae35;
  168.     h ^= (h >> 16);
  169.     return h;
  170. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement