void collisionTest() { printf("test collision add/remove\n"); HashTable<int,collisionHash,intEqual> collisiontable; for(int i=0;i<100;i++) { collisiontable.add(i); } for(int i=0;i<100;i+=2) { collisiontable.remove(i); size_t count = 0; for (HashTable<int,collisionHash,intEqual>::iterator i = collisiontable.begin(); i != collisiontable.end();i++) { count++; } INVARIANT(count == collisiontable.size(), boost::format("?! %d %d") % count % collisiontable.size()); } // multiple adds of the same key are *supposed* to add multiple times. collisiontable.clear(); SINVARIANT(collisiontable.size() == 0); for(int i=0;i<100;i++) { collisiontable.add(i); collisiontable.add(i); } SINVARIANT(collisiontable.size() == 200); // remove the keys (each twice) for(int i=0;i<100;i++) { collisiontable.remove(i, true); collisiontable.remove(i, true); } SINVARIANT(collisiontable.size() == 0); // an efficient way to do add/replace collisiontable.clear(); SINVARIANT(collisiontable.size() == 0); for(int i=0;i<100;i++) { bool replaced; collisiontable.addOrReplace(i, replaced); SINVARIANT(replaced == false); collisiontable.addOrReplace(i, replaced); SINVARIANT(replaced == true); } SINVARIANT(collisiontable.size() == 100); // remove, add, remove the keys for(int i=0;i<100;i++) { collisiontable.remove(i, true); collisiontable.add(i); collisiontable.remove(i); } SINVARIANT(collisiontable.size() == 0); }
void NodeController :: tryHash() { HashTable<int> numbersInHash; numbersInHash.add(123); numbersInHash.add(342); numbersInHash.add(123423); numbersInHash.add(123413); numbersInHash.add(-213); numbersInHash.add(123); numbersInHash.add(142342352); }
virtual Extent::Ptr getSharedExtent() { Extent::Ptr e = source.getSharedExtent(); if (e == NULL) return e; if (e->getTypePtr()->getName() != "NFS trace: attr-ops") return e; for (s.setExtent(e);s.morerecords();++s) { string fh = filehandle.stringval(); bool skip = false; for (vector<string>::iterator i=ignore_filehandles.begin(); i != ignore_filehandles.end();++i) { if (*i == fh) { skip = true; break; } } if (skip) continue; hteData *d = stats_table.lookup(hteData(type.stringval())); if (d == NULL) { hteData newd(type.stringval()); newd.file_size = new Stats; d = stats_table.add(newd); } d->file_size->add(filesize.val()); } return e; }
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; }
void testGrow(){ HashTable<std::string,int> testHash; testHash.add("0",0); unsigned long startingSize = testHash.backingArraySize; unsigned long i=0; while(testHash.backingArraySize == startingSize){ i++; std::ostringstream ss; ss << i; testHash.add(ss.str(),i); } unsigned long endingSize = testHash.backingArraySize; if(i*2 > startingSize){ std::cout << "ERROR: You didn't grown soon enough. Starting size was " << startingSize << ", didn't grow until " << i+1 << "adds done." << std::endl; return; } else { std::cout << "SUCCESS: Grow called at the right time. Starting size was " << startingSize << " and grow called on the " << i+1 << "th add call" << std::endl; } if(testHash.size() == i+1){ std::cout << "SUCCESS: Size of hash table unchanged by the grow" << std::endl; } else { std::cout << "ERROR: Size of hash table should be " << i+1 << ", but got " << testHash.size() << std::endl; return; } if(testHash.numRemoved == 0){ std::cout << "SUCCESS: After grow called, all removed items have been cleared out." << std::endl; } else { std::cout << "ERROR: When grow is called, you should clear out all the removed items." << std::endl; return; } for(unsigned long j=0; j<=i;j++){ std::ostringstream ss; ss << j; if(!testHash.keyExists(ss.str())){ std::cout << "ERROR: During grow the item with key '" << j << "' was lost" << std::endl; return; } } std::cout << "SUCCESS: After grow all items from the original table made it to the new one." << std::endl; }
void addEntry(HashTable<int> &HT, int x) //add entry { try { HT.add(x); //add entry if possible } catch (EntryExists& err ) //show error if not { cout << "Exception: " << err.getMessage() << endl << endl; } }
void UnionHash(HashTable & h1, HashTable & h2) { HashIterator iter(h2); iter.first(); while (iter.isValid()) { IMapping & cur = iter.query(); IMapping * matched = h1.find(cur.getKey()); if (!matched) h1.add(cur); iter.next(); } }
/*---------------------------------------------------------------------*//** アトムの登録 **//*---------------------------------------------------------------------*/ void EsKeywordSet::registerAtom(const EsAtom* atom) { // ハッシュテーブルに追加する const VcString* str = atom->getString(); HashTable* htbl = getHashTableInstance(); EsAtom::EsAtomHashEntry* entity = (EsAtom::EsAtomHashEntry*)htbl->add(str); if(entity == 0L) { ASSERT(false); return; } entity->setAtom(atom); }
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() { double p; cin>>p; BloomFilter F(p); HashTable T; int n,k=0; string s; freopen("dictionary+text.txt","r",stdin); for(int i=1;i<=99431;i++) { cin>>s; F.add(s); T.add(s); } cin>>k; int rightF=0,rightT=0,wrongF=0,wrongT=0,BloomLies=0; for(int i=1;i<=k;i++) { cin>>s; if(F.find(s)) rightF++; else wrongF++; if(T.find(s)) rightT++; else wrongT++; BloomLies=rightF-rightT; } cout<<rightF<<' '<<wrongF<<' '<<BloomLies; return 0; }
static Word* Word::newWord // GET NEW WORD ( const char *chrs // - characters in word , unsigned size ) // - # characters { static HashTable wordTable // hash table ( &Word::hashFun , &Word::hashCompare); Word* curr; // - current word _chr_vect vect // - character vector = { chrs, size }; curr = (Word *)wordTable.find( &vect ); if( 0 == curr ) { curr = (Word*)wordCarver.alloc(); curr = new( curr ) Word( chrs, size ); wordRing.append( curr ); wordTable.add( curr, &vect ); } else { ++ curr->_count; } return curr; }
//Add and remove some items, making sure they come back in the // correct order void testBasicMethods(){ HashTable<std::string,int> testHash; testHash.add("how",0); testHash.add("now",1); testHash.add("brown",2); testHash.add("cow",3); testHash.add("metal",4); testHash.add("daffodil",5); if(testHash.size() == 6){ std::cout << "SUCCESS: 6 items added" << std::endl; } else { std::cout << "ERROR: Added 6 items, but size says " << testHash.size() << std::endl; return; } int x0 = testHash.find("how"); int x1 = testHash.find("now"); int x2 = testHash.find("brown"); int x3 = testHash.find("cow"); int x4 = testHash.find("metal"); int x5 = testHash.find("daffodil"); if(x0 != 0 || x1 != 1 || x2 != 2 || x3 != 3 || x4 != 4 || x5 != 5){ std::cout << "ERROR: Expected 0,1,2,3,4,5, but got " << x0 <<"," << x1 << "," << x2 << "," << x3 << "," << x4 << "," << x5 << std::endl; return; } else { std::cout << "SUCCESS: 6 added items came back out with correct keys" << std::endl; } if(testHash.keyExists("daffodil")){ std::cout << "SUCCESS: keyExists found 'daffodil'" << std::endl; } else { std::cout << "ERROR: 'daffodil' is a valid key, but keyExists said false" << std::endl; return; } if(!testHash.keyExists("shiny")){ std::cout << "SUCCESS: keyExists did not find 'shiny'" << std::endl; } else { std::cout << "ERROR: 'shiny' is not a valid key, but keyExists said true" << std::endl; return; } bool didException = false; try { int xq = testHash.find("shiny"); } catch (std::string s) { std::cout << "SUCCESS: Caught exception: " << s << std::endl; didException = true; } catch (...) { std::cout << "ERROR: Caught an exception, but it wasn't a string type" << std::endl; return; } if(didException == false){ std::cout << "ERROR: find did not throw an exception when given a non-existen key" << std::endl; return; } testHash.remove("how"); testHash.remove("now"); if(testHash.size() == 4){ std::cout << "SUCCESS: 4 items after removing" << std::endl; } else { std::cout << "ERROR: After remove, expected 4 items, but size says " << testHash.size() << std::endl; return; } if(testHash.numRemoved == 2){ std::cout << "SUCCESS: numRemoved is 2, as expected" << std::endl; } else { std::cout << "ERROR: After remove, expected numRemoved == 2, but got " << testHash.numRemoved << std::endl; return; } if(testHash.keyExists("how") == false){ std::cout << "SUCCESS: Removed key 'how' is no longer in hash table." << std::endl; } else { std::cout << "ERROR: Removed key 'how' is still in the table" << std::endl; return; } if(testHash.keyExists("brown") && testHash.keyExists("cow") && testHash.keyExists("metal") && testHash.keyExists("daffodil")){ std::cout << "SUCCESS: The 4 items that weren't removed are still in the table." << std::endl; } else { std::cout << "ERROR: One of the 4 keys that ought to still be in the table has been lost" << std::endl; return; } }
virtual Extent::Ptr getSharedExtent() { long long reltime; Extent::Ptr e = source.getSharedExtent(); if (e == NULL) return e; SINVARIANT(e->getTypePtr()->getName() == "common-attr-rw-join"); hteData k; for (s.setExtent(e);s.morerecords();++s) { if (starttime == -1) starttime = packet_at.val(); // cout << "Found " << operation.stringval() << "\n"; if (operation.equal(str_read) == false) continue; ExtentType::int64 file_age = packet_at.val() - modify_time.val(); k.filehandle = filehandle.stringval(); k.server = serverip.val(); // this is return packet, so server ip // is in sourceip // cout << "ip: " << ipstring(sourceip.val()).c_str() << "\n"; reltime = packet_at.val() - starttime; hteData *v = seen.lookup(k); if (v == NULL) { seen.add(k); // recent-cache misses cum1 += filesize.val(); n1++; e1.time = reltime; e1.bytes = cum1; e1.n = n1; if ((double)(reltime - last1)/1000000 > NFSDSAnalysisMod::read_sampling || last1 == -1) { last1 = reltime; list1.push_back(e1); } fflush(stdout); if (file_age / 1.0e9 < RECENTAGE) // newly changed file { // all-cache misses fflush(stdout); cum2 += filesize.val(); n2++; e2.time = reltime; e2.bytes = cum2; e2.n = n2; if ((double)(reltime - last2)/1000000 > NFSDSAnalysisMod::read_sampling || last2 == -1) { last2 = reltime; list2.push_back(e2); } } } else // hits cache of recent files { if (file_age / 1.0e9 < RECENTAGE) // newly changed file { cum4 += in_bytes.val(); n4++; e4.time = reltime; e4.bytes = cum4; e4.n = n4; if ((double)(reltime - last4)/1000000 > NFSDSAnalysisMod::read_sampling || last4 == -1) { last4 = reltime; list4.push_back(e4); } } } if (file_age / 1.0e9 >= RECENTAGE) // hits cache of old files { cum3 += in_bytes.val(); n3++; e3.time = reltime; e3.bytes = cum3; e3.n = n3; if ((double)(reltime - last3)/1000000 > NFSDSAnalysisMod::read_sampling || last3 == -1) { last3 = reltime; list3.push_back(e3); } } } return e; }
/** * For internal use only. */ void AbstractType::initialize(void) { types.add(name(), this); }
/** * MAIN USER INTERFACE */ int main() { // init hash table HashTable h; // read in csv string line; ifstream file("randomNames.csv"); if (file.is_open()) { while (getline(file, line)) { int day = 0; int month = 0; int year = 0; // separate name and date int comma = line.find(","); string name = line.substr(0, comma); string date = line.substr(comma+1, line.length()); // get day int firstSlash = date.find("/"); string strDay = date.substr(0, firstSlash); day = atoi(strDay.c_str()); // get month int secondSlash = date.find("/", firstSlash + 1); string strMonth = date.substr(firstSlash + 1, secondSlash); month = atoi(strMonth.c_str()); // get year string strYear = date.substr(secondSlash + 1, date.length()); year = atoi(strYear.c_str()); // add anonymous date (doesn't matter it's copied into hash table) h.add(Date(month, day, year), name); } } // init vars int day = 0; int month = 0; int year = 0; // prompt user cout << "Input date to search: " << endl; // get day cout << "day => "; cin >> day; // get month cout << "month => "; cin >> month; // get year cout << "year => "; cin >> year; cout << endl; // if valid date if (day > 0 && day < 32 && month > 0 && month < 13 && year > 0) { try { // retrieve names vector<string> v = h.getNames(Date(month, day, year)); cout << "Birthdays on this day: " << endl; cout << "----------------------- " << endl; // print out names for (int i = 0; i < v.size(); i++) { cout << v[i] << endl; } } catch(int e) { cout << "No birthdays found!" << endl; } } else { cout << "Invalid date, exiting..."; return -1; } return 0; }