Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdbool.h>
- #include <ctype.h>
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <stdint.h>
- //#include "dictionary.h"
- #define Buckets 5000
- #define HashSeed 154625
- // Linked lists
- typedef struct linkedList
- {
- const char * val;
- struct linkedList * n;
- } linkedList;
- // Linked strings functions
- linkedList * add(const char * str, linkedList * first)
- {
- linkedList * new = malloc(sizeof(linkedList));
- new->val = str;
- new->n = first;
- return new;
- }
- void linkedPrint(linkedList * first)
- {
- linkedList * wsk = first;
- while(wsk != NULL)
- {
- printf("%s\n", wsk->val);
- wsk = wsk->n;
- }
- }
- /////////////////////////
- uint32_t murmurHash(const char * key, uint32_t len);
- linkedList * dictionary[Buckets];
- int WORDS = 0;
- unsigned int size(void)
- {
- return WORDS;
- }
- bool unload(void)
- {
- for(int i = 0; i<Buckets; i++)
- if (dictionary[i] != NULL)
- {
- }
- if (WORDS != 0) // something wrong happend
- return false;
- return true;
- }
- bool check(const char * word)
- {
- char wordSmall[strlen(word) + 1];
- int i = 0;
- while(word[i] != '\0')
- {
- wordSmall[i] = tolower(word[i]);
- i++;
- }
- wordSmall[i] = '\0';
- int HASH = murmurHash(wordSmall, strlen(wordSmall)) % Buckets;
- }
- bool load(const char *dictionary)
- {
- FILE * file;
- file = fopen (dictionary , "r");
- if (file == NULL)
- return false;
- while (true)
- {
- if (feof(file))
- break;
- char * str = malloc(sizeof(char)*46);
- if (str == NULL)
- return false;
- fscanf(file, "%s", str);
- int bucket = murmurHash(str, strlen(str)) % Buckets;
- dictionary[bucket] = add("GZ", dictionary[bucket]);
- WORDS++;
- }
- fclose (file);
- printf("LOADED %i WORDS\n", WORDS);
- return true;
- }
- linkedList * listy[2];
- int main()
- {
- char * test = malloc(sizeof(char) * 3);
- test[0] ='h';
- test[1] = 'i';
- test[2] = '\0';
- linkedList * lista = NULL;
- int tableValid = 0;
- for(int i = 0; i<2; i++){
- dictionary[i] = add("Hej", dictionary[i]);
- listy[i] = add("Marcin", listy[i]);
- listy[i] = add(test, listy[i]);
- }
- for(int i = 0; i<2; i++)
- linkedPrint(listy[i]);
- return 0;
- }
- uint32_t murmurHash (const char *key, uint32_t len) {
- uint32_t c1 = 0xcc9e2d51;
- uint32_t c2 = 0x1b873593;
- uint32_t r1 = 15;
- uint32_t r2 = 13;
- uint32_t m = 5;
- uint32_t n = 0xe6546b64;
- uint32_t h = 0;
- uint32_t k = 0;
- uint8_t *d = (uint8_t *) key;
- const uint32_t *chunks = NULL;
- const uint8_t *tail = NULL;
- int i = 0;
- int l = len / 4;
- h = HashSeed;
- chunks = (const uint32_t *) (d + l * 4);
- tail = (const uint8_t *) (d + l * 4);
- for (i = -l; i != 0; ++i) {
- k = chunks[i];
- k *= c1;
- k = (k << r1) | (k >> (32 - r1));
- k *= c2;
- h ^= k;
- h = (h << r2) | (h >> (32 - r2));
- h = h * m + n;
- }
- k = 0;
- switch (len & 3) {
- case 3: k ^= (tail[2] << 16);
- case 2: k ^= (tail[1] << 8);
- case 1:
- k ^= tail[0];
- k *= c1;
- k = (k << r1) | (k >> (32 - r1));
- k *= c2;
- h ^= k;
- }
- h ^= len;
- h ^= (h >> 16);
- h *= 0x85ebca6b;
- h ^= (h >> 13);
- h *= 0xc2b2ae35;
- h ^= (h >> 16);
- return h;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement