home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!dtix!darwin.sura.net!jvnc.net!gmd.de!Germany.EU.net!mcsun!news.funet.fi!funic!nntp.hut.fi!nntp!vkemp
- From: vkemp@niksula.hut.fi (Vesa KEMPpainen)
- Newsgroups: comp.lang.c++
- Subject: j = 8; table[j] != table[8] ???
- Message-ID: <VKEMP.92Aug25214845@phantom.hut.fi>
- Date: 26 Aug 92 01:48:45 GMT
- Sender: usenet@nntp.hut.fi (Usenet pseudouser id)
- Organization: Helsinki University of Technology, Finland
- Lines: 119
- Nntp-Posting-Host: phantom.cs.hut.fi
-
-
- I tried to compile and run this under Borland C++:
-
- // Hash.h
-
- #ifndef _Hash_
- #define _Hash_
- #define HASH_TABLE_SIZE 11
- #define NULL 0
-
- class Hash
- {
- protected:
- void insert(void *p);
- void *find(char *key);
- Hash();
- ~Hash();
- private:
- int hashValue(char *);
- struct Item
- {
- char *key;
- void *data;
- };
- Item *hashTable[HASH_TABLE_SIZE];
- };
- #endif
-
-
- // Hash.C
-
- #include "Hash.h"
- #include <string.h>
- #include <iostream.h>
- #include <process.h>
-
- Hash::Hash()
- {
- for(int i = 0; i < HASH_TABLE_SIZE; i++)
- hashTable[i] = NULL;
- }
-
- int
- Hash::hashValue(char *s)
- {
- unsigned hashnumber;
- for(hashnumber = 0; *s != '\0'; s++)
- hashnumber = *s + 31 * hashnumber;
- return (hashnumber % HASH_TABLE_SIZE);
- }
-
-
- void
- Hash::insert(void *p)
- {
- Item *newItem = new Item;
- newItem = (Item *)p;
- int hashnumber = hashValue(newItem->key);
-
- for (int i=0; i < HASH_TABLE_SIZE; i++)
- {
- if (hashTable[(hashnumber+i)%HASH_TABLE_SIZE] == NULL)
- {
- hashTable[(hashnumber+i)%HASH_TABLE_SIZE] = newItem; //!!!!!!!!
- return;
- }
- }
- cerr << "FATAL ERROR: hashtable full. Exiting ... ";
- exit(1);
- }
-
-
- void *
- Hash::find(char *key)
- {
- int hashnumber = hashValue(key);
-
- for (int i=0; i < HASH_TABLE_SIZE; i++)
- {
- if (hashTable[(hashnumber+i)%HASH_TABLE_SIZE] != NULL)
- {
- if (key == (Item *)hashTable[(hashnumber+i)%HASH_TABLE_SIZE]->key)
- return (Item *)hashTable[(hashnumber+i)%HASH_TABLE_SIZE]->data;
- }
- else return NULL;
- }
- return NULL;
- }
-
-
- Hash::~Hash()
- {
- }
-
-
- This compiles without errors but doesn't run correctly. There seems to be two
- problems:
-
- 1) In Hash::Insert hashTable[hashnumber] != hashTable[8] even though
- int hashnumber == 8.
-
- 2) hashTable[hashnumber] = newItem doesn't do anything. At least not when
- tracing the program and monitoring hashTable[8] and hashTable[hashnumber].
-
- hashTable[8] is always NULL and hashTable[hashnumber] is some address.
-
-
- Any suggestions? I've run out of ideas. (I've read the FAQ I found at garbo.)
-
- Please reply by email vkemp@silver-surfer.cs.hut.fi.
-
- If someone else is interested in the same problem let me know - I'll
- post my replies.
-
-
-
-
- --
- Vesa
-