Sorted ranges in combined sorted order?

Nordlöw via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Thu Oct 20 14:14:14 PDT 2016


On Thursday, 20 October 2016 at 20:49:38 UTC, Ali Çehreli wrote:
> Given a range of ranges where individual ranges are already 
> sorted, is there anything in Phobos that can visit the combined 
> range in sorted order?
>
> Although the range elements are not necessarily arrays, e.g.
>
>     [ [ 3, 10, 20 ]
>       [ 1, 2, 7 ]
>       [ 5, 6 ] ]
>
> The elements should appear without any copying as
>
>     [ 1, 2, 3, 5, 6, 7, 10, 20 ]
>
> The outer range may be unsorted but luckily it has very few 
> elements so a linear search should be fine.
>
> Ali

I have a variadic implemention of `std.algorithm.merge` lying 
around that does this. It includes all the D bells and whistles 
of lazy range including infiniteness propagation when possible.

Maybe it's time to get it in:

https://github.com/dlang/phobos/pull/3315#issuecomment-243089368

Note that it was first merged and then reverted by Andrei because 
he thought the existing `setUnion` at

https://dlang.org/phobos/std_algorithm_setops.html#setUnion

is a superset of `merge`. But @wilzbach and others want it merged 
:) anyway. I'll do another try. In the meantime you can use 
setUnion.

I also recall a discussion about uniqueness handling, but have 
forgotten the details.


More information about the Digitalmars-d-learn mailing list