| | 1 | | using System.Collections.Concurrent; |
| | 2 | |
|
| | 3 | | namespace MoreStructures.Utilities; |
| | 4 | |
|
| | 5 | | /// <summary> |
| | 6 | | /// An implementation of <see cref="IComparer{T}"/> for <see cref="char"/>, which compares chars taking into account |
| | 7 | | /// the fact that the char may be a special char, called terminator, which has to be considered smaller than any other |
| | 8 | | /// char. |
| | 9 | | /// </summary> |
| | 10 | | public class CharOrTerminatorComparer : IComparer<char> |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// The character acting as terminator, and which has to be considered smaller than any other char. |
| | 14 | | /// </summary> |
| 1103910 | 15 | | public char Terminator { get; } |
| | 16 | |
|
| 4 | 17 | | private CharOrTerminatorComparer(char terminator) |
| 4 | 18 | | { |
| 4 | 19 | | Terminator = terminator; |
| 4 | 20 | | } |
| | 21 | |
|
| 1 | 22 | | private static readonly ConcurrentDictionary<char, CharOrTerminatorComparer> _instances = new(); |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Builds a <see cref="CharOrTerminatorComparer"/> with the provided <paramref name="terminator"/>. Caches |
| | 26 | | /// instances. |
| | 27 | | /// </summary> |
| | 28 | | /// <param name="terminator"><inheritdoc cref="Terminator" path="/summary"/></param> |
| | 29 | | /// <returns>An instance of <see cref="CharOrTerminatorComparer"/>, new or previously created and cached.</returns> |
| | 30 | | public static CharOrTerminatorComparer Build(char terminator) |
| 11553 | 31 | | { |
| 11557 | 32 | | return _instances.GetOrAdd(terminator, t => new CharOrTerminatorComparer(t)); |
| 11553 | 33 | | } |
| | 34 | |
|
| | 35 | | /// <inheritdoc path="//*[not(self::summary or self::remarks)]"/> |
| | 36 | | /// <summary> |
| | 37 | | /// <inheritdoc/> |
| | 38 | | /// </summary> |
| | 39 | | /// <remarks> |
| | 40 | | /// Special rules applied by <see cref="CharOrTerminatorComparer"/>: |
| | 41 | | /// <br/> |
| | 42 | | /// - If one char is the terminator, and the other isn't, the one which is the terminator is smaller. |
| | 43 | | /// <br/> |
| | 44 | | /// - If none of the cases above applies, <see cref="char.CompareTo(char)"/> is used on <paramref name="x"/> |
| | 45 | | /// and <paramref name="y"/>. |
| | 46 | | /// </remarks> |
| | 47 | | public int Compare(char x, char y) |
| 378169 | 48 | | { |
| 378169 | 49 | | if (x == Terminator && y != Terminator) |
| 30660 | 50 | | return -1; |
| 347509 | 51 | | if (x != Terminator && y == Terminator) |
| 5392 | 52 | | return 1; |
| 342117 | 53 | | return x.CompareTo(y); |
| 378169 | 54 | | } |
| | 55 | |
|
| | 56 | | /// <inheritdoc path="//*[not(self::remarks)]"/> |
| | 57 | | /// <remarks> |
| | 58 | | /// Two <see cref="CharOrTerminatorComparer"/> are equal if they have the same <see cref="Terminator"/>. |
| | 59 | | /// </remarks> |
| | 60 | | public override bool Equals(object? obj) => |
| 30 | 61 | | obj is CharOrTerminatorComparer other && other.Terminator == Terminator; |
| | 62 | |
|
| | 63 | | /// <inheritdoc path="//*[not(self::remarks)]"/> |
| | 64 | | /// <remarks> |
| | 65 | | /// The <see cref="GetHashCode"/> is based on <see cref="Terminator"/> hash. |
| | 66 | | /// </remarks> |
| | 67 | | public override int GetHashCode() => |
| 4 | 68 | | Terminator.GetHashCode(); |
| | 69 | | } |