| | | 1 | | using System.Collections; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Runtime.CompilerServices; |
| | | 4 | | using System.Runtime.Serialization; |
| | | 5 | | |
| | | 6 | | namespace SwiftCollections; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Provides a randomized equality comparer for objects, enhancing hash code distribution to reduce collisions in hash-b |
| | | 10 | | /// </summary> |
| | | 11 | | internal sealed class SwiftObjectEqualityComparer : IEqualityComparer<object>, IEqualityComparer, ISerializable, IRandom |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// A 64-bit entropy value used to randomize hash codes for better distribution. |
| | | 15 | | /// </summary> |
| | | 16 | | private readonly long _entropy; |
| | | 17 | | |
| | | 18 | | /// <summary> |
| | | 19 | | /// Initializes a new instance of the <see cref="SwiftStringEqualityComparer"/> class with a unique entropy value. |
| | | 20 | | /// </summary> |
| | 4 | 21 | | public SwiftObjectEqualityComparer() |
| | 4 | 22 | | { |
| | 4 | 23 | | _entropy = SwiftHashTools.GetEntropy(); |
| | 4 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <summary> |
| | | 27 | | /// Determines whether the specified objects are equal. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <param name="x">The first object to compare.</param> |
| | | 30 | | /// <param name="y">The second object to compare.</param> |
| | | 31 | | /// <returns><c>true</c> if the specified objects are equal; otherwise, <c>false</c>.</returns> |
| | | 32 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 33 | | public new bool Equals(object x, object y) |
| | 5 | 34 | | { |
| | | 35 | | // Use a single null check for both objects to minimize branching |
| | 5 | 36 | | return x == y || (x != null && y != null && x.Equals(y)); |
| | 5 | 37 | | } |
| | | 38 | | |
| | | 39 | | /// <summary> |
| | | 40 | | /// Determines whether the specified object is equal to the current comparer. |
| | | 41 | | /// </summary> |
| | | 42 | | /// <param name="obj">The object to compare with the current comparer.</param> |
| | | 43 | | /// <returns><c>true</c> if the specified object is equal to the current comparer; otherwise, <c>false</c>.</returns |
| | | 44 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 2 | 45 | | public override bool Equals(object obj) => obj is SwiftObjectEqualityComparer other && _entropy == other._entropy; |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Returns a hash code for the specified object, incorporating entropy for better distribution. |
| | | 49 | | /// </summary> |
| | | 50 | | /// <param name="obj">The object for which to get a hash code.</param> |
| | | 51 | | /// <returns>A hash code for the specified object.</returns> |
| | | 52 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 53 | | public int GetHashCode(object obj) |
| | 5 | 54 | | { |
| | 6 | 55 | | if (obj == null) return 0; |
| | 6 | 56 | | if (obj is string text) return SwiftHashTools.MurmurHash3(text, (int)(_entropy & 0x7FFFFFFF)); |
| | 2 | 57 | | return obj.GetHashCode() ^ (int)(_entropy & 0x7FFFFFFF); |
| | 5 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Returns a hash code for the current comparer. |
| | | 62 | | /// </summary> |
| | | 63 | | /// <returns>A hash code for the current comparer.</returns> |
| | | 64 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | 2 | 65 | | public override int GetHashCode() => GetType().Name.GetHashCode() ^ (int)(_entropy & 0x7FFFFFFF); |
| | | 66 | | |
| | | 67 | | public void GetObjectData(SerializationInfo info, StreamingContext context) |
| | 1 | 68 | | { |
| | 1 | 69 | | info.AddValue("Entropy", _entropy); |
| | 1 | 70 | | } |
| | | 71 | | } |