Files
Common-VS2019/SacsStandardBase/Base/Utils/ListUtil.cs
T
2024-06-17 11:48:16 +01:00

107 lines
3.4 KiB
C#

using Sarand.Net.Standard.Base.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sarand.Net.Standard.Base.Utils
{
//=======================================================================
public static class ListUtil
{
//-------------------------------------------------------------------
public static void ClearAndDispose<T>(this IList<T> values) where T : IDisposable
{
while (values.Count > 0)
{
T value = values[0];
values.RemoveAt(0);
value.Dispose();
}
}
//-------------------------------------------------------------------
/// <summary>Is the list empty</summary>
public static Boolean IsEmpty<T>(this IEnumerable<T> items)
{
return !items.Any();
}
//-------------------------------------------------------------------
/// <summary>
/// Is the value in the list of items.
/// <para>NB String values are compared case-sensitive.</para>
/// <para>Use InCI() for case-insensitive string comparison.</para>
/// </summary>
public static Boolean In<T>(this T item, IEnumerable<T> items)
{
return items.Contains(item);
}
/// <summary>Is the String in the list of items [case-insensitive]</summary>
public static Boolean InCI(this String item, IEnumerable<String> items)
{
return items.ContainsCI(item);
}
//-------------------------------------------------------------------
public enum Equality
{
/// <summary>Exact - exact match between items in each list.</summary>
Exact = 0x01,
/// <summary>Unique values only - ignores number of instances of each value.</summary>
UniqueValuesOnly = 0x02,
}
/// <summary>
/// Compare content of two lists.
/// <para>- Exact ensures precise number and sequence of elements.</para>
/// <para>- Unique allows any number and sequence as long as each item is present in both lists.</para>
/// </summary>
public static bool Equals<T>(this IList<T> sourceList, IList<T> targetList, Equality equality)
{
bool sameLength = (sourceList.Count == targetList.Count);
if (equality == Equality.Exact)
{
// List must contain exactly the same members in each position.
if (!sameLength) return false;
for (int idx = 0; idx < sourceList.Count; idx++)
{
if (!sourceList[idx].Equals(targetList[idx])) return false;
}
return true;
}
// Compare just the unique values.
if (sourceList.Except(targetList).Any()) return false;
if (targetList.Except(sourceList).Any()) return false;
return true;
}
//-------------------------------------------------------------------
/// <summary>
/// Sort the list into random order.
/// </summary>
public static List<T> Randomise<T>(this List<T> values)
{
return values.OrderBy(_ => SacsRandomHelper.GetInt32(Int32.MaxValue)).ToList();
}
//-------------------------------------------------------------------
/// <summary>
/// Remove the specified range of items from the list to a new list.
/// </summary>
public static List<T> ExtractRange<T>(this List<T> values, int index, int count)
{
List<T> extractedValues = new List<T>();
if (values.Any())
{
count = Math.Min(values.Count(), count);
extractedValues.AddRange(values.GetRange(index, count));
values.RemoveRange(index, count);
}
return extractedValues;
}
//-------------------------------------------------------------------
}
//=======================================================================
}