sort error

Ali Çehreli acehreli at yahoo.com
Fri Jun 28 10:07:21 PDT 2013


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



More information about the Digitalmars-d-learn mailing list