home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 12867 < prev    next >
Encoding:
Text File  |  1992-08-25  |  2.6 KB  |  131 lines

  1. Path: sparky!uunet!dtix!darwin.sura.net!jvnc.net!gmd.de!Germany.EU.net!mcsun!news.funet.fi!funic!nntp.hut.fi!nntp!vkemp
  2. From: vkemp@niksula.hut.fi (Vesa KEMPpainen)
  3. Newsgroups: comp.lang.c++
  4. Subject: j = 8; table[j] != table[8] ???
  5. Message-ID: <VKEMP.92Aug25214845@phantom.hut.fi>
  6. Date: 26 Aug 92 01:48:45 GMT
  7. Sender: usenet@nntp.hut.fi (Usenet pseudouser id)
  8. Organization: Helsinki University of Technology, Finland
  9. Lines: 119
  10. Nntp-Posting-Host: phantom.cs.hut.fi
  11.  
  12.  
  13. I tried to compile and run this under Borland C++:
  14.  
  15. // Hash.h
  16.  
  17. #ifndef _Hash_
  18. #define _Hash_
  19. #define HASH_TABLE_SIZE 11
  20. #define NULL 0
  21.  
  22. class Hash
  23. {
  24.  protected:
  25.   void insert(void *p);
  26.   void *find(char *key);
  27.   Hash();
  28.   ~Hash();
  29.  private:
  30.   int hashValue(char *);
  31.   struct Item
  32.     {
  33.       char *key;
  34.       void *data;
  35.     };
  36.   Item *hashTable[HASH_TABLE_SIZE];
  37. };
  38. #endif
  39.  
  40.  
  41. // Hash.C
  42.  
  43. #include "Hash.h"
  44. #include <string.h>
  45. #include <iostream.h>
  46. #include <process.h>
  47.  
  48. Hash::Hash()
  49. {
  50.   for(int i = 0; i < HASH_TABLE_SIZE; i++)
  51.     hashTable[i] = NULL;
  52. }
  53.  
  54. int
  55.  Hash::hashValue(char *s)
  56. {
  57.   unsigned hashnumber;
  58.   for(hashnumber = 0; *s != '\0'; s++)
  59.     hashnumber = *s + 31 * hashnumber;
  60.   return (hashnumber % HASH_TABLE_SIZE);
  61. }
  62.  
  63.  
  64. void
  65.  Hash::insert(void *p)
  66. {
  67.   Item *newItem = new Item;
  68.   newItem = (Item *)p;
  69.   int hashnumber = hashValue(newItem->key);
  70.   
  71.   for (int i=0; i < HASH_TABLE_SIZE; i++)
  72.     {
  73.       if (hashTable[(hashnumber+i)%HASH_TABLE_SIZE] == NULL)
  74.     {
  75.       hashTable[(hashnumber+i)%HASH_TABLE_SIZE] = newItem; //!!!!!!!!
  76.       return;
  77.     }
  78.     }
  79.   cerr << "FATAL ERROR: hashtable full. Exiting ... ";
  80.   exit(1);
  81. }
  82.  
  83.  
  84. void *
  85.  Hash::find(char *key)
  86. {
  87.   int hashnumber = hashValue(key);
  88.   
  89.   for (int i=0; i < HASH_TABLE_SIZE; i++)
  90.     {
  91.       if (hashTable[(hashnumber+i)%HASH_TABLE_SIZE] != NULL)
  92.     {
  93.       if (key == (Item *)hashTable[(hashnumber+i)%HASH_TABLE_SIZE]->key)
  94.         return (Item *)hashTable[(hashnumber+i)%HASH_TABLE_SIZE]->data;
  95.     }
  96.       else return NULL;
  97.     }
  98.   return NULL;
  99. }
  100.  
  101.  
  102.  Hash::~Hash()
  103. {
  104. }
  105.  
  106.  
  107. This compiles without errors but doesn't run correctly. There seems to be two
  108. problems:
  109.  
  110. 1) In Hash::Insert hashTable[hashnumber] != hashTable[8] even though 
  111. int hashnumber == 8.
  112.  
  113. 2) hashTable[hashnumber] = newItem doesn't do anything. At least not when 
  114. tracing the program and monitoring hashTable[8] and hashTable[hashnumber].
  115.  
  116. hashTable[8] is always NULL and hashTable[hashnumber] is some address.
  117.  
  118.  
  119. Any suggestions? I've run out of ideas. (I've read the FAQ I found at garbo.)
  120.  
  121. Please reply by email vkemp@silver-surfer.cs.hut.fi.
  122.  
  123. If someone else is interested in the same problem let me know - I'll
  124. post my replies.
  125.  
  126.  
  127.  
  128.  
  129. --
  130. Vesa
  131.