Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collection extensions upd #2649

Merged
merged 16 commits into from
Jul 5, 2024

Conversation

Banalny-Banan
Copy link
Contributor

@Banalny-Banan Banalny-Banan commented Jun 27, 2024

Shuffle<T>(this IEnumerable<T> enumerable, int iterations = 1) - added documentation for thrown exceptions & added an actual shuffle instead of infinite recursive call

AddRange<T>(this IEnumerable<T> enumerable, IEnumerable<T> collection) - before was calling AddItem() in a loop. (multiple Concat()s internaly). It wouldnt even work because AddItem() is a pure method. Now using one Concat().

AddRange<T>(this T[] array, IEnumerable<T> collection) - same changes.

AddRange<T>(this HashSet<T> hashset, IEnumerable<T> collection) - before was calling Add() in a loop. Now using UnionWith().

@Banalny-Banan Banalny-Banan marked this pull request as ready for review June 27, 2024 21:42
@Banalny-Banan
Copy link
Contributor Author

I think there is also an issue with inconsistent null-checks - some methods throw exceptions (like Shuffle), some return default values (like Random).
I dont know if this is intensional, so didnt fix it.
Should i ?

@@ -41,7 +41,7 @@ public static IEnumerable<T> RemoveSpecified<T>(this IEnumerable<T> enumerable,
/// <typeparam name="T">Type of <see cref="IEnumerable{T}"/> elements.</typeparam>
/// <returns>A random item from the <see cref="IEnumerable{T}"/>.</returns>
public static T Random<T>(this IEnumerable<T> enumerable) =>
(enumerable as T[] ?? enumerable?.ToArray()) is { Length: > 0 } arr ? arr[UnityEngine.Random.Range(0, arr.Length)] : default;
(enumerable as IList<T> ?? enumerable?.ToArray()) is { Count: > 0 } arr ? arr[UnityEngine.Random.Range(0, arr.Count)] : default;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is to avoid calling .ToArray() if enumerable is a generic List<>. Arrays implement IList<> as well -
https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/language-specification/arrays#1723-arrays-and-the-generic-collection-interfaces

@Banalny-Banan
Copy link
Contributor Author

Banalny-Banan commented Jun 28, 2024

Maybe we should add an "Instance" field to Plugin<> class instead of making all those GetPlugin() methods ?
I remember it existing in the example plugin and assigned in OnEnabled()
We could just do it in base class

@iamalexrouse
Copy link
Member

Maybe we should add an "Instance" field to Plugin<> class instead of making all those GetPlugin() methods ? I remember it existing in the example plugin and assigned in OnEnabled() We could just do it in base class

Isn't this outside the scope of the PR?

Copy link
Contributor

@VALERA771 VALERA771 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use a Nw extension (ShuffleList)

Copy link
Member

@iamalexrouse iamalexrouse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm ok with this

@VALERA771
Copy link
Contributor

Okay. Now you need to target dev branch (read exiled announcements if any questions)

@Misfiy Misfiy changed the base branch from apis-rework to dev July 1, 2024 20:49
# Conflicts:
#	Exiled.API/Extensions/CommonExtensions.cs
@VALERA771 VALERA771 merged commit 05dbe4b into Exiled-Team:dev Jul 5, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants