Advertisement
Guest User

Untitled

a guest
Aug 16th, 2017
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. using std::cerr;
  4. using std::cout;
  5. using std::endl;
  6. #include <string>
  7.  
  8. using std::string;
  9.  
  10. #include <fstream>
  11. using std::ifstream;
  12.  
  13. #include <cstdlib>
  14.  
  15. template <typename HashedObj> //Figure 5.6 from the texts. Type declaration for separate chaining hash table//
  16. class HashTable
  17. {
  18. public:
  19.  
  20. explicit HashTable(int size = 101)
  21. {
  22. (theLists.resize(size) );
  23. currentSize = 0;
  24. }
  25.  
  26. int myhash( const HashedObj & x ) const //Figure 5.7 from the text. Figure 5.7 is the myhash
  27. { // member function for hashtables.
  28. int hashVal = hash ( x );
  29.  
  30. hashVal %= theLists.size ();
  31. if (hashVal < 0)
  32. hashVal += theLists.size();
  33.  
  34. return hashVal;
  35. }
  36.  
  37. void makeEmpty ()
  38. {
  39. for( int i = 0; i < theLists.size( ); i++)
  40. theLists[ i ].clear( );
  41. }
  42.  
  43. bool contains( const HashedObj & x ) const //Figure 5.9 makeEmpty, contains, and remove routines for separate chaining hash table //
  44. {
  45. const list<HashedObj> & whichList = theLists[ myhash(x)];
  46. return find( whichList.begin( ), whichList.end(), x) != whichList.end( );
  47. }
  48.  
  49. bool remove(const HashedObj & x)
  50. {
  51. list<HashedObj> & whichList = theLists[ myhash(x)];
  52. list<HashedObj>::iterator itr = find (whichList.begin( ), whichList.end( ), x );
  53.  
  54. if( itr == whichList.end())
  55. return false;
  56.  
  57. whichList.erase ( itr );
  58. --currentSize;
  59. return true;
  60. }
  61.  
  62. bool insert( const HashedObj & x)
  63. {
  64. list<HashedObj> & whichList = theLists[ myhash( x )];
  65. if( find( whichList.begin( ), whichList.end(), x) != whichList.end( ) )
  66. return false;
  67. whichList.push_back( x );
  68.  
  69. //Rehash//
  70. if( ++currentSize > theLists.size()) // Figure 5.10 insert routine for separate chaining hash table //
  71. rehash( );
  72.  
  73. return true;
  74. }
  75.  
  76. /**
  77. * A hash routine for string objects.
  78. Figure 5.4 from Textbook
  79. */
  80.  
  81. unsigned int hash( const string & key, int tableSize)
  82. {
  83. int hashVal = 0;
  84.  
  85. for( int i = 0; i < key.length( ); i++)
  86. hashVal = 37 * hashVal + key [ i ];
  87.  
  88. hashVal %= tablesize;
  89. if (hashVal < 0)
  90. hashVal += tableSize;
  91.  
  92. return hashVal;
  93. }
  94.  
  95. void HashTable:: printStats()
  96. {
  97. // Compute statistics on hash table //
  98. for (i=0; i<table_size; i++)
  99. {
  100. j = 0;
  101. if ((table[i].next_ptr == NULL) && (table[i].key != 0)) j = 1;
  102. if (table[i].next_ptr != NULL)
  103. {
  104. j++;
  105. ptr = &table[i].key;
  106. do
  107. {
  108. ptr = ptr->next_ptr;
  109. j++;
  110. }
  111. while (ptr->next_ptr != NULL);
  112. }
  113. count[j]++;
  114. }
  115. // Compute mean chain length //
  116. mean_chain_len = 0.0;
  117. for (i=0; i<table_size; i++)
  118. mean_chain_len = mean_chain_len + i * ((double) count[i] / table_size);
  119.  
  120. }
  121. };
  122.  
  123. /* class myClass
  124. {
  125. public:
  126. explicit HashTable(int size);
  127. bool contains(const HashedObj & x ) const;
  128. void makeEmpty();
  129. void insert( const HashedObj & x);
  130. void remove( const HashedObj & x);
  131. void HashTable::printStats();
  132.  
  133. private:
  134. std::vector<int><list<HashedObj> > theLists; // The array of Lists
  135. int currentSize;
  136.  
  137. void rehash();
  138. int myhash ( const HashedObj & x) const;
  139.  
  140. unsigned int hash( const string & key, int tableSize);
  141. };*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement