Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- template <class K, class V>
- void SCHashTable<K,V>::remove( K const & key ) {
- typename list< pair<K,V> >::iterator it;
- /**
- * @todo Implement this function.
- *
- * Please read the note in the lab spec about list iterators and the
- * erase() function on std::list!
- */
- int idx = hash(key, size);
- for(it = table[idx].begin(); it!= table[idx].end(); it++) {
- if(it->first == key) {
- table[idx].erase(it);
- elems--;
- break;
- }
- }
- }
- template <class K, class V>
- void SCHashTable<K,V>::resizeTable() {
- typename list< pair<K,V> >::iterator it;
- /**
- * @todo Implement this function.
- *
- * Please read the note in the spec about list iterators!
- * The size of the table should be the closest prime to size * 2.
- *
- * @hint Use findPrime()!
- */
- int origSize = 0;
- size_t nsize = findPrime(size*2);
- //list< pair<K,V> > * original = table;
- std::list< std::pair<K,V> > * temp = new list< pair<K,V> >[nsize];
- //table = new list< pair<K,V> >[size];
- //elems = 0;
- //typename list< pair<K,V> >::iterator it;
- for(int i = 0; i < (int)size; i++) {
- for(it = table[i].begin(); it != table[i].end(); it++) {
- //insert(it->first, it->second);
- origSize = hash(it->first, nsize);
- pair<K,V> p(it->first, it->second);
- temp[origSize].push_back(p);
- }
- }
- delete[] table;
- table = temp;
- size = nsize;
- }
- template <class K, class V>
- void LPHashTable<K,V>::insert( K const & key, V const & value ) {
- /**
- * @todo Implement this function.
- *
- * @note Remember to resize the table when necessary (load factor >=
- * 0.7). **Do this check *after* increasing elems!!** Also, don't
- * forget to mark the cell for probing with should_probe!
- */
- if((double)elems/size >= .7) {
- resizeTable();
- }
- int idx = hash(key, size);
- table[idx] = new pair<K,V>(key, value);
- should_probe[idx] = true;
- elems++;
- }
- template <template <class K, class V> class Dict>
- vector< pair<string, int> > WordFreq<Dict>::getWords( int threshold ) const {
- TextFile infile( filename );
- vector< pair<string, int> > ret;
- /**
- * @todo Implement this function.
- * @see char_counter.cpp if you're having trouble.
- */
- Dict<string, int> hashtable(100000);
- while(infile.good())
- {
- string word = infile.getNextWord();
- hashtable[word]++;
- }
- typename Dict<string, int>::iterator it;
- for(it = hashtable.begin(); it != hashtable.end(); it++)
- {
- if(it->second >= threshold)
- ret.push_back(*it);
- }
- return ret;
- }
- template <template <class K, class V> class Dict>
- bool AnagramFinder<Dict>::checkWord( const string & word, const string & test ) {
- if (word.length() != test.length())
- {
- return false;
- }
- Dict<char, int> wordHash(256);
- for(int i = 0; i < (int) word.length(); i++)
- {
- wordHash[word[i]]++;
- }
- Dict<char, int> testHash(256);
- for(int i = 0; i < (int) test.length(); i++)
- {
- testHash[test[i]]++;
- }
- for(int i = 0; i < 256; i++)
- {
- if(testHash[i] != wordHash[i])
- return false;
- }
- return true;
- }
- LogfileParser::LogfileParser( const string & fname ) : whenVisitedTable( 256 ) {
- SCHashTable< string, bool > pageVisitedTable( 256 );
- ifstream infile( fname.c_str() );
- string line;
- while( infile.good() ) {
- getline( infile, line );
- // if the line length is 0, move on to the next loop iteration
- if( line.length() == 0 )
- continue;
- // otherwise parse the line and update the hash tables and vector
- LogLine ll( line );
- /**
- * @todo Finish implementing this function.
- *
- * Given the LogLine above, you should be able to update the member
- * variable hash table and any other hash tables necessary to solve
- * this problem. This should also build the uniqueURLs member
- * vector as well.
- */
- string k = ll.customer + ll.url;
- if (whenVisitedTable[k] < ll.date)
- whenVisitedTable[k] = ll.date;
- if(!pageVisitedTable[ll.url]) {
- pageVisitedTable[ll.url] = true;
- uniqueURLs.push_back(ll.url);
- }
- }
- infile.close();
- }
- bool LogfileParser::hasVisited( const string & customer, const string & url ) const {
- /**
- * @todo Implement this function.
- */
- return whenVisitedTable.keyExists(customer+url);
- }
- time_t LogfileParser::dateVisited( const string & customer, const string & url ) const {
- /**
- * @todo Implement this function.
- */
- if(!hasVisited(customer, url))
- return time_t();
- return whenVisitedTable.find(customer+url);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement