Small Vectors Proposal
Mikola Lysenko
mclysenk at mtu.edu
Tue Jan 30 13:08:28 PST 2007
Frits van Bommel wrote:
> Mikola Lysenko wrote:
>> Hmm... Fair enough. We could define ordering on a per component
>> level arbitrarily so associative arrays work. I could see this being
>> confusing in some situations, but it would be worth it in order to
>> make the types consistent. Therefore:
>>
>> float3(x, y, z) < float3(a, b, c)
>>
>> would translate too:
>>
>> (x < a) ? true : ((y < b) ? true : (z < c))
>
> Your code is wrong when e.g. (x > a) and (y < b). It returns false true
> while it should be false; x != a means ((x, y, z) < (a, b, c)) == (x < a).
>
> Think of the order of words in a dictionary: first you compare the first
> character, if it's equal you go on to the second, and so on. The first
> character that differs determines the sort order.
>
> It'd be implemented something like this:
> ---
> int compare (short3 a, short3 b)
> if (a.x != b.x) return a.x - b.x;
> if (a.y != b.y) return a.y - b.y;
> if (a.z != b.z) return a.z - b.z;
> return 0; // all elements equal, so the vectors are equal
> }
> ---
> Note 1: I used shorts to produce clear code but avoid overflows.
> Note 2: This uses the opCmp/TypeInfo.compare return value convention:
> negative means a < b, positive means a > b, zero means equality.
>
Whoops! I wasn't really thinking too carefully when I wrote that.
You're correct, a lexicographic ordering is definitely the simplest in
this case.
More information about the Digitalmars-d
mailing list