Failed to sort range

Sergei Nosov sergei.nosov at gmail.com
Tue May 28 06:03:56 PDT 2013


On Tuesday, 28 May 2013 at 12:57:12 UTC, Sergei Nosov wrote:
> Hi!
>
> I'm trying to implement an array, which uses malloc to allocate 
> memory. Also, I want to implement a random access range 
> interface for it.
>
> That went pretty well, until I tried to sort it. Sorting 
> function asserted "Failed to sort range of type Array!(int)."
>
> I've spent quite some time trying to figure out what's going on 
> with no success.
>
> The implementation can be found at:
> https://gist.github.com/snosov1/5662471
>
> I used
> DMD64 D Compiler v2.062 and
> LDC - the LLVM D compiler (trunk): based on DMD v2.062 and LLVM 
> 3.2svn
> on Ubuntu. Phobos version was the one that came with the dmd 
> compiler.
>
> Does anyone have any ideas what's wrong with the code I've 
> provided?

Forgot to mention, that my hand-made sorting function (simply a 
copy-paste of some quicksort implementation that uses array 
indexing syntax) works just fine

void qSort(alias less, Range)(Range A, int low, int high) {
     int i = low;
     int j = high;
     auto x = A[(low+high)/2];
     do {
         while(less(A[i], x)) ++i;
         while(less(x, A[j])) --j;
         if(i <= j){
             auto temp = A[i];
             A[i] = A[j];
             A[j] = temp;
             i++; j--;
         }
     } while(i < j);
     if(low < j) qSort!less(A, low, j);
     if(i < high) qSort!less(A, i, high);
}


More information about the Digitalmars-d-learn mailing list