mercredi 21 décembre 2016

merging adjacent entries from a std::set

I have an STL set containing special string elements that are ordered according to a custom predicate. Each string uses its horizontal position (not the text) to order itself. I am trying to merge adjacent strings on a line together using a single string object by merging the strings that go end to end together. So far I have managed to find these adjacent set elements

I need to go through this std::set copying non adjacent elements and then merge together the adjacent elements using the specified predicate (see lambda below). Could someone help me with the details.

This is the set ordering comparator which I use to construct a set of these special VCDUText elements (basically strings with an X,Y and some extra attribute stuff). The comparator basically orders the text according to its offset from the screen with MAXCOL columns.

static const auto gPositionComp =
    [](const VCDUText& lhs, const VCDUText& rhs) -> bool {
        auto lhsPos = lhs.mY * MAXCOL + lhs.mX;
        auto rhsPos = rhs.mY * MAXCOL + rhs.mX;
        return lhsPos < rhsPos;
    };

The removedText is initialized below (not shown). The place that I am having trouble is somehow trying to copy elements from the removedText to tempResult, while at the same time merging the adjacent entries. Note all the elements may be adjacent in which case the result should be just 1 element containing the merged strings.

        std::set<VCDUText, decltype(gPositionComp)> removedText(gPositionComp);
        // initialized here ...

        //....
        // transform removed entries to spaces
        std::set<VCDUText, decltype(gPositionComp)> tempResult (gPositionComp);
        std::for_each(removedText.cbegin(), removedText.cend(),
            [&tempResult](const VCDUText& rNext) {
                tempResult.emplace(rNext.mText, WHITE, BIG_CHAR, NONE, rNext.mX, rNext.mY);
            });

        auto adjIter = std::adjacent_find(tempResult.cbegin(), tempResult.cend(), 
            [](const VCDUText& lhs, const VCDUText& rhs){
                if (lhs.mX + lhs.mText.size() == rhs.mX) {
                    return true;
                }
                return false;
            });

        std::set<VCDUText, decltype(gPositionComp)> temp1 (gPositionComp);
        while (adjIter != tempResult.cend()) {
            // HELP NEEDED HERE HOW DO I MERGE THE ADJACENT ELEMENTS 
        }

Aucun commentaire:

Enregistrer un commentaire