void data_dpt::dp_merge() { if(size()<2) return; unsigned initial_size=size(); unsigned from=0; unsigned to=0; /* look for similar elements */ for(const_iterator it1=begin(); it1!=end(); ++it1) { const_iterator it2=it1; ++it2; /* all ok -- ends */ if(it2==end()) return; for(; it2!=end(); ++it2) { if(it1 == it2) { from=it2->eq_class; to=it1->eq_class; erase(it2); break; } } } /* merge */ for(iterator it3=begin(); it3!=end(); ++it3) if(it3->eq_class==from) it3->eq_class=to; /* strictly monotonous => converges */ assert(initial_size>size()); /* repeat until classes are disjunct */ dp_merge(); }
/// merge in N^3 void data_dpt::dp_merge() { if(data.size()<2) return; unsigned initial_size=data.size(); unsigned from=0; unsigned to=0; /* look for similar elements */ for(auto it1=data.cbegin(); it1!=data.cend(); ++it1) { auto it2=it1; ++it2; /* all ok -- ends */ if(it2==data.cend()) return; for(; it2!=data.cend(); ++it2) { if(it1 == it2) { from=it2->eq_class; to=it1->eq_class; data.erase(it2); break; } } } /* merge */ for(auto it3=data.begin(); it3!=data.end(); ++it3) if(it3->eq_class==from) it3->eq_class=to; /* strictly monotonous => converges */ INVARIANT(initial_size>data.size(), "strictly monotonous => converges"); /* repeat until classes are disjunct */ dp_merge(); }