sort error
snow
marcel.patzwahl at gmail.com
Sat Jun 29 05:46:13 PDT 2013
On Friday, 28 June 2013 at 17:07:22 UTC, Ali Çehreli wrote:
> On 06/28/2013 07:00 AM, snow wrote:> Hello there,
> > Ive got the following code
> >
> > http://dpaste.dzfl.pl/e391a268
> >
> > This code throws me a "Range Exception" in Algorithm.d.
> >
> > If I use a lower number of random vectors, like 100, the code
> > terminates. Also, if I delete the template instruction like
> this :
> >
> > sort(individuals);
> >
> > I also don't get an exception.
>
> Yes, what is thrown is an Error. (Error and Exception are
> different hierarchies under Throwable.)
>
> > Does anybody know, why this is the case?
>
> Your opCmp does not provide a complete ordering of objects:
>
> int opCmp(ref const Vector3D vec) {
> if (this.x > vec.x && this.y > vec.y && this.z > vec.z)
> return 1;
>
> if (this.x < vec.x && this.y < vec.y && this.z < vec.z)
> return -1;
>
> return 0;
> }
>
> According to that function, the following two values are
> neither less than nor greater than the other:
>
> // passes:
> assert(!(Vector3D(1, 2, 3) < Vector3D(2, 1, 3)) &&
> !(Vector3D(1, 2, 3) > Vector3D(2, 1, 3)));
>
> Ali
I tried this now:
int opCmp(ref const Vector3D vec) {
if (this.x > vec.x && this.y > vec.y && this.z > vec.z)
return 1;
return -1;
}
this should be a total ordering, because a Vector is always
greater or smaller, than another, but I still get the same
exception.
More information about the Digitalmars-d-learn
mailing list