Skip to content

Commit

Permalink
Collection utils
Browse files Browse the repository at this point in the history
  • Loading branch information
TheArcaneBrony committed Aug 5, 2024
1 parent 26b02bc commit 4d32676
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions ArcaneLibs/Extensions/CollectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,27 @@ public static async IAsyncEnumerable<T> ToAsyncEnumerable<T>(this IEnumerable<Ta

public static int GetWidth<T>(this T[,] array) => array.GetLength(1);
public static int GetHeight<T>(this T[,] array) => array.GetLength(0);

public static void MergeBy<T>(this List<T> list, IEnumerable<T> other, Func<T, T, bool> predicate, Action<T, T> mergeAction) {
foreach (var item in other) {
var existing = list.FirstOrDefault(x => predicate(x, item));
if (existing is not null) {
mergeAction(existing, item);
} else {
list.Add(item);
}
}
}

public static void ReplaceBy<T>(this List<T> list, IEnumerable<T> other, Func<T, T, bool> predicate) {
foreach (var item in other) {
var existing = list.FirstOrDefault(x => predicate(x, item));
if (existing is not null) {
var index = list.IndexOf(existing);
list[index] = item;
} else {
list.Add(item);
}
}
}
}

0 comments on commit 4d32676

Please sign in to comment.