The C++20 standard introduces a comparison operator “<=>” (Spaceship operator), which is used to compare two values and return the result of the comparison. Compared to sequential size checks, the “Spaceship operator” simplifies the process of comparing values, making the code more concise.The result returned by the Spaceship operator belongs to strong_ordering, partial_ordering, and weak_ordering. Since vector components can be floating-point numbers, partial_ordering is used as the type for storing the result.In DirectX Math, the approach to comparing vector values is as follows: sequentially check the corresponding x, y, z, and w component values of the two vectors. If they are not equal, the final comparison result is determined by the current component’s comparison result, continuing until the last component is compared. For example, for vectors (1,2,3,4) and (1,2,3,1), the first three components are equal, so the final result is determined by the comparison of the fourth component.The following code implements the overloaded Spaceship operator for comparison and provides test examples. This overloaded method is applicable for comparing one to four-dimensional vectors. The code can run independently.
import <Windows.h>;import <iostream>;import <DirectXMath.h>;import <DirectXPackedVector.h>;
using namespace std;using namespace DirectX;using namespace DirectX::PackedVector;
ostream& XM_CALLCONV operator<<(ostream& os, FXMVECTOR v){
XMFLOAT4 dest;
XMStoreFloat4(&dest, v);
os << "(" << dest.x << ", " << dest.y << ", " << dest.z << ", " << dest.w << ")";
return os;
}
partial_ordering XM_CALLCONV operator<=>(FXMVECTOR u, FXMVECTOR v){
XMFLOAT4 firstVec{}, secondVec{};
XMStoreFloat4(&firstVec, u);
XMStoreFloat4(&secondVec, v);
partial_ordering cmpX{ firstVec.x <> secondVec.x };
partial_ordering cmpY{ firstVec.y <> secondVec.y };
partial_ordering cmpZ{ firstVec.z <> secondVec.z };
partial_ordering cmpW{ firstVec.w <> secondVec.w };
if (cmpX != partial_ordering::equivalent)
return cmpX;
else if (cmpY != partial_ordering::equivalent)
return cmpY;
else if (cmpZ != partial_ordering::equivalent)
return cmpZ;
else
return cmpW;
}
void showResult(const partial_ordering& result){
if (result == partial_ordering::equivalent)
cout << "equivalent" << endl;
if (result == partial_ordering::less)
cout << "less" << endl;
if (result == partial_ordering::greater)
cout << "greater" << endl;
}
int main(){
if (!XMVerifyCPUSupport())
{
cout << "DirectX Math not supported" << endl;
return 0;
}
XMVECTOR vec1{ XMVectorSet(1.0f,2.0f,3.0f,4.0f) };
XMVECTOR vec2{ XMVectorSet(1.0f,0.0f,0.0f,0.0f) };
XMVECTOR vec3{ XMVectorSet(0.0f,2.0f,0.0f,0.0f) };
XMVECTOR vec4{ XMVectorSet(0.0f,0.0f,3.0f,0.0f) };
XMVECTOR vec5{ XMVectorSet(0.0f,0.0f,0.0f,4.0f) };
//equivalent
auto result1{ vec1 <> (vec2 + vec3 + vec4 + vec5) };
//less
auto result2{ vec2 <> vec1 };
//greater
auto result3{ vec1 <> vec3 };
cout << "Vector 1: " << vec1 << endl;
cout << "Vector 2: " << vec2 << endl;
cout << "Vector 3: " << vec3 << endl;
cout << "Vector 4: " << vec4 << endl;
cout << "Vector 5: " << vec5 << endl << endl;
cout << "result 1: Vector 1 compare with (Vector 2 + ... + Vector 5): ";
showResult(result1);
cout << "result 2: Vector 2 compare with Vector 1: ";
showResult(result2);
cout << "result 3: Vector 1 compare with Vector 3: ";
showResult(result3);
return 0;
}
This is a personal opinion and is for reference only.