Recent Posts
PHP | 10 sec ago
JavaScript | 28 sec ago
None | 29 sec ago
Java | 32 sec ago
None | 34 sec ago
None | 56 sec ago
None | 1 min ago
None | 1 min ago
None | 1 min ago
None | 1 min ago
Sitereport
Find cool info about any domain on the internet?
visit sitereport
Free Subdomains
Want a pastebin.com sub-domain for your community?
learn more...
What is pastebin?
Pastebin is a website that hosts all your text & code on dedicated servers for easy sharing.
learn more...
Learn a little bit about the new Pastebin.com on our help page. hide message
By Anonymous on the 9th of Feb 2010 08:29:42 PM Download | Raw | Embed | Report
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. unsigned int hashf(const char *buf, size_t len) {
  6.         unsigned int h = 0;
  7.         size_t i;
  8.         for (i = 0; i < len; i++) {
  9.                 h += buf[i];
  10.                 h += h << 10;
  11.                 h ^= h >> 7;
  12.         }
  13.         h += h << 3;
  14.         h ^= h >> 11;
  15.         h += h << 15;
  16.         return h;
  17. }
  18.  
  19. int isFull(char *table, size_t size) {
  20.         int i;
  21.         for (i = 0; i < size; i++) {
  22.                 if (&table[i] == NULL) {
  23.                         return 0;
  24.                 }
  25.         }
  26.         return 1;
  27. }
  28.  
  29. int main(int argc, char *argv[]) {
  30.         if (argc != 3) return EXIT_FAILURE;
  31.         int size = atoi(argv[2]);
  32.         char *hashtable[size];
  33.         int i;
  34.         for (i = 0; i < size; i++) {
  35.                 hashtable[i] = NULL;
  36.         }
  37.         FILE *file = fopen(argv[1], "r");
  38.         if (file == NULL) {
  39.                 printf("ERROR OPENING FILE!!!\n");
  40.                 return EXIT_FAILURE;
  41.         }
  42.         while (!feof(file)) {
  43.                 char word[30];
  44.                 fscanf(file, "%s", word);
  45.                 if(isFull(*hashtable, size)) {
  46.                         printf("Error: table full\n");
  47.                         return EXIT_FAILURE;
  48.                 }
  49.                 unsigned int h = hashf(word, strlen(word)) % size;
  50.                 i = 1;
  51.                 while (hashtable[h] != NULL) {
  52.                         h = (hashf(word, strlen(word)) + i*i) % size;
  53.                         i++;
  54.                 }
  55.                 hashtable[h] = word;
  56.                 printf("laitettu %s paikkaan %d \n", hashtable[h], h);
  57.         }
  58.         fclose(file);
  59.  
  60.         //Tulostetaan hashi
  61.         for (i = 0; i < size; i++) {
  62.                 if (i == size - 1) {
  63.                         if (hashtable[i] == NULL) {
  64.                                 printf(" \n");
  65.                         } else {
  66.                                 printf("%s\n", hashtable[i]);
  67.                         }
  68.                 } else {
  69.                         if (hashtable[i] == NULL) {
  70.                                 printf(" ,");
  71.                         } else {
  72.                                 printf("%s,", hashtable[i]);
  73.                         }
  74.                 }
  75.         }
  76.         return EXIT_SUCCESS;
  77. }
Submit a correction or amendment below. Make A New Post
To highlight particular lines, prefix each line with @h@
Syntax highlighting:
Post expiration:
Post exposure:
Name / Title:
Email: