void testUnordered (void ) { using namespace tech; typedef unordered_map<std::string, unsigned long> Map; typedef unordered_set<std::string> Set; typedef unordered_map<unsigned long,std::string> RMap; Map colors; RMap rcolors; Set colornames; colornames.insert("black"); colors["black"] = 0x000000ul; colornames.insert("red"); colors["red"] = 0xff0000ul; colornames.insert("white"); colors.insert(Map::value_type("white",0xfffffful)); colornames.insert("green"); colors["green"] = 0x00ff00ul; colornames.insert("blue"); colors["blue"] = 0x0000fful; colornames.insert("blue"); colors["blue"] = 0x0000fful;//again just for kicks TS_ASSERT_EQUALS(colors["red"],0xff0000ul); TS_ASSERT_EQUALS(colors["black"],0x000000ul); TS_ASSERT_EQUALS(colors["white"],0xfffffful); TS_ASSERT_EQUALS(colors["green"],0x00ff00ul); TS_ASSERT_EQUALS(colors["blue"],0x0000fful); for (Map::iterator i = colors.begin(); i != colors.end(); ++i) { rcolors[i->second]=i->first; switch (i->second) { case 0xff0000ul: TS_ASSERT_EQUALS(i->first,"red"); break; case 0x00ff00ul: TS_ASSERT_EQUALS(i->first,"green"); break; case 0x0000fful: TS_ASSERT_EQUALS(i->first,"blue"); break; case 0x000000ul: TS_ASSERT_EQUALS(i->first,"black"); break; case 0xfffffful: TS_ASSERT_EQUALS(i->first,"white"); break; default: TS_FAIL("Color not matched"); break; } } for (Set::iterator i=colornames.begin();i!=colornames.end();++i) { TS_ASSERT(*i=="red"||*i=="white"||*i=="black"||*i=="blue"||*i=="green"); } TS_ASSERT_EQUALS((int)colornames.size(),5); TS_ASSERT_EQUALS((int)colors.size(),5); TS_ASSERT_EQUALS((int)rcolors.size(),5); TS_ASSERT_DIFFERS(colors.find("white"),colors.end()); colors.erase(colors.find("white")); TS_ASSERT_EQUALS(colors.find("white"),colors.end()); TS_ASSERT_EQUALS((int)colors.size(),4); }
main() { Set<int> s; assert(s.size() == 0); assert(s.empty()); s.insert(10); Set<int>::iterator iter = s.begin(); assert(*iter == 10); s.insert(6); s.insert(6); assert(s.count(6) == 1); assert(s.count(10) == 1); assert(s.count(12) == 0); iter = s.begin(); assert(*iter == 6); ++iter; assert(*iter == 10); ++iter; assert(iter == s.end()); s.insert(7); s.insert(9); s.insert(9); s.insert(8); s.insert(11); iter = s.begin(); assert(*iter == 6); ++iter; assert(*iter == 7); ++iter; assert(*iter == 8); ++iter; assert(*iter == 9); ++iter; assert(*iter == 10); ++iter; assert(*iter == 11); Set<int> s2; s2.insert(3); s2.insert(7); s2.insert(-1); s2.insert(16); s2.insert(11); s2.insert(4); iter = s2.find(3); assert(*iter == 3); iter = s2.find(888); assert(iter == s2.end()); s2.erase(7); iter = s2.begin(); assert(*iter == -1); ++iter; assert(*iter == 3); ++iter; assert(*iter == 4); ++iter; assert(*iter == 11); ++iter; assert(*iter == 16); ++iter; assert(iter == s2.end()); s2.erase(16); iter = s2.begin(); assert(*iter == -1); ++iter; assert(*iter == 3); ++iter; assert(*iter == 4); ++iter; assert(*iter == 11); ++iter; assert(iter == s2.end()); s2.erase(3); iter = s2.begin(); assert(*iter == -1); ++iter; assert(*iter == 4); ++iter; assert(*iter == 11); ++iter; assert(iter == s2.end()); s2.erase(11); iter = s2.begin(); assert(*iter == -1); ++iter; assert(*iter == 4); ++iter; assert(iter == s2.end()); s2.erase(-1); iter = s2.begin(); assert(*iter == 4); ++iter; assert(iter == s2.end()); s2.erase(4); iter = s2.begin(); assert(iter == s2.end()); cout << "All tests passed." << endl; }
int main(){ Set t; assert( t.size( ) == 0 ); assert( t.insert("hello") == true ); assert( t.size( ) == 1 ); assert( t.insert("how") == true ); assert( t.size( ) == 2 ); assert( t.insert("are") == true ); assert( t.size( ) == 3 ); assert( t.insert("you?") == true ); assert( t.size( ) == 4 ); assert( t.insert("all") == true ); assert( t.size( ) == 5 ); assert( t.insert("great!") == true ); assert( t.size( ) == 6 ); assert( t.insert("jello") == true ); assert( t.size( ) == 7 ); assert( t.insert("apples") == true ); assert( t.size( ) == 8 ); assert( t.insert("zoo") == true ); assert( t.size( ) == 9 ); assert( t.insert("zoo") == false ); assert( t.size( ) == 9 ); assert( t.insert("zebra") == true ); assert( t.size( ) == 10 ); assert( t.insert("zap") == true ); assert( t.size( ) == 11 ); assert( t.insert("aladdin") == true ); assert( t.size( ) == 12 ); assert( t.find("hello") == true ); assert( t.find("how") == true ); assert( t.find("are") == true ); assert( t.find("you") == false ); assert( t.find("you?") == true ); assert( t.find("all") == true ); assert( t.find("great") == false ); assert( t.find("great!") == true ); assert( t.find("jello") == true ); assert( t.find("apples!") == false ); assert( t.find("apples") == true ); assert( t.find("zoo") == true ); assert( t.find("zooz") == false ); assert( t.find("zap") == true ); assert( t.find("zebra") == true ); assert( t.find("aladdin") == true ); cout << t; t.erase( "zoo" ); t.erase( "hello" ); cout << t; Set p = t; //assert( t.size( ) == 9 ); assert( (p==t) == true ); assert( (p!=t) == false ); p.insert( "gremlin" ); assert( (p==t) == false ); assert( (p!=t) == true ); assert( (p>=t) == true ); assert( (t>=p) == false ); assert( (p<=t) == false ); assert( (t<=p) == true ); p.erase( "how" ); assert( (p>=t) == false ); cout << p << endl; Set::Iterator i; for( i = p.begin( ); i != p.end( ); i++ ) { cout << *i << " "; } cout << "\ntesting complete" << endl; }
int main(int argc, char *argv[]) { //start here if (argc < 2) { cout << endl << "provide a filename" << endl; return 1; } ifstream mainData; mainData.open(argv[1]); if (mainData.fail()) { cout << endl << "Can't open target file plz start over" << endl; return 1; } //create the list for webpages, also the map connects webpage names and List index map<string, WebPage> allPages; map<string, WebPage>::iterator mIt; while (!mainData.fail()) { string pName; getline(mainData, pName, '\n'); if (mainData.fail()) { break; } WebPage temPage(pName); try { allPages.insert(pair<string, WebPage>(pName, temPage)); } catch (exception& e) { cout << e.what() << endl; } } //produce incoming links for all webpages for (mIt = allPages.begin(); mIt != allPages.end(); mIt++) { Set<string>::iterator ssIt; Set<string> forAddLink = mIt->second.allOutgoingLinks(); for (ssIt = forAddLink.begin(); ssIt != forAddLink.end(); ssIt++) { allPages[*ssIt].addIncomingLink(mIt->first); } } // creating the set of all distinct words in webpages Set<string> everyWords; for (mIt = allPages.begin(); mIt != allPages.end(); mIt++) { everyWords = everyWords.setUnion(mIt->second.allWords()); } //creating the map connects each word with its related pages map<string, Set<string> > wordPageMap; Set<string>::iterator wIt; for (wIt = everyWords.begin(); wIt != everyWords.end(); wIt++) { Set<string> setToInsert; for (mIt = allPages.begin(); mIt != allPages.end(); mIt++) { Set<string> setToEva = mIt->second.allWords(); if (setToEva.find(*wIt) != setToEva.end()) { setToInsert.insert(mIt->second.filename()); } } wordPageMap.insert(pair<string, Set<string> >(*wIt, setToInsert)); } QApplication app(argc, argv); MySearch sWindow; sWindow.passIn(everyWords, wordPageMap, allPages); sWindow.show(); return app.exec(); return 0; }
int StatusJob::execute() { bool matched = false; const char *alternatives = "fileids|watchedpaths|dependencies|symbols|symbolnames|sources|jobs|info|compilers"; if (!strcasecmp(query.constData(), "fileids")) { matched = true; if (!write(delimiter) || !write("fileids") || !write(delimiter)) return 1; const Hash<uint32_t, Path> paths = Location::idsToPaths(); for (Hash<uint32_t, Path>::const_iterator it = paths.begin(); it != paths.end(); ++it) { if (!write<256>(" %u: %s", it->first, it->second.constData())) return 1; } if (isAborted()) return 1; } std::shared_ptr<Project> proj = project(); if (!proj) { if (!matched) write(alternatives); return matched ? 0 : 1; } if (query.isEmpty() || !strcasecmp(query.constData(), "watchedpaths")) { matched = true; if (!write(delimiter) || !write("watchedpaths") || !write(delimiter)) return 1; Set<Path> watched = proj->watchedPaths(); if (!write("Indexer")) return 1; for (Set<Path>::const_iterator it = watched.begin(); it != watched.end(); ++it) { if (!write<256>(" %s", it->constData())) return 1; } if (proj->fileManager) { if (!write("FileManager")) return 1; watched = proj->fileManager->watchedPaths(); for (Set<Path>::const_iterator it = watched.begin(); it != watched.end(); ++it) { if (!write<256>(" %s", it->constData())) return 1; } } if (isAborted()) return 1; } if (query.isEmpty() || !strcasecmp(query.constData(), "dependencies")) { matched = true; const DependencyMap map = proj->dependencies(); if (!write(delimiter) || !write("dependencies") || !write(delimiter)) return 1; DependencyMap depsReversed; for (DependencyMap::const_iterator it = map.begin(); it != map.end(); ++it) { if (!write<256>(" %s (%d) is depended on by", Location::path(it->first).constData(), it->first)) return 1; const Set<uint32_t> &deps = it->second; for (Set<uint32_t>::const_iterator dit = deps.begin(); dit != deps.end(); ++dit) { if (!write<256>(" %s (%d)", Location::path(*dit).constData(), *dit)) return 1; depsReversed[*dit].insert(it->first); } if (isAborted()) return 1; } for (DependencyMap::const_iterator it = depsReversed.begin(); it != depsReversed.end(); ++it) { write<256>(" %s (%d) depends on", Location::path(it->first).constData(), it->first); const Set<uint32_t> &deps = it->second; for (Set<uint32_t>::const_iterator dit = deps.begin(); dit != deps.end(); ++dit) { if (!write<256>(" %s (%d)", Location::path(*dit).constData(), *dit)) return 1; } if (isAborted()) return 1; } } if (query.isEmpty() || !strcasecmp(query.constData(), "symbols")) { matched = true; const SymbolMap &map = proj->symbols(); write(delimiter); write("symbols"); write(delimiter); for (SymbolMap::const_iterator it = map.begin(); it != map.end(); ++it) { const Location loc = it->first; const std::shared_ptr<CursorInfo> ci = it->second; write(loc); write(ci); write("------------------------"); if (isAborted()) return 1; } } if (query.isEmpty() || !strcasecmp(query.constData(), "symbolnames")) { matched = true; const SymbolNameMap &map = proj->symbolNames(); write(delimiter); write("symbolnames"); write(delimiter); for (SymbolNameMap::const_iterator it = map.begin(); it != map.end(); ++it) { write<128>(" %s", it->first.constData()); const Set<Location> &locations = it->second; for (Set<Location>::const_iterator lit = locations.begin(); lit != locations.end(); ++lit) { const Location &loc = *lit; write<1024>(" %s", loc.key().constData()); } if (isAborted()) return 1; } } if (query.isEmpty() || !strcasecmp(query.constData(), "sources")) { matched = true; const SourceMap &map = proj->sources(); if (!write(delimiter) || !write("sources") || !write(delimiter)) return 1; for (SourceMap::const_iterator it = map.begin(); it != map.end(); ++it) { if (!write<512>(" %s: %s", it->second.sourceFile().constData(), it->second.toString().constData())) return 1; } } if (query.isEmpty() || !strcasecmp(query.constData(), "jobs")) { matched = true; if (!write(delimiter) || !write("jobs") || !write(delimiter)) return 1; Server::instance()->dumpJobs(connection()); } if (query.isEmpty() || !strcasecmp(query.constData(), "compilers")) { matched = true; if (!write(delimiter) || !write("compilers") || !write(delimiter)) return 1; Source source; for (const Path &compiler : CompilerManager::compilers()) { source.compilerId = Location::insertFile(compiler); source.defines.clear(); source.includePaths.clear(); CompilerManager::applyToSource(source, true, true); write(compiler); write(" Defines:"); for (const auto &it : source.defines) write<512>(" %s", it.toString().constData()); write(" Includepaths:"); for (const auto &it : source.includePaths) write<512>(" %s", it.toString().constData()); write(""); } } if (query.isEmpty() || !strcasecmp(query.constData(), "info")) { matched = true; if (!write(delimiter) || !write("info") || !write(delimiter)) return 1; String out; Log log(&out); #ifdef NDEBUG out << "Running a release build\n"; #else out << "Running a debug build\n"; #endif const Server::Options &opt = Server::instance()->options(); out << "socketFile" << opt.socketFile << '\n' << "dataDir" << opt.dataDir << '\n' << "options" << String::format("0x%x\n", opt.options) << "jobCount" << opt.jobCount << '\n' << "unloadTimer" << opt.unloadTimer << '\n' << "rpVisitFileTimeout" << opt.rpVisitFileTimeout << '\n' << "rpIndexerMessageTimeout" << opt.rpIndexerMessageTimeout << '\n' << "rpConnectTimeout" << opt.rpConnectTimeout << '\n' << "rpConnectTimeout" << opt.rpConnectTimeout << '\n' << "syncThreshold" << opt.syncThreshold << '\n' << "threadStackSize" << opt.threadStackSize << '\n' << "defaultArguments" << opt.defaultArguments << '\n' << "includePaths" << opt.includePaths << '\n' << "defines" << opt.defines << '\n' << "ignoredCompilers" << opt.ignoredCompilers; write(out); } if (!matched) { write<256>("rc -s %s", alternatives); return 1; } else { return 0; } }
void FoliageSystem::Process(float32 timeElapsed) { TIME_PROFILE("FoliageSystem::Process"); VegetationRenderObject* vegetationRO = GetVegetation(foliageEntity); if(vegetationRO && vegetationRO->ReadyToRender()) { WindSystem * windSystem = GetScene()->windSystem; Camera * camera = GetScene()->GetRenderSystem()->GetMainCamera(); Vector<AbstractQuadTreeNode<VegetationSpatialData>*> & visibleCells = vegetationRO->BuildVisibleCellList(camera); uint32 cellsCount = visibleCells.size(); Set<AbstractQuadTreeNode<VegetationSpatialData>* > updatableCells; for(uint32 i = 0; i < cellsCount; ++i) { AbstractQuadTreeNode<VegetationSpatialData>* cell = visibleCells[i]; if(cell->data.width <= MAX_ANIMATED_CELL_WIDTH) { bool isMinAnimatedLod = (MIN_ANIMATED_CELL_WIDTH == cell->data.width); if(isMinAnimatedLod) { updatableCells.insert(cell->parent); } else { updatableCells.insert(cell); } } } Vector4 layersAnimationSpring = vegetationRO->GetLayersAnimationSpring(); const Vector4& layerAnimationDrag = vegetationRO->GetLayerAnimationDragCoefficient(); Set<AbstractQuadTreeNode<VegetationSpatialData>* >::iterator endIt = updatableCells.end(); for(Set<AbstractQuadTreeNode<VegetationSpatialData>* >::iterator it = updatableCells.begin(); it != endIt; ++it) { AbstractQuadTreeNode<VegetationSpatialData>* cell = *it; VegetationSpatialData& cellData = cell->data; const Vector3 & min = cellData.bbox.min; const Vector3 & max = cellData.bbox.max; Vector3 cellPos[4] = { Vector3(min.x, min.y, max.z), Vector3(min.x, max.y, max.z), Vector3(max.x, min.y, max.z), Vector3(max.x, max.y, max.z) }; for(uint32 layerIndex = 0; layerIndex < 4; ++layerIndex) { Vector3 windVec = windSystem->GetWind(cellPos[layerIndex]); Vector2 windVec2D(windVec.x, windVec.y); Vector2 & offset = cellData.animationOffset[layerIndex]; Vector2 & velocity = cellData.animationVelocity[layerIndex]; velocity += (windVec2D - layersAnimationSpring.data[layerIndex] * offset - layerAnimationDrag.data[layerIndex] * velocity * velocity.Length()) * timeElapsed; offset += velocity * timeElapsed; } if(cell->children != NULL) { for(uint32 childIndex = 0; childIndex < 4; ++childIndex) { cell->children[childIndex]->data.animationOffset[0] = cellData.animationOffset[0]; cell->children[childIndex]->data.animationOffset[1] = cellData.animationOffset[1]; cell->children[childIndex]->data.animationOffset[2] = cellData.animationOffset[2]; cell->children[childIndex]->data.animationOffset[3] = cellData.animationOffset[3]; cell->children[childIndex]->data.animationVelocity[0] = cellData.animationVelocity[0]; cell->children[childIndex]->data.animationVelocity[1] = cellData.animationVelocity[1]; cell->children[childIndex]->data.animationVelocity[2] = cellData.animationVelocity[2]; cell->children[childIndex]->data.animationVelocity[3] = cellData.animationVelocity[3]; } } } } }
boost::optional<runtime_signature> resolve_overloads(Set const& overloads, Args...) { auto match = overloads.find(runtime_signature { typeid(Args)... }); if (overloads.end() == match) return boost::none; return *match; }
int main(int argc, char ** argv) { std::cerr << std::fixed << std::setprecision(3); std::ofstream devnull("/dev/null"); DB::ReadBufferFromFileDescriptor in(STDIN_FILENO); size_t n = atoi(argv[1]); size_t elems_show = 1; using Vec = std::vector<std::string>; using Set = std::unordered_map<std::string, int>; using RefsSet = std::unordered_map<StringRef, int, StringRefHash>; using DenseSet = google::dense_hash_map<std::string, int>; using RefsDenseSet = google::dense_hash_map<StringRef, int, StringRefHash>; using RefsHashMap = HashMap<StringRef, int, StringRefHash>; Vec vec; vec.reserve(n); { Stopwatch watch; std::string s; for (size_t i = 0; i < n && !in.eof(); ++i) { DB::readEscapedString(s, in); DB::assertChar('\n', in); vec.push_back(s); } std::cerr << "Read and inserted into vector in " << watch.elapsedSeconds() << " sec, " << vec.size() / watch.elapsedSeconds() << " rows/sec., " << in.count() / watch.elapsedSeconds() / 1000000 << " MB/sec." << std::endl; } { DB::Arena pool; Stopwatch watch; const char * res = nullptr; for (Vec::iterator it = vec.begin(); it != vec.end(); ++it) { const char * tmp = pool.insert(it->data(), it->size()); if (it == vec.begin()) res = tmp; } std::cerr << "Inserted into pool in " << watch.elapsedSeconds() << " sec, " << vec.size() / watch.elapsedSeconds() << " rows/sec., " << in.count() / watch.elapsedSeconds() / 1000000 << " MB/sec." << std::endl; devnull.write(res, 100); devnull << std::endl; } { Set set; Stopwatch watch; for (Vec::iterator it = vec.begin(); it != vec.end(); ++it) set[*it] = 0; std::cerr << "Inserted into std::unordered_map in " << watch.elapsedSeconds() << " sec, " << vec.size() / watch.elapsedSeconds() << " rows/sec., " << in.count() / watch.elapsedSeconds() / 1000000 << " MB/sec." << std::endl; size_t i = 0; for (Set::const_iterator it = set.begin(); i < elems_show && it != set.end(); ++it, ++i) { devnull << it->first; devnull << std::endl; } } { RefsSet set; Stopwatch watch; for (Vec::iterator it = vec.begin(); it != vec.end(); ++it) set[StringRef(*it)] = 0; std::cerr << "Inserted refs into std::unordered_map in " << watch.elapsedSeconds() << " sec, " << vec.size() / watch.elapsedSeconds() << " rows/sec., " << in.count() / watch.elapsedSeconds() / 1000000 << " MB/sec." << std::endl; size_t i = 0; for (RefsSet::const_iterator it = set.begin(); i < elems_show && it != set.end(); ++it, ++i) { devnull.write(it->first.data, it->first.size); devnull << std::endl; } } { DB::Arena pool; RefsSet set; Stopwatch watch; for (Vec::iterator it = vec.begin(); it != vec.end(); ++it) set[StringRef(pool.insert(it->data(), it->size()), it->size())] = 0; std::cerr << "Inserted into pool and refs into std::unordered_map in " << watch.elapsedSeconds() << " sec, " << vec.size() / watch.elapsedSeconds() << " rows/sec., " << in.count() / watch.elapsedSeconds() / 1000000 << " MB/sec." << std::endl; size_t i = 0; for (RefsSet::const_iterator it = set.begin(); i < elems_show && it != set.end(); ++it, ++i) { devnull.write(it->first.data, it->first.size); devnull << std::endl; } } { DenseSet set; set.set_empty_key(DenseSet::key_type()); Stopwatch watch; for (Vec::iterator it = vec.begin(); it != vec.end(); ++it) set[*it] = 0; std::cerr << "Inserted into google::dense_hash_map in " << watch.elapsedSeconds() << " sec, " << vec.size() / watch.elapsedSeconds() << " rows/sec., " << in.count() / watch.elapsedSeconds() / 1000000 << " MB/sec." << std::endl; size_t i = 0; for (DenseSet::const_iterator it = set.begin(); i < elems_show && it != set.end(); ++it, ++i) { devnull << it->first; devnull << std::endl; } } { RefsDenseSet set; set.set_empty_key(RefsDenseSet::key_type()); Stopwatch watch; for (Vec::iterator it = vec.begin(); it != vec.end(); ++it) set[StringRef(it->data(), it->size())] = 0; std::cerr << "Inserted refs into google::dense_hash_map in " << watch.elapsedSeconds() << " sec, " << vec.size() / watch.elapsedSeconds() << " rows/sec., " << in.count() / watch.elapsedSeconds() / 1000000 << " MB/sec." << std::endl; size_t i = 0; for (RefsDenseSet::const_iterator it = set.begin(); i < elems_show && it != set.end(); ++it, ++i) { devnull.write(it->first.data, it->first.size); devnull << std::endl; } } { DB::Arena pool; RefsDenseSet set; set.set_empty_key(RefsDenseSet::key_type()); Stopwatch watch; for (Vec::iterator it = vec.begin(); it != vec.end(); ++it) set[StringRef(pool.insert(it->data(), it->size()), it->size())] = 0; std::cerr << "Inserted into pool and refs into google::dense_hash_map in " << watch.elapsedSeconds() << " sec, " << vec.size() / watch.elapsedSeconds() << " rows/sec., " << in.count() / watch.elapsedSeconds() / 1000000 << " MB/sec." << std::endl; size_t i = 0; for (RefsDenseSet::const_iterator it = set.begin(); i < elems_show && it != set.end(); ++it, ++i) { devnull.write(it->first.data, it->first.size); devnull << std::endl; } } { RefsHashMap set; Stopwatch watch; for (Vec::iterator it = vec.begin(); it != vec.end(); ++it) { RefsHashMap::iterator inserted_it; bool inserted; set.emplace(StringRef(*it), inserted_it, inserted); } std::cerr << "Inserted refs into HashMap in " << watch.elapsedSeconds() << " sec, " << vec.size() / watch.elapsedSeconds() << " rows/sec., " << in.count() / watch.elapsedSeconds() / 1000000 << " MB/sec." << std::endl; size_t i = 0; for (RefsHashMap::const_iterator it = set.begin(); i < elems_show && it != set.end(); ++it, ++i) { devnull.write(it->first.data, it->first.size); devnull << std::endl; } //std::cerr << set.size() << ", " << set.getCollisions() << std::endl; } { DB::Arena pool; RefsHashMap set; Stopwatch watch; for (Vec::iterator it = vec.begin(); it != vec.end(); ++it) { RefsHashMap::iterator inserted_it; bool inserted; set.emplace(StringRef(pool.insert(it->data(), it->size()), it->size()), inserted_it, inserted); } std::cerr << "Inserted into pool and refs into HashMap in " << watch.elapsedSeconds() << " sec, " << vec.size() / watch.elapsedSeconds() << " rows/sec., " << in.count() / watch.elapsedSeconds() / 1000000 << " MB/sec." << std::endl; size_t i = 0; for (RefsHashMap::const_iterator it = set.begin(); i < elems_show && it != set.end(); ++it, ++i) { devnull.write(it->first.data, it->first.size); devnull << std::endl; } } return 0; }
void MainWin::search(){ results->clear(); string input = txtSearch->text().toStdString(); for(int j = 0; input[j]; j++){ //converts to lower case and checks for valid input if(input[j] != ' ' && !isalnum(input[j])){ QMessageBox *message = new QMessageBox; message->setWindowTitle("Error!"); message->setText("Invalid search. \nIs your input alphanumeric?"); message->show(); return void(); } else input[j] = tolower(input[j]); } try{ Set<WebPage*> result; char cinput[input.size()+1]; strcpy(cinput, input.c_str()); //convert to char* to tokenize char* cword1 = strtok(cinput," "); //extract first word string word1(cword1); //convert char* to string to trim and input into set trim(word1); Set<WebPage*> and1 = words.at(word1); result = and1; if(btnAnd->isChecked()){ cword1 = strtok(NULL," "); //could just do ", " to get rid of leading white space while(cword1!=NULL){ //but i already made the trim function so too bad string word2(cword1); //essentially doing what I did above trim(word2); Set<WebPage*> and2 = words.at(word2); result = and1.setIntersection(and2); //check for intersections Set<WebPage*> and1 = result; //*** resets and1 to be result thus and2 will check for cword1 = strtok(NULL," "); //an intersection of previous words } } else if (btnOr->isChecked()){ cword1 = strtok(NULL," "); //could just do ", " to get rid of leading white space while(cword1!=NULL){ //but i already made the trim function so too bad string word2(cword1); trim(word2); Set<WebPage*> and2 = words.at(word2); result = and1.setUnion(and2); Set<WebPage*> and1 = result; cword1 = strtok(NULL," "); } } if(result.size() == 0){ QMessageBox *message = new QMessageBox; message->setWindowTitle("Error!"); message->setText("No search results."); message->show(); } else{ for(Set<WebPage*>::iterator it = result.begin(); it != result.end(); ++it){ WebPage* wp = *it; results->addItem(QString::fromStdString(wp->filename())); } } } catch(logic_error &e){ QMessageBox *message = new QMessageBox; message->setWindowTitle("Error!"); message->setText("No search results."); message->show(); } }
Set<T> Set<T>::setUnion (const Set<T> & other) const { std::vector<T> vthis, vother, sorted; Set<T> unionSet; for(Set<T>::Iterator i = this->begin(); i != this->end(); ++i) { vthis.push_back(*i); } for(Set<T>::Iterator i = other.begin(); i != other.end(); ++i) { vother.push_back(*i); } vthis = MergeSort::sort(vthis); vother = MergeSort::sort(vother); Set<T>::Iterator ithis = this->begin(), iother = other.begin(); while (ithis != this->end() || iother != other.end()) { if ((ithis == this->end()) != (iother == other.end())) { if (ithis == this->end() && sorted.back() != *iother) { sorted.push_back(*iother); ++iother; } else if (iother == other.end() && sorted.back() != *ithis) { sorted.push_back(*ithis); ++ithis; } else if (iother != other.end()) { ++iother; } else { ++ithis; } } else { if(*iother == *ithis) { sorted.push_back(*ithis); ++ithis; ++iother; } else if (*iother > *ithis) { sorted.push_back(*ithis); ++ithis; } else { sorted.push_back(*iother); ++iother; } } } for (typename std::vector<T>::iterator i = sorted.begin(); i != sorted.end(); ++i) { unionSet.add(*i); } return unionSet; }