Concatenate 2 ranges

Saurabh Das via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Nov 11 07:03:01 PST 2016


On Friday, 11 November 2016 at 13:30:17 UTC, RazvanN wrote:
> I am trying to concatenate 2 ranges of the same type 
> (SortedRange in my case). I have tried merge, join and chain, 
> but the problem is that the result is not an object of the type 
> of the initial ranges. For example:
>
> 1. If I use chain(r1, r2), the result will be an object of type 
> Result which I cannot cast to my specific type (SortedRange).
>
> 2. If I use merge(r1, r2), the result will be an object of type 
> Merge!(typeof(r1), typeof(r)).
>
> I know that I can use the .array property, but I think that 
> this iterates through all of my elements. Using 
> assumeSorted(chain(r1, r2).array) will return a SortedRange, 
> but I am not sure what the complexity for this operation is.
>
> Is there a way to concatenate 2 ranges (SortedRange in my case) 
> in O(1) time?

I think chain of 2 sorted ranges will not necessarily be sorted.

For example, chain([1, 2, 10], [4, 5, 11]) -> [1, 2, 10, 4, 5, 
11] which is not sorted.

You will need some kind of a merge to get a sorted range from 2 
already sorted ranges.

Thanks,
SD



More information about the Digitalmars-d-learn mailing list