Ranges help
Christophe
travert at phare.normalesup.org
Fri Oct 14 01:34:19 PDT 2011
Xinok , dans le message (digitalmars.D.learn:30054), a écrit :
> This is in relation to my sorting algorithm. This is what I need to
> accomplish with ranges in the most efficient way possible:
>
> 1. Merge sort - This involves copying elements to a temporary buffer,
> which can simply be an array, then merging the two lists together. The
> important thing is that it may merge left to right, or right to left,
> which requires a bidirectional range.
>
> c[] = a[0..$/2];
> foreach(a; arr) if(!b.empty && !c.empty) if(b.front <= c.front){
> a = b.front; b.popFront();
> } else{
> a = c.front; c.popFront();
> }
>
> 2. Range swap - First, I need to do a binary search, which requires a
> random access range. Then I need to swap two ranges of elements.
>
> while(!a.empty && !b.empty){
> swap(a.front, b.front);
> a.popFront(); b.popFront();
> }
>
>
> That's the best I can come up with. I'm wondering if there's a more
> efficient way to accomplish what I have above.
>
> I also need to figure out the template constraints. Would this be
> correct? Or would this be too much?
>
> isRandomAccessRange && !isFiniteRange && isBidirectionalRange && hasSlicing
You should look at:
std.algorithm.SetUnion
std.algorithm.swapRanges
--
Christophe
More information about the Digitalmars-d-learn
mailing list