Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C#: Use HashCode.Combine() for basic composite types instead of xor #82240

Merged
merged 1 commit into from
Sep 26, 2023
Merged

C#: Use HashCode.Combine() for basic composite types instead of xor #82240

merged 1 commit into from
Sep 26, 2023

Conversation

jaros3
Copy link

@jaros3 jaros3 commented Sep 24, 2023

Vector2I.GetHashCode() creates hash conflicts if X and Y concide:
(0, 0) => 0.GetHashCode() ^ 0.GetHashCode() == 0
(1, 1) => 1.GetHashCode() ^ 1.GetHashCode() == 0
(2, 2) => 2.GetHashCode() ^ 2.GetHashCode() == 0

Let's use HashCode.Combine(X, Y) instead. It will combine hash codes properly and minimize conflicts.

@jaros3 jaros3 requested a review from a team as a code owner September 24, 2023 11:58
@Chaosus Chaosus added this to the 4.2 milestone Sep 24, 2023
@AThousandShips
Copy link
Member

AThousandShips commented Sep 24, 2023

This differs from all the other vectors, and all other types in fact

If this is to be done it needs to be done completely

@AThousandShips
Copy link
Member

AThousandShips commented Sep 24, 2023

This is not all cases either, you're missing AABB, Basis, Rect2(I), Transform2/3D, Quaternion, Plane, Projection

@AThousandShips AThousandShips changed the title Vector2I.GetHashCode() uses multiplication and addition instead of Xor [C#] Use multiplication instead of xor for GetHashCode() Sep 24, 2023
@AThousandShips AThousandShips changed the title [C#] Use multiplication instead of xor for GetHashCode() [C#] Use hash helper instead of xor for GetHashCode() Sep 24, 2023
@AThousandShips AThousandShips changed the title [C#] Use hash helper instead of xor for GetHashCode() [C#] Use HashCode.Combine instead of xor for GetHashCode() Sep 25, 2023
Copy link
Member

@RedworkDE RedworkDE left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@AThousandShips AThousandShips changed the title [C#] Use HashCode.Combine instead of xor for GetHashCode() [C#] Use HashCode.Combine() for basic composite types instead of xor Sep 25, 2023
@akien-mga
Copy link
Member

For the record, your commit seems not to be linked to your GitHub account. See: Why are my commits linked to the wrong user? for more info.

@akien-mga akien-mga merged commit 42011d8 into godotengine:master Sep 26, 2023
15 checks passed
@akien-mga
Copy link
Member

Thanks! And congrats for your first merged Godot contribution 🎉

Remember to add the email used for this commit as secondary email in your GitHub profile, if you want to claim this commit as authored by your GitHub account. Otherwise your next PR will still show as "First-time contributor".

@akien-mga akien-mga changed the title [C#] Use HashCode.Combine() for basic composite types instead of xor C#: Use HashCode.Combine() for basic composite types instead of xor Oct 3, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants