in Chemistry/src/DataModel/Extensions.cs [65:109]
internal static TValue GetValueOrDefault<TKey, TValue>(
this Dictionary<TKey, TValue> dictionary, TKey key,
TValue defaultValue = default
) =>
dictionary.TryGetValue(key, out var value)
? value
: defaultValue;
#endregion
#region Array Comparers
/// <summary>
/// Compares two equal-length sequences of integers by perform an element-wise
/// comparison, starting from the first element.
/// </summary>
/// <param name="xArr">First sequence of elements.</param>
/// <param name="yArr">Second sequence of elements.</param>
/// <param name="comparer">Comparer used to define ordering of elements.</param>
/// <returns>
/// Returns <c>1</c> if <paramref name="xArr"/> is greater than <paramref name="yArr"/>.
/// Returns <c>-1</c> if <paramref name="xArr"/> is less than <paramref name="yArr"/>.
/// Returns <c>0</c> if <paramref name="xArr"/> is equal to <paramref name="yArr"/>.
/// </returns>
/// <example>
/// CompareIntArray(new Int64[] {5}, new Int64[] {7}) == -1;
/// CompareIntArray(new Int64[] {5,7}, new Int64[] {5,6}) == 1;
/// CompareIntArray(new Int64[] {2,1,3}, new Int64[] {2,2,3}) == -1;
/// </example>
public static int CompareArray<TElement>(IEnumerable<TElement> xArr, IEnumerable<TElement> yArr, IComparer<TElement> comparer = null)
{
var useComparer = comparer == null ? Comparer<TElement>.Default : comparer;
foreach (var item in xArr.Zip(yArr, (x, y) => (x, y)))
{
if (useComparer.Compare(item.y, item.x) > 0)
{
return -1;
}
else if (useComparer.Compare(item.y, item.x) < 0)
{
return 1;
}
}
return 0;
}