TEST(HashTableTest, expandKey) { HashTable<std::string,std::string> ht; char buf[10]; std::string c; for(int i = 0; i < 100; i ++) { sprintf(buf,"a%d",i); //ht.add(buf, "ab"); ht[buf] = "ab"; ht[buf] = "ac"; c = ht[buf]; std::cout << "buf:" << buf << ",value:" << ht[buf] << ",c:" << c << std::endl; } //std::cout << "11:" << ht["11"]<< std::endl; EXPECT_EQ(100UL,ht.count()); ht.remove("a8"); EXPECT_EQ(99UL,ht.count()); //EXPECT_TRUE(ht["a13"] == "ac"); EXPECT_TRUE(ht.containsKey("a13")); EXPECT_TRUE(ht.contains("a13","ac")); EXPECT_FALSE(ht.containsKey("a1012")); EXPECT_FALSE(ht.contains("a1123","ac")); EXPECT_FALSE(ht.contains("a12","ab")); for(int i = 0; i < 1000; i ++) { sprintf(buf,"a%d",i); //ht.add(buf, "ab"); ht[buf] = "ab"; ht[buf] = "ac"; c = ht[buf]; std::cout << "buf:" << buf << ",value:" << ht[buf] << ",c:" << c << std::endl; } ht.remove("a18"); EXPECT_EQ(999UL,ht.count()); //EXPECT_TRUE(ht["a13"] == "ac"); EXPECT_TRUE(ht.containsKey("a13")); EXPECT_TRUE(ht.contains("a13","ac")); EXPECT_TRUE(ht.containsKey("a112")); EXPECT_FALSE(ht.contains("a1203","ac")); }
int main() { HashTable table = HashTable(); string a = "alpha"; string b = "beta"; cout << a.compare(b) << endl; cout << table.insert("omega") << endl; bool contain = table.contains("omega"); cout << contain << endl; table.insert("text"); table.insert("them"); contain = table.contains(reverseString("txet")); cout << contain << endl; return 0; }
void NodeController :: tryHash() { HashTable<int> tempTable; HashNode<int> tempArray[10]; for(int spot = 0; spot < 10; spot++) { int randomValue = rand(); int randomKey = rand(); HashNode<int> temp = HashNode<int>(randomKey, randomValue); tempTable.add(temp); tempArray[spot] = temp; } bool test = tempTable.contains(tempArray[3]); string result; if(test) { result = "It's there"; } else { result = "Not anywhere"; } cout << result << endl; }
TEST(HashTableTest, IntKey) { HashTable<uint,uint,IntHash> ht; EXPECT_EQ(0UL,ht.count()); EXPECT_FALSE(ht.containsKey(1)); ht.add(1,1); EXPECT_EQ(1UL,ht.count()); EXPECT_TRUE(ht.containsKey(1)); EXPECT_TRUE(ht.contains(1,1)); ht[1] = 0; EXPECT_TRUE(ht[1] == 0U); ht.remove(1); EXPECT_EQ(0UL,ht.count()); }
int main( int argc, char *argv[] ) { string dicType = argv[1]; string hashType = argv[2]; string filename = argv[3]; HashTable<string> smallChain; HashTable<string> largeChain; HashTable2<string> smallProbe; HashTable2<string> largeProbe; int collission = 0; list<int> l; list<int> l2; ifstream myfile("small.txt"); string line; while(getline(myfile, line)) { smallChain.insert(line); } myfile.close(); ifstream myfile2("small.txt"); while(getline(myfile2, line)) { smallProbe.insert(line); collission = smallProbe.countC(line); l.push_back(collission); } myfile2.close(); ifstream myfile3("large.txt"); while(getline(myfile3, line)) { largeChain.insert(line); } myfile3.close(); ifstream myfile4("large.txt"); while(getline(myfile4, line)) { largeProbe.insert(line); collission = largeProbe.countC(line); l2.push_back(collission); } myfile4.close(); string outputFilename = filename; string outputFilename2 = filename; string outputFilename3 = filename; if(filename.find('.') != string::npos) { int index = filename.find('.'); outputFilename.erase(index, outputFilename.size() - 1); outputFilename2.erase(index, outputFilename2.size() - 1); outputFilename3.erase(index, outputFilename3.size() - 1); } outputFilename += ".out"; outputFilename2 += ".stats"; outputFilename3 += "2.txt"; ifstream myfile5(filename.c_str()); ofstream outfile(outputFilename.c_str()); ofstream outfile2(outputFilename2.c_str()); ofstream outfile3(outputFilename3.c_str()); while(getline(myfile5, line)) { istringstream ss(line); char value; while (ss >> value) { if ((value >= 97 && value <= 122) || (value >= 65 && value <= 90)) { value = tolower(value); } else { value = ' '; } outfile3 << value; if (ss.peek() == ' ') { outfile3 << ' '; } } outfile3 << endl; } myfile5.close(); ifstream myfile6(outputFilename3.c_str()); //outfile3 is the processed text file if (dicType == "-1") { if (hashType == "-chain") { while(getline(myfile6, line)) { istringstream ss(line); string value; while (ss >> value) { if (smallChain.contains(value) == false) { outfile << value << endl; } } } vector<int> a(50, 0); a = smallChain.countLists(a); for (int i = 0; i < a.size(); i++) { if (a[i] != 0) { outfile2 << i << " " << a[i] << endl; } } } else if (hashType == "-probe") { while(getline(myfile6, line)) { istringstream ss(line); string value; while (ss >> value) { if (smallProbe.contains(value) == false) { outfile << value << endl; } } } l.sort(); list<int>::iterator itr = l.begin(); int biggest = l.back(); vector<int> v; v.resize(biggest + 1); for (int i = 0; i < l.size(); i++) { v[*itr]++; itr++; } for (int i = 0; i < v.size(); i++) { outfile2 << i << " " << v[i] << endl; } } else {
int main() { string dict_name, txt_name, out_name, line, word, wrd2chk; ifstream dict_file, txt_file; ofstream out_file; HashTable table; int line_cnt = 0; int i, len, line_len; bool nochk_flag = false; bool mentioned = false; //read in the dictionary cout << "Enter name of dictionary: "; cin >> dict_name; dict_file.open(dict_name.c_str(),ios::in); if(!(dict_file.is_open())) { cerr << "Error opening dictionary file\n" << endl; exit(1); } clock_t startTime = clock(); while(getline(dict_file,word)) { table.insert(word); } cout <<"Total time (in seconds) to load dictionary: " << double(clock()-startTime)/(double)CLOCKS_PER_SEC<< endl; //open the input file cout << "Enter name of input file: "; cin >> txt_name; txt_file.open(txt_name.c_str(),ios::in); if(!(txt_file.is_open())) { cerr << "Error opening input file\n" << endl; exit(1); } //open or create the outfile cout << "Enter name of output file: "; cin >> out_name; out_file.open(out_name.c_str(),ios::out); if(!(out_file.is_open())) { cerr << "Error opening/creating output file\n" << endl; exit(1); } startTime = clock(); //start moving through the file while(getline(txt_file,line)) { line_cnt++; i = 0; //reset to first character of each line; wrd2chk.clear(); len = 0; line_len = line.length(); if(line_len == 0) { continue; } //while not at the end of the line, keep taking words while(i <= line_len) { if(line[i] >= '0' && line[i] <= '9' || line[i] >= 'A' && line[i] <= 'Z' || line[i] >= 'a' && line[i] <= 'z' || line[i] == '-' || line[i] == 39) { if(line[i] >= '0' && line[i] <= '9') { nochk_flag = true; } len++; if(len <= 20) { wrd2chk += line[i]; } else { if(mentioned == false) { transform(wrd2chk.begin(),wrd2chk.end(),wrd2chk.begin(),::tolower); out_file << "Long word at line " << line_cnt << ", starts: " << wrd2chk << endl; mentioned = true; } nochk_flag = true; } i++; } else { //invalid character found if(nochk_flag) { wrd2chk.clear(); len = 0; nochk_flag = false; mentioned = false; i++; } else { if(table.contains(wrd2chk)) { wrd2chk.clear(); len = 0; i++; } else if(wrd2chk.length()) { transform(wrd2chk.begin(),wrd2chk.end(),wrd2chk.begin(),::tolower); out_file << "Unknown word at line " << line_cnt << ": " << wrd2chk << endl; wrd2chk.clear(); len = 0; i++; } else { wrd2chk.clear(); len = 0; i++; } } } continue; } } cout <<"Total time (in seconds) to check document: " <<double(clock()-startTime)/(double)CLOCKS_PER_SEC<< endl; }
void character_histogram() { HashTable<char, long long> ht; std::ifstream input("/home/jacob/Documents/Eclipse Workspace/C++/GenericDataStructures/test_files/PG-MobyDick.txt"); char c; if(input.is_open()) { while(input.get(c)) { if(ht.contains(c)) { ht[c]+=1; } else { ht[c] = 1; } } } ht.remove('c'); HashTable<char, long long>::iterator it = *ht.begin(); while(it.pos() < it.end()) { //std::cout<<"'"<< it.key()<<"'" << "=>"<< it.value() << std::endl; it++; } it--; while(it.pos() >= it.begin()) { //std::cout<<"'"<< it.key()<<"'" << "=>"<< it.value() << std::endl; it--; } MinHeap<huff_node*> heap1; HashTable<char, long long>::iterator it2 = *ht.begin(); while(it2.pos() < it2.end()) { huff_node * node = new huff_node; node->c = it2.key(); node->freq = it2.value(); node->is_leaf = true; heap1.insert(node); it2++; } while(heap1.size() > 2) { huff_node *left = heap1.remove_min(), *right = heap1.remove_min(), *new_node= new huff_node; new_node->is_leaf = false; new_node->freq = left->freq + right->freq; new_node->left = left; new_node->right = right; heap1.insert(new_node); } huff_node *left = heap1.remove_min(), *right = heap1.remove_min(), *root= new huff_node; root->is_leaf = false; root->freq = left->freq + right->freq; root->left = left; root->right = right; //HashTable<char, long long>::iterator it3 = *ht.begin(); //root is a huffman tree, could be traversed to create a compression algorithm }
// print whether an item is contains in the table void print_contain_message(int x, HashTable &h) { string contained = (h.contains(x)) ? "" : " not"; cout << x << " is" << contained << " in the table.\n"; }
void testBQ( const string & fileName ) { cout << "\n********** Priority queues **********\n\n"; ifstream f1; f1.open( fileName.c_str() ); if( f1.fail( ) ){ cout << "Error opening " << fileName; exit( 1 ); } int numItems = 10000; cout << "Part 1. Inserting words in Binomial Queue\n"; BinomialQueue BQ; HashTable HT ( BUCKET_SIZE ); string wordToSearch, line; int numberOfLines = 1; BQ.setNumberOfComparisonsAndAssignments( 0, 0 ); while( f1 ){ // read the document file getline( f1, line ); string::iterator it; for( it = line.begin( ); it < line.end( ); it++ ){ if( isspace( *it ) || ( ispunct( *it ) && *it != 39) ){ // || *it == 34){ if( wordToSearch != "" && !HT.contains( wordToSearch ) ){ BinomialNode *bn = BQ.insert( wordToSearch, numberOfLines ); // BinomialNode *bn = BQ.insertFast( wordToSearch, numberOfLines ); HT.insert( wordToSearch , bn ); } else if( HT.contains( wordToSearch ) ){ HT.updateLineNumberForWord( wordToSearch, numberOfLines ); } wordToSearch.clear(); continue; } else{ wordToSearch += *it; // build up the word } } numberOfLines++; line.clear(); // prepare to read a new line } cout << BQ.getCurrentSize( ) << " items inserted in Binomial Queue\n\n"; // BQ.printRoots( ); // debugging /************************************************************ * (2) Count and print out the total number of comparisons * and assignments executed for the insertions of all the * N elements into the binomial queue. ***********************************************************/ cout << "Part 2. Print out number of operations\n"; cout << "Number of comparisons : " << BQ.getCmps( ) << endl; cout << "Number of assignments : " << BQ.getAsmts( ) << endl; /************************************************************ * (3) Test the deleteMin() operation by applying a sequence * of 10 deleteMin() and by printing out the result (i.e. key * deleted along with associated line numbers). ***********************************************************/ cout << "\nPart 3. "; cout << "Executing 10 deleteMin() operations:\n"; for( int i = 0; i < 10; ++i ){ string minValue = BQ.findMin( ); // save element for printing line numbers cout << "Min. val.: " << setw( 10 ) << left << minValue << " "; // print element HT.printLineNumbersForWord( minValue ); BQ.deleteMin( ); // delete here. HT.remove( minValue ); } cout << endl; /************************************************************ * (4) Test the function find() as follows: Prompt the user * to input a string key. Execute the private function * find(key). If find returns a pointer to a node that indeed * holds key printout that find() was successful; printout * the set of line numbers as well. Otherwise, printout that * find() did not find the key. ***********************************************************/ cout << "Part 4. Testing find() function [once]:\n"; cout << "Enter a string key: "; string key; cin >> key; BinomialNode *keyNode1 = HT.find( key ); if( keyNode1 ){ cout << "find() function found the key \"" << key << "\""; cout << " in lines: "; HT.printLineNumbersForWord( key ); cout << endl; } else{ cout << "find() function did not find the key \"" << key << "\"" << endl; } // delete keyNode1; /****************************************************************** * (5) You are ready to implement now the remove(key) operation. * For a given key, find its position p (p is a pointer) in the * priority queue using the hash table. If the key is found delete * it from the bq (Hint: your code should percolate the key up all * the way to the root and then delete the root). Test your * implementation by applying a sequence of remove(key) operations, * and verify their correctness. For instance after remove(key), * find(key) should return “not found”. Prompt the user 5 times * to input a key. Execute then the remove(key) operation and * verify (by printing whether the removal was successful or not). *****************************************************************/ cout << "Part 5. Removing 5 keys and verifying the result" << endl; key = ""; // BinomialNode *keyNode2; for( int i = 0; i < 5; ++i ){ cout << "Enter a key to remove: "; cin >> key; BinomialNode *keyNode2 = HT.find( key ); if( keyNode2 ){ cout << "The key: \"" << key << "\" is removed " << endl; BQ.remove( keyNode2 ); // remove from Binomial Queue HT.remove( key ); // update Hash Table as well cout << "Trying to find it..." << endl; BinomialNode *temp = HT.find( key ); if( temp ) // this should not happen cout << "Hmm.. \"" << temp->element << "\" found!" << endl; else // good! cout << "Good! Deletion is successful" << endl; // cout << BQ.getCurrentSize( ); // debugging // BQ.printRoots( ); } else{ cout << "Cannot remove because the key \"" << key << "\" was not found" << endl; } } /****************************************************************** * (6) Write a faster insert(key) function for the binomial queue. * In order to achieve that you have to modify the merge() function * and make it specific to the merging of one element only. *****************************************************************/ cout << "\nPart 6. Testing fast insert(key) function [5 times]\n"; cout << "Keys will be inserted in line 0\n"; for( int i = 0; i < 5; ++i ){ key = ""; cout << "Enter a key to insert: "; cin >> key; BinomialNode *bn = BQ.insertFast( key, 0 ); HT.insert( key , bn ); cout << "\"" << key << "\" inserted in Binomial Queue at address: " << bn << endl; cout << "\"" << key << "\" found in Hash Table and has pointer: " << HT.find( key ) << endl; // cout << BQ.getCurrentSize( ); // debugging // BQ.printRoots( ); } cout << "\nPart 6a. Checking the improvement of insertFast() function over the original insert() function [3 times]: \n"; for( int i = 0; i < 3; ++i ){ string w3 = ""; cout << "\nEnter the key to check: "; cin >> w3; BQ.setNumberOfComparisonsAndAssignments( 0, 0 ); cout << "insert(\"" << w3 << "\", 10 )\n"; BinomialNode *bn = BQ.insert( w3, 10 ); HT.insert( "w3", bn ); cout << "\t\tNumber of comparisons = " << BQ.getCmps( ) << endl; cout << "\t\tNumber of assignments = " << BQ.getAsmts( ); // BQ.printRoots( ); cout << "\nNow remove it, reset the counters and test in again:\n\n"; BQ.remove( bn ); HT.remove( "w3"); BQ.setNumberOfComparisonsAndAssignments( 0, 0 ); cout << "insertFast(\"" << w3 << "\", 10 )\n"; BinomialNode *bn2 = BQ.insertFast( w3, 10 ); HT.insert( "w3", bn2 ); cout << "\t\tNumber of comparisons = " << BQ.getCmps( ) << endl; cout << "\t\tNumber of assignments = " << BQ.getAsmts( ); // BQ.printRoots( ); } //////////////////////////////////////////////////////////////////////////////// f1.close(); cout << endl << "End of program. Thanks" << endl; }