Imagine that you have n sets of elements in a tuple. For example the tuple could be
std::tuple<topBottomStr, topBottomStr, topBottomStr> or
std::tuple<fraction, fraction, fraction>
So maybe there is some template that represents "topbottomthings"
template<typename T>
class TopBottomThing
{
private:
T top;
T bottom;
};
The point is that what is in the tuple has a notion of a top and a bottom. My question is, I need a function that returns an
std::vector<std::tuple<TopBottomThing>> ArrangeTopBottomThingFunc(std::vector<std::tuple<TopBottomThing>> tup)
where the items in the return value
std::vector<std::tuple<TopBottomThing>> retVal;
are arranged so that each consecutive entry's TopBottomThing, the bottom of std::vector<n>
matches the top of std::vector<n + 1>
, matches the bottom of std::vector<n + 2>
etc, or the top of std::vector<n>
matches the top of std::vector<n + 1>
, or the bottom of std::vector<n>
matches the bottom of std::vector<n + 1>
. Only one case is guaranteed to happen as a constraint to the function. If the length of the input vector is n, either 0 matches or n - 1 matches are guaranteed.
For example, if std::vector<std::tuple<TopBottomThing>>
tup represents standard fractions:
2/3 4/5 3/2,
ArrangeTopBottomThingFunc with that std::vector<tup>
as input would return a sorted by this criterion an std::vector like this:
[0] = 3/2 [1] = 2/3 [2] = 4/5 (two matches top/bottom)
of if std::tuple tup had these fractions:
4/7 3/2 1/2
[0] = 3/2 [1] = 1/2 [2] = 4/7 (two matches bottom/bottom)
or
4/5 7/8 4/6
[0] 4/5 [1] = 4/6 [2] = 7/8 (two matches top/top)
or the degenerate case
4/5 6/3 2/7 (no matches)
would return in the order given.
or say std::tuple tup represents strings like this
"Straberry/Banana" "Blueberry/Kiwi" "Kiwi/Banana"
[0] = "Kiwi/Banana" [1] = "Straberry/Banana" [2] = "Blueberry/Kiwi"
At least two of the three (n) top or bottom are guaranteed to match. It would be great if the algorithm worked for any length of
std::vector<std::tuple<TopBottomThing>> input.
It simply sorts and returns an std::vector those things whose top/top or bottom/bottom or top/bottom match in ascending order. The rest that don't match in this way are simply added in any order.
Finally, it would be nice if the calling function understood how the thing was sorted by returning an enum like this:
enum class WHICHSORT { NO_MATCHES, BOTH_TOP, BOTH_BOTTOM, MIXED_TOP_BOTTOM };