Skip to content

Commit

Permalink
organize, optimize, & add new extension methods
Browse files Browse the repository at this point in the history
  • Loading branch information
radj307 committed Oct 10, 2023
1 parent f938632 commit b340cfe
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 42 deletions.
35 changes: 3 additions & 32 deletions VolumeControl.TypeExtensions/EnumerableExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,9 @@ public static void ForEach<T, TReturn>(this IEnumerable<T> enumerable, Func<T, T
func(item);
}
}
/// <summary>Performs the specified <paramref name="action"/> on each <paramref name="enumerable"/> element.</summary>
/// <summary>
/// Performs the specified <paramref name="action"/> on each <paramref name="enumerable"/> element.
/// </summary>
/// <returns><paramref name="enumerable"/>, allowing this method to be used in a pipeline.</returns>
public static IEnumerable<T> ForwardForEach<T>(this IEnumerable<T> enumerable, Action<T> action) where T : class
{
Expand All @@ -62,37 +64,6 @@ public static IEnumerable<T> ForwardForEach<T>(this IEnumerable<T> enumerable, A
return enumerable;
}
/// <summary>
/// Converts each element in the given <paramref name="enumerable"/> from type <typeparamref name="TIn"/> to type <typeparamref name="TOut"/>, and returns the converted objects as a new list of type <typeparamref name="TList"/>.
/// </summary>
/// <typeparam name="TList">The type of list to return.</typeparam>
/// <typeparam name="TOut">The type to convert each item to.</typeparam>
/// <typeparam name="TIn">The type to convert each item from.</typeparam>
/// <param name="enumerable">Any type implementing <see cref="IEnumerable{T}"/>.</param>
/// <param name="converter">A converter method that accepts <typeparamref name="TIn"/> and returns <typeparamref name="TOut"/>.</param>
/// <returns>The converted list of items.</returns>
public static TList ConvertEach<TList, TOut, TIn>(this IEnumerable<TIn> enumerable, Func<TIn, TOut> converter) where TList : IList, IEnumerable, IList<TOut>, IEnumerable<TOut>, ICollection, ICollection<TOut>, new()
{
TList l = new();
foreach (TIn? item in enumerable)
l.Add(converter(item));
return l;
}
/// <summary>
/// Converts each element in the given <paramref name="enumerable"/> from type <typeparamref name="TIn"/> to type <typeparamref name="TOut"/>, and returns the converted objects as a new <see cref="List{T}"/>.
/// </summary>
/// <typeparam name="TOut">The type to convert each item to.</typeparam>
/// <typeparam name="TIn">The type to convert each item from.</typeparam>
/// <param name="enumerable">Any type implementing <see cref="IEnumerable{T}"/>.</param>
/// <param name="converter">A converter method that accepts <typeparamref name="TIn"/> and returns <typeparamref name="TOut"/>.</param>
/// <returns>The converted list of items.</returns>
public static List<TOut> ConvertEach<TOut, TIn>(this IEnumerable<TIn> enumerable, Func<TIn, TOut> converter)
{
List<TOut> l = new();
foreach (TIn? item in enumerable)
l.Add(converter(item));
return l;
}
/// <summary>
/// Does the same thing as the standard Linq Select() method, except this one uses a <paramref name="predicate"/> and applies the <paramref name="keySelector"/> only when the predicate returns <see langword="true"/>.
/// </summary>
/// <typeparam name="T"></typeparam>
Expand Down
36 changes: 30 additions & 6 deletions VolumeControl.TypeExtensions/IReadOnlyListExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ public static int IndexOf<T>(this IReadOnlyList<T> list, T item, int index, int
{
if (index > list.Count)
throw new ArgumentOutOfRangeException($"Index {index} is out of range for the given list with size {list.Count}!");
if (item is not null)
if (item != null)
{
for (int i = index, j = 0; j < count && i < list.Count; ++i, ++j)
for (int i = index, max = list.Count, j = 0; j < count && i < max; ++i, ++j)
{
if (item.Equals(list[i]))
{
Expand All @@ -46,9 +46,9 @@ public static int IndexOf<T>(this IReadOnlyList<T> list, T item, int index)
{
if (index > list.Count)
throw new ArgumentOutOfRangeException($"Index {index} is out of range for the given list with size {list.Count}!");
if (item is not null)
if (item != null)
{
for (int i = index; i < list.Count; ++i)
for (int i = index, max = list.Count; i < max; ++i)
{
if (item.Equals(list[i]))
{
Expand All @@ -69,9 +69,9 @@ public static int IndexOf<T>(this IReadOnlyList<T> list, T item, int index)
/// <returns>The index of <paramref name="item"/> if found in the list; otherwise -1.</returns>
public static int IndexOf<T>(this IReadOnlyList<T> list, T item)
{
if (item is not null)
if (item != null)
{
for (int i = 0; i < list.Count; ++i)
for (int i = 0, max = list.Count; i < max; ++i)
{
if (item.Equals(list[i]))
{
Expand All @@ -81,5 +81,29 @@ public static int IndexOf<T>(this IReadOnlyList<T> list, T item)
}
return -1;
}
/// <summary>
/// Gets the index of the first occurrence of <paramref name="item"/> in this <paramref name="list"/>.
/// </summary>
/// <typeparam name="T">The type of object in the <see cref="IReadOnlyList{T}"/>.</typeparam>
/// <param name="list">(implicit) The <see cref="IReadOnlyList{T}"/> to search.</param>
/// <param name="item">The object to locate in the <see cref="IReadOnlyList{T}"/>.</param>
/// <param name="index">The index of <paramref name="item"/> if found in the list; otherwise -1.</param>
/// <returns><see langword="true"/> when <paramref name="item"/> was found in the list; otherwise <see langword="false"/>.</returns>
public static bool IndexOf<T>(this IReadOnlyList<T> list, T item, out int index)
{
if (item != null)
{
for (int i = 0, max = list.Count; i < max; ++i)
{
if (item.Equals(list[i]))
{
index = i;
return true;
}
}
}
index = -1;
return false;
}
}
}
79 changes: 75 additions & 4 deletions VolumeControl.TypeExtensions/ListExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,35 @@ public static void SelectiveUpdate<T>(this List<T> l, IEnumerable<T> other)
/// <returns>The resulting <paramref name="list"/>; this can safely be ignored when using this method outside of a pipeline.</returns>
public static IList<T> ForEach<T>(this IList<T> list, Action<T> action)
{
foreach (T? item in list)
action(item);
for (int i = 0, max = list.Count; i < max; ++i)
{
action(list[i]);
}
return list;
}
/// <inheritdoc cref="List{T}.ForEach(Action{T})"/>
/// <returns>The resulting <paramref name="list"/>; this can safely be ignored when using this method outside of a pipeline.</returns>
public static IList ForEach(this IList list, Action<object?> action)
{
foreach (object? item in list)
action(item);
for (int i = 0, max = list.Count; i < max; ++i)
{
action(list[i]);
}
return list;
}
/// <summary>
/// Adds a range of items to the <paramref name="list"/>.
/// </summary>
/// <param name="list">(implicit) The list to add the items to.</param>
/// <param name="enumerable">An <see cref="IEnumerable"/> of items to add.</param>
public static void AddRange(this IList list, IEnumerable enumerable)
{
foreach (var item in enumerable)
{
list.Add(item);
}
}
/// <summary>
/// Adds <paramref name="obj"/> to the <paramref name="list"/> if it isn't a duplicate of any existing elements.
/// </summary>
/// <param name="list">List</param>
Expand All @@ -66,5 +82,60 @@ public static void AddRangeIfUnique(this IList list, IEnumerable range)
foreach (object? item in range)
list.AddIfUnique(item);
}
/// <summary>
/// Converts each element in the given <paramref name="enumerable"/> from type <typeparamref name="TIn"/> to type <typeparamref name="TOut"/>, and returns the converted objects as a new list of type <typeparamref name="TList"/>.
/// </summary>
/// <typeparam name="TList">The type of list to return.</typeparam>
/// <typeparam name="TOut">The type to convert each item to.</typeparam>
/// <typeparam name="TIn">The type to convert each item from.</typeparam>
/// <param name="enumerable">Any type implementing <see cref="IEnumerable{T}"/>.</param>
/// <param name="converter">A converter method that accepts <typeparamref name="TIn"/> and returns <typeparamref name="TOut"/>.</param>
/// <returns>The converted list of items.</returns>
public static TList ConvertEach<TList, TOut, TIn>(this IEnumerable<TIn> enumerable, Func<TIn, TOut> converter) where TList : IList, IEnumerable, IList<TOut>, IEnumerable<TOut>, ICollection, ICollection<TOut>, new()
{
TList l = new();
foreach (TIn? item in enumerable)
l.Add(converter(item));
return l;
}
/// <summary>
/// Converts each element in the given <paramref name="enumerable"/> from type <typeparamref name="TIn"/> to type <typeparamref name="TOut"/>, and returns the converted objects as a new <see cref="List{T}"/>.
/// </summary>
/// <typeparam name="TOut">The type to convert each item to.</typeparam>
/// <typeparam name="TIn">The type to convert each item from.</typeparam>
/// <param name="enumerable">Any type implementing <see cref="IEnumerable{T}"/>.</param>
/// <param name="converter">A converter method that accepts <typeparamref name="TIn"/> and returns <typeparamref name="TOut"/>.</param>
/// <returns>The converted list of items.</returns>
public static IList<TOut> ConvertEach<TOut, TIn>(this IEnumerable<TIn> enumerable, Func<TIn, TOut> converter)
{
List<TOut> l = new();
foreach (TIn? item in enumerable)
l.Add(converter(item));
return l;
}
/// <summary>
/// Gets the index of the first occurrence of <paramref name="item"/> in this <paramref name="list"/>.
/// </summary>
/// <typeparam name="T">The type of object in the <see cref="IList{T}"/>.</typeparam>
/// <param name="list">(implicit) The <see cref="IList{T}"/> to search.</param>
/// <param name="item">The object to locate in the <see cref="IList{T}"/>.</param>
/// <param name="index">The index of <paramref name="item"/> if found in the list; otherwise -1.</param>
/// <returns><see langword="true"/> when <paramref name="item"/> was found in the list; otherwise <see langword="false"/>.</returns>
public static bool IndexOf<T>(this IList<T> list, T item, out int index)
{
if (item != null)
{
for (int i = 0, max = list.Count; i < max; ++i)
{
if (item.Equals(list[i]))
{
index = i;
return true;
}
}
}
index = -1;
return false;
}
}
}

0 comments on commit b340cfe

Please sign in to comment.