2023-06-11 18:36:02 +00:00
|
|
|
namespace AdventOfCode.Utils.Extensions;
|
2023-06-08 12:59:21 +00:00
|
|
|
|
2023-06-08 23:54:44 +00:00
|
|
|
public static class LinqExtensions
|
2023-06-08 12:59:21 +00:00
|
|
|
{
|
|
|
|
/// <summary>
|
2023-06-24 21:26:39 +00:00
|
|
|
/// Returns a slice of the given list
|
2023-06-08 12:59:21 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="list">The list you pretend to slice</param>
|
|
|
|
/// <param name="startIndex">The first index of the slice</param>
|
|
|
|
/// <param name="endIndex">The index after the last index of the slice</param>
|
|
|
|
/// <returns></returns>
|
2023-06-11 18:36:02 +00:00
|
|
|
public static List<T> Sublist<T>(this List<T> list, int startIndex, int endIndex = default)
|
2023-06-08 12:59:21 +00:00
|
|
|
{
|
2023-06-24 21:26:39 +00:00
|
|
|
var result = new List<T>();
|
2023-06-11 18:36:02 +00:00
|
|
|
if (endIndex == default)
|
|
|
|
endIndex = list.Count;
|
2023-06-24 21:26:39 +00:00
|
|
|
for (var i = startIndex; i < endIndex; i++)
|
2023-06-08 12:59:21 +00:00
|
|
|
result.Add(list[i]);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2023-06-24 21:26:39 +00:00
|
|
|
/// Returns a column of a give matrix list
|
2023-06-08 12:59:21 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="list">2 dimensions list</param>
|
|
|
|
/// <param name="column">Column index</param>
|
|
|
|
/// <returns></returns>
|
|
|
|
public static List<T> GetColumn<T>(this List<List<T>> list, int column)
|
|
|
|
{
|
2023-06-24 21:26:39 +00:00
|
|
|
var result = new List<T>();
|
|
|
|
foreach (var row in list) result.AddRange(row.Where((t, i) => i == column));
|
2023-06-08 12:59:21 +00:00
|
|
|
return result;
|
|
|
|
}
|
2023-06-08 17:29:44 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2023-06-24 21:26:39 +00:00
|
|
|
/// Reverses a given list
|
2023-06-08 17:29:44 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="list">The list to reverse</param>
|
|
|
|
/// <returns>The reversed list</returns>
|
|
|
|
public static List<T> Reversed<T>(this List<T> list)
|
|
|
|
{
|
2023-06-24 21:26:39 +00:00
|
|
|
var reversed = new List<T>();
|
|
|
|
for (var i = 0; i < list.Count; i++)
|
|
|
|
reversed.Add(list[list.Count - 1 - i]);
|
2023-06-08 17:29:44 +00:00
|
|
|
return reversed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
2023-06-24 21:26:39 +00:00
|
|
|
/// Clones a list of elements
|
2023-06-08 17:29:44 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="original">The original list</param>
|
|
|
|
/// <returns>A copy of the original list</returns>
|
|
|
|
public static List<T> Clone<T>(this List<T> original)
|
|
|
|
{
|
2023-06-24 21:26:39 +00:00
|
|
|
var ret = new List<T>(original.Count);
|
|
|
|
foreach (var element in original)
|
2023-06-08 17:29:44 +00:00
|
|
|
ret.Add(element);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
public static Dictionary<TKey, TValue> Clone<TKey, TValue>
|
|
|
|
(this Dictionary<TKey, TValue> original)
|
|
|
|
where TValue : ICloneable
|
|
|
|
where TKey : notnull
|
|
|
|
{
|
2023-06-24 21:26:39 +00:00
|
|
|
var ret = new Dictionary<TKey, TValue>(original.Count, original.Comparer);
|
|
|
|
foreach (var entry in original)
|
|
|
|
ret.Add(entry.Key, (TValue)entry.Value.Clone());
|
2023-06-08 17:29:44 +00:00
|
|
|
return ret;
|
|
|
|
}
|
2023-06-24 21:26:39 +00:00
|
|
|
|
2023-06-08 17:29:44 +00:00
|
|
|
public static Dictionary<TKey, List<TValue>> Clone<TKey, TValue>
|
|
|
|
(this Dictionary<TKey, List<TValue>> original)
|
|
|
|
where TKey : notnull
|
|
|
|
{
|
2023-06-24 21:26:39 +00:00
|
|
|
var ret = new Dictionary<TKey, List<TValue>>(original.Count, original.Comparer);
|
|
|
|
foreach (var entry in original)
|
2023-06-08 17:29:44 +00:00
|
|
|
ret.Add(entry.Key, entry.Value.Clone());
|
|
|
|
return ret;
|
|
|
|
}
|
2023-06-08 23:43:55 +00:00
|
|
|
|
|
|
|
public static void Fill<T>(this List<T> list, int count, T element)
|
2023-06-24 21:26:39 +00:00
|
|
|
where T : ICloneable
|
2023-06-08 23:43:55 +00:00
|
|
|
{
|
2023-06-24 21:26:39 +00:00
|
|
|
for (var i = 0; i < count; i++)
|
2023-06-08 23:43:55 +00:00
|
|
|
list.Add(element);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Fill<T>(this List<List<T>> list, int count, List<T> element)
|
|
|
|
{
|
2023-06-24 21:26:39 +00:00
|
|
|
for (var i = 0; i < count; i++)
|
2023-06-08 23:43:55 +00:00
|
|
|
list.Add(element.Clone());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void Set<T>(this List<T> list, int index, T element)
|
|
|
|
{
|
2023-06-24 21:26:39 +00:00
|
|
|
var backup = list.Clone();
|
2023-06-08 23:43:55 +00:00
|
|
|
list.Clear();
|
2023-06-24 21:26:39 +00:00
|
|
|
for (var i = 0; i < list.Count; i++) list.Add(i == index ? element : list[i]);
|
2023-06-08 23:43:55 +00:00
|
|
|
}
|
2023-06-08 12:59:21 +00:00
|
|
|
}
|