| | | 1 | | using AmbientServices.Utilities; |
| | | 2 | | using System; |
| | | 3 | | |
| | | 4 | | namespace AmbientServices.Extensions; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// A static class that extends <see cref="System.Array"/>. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <remarks> |
| | | 10 | | /// <pitch>Value semantics for arrays: compare two arrays by their contents and hash an array by its contents, for use i |
| | | 11 | | /// <pledge>Equality is deep: element order is significant, and elements that are themselves arrays are compared by valu |
| | | 12 | | /// </remarks> |
| | | 13 | | public static class ArrayExtensions |
| | | 14 | | { |
| | | 15 | | /// <summary> |
| | | 16 | | /// Compares two arrays of arbitrary type to see if the contents are the same. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <typeparam name="TYPE">The type of item in the array.</typeparam> |
| | | 19 | | /// <param name="array1">The first array to compare.</param> |
| | | 20 | | /// <param name="array2">The second array to compare.</param> |
| | | 21 | | /// <returns><b>true</b> if the contents of the arrays are the same, <b>false</b> if the contents of the arrays are |
| | | 22 | | public static bool ValueEquals<TYPE>(this TYPE[] array1, TYPE[] array2) |
| | | 23 | | { |
| | 2 | 24 | | return ArrayUtilities.ValueEquals(typeof(TYPE), array1, array2); |
| | | 25 | | } |
| | | 26 | | /// <summary> |
| | | 27 | | /// Gets a hash code for the array based on the value of the array. |
| | | 28 | | /// </summary> |
| | | 29 | | /// <typeparam name="TYPE">The type of item in the array.</typeparam> |
| | | 30 | | /// <param name="array">The array to get the value hash code for.</param> |
| | | 31 | | /// <returns>A hash code based on the values in the array.</returns> |
| | | 32 | | public static int ValueHashCode<TYPE>(this TYPE[] array) |
| | | 33 | | { |
| | | 34 | | #if NET5_0_OR_GREATER |
| | 2 | 35 | | ArgumentNullException.ThrowIfNull(array); |
| | | 36 | | #else |
| | | 37 | | if (array is null) throw new ArgumentNullException(nameof(array)); |
| | | 38 | | #endif |
| | | 39 | | // delegate to the shared engine so the hash recurses into nested arrays exactly where ValueEquals does (jagged |
| | 2 | 40 | | return ArrayUtilities.ValueHashCode(typeof(TYPE), array); |
| | | 41 | | } |
| | | 42 | | } |