Advertisement
Guest User

Untitled

a guest
Apr 19th, 2015
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. template <class K, class V>
  2. void SCHashTable<K,V>::remove( K const & key ) {
  3. typename list< pair<K,V> >::iterator it;
  4. /**
  5. * @todo Implement this function.
  6. *
  7. * Please read the note in the lab spec about list iterators and the
  8. * erase() function on std::list!
  9. */
  10.  
  11. int idx = hash(key, size);
  12. for(it = table[idx].begin(); it!= table[idx].end(); it++) {
  13. if(it->first == key) {
  14. table[idx].erase(it);
  15. elems--;
  16. break;
  17. }
  18. }
  19. }
  20.  
  21.  
  22.  
  23. template <class K, class V>
  24. void SCHashTable<K,V>::resizeTable() {
  25. typename list< pair<K,V> >::iterator it;
  26. /**
  27. * @todo Implement this function.
  28. *
  29. * Please read the note in the spec about list iterators!
  30. * The size of the table should be the closest prime to size * 2.
  31. *
  32. * @hint Use findPrime()!
  33. */
  34.  
  35. int origSize = 0;
  36. size_t nsize = findPrime(size*2);
  37. //list< pair<K,V> > * original = table;
  38. std::list< std::pair<K,V> > * temp = new list< pair<K,V> >[nsize];
  39. //table = new list< pair<K,V> >[size];
  40. //elems = 0;
  41.  
  42. //typename list< pair<K,V> >::iterator it;
  43.  
  44. for(int i = 0; i < (int)size; i++) {
  45. for(it = table[i].begin(); it != table[i].end(); it++) {
  46. //insert(it->first, it->second);
  47. origSize = hash(it->first, nsize);
  48. pair<K,V> p(it->first, it->second);
  49. temp[origSize].push_back(p);
  50. }
  51. }
  52.  
  53. delete[] table;
  54. table = temp;
  55. size = nsize;
  56.  
  57. }
  58.  
  59.  
  60.  
  61. template <class K, class V>
  62. void LPHashTable<K,V>::insert( K const & key, V const & value ) {
  63. /**
  64. * @todo Implement this function.
  65. *
  66. * @note Remember to resize the table when necessary (load factor >=
  67. * 0.7). **Do this check *after* increasing elems!!** Also, don't
  68. * forget to mark the cell for probing with should_probe!
  69. */
  70.  
  71. if((double)elems/size >= .7) {
  72. resizeTable();
  73. }
  74.  
  75. int idx = hash(key, size);
  76. table[idx] = new pair<K,V>(key, value);
  77. should_probe[idx] = true;
  78. elems++;
  79.  
  80. }
  81.  
  82.  
  83. template <template <class K, class V> class Dict>
  84. vector< pair<string, int> > WordFreq<Dict>::getWords( int threshold ) const {
  85. TextFile infile( filename );
  86. vector< pair<string, int> > ret;
  87. /**
  88. * @todo Implement this function.
  89. * @see char_counter.cpp if you're having trouble.
  90. */
  91.  
  92. Dict<string, int> hashtable(100000);
  93. while(infile.good())
  94. {
  95. string word = infile.getNextWord();
  96. hashtable[word]++;
  97. }
  98.  
  99. typename Dict<string, int>::iterator it;
  100. for(it = hashtable.begin(); it != hashtable.end(); it++)
  101. {
  102. if(it->second >= threshold)
  103. ret.push_back(*it);
  104. }
  105.  
  106. return ret;
  107. }
  108.  
  109.  
  110. template <template <class K, class V> class Dict>
  111. bool AnagramFinder<Dict>::checkWord( const string & word, const string & test ) {
  112.  
  113. if (word.length() != test.length())
  114. {
  115. return false;
  116. }
  117.  
  118. Dict<char, int> wordHash(256);
  119.  
  120. for(int i = 0; i < (int) word.length(); i++)
  121. {
  122. wordHash[word[i]]++;
  123. }
  124.  
  125. Dict<char, int> testHash(256);
  126.  
  127. for(int i = 0; i < (int) test.length(); i++)
  128. {
  129. testHash[test[i]]++;
  130. }
  131.  
  132. for(int i = 0; i < 256; i++)
  133. {
  134. if(testHash[i] != wordHash[i])
  135. return false;
  136. }
  137.  
  138. return true;
  139. }
  140.  
  141.  
  142. LogfileParser::LogfileParser( const string & fname ) : whenVisitedTable( 256 ) {
  143. SCHashTable< string, bool > pageVisitedTable( 256 );
  144. ifstream infile( fname.c_str() );
  145. string line;
  146. while( infile.good() ) {
  147. getline( infile, line );
  148.  
  149. // if the line length is 0, move on to the next loop iteration
  150. if( line.length() == 0 )
  151. continue;
  152.  
  153. // otherwise parse the line and update the hash tables and vector
  154. LogLine ll( line );
  155. /**
  156. * @todo Finish implementing this function.
  157. *
  158. * Given the LogLine above, you should be able to update the member
  159. * variable hash table and any other hash tables necessary to solve
  160. * this problem. This should also build the uniqueURLs member
  161. * vector as well.
  162. */
  163.  
  164. string k = ll.customer + ll.url;
  165. if (whenVisitedTable[k] < ll.date)
  166. whenVisitedTable[k] = ll.date;
  167.  
  168. if(!pageVisitedTable[ll.url]) {
  169. pageVisitedTable[ll.url] = true;
  170. uniqueURLs.push_back(ll.url);
  171. }
  172. }
  173. infile.close();
  174. }
  175.  
  176.  
  177. bool LogfileParser::hasVisited( const string & customer, const string & url ) const {
  178. /**
  179. * @todo Implement this function.
  180. */
  181.  
  182. return whenVisitedTable.keyExists(customer+url);
  183. }
  184.  
  185.  
  186. time_t LogfileParser::dateVisited( const string & customer, const string & url ) const {
  187. /**
  188. * @todo Implement this function.
  189. */
  190. if(!hasVisited(customer, url))
  191. return time_t();
  192. return whenVisitedTable.find(customer+url);
  193.  
  194. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement