hashing size problem

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Mar 2005
Posts: 3
Reputation: ch1ck3n is an unknown quantity at this point 
Solved Threads: 0
ch1ck3n ch1ck3n is offline Offline
Newbie Poster

hashing size problem

 
0
  #1
Mar 21st, 2007
My code is below. I'm pretty sure my issues are spanning my my .resize(). It's possible I'm doing my hashing completely wrong too, but it was right at one point. This code wont run at all right now, but it compiles. Is there a better way to go about sizing my container?

  1. #include <iostream>
  2. #include <string>
  3. #include <fstream>
  4. #include <vector>
  5. #include <list>
  6. using namespace std;
  7.  
  8. typedef vector<list<string> > hash_set;
  9.  
  10. void hashInsert(hash_set &vtr, string word);
  11. int getHash(string word);
  12.  
  13. int main()
  14. {
  15. int size = 0;
  16. string word;
  17.  
  18. hash_set vtr;
  19.  
  20. ifstream firstfile ("doc1.txt");
  21. if (firstfile)
  22. {
  23. while ( firstfile >> word )
  24. {
  25. size++;
  26. vtr.resize(2*size);
  27. // hashInsert(vtr, word);
  28. }
  29. firstfile.close();
  30. }
  31. else cout << "Unable to open file";
  32.  
  33. ifstream samefile ("doc1.txt");
  34. if (samefile)
  35. {
  36. while ( samefile >> word )
  37. {
  38. hashInsert(vtr, word);
  39. }
  40. samefile.close();
  41. }
  42. else cout << "Unable to open file";
  43.  
  44.  
  45. for (size_t vtrIndex=0; vtrIndex < vtr.size(); ++vtrIndex)
  46. {
  47. for (list<string>::iterator it = vtr[vtrIndex].begin(); it != vtr[vtrIndex].end(); ++it)
  48. {
  49. cout << *it << " " << vtrIndex << '\n';
  50. }
  51. }
  52.  
  53. return 0;
  54. }
  55.  
  56.  
  57.  
  58. void hashInsert(hash_set &vtr, string word)
  59. {
  60. vtr[getHash(word)].push_back(word);
  61. }
  62.  
  63. int getHash(string word)
  64. {
  65. int output;
  66. int i;
  67.  
  68. output = word[word.size()-1];
  69.  
  70. for (i = (word.size()-2); i >= 0; i--)
  71. {
  72. output = (128*output + word[i]) % 127;
  73. }
  74.  
  75. return output;
  76. }
Reply With Quote Quick reply to this message  
Join Date: Feb 2007
Posts: 539
Reputation: thekashyap will become famous soon enough thekashyap will become famous soon enough 
Solved Threads: 50
thekashyap's Avatar
thekashyap thekashyap is offline Offline
Posting Pro

Re: hashing size problem

 
0
  #2
Mar 22nd, 2007
Something more than "This code wont run at all right now" would be helpful.
I don't see why you would want to keep resizeing your vector. It's simply not needed to start with. I would suggest just comment out that code, let vector resize by itself, later when things work you can optimize.
Anyway I don't think there is any issue with resize(). Also given the way you're storing teh data (vector<list<..>> ) I don't see any problems that your hash function can cause.
-------------
O now I saw your hashing code more closely. Well, given that you have done this:
output = (128*output + word[i]) % 127;
It's always ensured that getHash() returns something between 0-127. So line 20 to 31 in your original code can be replaced by vtr.resize(128) ;
Last edited by thekashyap; Mar 22nd, 2007 at 1:10 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 3
Reputation: ch1ck3n is an unknown quantity at this point 
Solved Threads: 0
ch1ck3n ch1ck3n is offline Offline
Newbie Poster

Re: hashing size problem

 
0
  #3
Mar 22nd, 2007
Thanks for the response, I figured out what i was doing with the size. I ended up making it the size of the input file (how many words it contained). After i got my other functions working properly, it worked like a charm. Thanks again.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC