const std::wstring tolower_and_del_delims(const std::wstring& str, const bool reverse) { std::wstring ret_str; if(reverse) { remove_copy_if(str.rbegin(), str.rend(), std::back_inserter(ret_str), is_delim); } else { remove_copy_if(str.begin(), str.end(), std::back_inserter(ret_str), is_delim); } std::transform(ret_str.begin(), ret_str.end(), ret_str.begin(), std::towlower); return ret_str; }
void ShowBig() { remove_copy_if( Rect_List.begin(), Rect_List.end(), ostream_iterator<CRectangle>( cout, "\n" ), AreaComp( 5.0 ) ); remove_copy_if( Tria_List.begin(), Tria_List.end(), ostream_iterator<CTriangle>( cout, "\n" ), AreaComp( 5.0 ) ); remove_copy_if( Circ_List.begin(), Circ_List.end(), ostream_iterator<CCircle>( cout, "\n" ), AreaComp( 5.0 ) ); }
void pure_numeric_algo(){ cout<<endl<<"pure_numeric_algo :"<<endl; int ia[11] = {0, 1, 2, 3, 4, 5, 6,6,6, 7, 8 }; vector<int> iv(ia,ia+11); vector<int> iv2(ia+6,ia+8); vector<int>::iterator itr; itr = adjacent_find(iv.begin(),iv.end(), equal_to<int>()); //找到相邻元素相等的第一个元素 cout<<"adjacent_find: "<<*itr<<endl; cout<<"count: "<<count(iv.begin(),iv.end(), 6)<<endl; //找到元素值等于6的个数 cout<<"count_if: "<<count_if(iv.begin(),iv.end(), bind2nd(less<int>() , 7))<<endl; //找到小于7的元素个数 itr = find(iv.begin(),iv.end(), 4); //找到元素等于4的第一个元素位置 cout<<"find: "<<*itr<<endl; itr = find_if(iv.begin(),iv.end(), bind2nd(greater<int>() , 2)); //找到元素大于2的第一个元素位置 cout<<"find_if: "<<*itr<<endl; itr = find_end(iv.begin(),iv.end(), iv2.begin(),iv2.end()); //找到iv序列中最后子序列匹配出现的位置 cout<<"find_end: "<<*(itr+3)<<endl; itr = find_first_of(iv.begin(),iv.end(), iv2.begin(),iv2.end()); //找到iv序列中最先子序列匹配出现的位置 cout<<"find_end: "<<*(itr+3)<<endl; remove(iv.begin(),iv.end(), 6); //删除元素,向前移,但是容器size不变,后面会剩余数据 cout<<"remove: "<<iv<<endl; vector<int> iv3(12,-1); remove_copy(iv.begin(),iv.end(), iv3.begin(), 6); //删除元素,将数据拷贝到新容器,后面会剩余数据 cout<<"remove_copy: "<<iv3<<endl; remove_if(iv.begin(),iv.end(), bind2nd(less<int>(), 6)); //删除小于6的元素,后面会剩余数据 cout<<"remove_if: "<<iv<<endl; remove_copy_if(iv.begin(),iv.end(), iv3.begin(), bind2nd(less<int>(), 7)); //删除小于7的元素,并拷贝到新容器 cout<<"remove_copy_if: "<<iv3<<endl; replace(iv.begin(),iv.end(), 6, 3); //将所有元素值为6的改为3 cout<<"replace: "<<iv<<endl; replace_copy(iv.begin(),iv.end(),iv3.begin(), 3, 5); //将所有元素值为3的改为5,结果保存在新容器中 cout<<"replace_copy: "<<iv3<<endl; replace_if(iv.begin(),iv.end(), bind2nd(less<int>(),5), 2); //将所有元素值小于5的改为2 cout<<"replace_if: "<<iv<<endl; replace_copy_if(iv.begin(),iv.end(),iv3.begin(), bind2nd(equal_to<int>(),8), 9); //将所有元素值为8的改为9,结果保存在新容器中 cout<<"replace_copy_if: "<<iv3<<endl; reverse(iv.begin(),iv.end()); cout<<"reverse: "<<iv<<endl; //反转 reverse_copy(iv.begin(),iv.end(),iv3.begin()); cout<<"reverse_copy: "<<iv3<<endl; //反转,结果保存在新容器 rotate(iv.begin(),iv.begin() + 4, iv.end()); cout<<"rotate: "<<iv<<endl; //互换元素 rotate_copy(iv.begin(),iv.begin() + 5,iv.end(),iv3.begin()); cout<<"rotate_copy: "<<iv3<<endl; //互换元素,结果保存在新容器 int ia2[] = {2, 8}; vector<int> iv4(ia2,ia2+2); cout<<"search: "<<*search(iv.begin(),iv.end(),iv4.begin(),iv4.end())<<endl; //查找子序列出现的第一次出现地点 swap_ranges(iv4.begin(),iv4.end(),iv.begin()); //按区域交换 cout<<"swap_ranges: "<<iv<<endl<<iv4<<endl; transform(iv.begin(),iv.end(),iv.begin(),bind2nd(minus<int>(), 2)); //所有元素减2 cout<<"transform: "<<iv<<endl; transform(iv4.begin(),iv4.end(),iv.begin(),iv4.begin(),plus<int>()); //区间对应元素相加 cout<<"transform: "<<iv4<<endl; /************************************************************************/ vector<int> iv5(ia,ia+11); vector<int> iv6(ia+4,ia+8); vector<int> iv7(15); cout<<"max_element: "<<*max_element(iv5.begin(), iv5.end())<<endl; //最大元素游标 cout<<"min_element: "<<*min_element(iv5.begin(), iv5.end())<<endl; cout<<"includes: "<<includes(iv5.begin(),iv5.end(),iv6.begin(),iv6.end())<<endl; //iv6中元素是不是都在iv5中,这两个必须排过序 merge(iv5.begin(),iv5.end(),iv6.begin(),iv6.end(),iv7.begin()); //两个排序号的容器合并 cout<<"merge: "<<iv7<<endl; partition(iv7.begin(),iv7.end(),bind2nd(equal_to<int>(), 5)); //满足条件的放在左边,不满足条件的放在右边 cout<<"partition: "<<iv7<<endl; unique(iv5.begin(),iv5.end()); //去重,重复的元素放在后面 cout<<"unique: "<<iv5<<endl; unique_copy(iv5.begin(),iv5.end(),iv7.begin()); //去重,结果保存在新容器 cout<<"unique_copy: "<<iv7<<endl; }
TEST(MapFilterReduce, Filter2) { vector<string> v1{"one", "two", "three"}, v2; remove_copy_if(v1.begin(), v1.end(), back_inserter(v2), [](string& s) { return s == "one"; }); ASSERT_THAT(v2, ElementsAre("two", "three")); }
list<AspFluentRef> operator()(const AnswerSet &planWithStates) const { list<AspFluentRef> actionsOnly; remove_copy_if(planWithStates.getFluents().begin(), planWithStates.getFluents().end(), back_inserter(actionsOnly),not1(IsAnAction(allActions))); return actionsOnly; }
bool operator()(const AnswerSet& plan) const { list<AspFluent> actionsOnly; remove_copy_if(plan.getFluents().begin(), plan.getFluents().end(), back_inserter(actionsOnly),not1(IsAnAction(allActions))); set< list< AspFluent>, LexComparator >::const_iterator sub = find_if(allPlans.begin(),allPlans.end(),IsSubSequence(actionsOnly)); return sub != allPlans.end(); }
void AtomRadiiTable::fromString(const string& s) { // create deblanked string string s1; remove_copy_if(s.begin(), s.end(), back_inserter(s1), ::isspace); // replace commas with space so we can use the split function replace(s1.begin(), s1.end(), ',', ' '); istringstream ss1(s1); CustomRadiiStorage rds; for (string w; ss1 >> w;) {
void index_set::copy_vars(const Set* indices) { vector<int> tmp; // copy_if variable -> copy_if_not not variable remove_copy_if(indices->begin(), indices->end(), inserter(tmp, tmp.begin()), bind1st(mem_fun(&index_set::not_variable), this)); ASSERT(!tmp.empty()); constraint_variable_set.push_back(tmp); }
void CRegionalMetaModel::FindTrustRegions( const REAL* prInput, vector<CTrustRegion*>& hitRegions ) { ASSERT( !m_vcMetaModels.empty() ); hitRegions.clear(); int nInputs = GetInputs(); vector<REAL> vcInput( prInput, prInput+nInputs ); remove_copy_if( m_vcRegions.begin()+1, m_vcRegions.end(), back_inserter(hitRegions), op_not_in_region(vcInput) ); //the first neural net is the global prediction, always available. //modifed on 02/12/05. hitRegions.insert( hitRegions.begin(), m_vcRegions.front() ); }
void DialogTimingProcessor::SortDialogues() { std::set<wxString> styles; for (size_t i = 0; i < StyleList->GetCount(); ++i) { if (StyleList->IsChecked(i)) { styles.insert(StyleList->GetString(i)); } } Sorted.clear(); Sorted.reserve(c->ass->Line.size()); if (onlySelection->IsChecked()) { SelectionController<AssDialogue>::Selection sel = c->selectionController->GetSelectedSet(); remove_copy_if(sel.begin(), sel.end(), back_inserter(Sorted), bind(bad_line, &styles, std::tr1::placeholders::_1)); } else { std::vector<AssDialogue*> tmp(c->ass->Line.size()); transform(c->ass->Line.begin(), c->ass->Line.end(), back_inserter(tmp), cast<AssDialogue*>()); remove_copy_if(tmp.begin(), tmp.end(), back_inserter(Sorted), bind(bad_line, &styles, std::tr1::placeholders::_1)); } sort(Sorted.begin(), Sorted.end(), AssFile::CompStart); }
STDMETHODIMP CSelectExportObjects::DoAction (IDataObject *pIDataObj, DWORD dwReserved) { // Eingabeobjekte einfach mit zu unserer Objektmenge hinzufügen if (NULL != pIDataObj) { // evtl. keine Eingabeobjektmenge gegeben COM_TRY { // aber nur Objekte der geforderten Datenquellen übernehmen WEnumLONG EnumObjs; OutEnumLONG iter_out (&m_Objects); if (S_OK == GetEnumLONGData (pIDataObj, __uuidof(IEnumLONG), EnumObjs.ppv())) { remove_copy_if (InEnumLONG(EnumObjs), InEnumLONG(), iter_out, CObjectToCopy(m_setDataSources)); } } COM_CATCH; }
int main() { int ia[] = { 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 }; vector< int, allocator >::iterator iter; vector< int, allocator > vec( ia, ia+10 ); ostream_iterator< int > ofile( cout, " " ); cout << "original element sequence:\n"; copy( vec.begin(), vec.end(), ofile ); cout << '\n'; iter = remove_if( vec.begin(), vec.end(), bind2nd(less<int>(),10) ); vec.erase( iter, vec.end() ); cout << "sequence after applying remove_if < 10:\n"; copy( vec.begin(), vec.end(), ofile ); cout << '\n'; vector< int, allocator > vec_res( 10 ); iter = remove_copy_if( ia, ia+10, vec_res.begin(), EvenValue() ); cout << "sequence after applying remove_copy_if even:\n"; copy( vec_res.begin(), iter, ofile ); cout << '\n'; }
void Delaunay::Triangulate(const vertexSet& vertices, triangleSet& output) { if (vertices.size() < 3) return; // nothing to handle // Determine the bounding box. cvIterator itVertex = vertices.begin(); REAL xMin = itVertex->GetX(); REAL yMin = itVertex->GetY(); REAL xMax = xMin; REAL yMax = yMin; ++itVertex; // If we're here, we know that vertices is not empty. for (; itVertex != vertices.end(); itVertex++) { xMax = itVertex->GetX(); // Vertices are sorted along the x-axis, so the last one stored will be the biggest. REAL y = itVertex->GetY(); if (y < yMin) yMin = y; if (y > yMax) yMax = y; } REAL dx = xMax - xMin; REAL dy = yMax - yMin; // Make the bounding box slightly bigger, just to feel safe. REAL ddx = dx * 0.01F; REAL ddy = dy * 0.01F; xMin -= ddx; xMax += ddx; dx += 2 * ddx; yMin -= ddy; yMax += ddy; dy += 2 * ddy; // Create a 'super triangle', encompassing all the vertices. We choose an equilateral triangle with horizontal base. // We could have made the 'super triangle' simply very big. However, the algorithm is quite sensitive to // rounding errors, so it's better to make the 'super triangle' just big enough, like we do here. vertex vSuper[3]; vSuper[0] = vertex(xMin - dy * sqrt3 / 3.0F, yMin); // Simple highschool geometry, believe me. vSuper[1] = vertex(xMax + dy * sqrt3 / 3.0F, yMin); vSuper[2] = vertex((xMin + xMax) * 0.5F, yMax + dx * sqrt3 * 0.5F); triangleSet workset; workset.insert(triangle(vSuper)); for (itVertex = vertices.begin(); itVertex != vertices.end(); itVertex++) { // First, remove all 'completed' triangles from the workset. // A triangle is 'completed' if its circumcircle is entirely to the left of the current vertex. // Vertices are sorted in x-direction (the set container does this automagically). // Unless they are part of the 'super triangle', copy the 'completed' triangles to the output. // The algorithm also works without this step, but it is an important optimalization for bigger numbers of vertices. // It makes the algorithm about five times faster for 2000 vertices, and for 10000 vertices, // it's thirty times faster. For smaller numbers, the difference is negligible. tIterator itEnd = remove_if(workset.begin(), workset.end(), triangleIsCompleted(itVertex, output, vSuper)); edgeSet edges; // A triangle is 'hot' if the current vertex v is inside the circumcircle. // Remove all hot triangles, but keep their edges. itEnd = remove_if(workset.begin(), itEnd, vertexIsInCircumCircle(itVertex, edges)); workset.erase(itEnd, workset.end()); // remove_if doesn't actually remove; we have to do this explicitly. // Create new triangles from the edges and the current vertex. for (edgeIterator it = edges.begin(); it != edges.end(); it++) workset.insert(triangle(it->m_pV0, it->m_pV1, & (* itVertex))); } // Finally, remove all the triangles belonging to the 'super triangle' and move the remaining // triangles tot the output; remove_copy_if lets us do that in one go. tIterator where = output.begin(); remove_copy_if(workset.begin(), workset.end(), inserter(output, where), triangleHasVertex(vSuper)); }
Point2f VanishingPointDetector::CalculateVanishingPoint(const Mat &image) { if (!initialized) { return Point2f(-1, -1); } if (stabilized) { return currentVanishingPoint; } float A = 0.0; float B = 0.0; float C = 0.0; float D = 0.0; float E = 0.0; // Pre-Filtering Mat workingCopy = image.clone(); // cvtColor(workingCopy, workingCopy, CV_RGB2GRAY); Sobel(workingCopy, workingCopy, -1, 1, 0); threshold(workingCopy, workingCopy, threshHold, 255, THRESH_BINARY); vector<Vec2f> lines; vector<Vec2f> filteredLines; HoughLines(workingCopy, lines, 2, CV_PI / 60, cvRound(125), 0, 0, CV_PI / 18, CV_PI * 17 / 18); remove_copy_if(lines.begin(), lines.end(), std::back_inserter(filteredLines), VisionUtils::IsHorizontal); map<float, vector<float> > groupedLines; for (size_t i = 0; i < min(filteredLines.size(), maxLines); i++) { Vec2f line = filteredLines[i]; float roundedTheta = cvRound(line[1] * 100) / 100.0f; groupedLines[roundedTheta].push_back(line[0]); } int i = 0; for (map<float, vector<float> >::iterator it = groupedLines.begin(); it != groupedLines.end(); ++it) { i++; float rho = it->second.front(), theta = it->first; float p = exp2f( -powf(rho - currentVanishingPoint.x * cos(theta) - currentVanishingPoint.y * sin(theta), 2) / 20000); float a = cos(theta); float b = sin(theta); float H = 10.0f / (i + 1); A += p * H * a * a; B += p * H * b * b; C += p * H * a * b; D += p * H * a * rho; E += p * H * b * rho; } Mat L = Mat_<float>(2, 2); L.at<float>(0) = A; L.at<float>(1) = C; L.at<float>(2) = C; L.at<float>(3) = B; Mat R = Mat_<float>(2, 1); R.at<float>(0) = D; R.at<float>(1) = E; Mat X; if (solve(L, R, X, DECOMP_CHOLESKY)) { Point2f newVanishingPoint(X.at<float>(0), X.at<float>(1)); if (GeneralUtils::Equals(newVanishingPoint, currentVanishingPoint, 5)) { stabilizationCounter++; stabilized = stabilizationCounter >= 10; } else { stabilizationCounter = 0; } currentVanishingPoint = newVanishingPoint; } return currentVanishingPoint; }
std::vector< AnswerSet > Clingo::computeAllPlans(const std::vector<actasp::AspRule>& goal, double suboptimality) const throw () { if (suboptimality < 1) { stringstream num; num << suboptimality; throw logic_error("Clingo: suboptimality value cannot be less then one, found: " + num.str()); } string query = generatePlanQuery(goal,true); list<AnswerSet> firstAnswerSets = krQuery(query,0,max_n,"planQuery.asp",0); if (firstAnswerSets.empty()) return vector<AnswerSet>(); //when actions are filtered and there are not state fluents, //the last time step is of the last action, and actions start at //zero, so we need +1 unsigned int shortestLength = firstAnswerSets.begin()->maxTimeStep()+1; int maxLength = floor(suboptimality * shortestLength); if (maxLength == shortestLength) return vector<AnswerSet>(firstAnswerSets.begin(), firstAnswerSets.end()); set< list <AspFluentRef>, LexComparator > goodPlans; transform(firstAnswerSets.begin(), firstAnswerSets.end(),inserter(goodPlans,goodPlans.begin()),AnswerSetToList()); query = generatePlanQuery(goal,true); list<AnswerSet> moreAnswerSets = krQuery(query,maxLength,maxLength,"planQuery.asp",0); list<AnswerSet>::iterator currentFirst = find_if(moreAnswerSets.begin(),moreAnswerSets.end(),PlanLongerThan(moreAnswerSets.begin()->maxTimeStep())); set< list<AspFluentRef>, LexComparator > badPlans; //this object remembers the set of bad plans, cannot be created inside the loop IsNotLocallyOptimal isNotLocallyOptimal(&goodPlans, &badPlans,allActions,shortestLength,true); list<AnswerSetRef> goodPointers(firstAnswerSets.begin(),firstAnswerSets.end()); while (currentFirst != moreAnswerSets.end()) { //process the plans in groups of increasing length list<AnswerSet>::iterator currentLast = find_if(currentFirst,moreAnswerSets.end(),PlanLongerThan(currentFirst->maxTimeStep())); size_t size_pre_copy = goodPointers.size(); remove_copy_if(currentFirst,currentLast,back_inserter(goodPointers),isNotLocallyOptimal); list<AnswerSetRef>::iterator from = goodPointers.begin(); advance(from,size_pre_copy); transform(from, goodPointers.end(),inserter(goodPlans,goodPlans.begin()),AnswerSetToList()); currentFirst = currentLast; } vector<AnswerSet> finalVector(goodPointers.begin(),goodPointers.end()); cout << " --- good plans ---" << endl; vector< AnswerSet>::const_iterator printIt = finalVector.begin(); for (; printIt != finalVector.end(); ++printIt) { copy(printIt->getFluents().begin(),printIt->getFluents().end(),ostream_iterator<string>(cout, " ")); cout << endl; } cout << " ---- " << endl; return finalVector; }
MultiPolicy Clingo::computePolicy(const std::vector<actasp::AspRule>& goal, double suboptimality) const throw (std::logic_error) { if (suboptimality < 1) { stringstream num; num << suboptimality; throw logic_error("Clingo: suboptimality value cannot be less then one, found: " + num.str()); } string query = generatePlanQuery(goal,false); clock_t kr1_begin = clock(); list<AnswerSet> firstAnswerSets = krQuery(query,1,max_n,"planQuery.asp",0); clock_t kr1_end = clock(); cout << "The first kr call took " << (double(kr1_end - kr1_begin) / CLOCKS_PER_SEC) << " seconds" << endl; MultiPolicy policy(allActions); if (firstAnswerSets.empty()) return policy; unsigned int shortestLength = firstAnswerSets.begin()->maxTimeStep(); for_each(firstAnswerSets.begin(),firstAnswerSets.end(),PolicyMerger(policy)); int maxLength = floor(suboptimality * shortestLength); if (maxLength == shortestLength) return policy; //all accepted plans sorted lexicographically set< list <AspFluentRef>, LexComparator > goodPlans; //remove the states from the plans transform(firstAnswerSets.begin(),firstAnswerSets.end(),inserter(goodPlans,goodPlans.begin()), CleanPlan(allActions)); query = generatePlanQuery(goal,false); clock_t kr2_begin = clock(); list<AnswerSet> answerSets = krQuery(query,maxLength,maxLength,"planQuery.asp",0); clock_t kr2_end = clock(); cout << "The second kr call took " << (double(kr2_end - kr2_begin) / CLOCKS_PER_SEC) << " seconds" << endl; //skip the minimial plans list<AnswerSet>::iterator currentFirst = find_if(answerSets.begin(),answerSets.end(),PlanLongerThan(answerSets.begin()->maxTimeStep())); set< list <AspFluentRef>, LexComparator > badPlans; //this object remembers the set of bad plans, cannot be created inside the loop IsNotLocallyOptimal isNotLocallyOptimal(&goodPlans,&badPlans, allActions, shortestLength,false); clock_t filter_begin = clock(); while (currentFirst != answerSets.end()) { //process the plans in groups of increasing length list<AnswerSet>::iterator currentLast = find_if(currentFirst,answerSets.end(),PlanLongerThan(currentFirst->maxTimeStep())); list<AnswerSetRef> goodPointers; remove_copy_if(currentFirst,currentLast,back_inserter(goodPointers),isNotLocallyOptimal); for_each(goodPointers.begin(),goodPointers.end(),PolicyMerger(policy)); transform(goodPointers.begin(),goodPointers.end(),inserter(goodPlans, goodPlans.begin()), CleanPlan(allActions)); currentFirst = currentLast; } clock_t filter_end = clock(); set< list <AspFluentRef>, LexComparator >::const_iterator printIt = goodPlans.begin(); for (; printIt != goodPlans.end(); ++printIt) { copy(printIt->begin(),printIt->end(),ostream_iterator<string>(cout, " ")); cout << endl; } cout << "filtering took " << (double(filter_end - filter_begin) / CLOCKS_PER_SEC) << " seconds" << endl; return policy; }