Having trouble with hash table

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Jul 2009
Posts: 1
Reputation: kokotsu is an unknown quantity at this point 
Solved Threads: 0
kokotsu's Avatar
kokotsu kokotsu is offline Offline
Newbie Poster

Having trouble with hash table

 
0
  #1
Jul 20th, 2009
Hello, I was trying to create a Coalesced hash table by self teaching myself using various sources. I fixed most of the errors, but when i compile on either my visual C++ compiler or Dev -C++ both saids there is a undefine linker reference for the hash members in my Main.

  1. #include <iostream>
  2. using namespace std;
  3.  
  4. #include "Hash_file.h"
  5.  
  6.  
  7.  
  8. int main(void)
  9. {
  10. Hash_Map<int> Numbers(1);
  11. Numbers.insert("one", 1);
  12.  
  13. cout<<Numbers.find("one");
  14.  
  15.  
  16. cin.get();
  17. return 0;
  18. }


I thought it had something to do with some error either in my header file or source file.. but im not sure now

  1. #pragma once
  2. #ifndef HASH_MAP
  3. #define HASH_MAP
  4. #include <string>
  5.  
  6.  
  7.  
  8.  
  9. typedef struct Entry_t
  10. {
  11. void * Object;
  12. Entry_t * next;
  13. std::string Key;
  14. int position;
  15. }Entry;
  16.  
  17.  
  18.  
  19. template<class Map>
  20. class Hash_Map
  21. {
  22.  
  23.  
  24. private :
  25. Entry **Primary;
  26. Entry **Secondary;
  27. int Num;
  28. size_t secondary_size;
  29.  
  30.  
  31. public:
  32. Hash_Map(const Hash_Map & Map_secondary);
  33. // the default SuperFastHash comes from the included header file
  34. Hash_Map( int num );
  35.  
  36. int insert(std::string key, Map map);
  37.  
  38. int deletion(std::string key);
  39.  
  40. bool isFull();
  41.  
  42. Map * find(std::string key);
  43.  
  44. int Secondaryinsert(std::string key, Map & map);
  45.  
  46. int Swap_Hash(int leftoff);
  47.  
  48.  
  49.  
  50.  
  51. };
  52.  
  53. #endif

  1. #include "Hash_file.h"
  2. #include <cstring>
  3.  
  4.  
  5. //using Coalesced_hashing
  6.  
  7. //also will use incrementing for Resize
  8.  
  9.  
  10.  
  11. template< class Map>
  12. Hash_Map<Map>::Hash_Map( const Hash_Map & Map_secondary)
  13. {
  14. Primary = Map_secondary.Primary;
  15.  
  16. Secondary = Map_secondary.Secondary;
  17.  
  18. Num = Map_secondary.Num;
  19. }
  20.  
  21. template< class Map>
  22. Hash_Map<Map>::Hash_Map( int num)
  23. {
  24. Num = num;
  25.  
  26. Primary = new Entry[num];
  27. int i= 0;
  28. while( i < Num)
  29. {
  30. Primary[Num] = NULL;
  31. }
  32. Secondary = NULL;
  33.  
  34. }
  35.  
  36.  
  37.  
  38. template< class Map>
  39. int Hash_Map<Map>::insert(std::string key, Map map)
  40. {
  41. unsigned h = SuperFastHash(key.c_str(), strlen(key.c_str())) % Num;
  42.  
  43. if(Primary[h] == NULL)
  44. {
  45. Primary[h] = new Entry;
  46. Primary[h]->Object = map;
  47. Primary[h]->Key = key;
  48. Primary[h]->position = h;
  49. }
  50. else
  51. {
  52. Entry * it;
  53. int cursor = 0;
  54.  
  55. while(cursor < Num && Primary[cursor] != NULL)
  56. ++cursor;
  57. if( cursor == Num)
  58. return -1;
  59.  
  60. Primary[cursor] = new Entry;
  61. Primary[cursor]->Object = map;
  62. Primary[cursor]->Key = key;
  63. Primary[cursor]->position = cursor;
  64. it = Primary[h];
  65.  
  66. while(it->next != NULL)
  67. it = it->next;
  68.  
  69. it->next = Primary[cursor];
  70.  
  71. }
  72. return 0;
  73.  
  74. }
  75.  
  76. template< class Map>
  77. Map * Hash_Map<Map>::find(std::string key)
  78. {
  79. unsigned h = SuperFastHash(key.c_str(),strlen(key.c_str())) % Num;
  80.  
  81. if ( Primary[h] != NULL ) {
  82. Entry *it;
  83.  
  84. /* Search the chain at index h */
  85. for ( it = Primary[h]; it != NULL; it = it->next ) {
  86. if ( strcmp ( key.c_str(), it->Key.c_str() ) == 0 )
  87. return (Map)it->Object;
  88. }
  89. }
  90. return NULL;
  91.  
  92. }
  93.  
  94.  
  95. template<class Map>
  96. int Hash_Map<Map>::deletion(std::string key)
  97. {
  98. unsigned h = SuperFastHash(key.c_str(),strlen(key.c_str())) % Num;
  99.  
  100. if(Primary[h] != NULL)
  101. {
  102.  
  103.  
  104. if( strcmp ( key.c_str(), Primary[h]->Key.c_str() ) == 0)
  105. {
  106. delete Primary[h];
  107. Primary[h] = NULL;
  108. }
  109. else
  110. {
  111. Entry * tempt1 = Primary[h]->next;
  112. Entry * tempt2 = Primary[h];
  113. while( tempt1 != NULL ) {
  114. if ( strcmp ( key.c_str(), tempt1->Key.c_str() ) == 0 )
  115. break;
  116. tempt1 = tempt1->next;
  117. tempt2 = tempt2->next;
  118. }
  119.  
  120. tempt2->next = tempt1->next;
  121.  
  122. unsigned int P = tempt1->position;
  123.  
  124. delete Primary[P];
  125. Primary[P] = NULL;
  126. }
  127. return 0;
  128. }
  129.  
  130. return -1;
  131.  
  132. }
  133.  
  134.  
  135.  
  136.  
  137. template< class Map>
  138. bool Hash_Map<Map>:: isFull()
  139. {
  140. int cursor =0;
  141. while(cursor < Num && Primary[cursor] != NULL)
  142. ++cursor;
  143. if(cursor == Num )
  144. return true;
  145.  
  146.  
  147. return false;
  148.  
  149. }
  150.  
  151. template< class Map>
  152. int Hash_Map<Map>::Secondaryinsert(std::string key, Map & map)
  153. {
  154.  
  155. if(Secondary == NULL)
  156. {
  157.  
  158. secondary_size = Num * 1.5;
  159.  
  160. Secondary = new Entry[secondary_size];
  161. int i;
  162. while( i < secondary_size )
  163. {
  164. Secondary[ i ] = NULL; ++i;
  165. }
  166.  
  167. }
  168.  
  169. unsigned h = SuperFastHash(key.c_str(),strlen(key.c_str())) %secondary_size;
  170.  
  171.  
  172. if(Secondary[h] == NULL)
  173. { Secondary[ h ] = new Entry;
  174. Secondary[h]->Object = map;
  175. Secondary[h]->Key = key;
  176. Secondary[ h ]->position = h;
  177. }
  178. else
  179. {
  180. Entry * it;
  181. int cursor = 0;
  182.  
  183. while(cursor < secondary_size&& Secondary[cursor] != NULL)
  184. ++cursor;
  185. if( cursor == secondary_size)
  186. return -1;
  187.  
  188. Secondary[cursor] = new Entry;
  189. Secondary[cursor]->Object = map;
  190. Secondary[cursor]->Key = key;
  191. Secondary[cursor]->position = cursor;
  192. it = Secondary[h];
  193.  
  194. while(it->next != NULL)
  195. it = it->next;
  196.  
  197. it->next = Secondary[cursor];
  198. }
  199.  
  200. return 0;
  201. }
  202. template< class Map>
  203. int Hash_Map<Map>::Swap_Hash(int leftoff)
  204. {
  205. int Num_of_buckets_transfer = Num * .40;
  206.  
  207. int i;
  208. int j;
  209. if( leftoff != 0 )
  210. for( i = leftoff, j = 0; j < Num_of_buckets_transfer; --i, j++)
  211. {
  212. unsigned int h;
  213. h = SuperFastHash(Primary[i]->Key.c_str(),strlen(Primary[i]->Key.c_str())) % secondary_size;
  214. if(Secondary[h] == NULL)
  215. {
  216. Secondary[h] = new Entry;
  217. Secondary[h]->Key = Primary[i]->Key;
  218. Secondary[h]->Object = Primary[i]->Object;
  219. Secondary[ h ]->position = h;
  220. }
  221. else
  222. {
  223. Entry * it;
  224. int cursor = 0;
  225.  
  226. while(cursor < secondary_size&& Secondary[cursor] != NULL)
  227. ++cursor;
  228. if( cursor == secondary_size)
  229. return -1;
  230.  
  231. Secondary[cursor] = new Entry;
  232. Secondary[cursor]->Key = Primary[i]->Key;
  233. Secondary[cursor]->Object = Primary[i]->Object;
  234. Secondary[cursor]->position = cursor;
  235. it = Secondary[h];
  236.  
  237. while(it->next != NULL)
  238. it = it->next;
  239.  
  240. it->next = Secondary[cursor];
  241. }
  242. }
  243.  
  244. return leftoff;
  245. }


I tried to rebuild, and i made sure all the files were linked properly...
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,613
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 469
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Having trouble with hash table

 
0
  #2
Jul 21st, 2009
kokotsu,
You have a problem with templates. Read,
How can I avoid linker errors with my template classes?
Failure is not fatal, but failure to change might be. - John Wooden
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC