The question
I have two ranges, call them v,w
that are sorted in a given fashion and can be compared (call the order relation T
). I want to compare them lexicographically but after sorting them in a different way (call this other order relation S
). For this I do not really need the ranges to be completely sorted: I only need to lazily evaluate the elements on the sorted vectors until I find a difference. For example if the maximum of v
in this new order is larger than the maximum of w
, then I need to only look once in the ordered vectors. In the worst case that v == w
I'd look up in all elements.
I understand that C++20 std::ranges::views
allows me to get a read only view of v
and w
that is lazily evaluated. Is it possible to get a custom sorted view that is still lazily evaluated? if I were able to define some pseudocode like
auto v_view_sorted_S = v | std::views::lazily_sort();
auto w_view_sorted_S = w | std::views::lazily_sort();
Then I could simply call std::ranges::lexicographical_compare(v_view_sorted_S, w_view_sorted_S)
.
How does one implement this?
Would simply calling std::ranges::sort(std::views::all(v))
work? in the sense that will it accept a view instead of an actual range and more importantly evaluate the view lazily? I get from the comments to the reply in this question that with some conditions std::ranges::sort
can be applied to views, and even transformed ones. But I suspect that it sorts them at the call time, is that the case?
The case I want it used:
I am interested in any example but the very particular use case that I have is the following. It is irrelevant for the question, but helps putting this in context
The structures v
and w
are of the form
std::array<std::vector<unsigned int>,N> v;
Where N
is a compile-time constant. Moreover, for each 0 <= i < N
, v[i]
is guaranteed to be non-increasing. The lexicographical order thus obtained for any two ordered arrays is what I called T
above.
What I am interested is in comparing them by the following rule: given an entry a = v[i][j]
and b = v[k][l]
with 0 <= i,k < N
and j,l >= 0
. Then declare a > b
if that relation holds as unsigned integers or if a == b
as unsigned integers and i < k
.
After ordering all entries of v
and w
with respect to this order, then I want to compare them lexicographically.
Example, if v = {{2,1,1}, {}, {3,1}}
, w = {{2,1,0}, {2}, {3,0}}
and z = {{2,1,0}, {3}, {2,0}}
, then z > w > v
.