| | | 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 deterministic string comparer used by SwiftCollections when no comparer is supplied. |
| | | 10 | | /// </summary> |
| | | 11 | | internal sealed class SwiftDeterministicStringEqualityComparer : IEqualityComparer<string>, IEqualityComparer, ISerializ |
| | | 12 | | { |
| | | 13 | | private readonly int _seed; |
| | | 14 | | |
| | 6 | 15 | | public SwiftDeterministicStringEqualityComparer() : this(SwiftHashTools.DefaultDeterministicStringHashSeed) { } |
| | | 16 | | |
| | 7 | 17 | | internal SwiftDeterministicStringEqualityComparer(int seed) |
| | 7 | 18 | | { |
| | 7 | 19 | | _seed = seed; |
| | 7 | 20 | | } |
| | | 21 | | |
| | | 22 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 54085 | 23 | | public bool Equals(string x, string y) => x == y || (x != null && y != null && x.Equals(y)); |
| | | 24 | | |
| | | 25 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 26 | | public new bool Equals(object x, object y) |
| | 5 | 27 | | { |
| | 5 | 28 | | return x == y || (x != null && y != null && (x is string a && y is string b ? a.Equals(b) : x.Equals(y))); |
| | 5 | 29 | | } |
| | | 30 | | |
| | | 31 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | 3 | 32 | | public override bool Equals(object obj) => obj is SwiftDeterministicStringEqualityComparer other && _seed == other._ |
| | | 33 | | |
| | | 34 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 35 | | public int GetHashCode(string obj) |
| | 304854 | 36 | | { |
| | 304855 | 37 | | if (obj == null) return 0; |
| | 304853 | 38 | | return SwiftHashTools.MurmurHash3(obj, _seed); |
| | 304854 | 39 | | } |
| | | 40 | | |
| | | 41 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 42 | | public int GetHashCode(object obj) |
| | 3 | 43 | | { |
| | 4 | 44 | | if (obj == null) return 0; |
| | 2 | 45 | | if (obj is string text) return SwiftHashTools.MurmurHash3(text, _seed); |
| | 2 | 46 | | return obj.GetHashCode() ^ _seed; |
| | 3 | 47 | | } |
| | | 48 | | |
| | | 49 | | [MethodImpl(MethodImplOptions.NoInlining)] |
| | 2 | 50 | | public override int GetHashCode() => GetType().Name.GetHashCode() ^ _seed; |
| | | 51 | | |
| | | 52 | | public void GetObjectData(SerializationInfo info, StreamingContext context) |
| | 1 | 53 | | { |
| | 1 | 54 | | info.AddValue("Seed", _seed); |
| | 1 | 55 | | } |
| | | 56 | | } |