lundi 1 octobre 2018

Merging certain elements in a c++ vector or similar data structure in the most efficient way

How do I merge certain elements in a vector or similar data structure by only iterating over the complete list once? Is there a more efficient way than what I have got?

I have a vector of vectors of points: std::vector<std::vector<cv::Point>> contours

And I need to compare always two of them and then decide if I want to merge them or continue comparing.

ContourMoments has some helper functions to calculate the distance between points for example. The function merge() only takes all the points from one ContourMoments object and adds them to the calling ContourMoments object.

for(auto it = contours.begin() ; it < contours.end() ; /*++it*/)
{
    if (it->size() < 5)
    {
        it = contours.erase(it);
        continue;
    }
    for (auto it2 = it + 1; it2 < contours.end(); /*++it2*/)
    {
        if (it2->size() < 5)
        {
            it2 = contours.erase(it2);
            continue; 
        }
        ContourMoments cm1(*it);
        ContourMoments cm2(*it2);
        if (cm1.centerDistanceTo(cm2) > 2.0)
        {
            ++counter;
            ++it2;
            continue;
        }
        cm1.merge(std::move(cm2));
        it2 = contours.erase(it2);
    }
    std::advance(it, counter);
}

The merge() function:

void merge(const ContourMoments &cm)
{
    contour.insert(contour.end(), std::make_move_iterator(cm.contour.begin()), std::make_move_iterator(cm.contour.end()));
    init();
}

Thanks in advance!

Best wishes

Aucun commentaire:

Enregistrer un commentaire