If this has been asked before please forgive me, I could not find it.
I have a custom type for which I can implement (fuzzy) equality but no < operator that is transitive.
The comparison is costly but I have not many elements.
I need to sort out polygons that are alomst identical (they overlap to a large fraction). Since ordering using < is impossible due to the lack of a transitive implementation I am using a std::list like the following:
typedef std::list<Polygon> PolyList;
PolyList purged(rawList);
for (PolyList::iterator iter= purged.begin(); iter!= purged.end(); ++iter) {
for(PolyList::iterator toRemove = find(boost::next(iter),purged.end(),*iter); toRemove != purged.end(); ){
PolyList::iterator next = purged.erase(toRemove);
toRemove = find(next,purged.end(),*iter);
}
}
The complexity is n*n/2 which is unavoidable in my opinion and
while the algorithm works fine, it is still very cumbersome to read and write and I am almost sure there is a standard algorithm for it that I just don’t know or at least something as fast but neater to type. As I said sorting is not an option due to the fuzzyness of the data so no unique set or sort.
Many thanks in advance for helping me out
You’re probably not going to find an asnwer in the Standard, since your “duplicates” sound like they’re not transitive either. That’s to say that
a==b && b==cdoes not implya==c.For that reason alone, any algorithm has to compare all pairs, which gives you
(N*N-1)/2comparisons (assuming your equality is symmetric, i.e.a==bdoes implyb==a).