diff --git a/Modules/Extensions/CollectionExtensions.cs b/Modules/Extensions/CollectionExtensions.cs index eb68bdc43..9c75db60e 100644 --- a/Modules/Extensions/CollectionExtensions.cs +++ b/Modules/Extensions/CollectionExtensions.cs @@ -1,7 +1,7 @@ using System.Collections.Generic; using System.Linq; -// ReSharper disable InconsistentNaming +// ReSharper disable ConvertIfStatementToReturnStatement namespace EHR { @@ -10,14 +10,14 @@ namespace EHR /// /// Returns the key of a dictionary by its value /// - /// The dictionary - /// The value used to search for the corresponding key - /// The Key type of the dictionary - /// The Value type of the dictionary - /// The key of the dictionary that corresponds to the value, or the default value of if the value is not found - public static K GetKeyByValue(this Dictionary dict, V value) + /// The to search + /// The used to search for the corresponding key + /// The type of the keys in the + /// The type of the values in the + /// The key of the that corresponds to the given , or the default value of if the is not found in the + public static TKey GetKeyByValue(this Dictionary dictionary, TValue value) { - foreach (KeyValuePair pair in dict) + foreach (KeyValuePair pair in dictionary) { if (pair.Value.Equals(value)) { @@ -45,7 +45,7 @@ namespace EHR /// /// The type of the collection /// The collection to be shuffled - /// The shuffled collection as a List + /// The shuffled collection as a public static List Shuffle(this IEnumerable collection) { var list = collection.ToList(); @@ -60,99 +60,17 @@ namespace EHR return list; } - } - /* - // Credit: https://github.com/dabao40/TheOtherRolesGMIA/blob/main/TheOtherRoles/Utilities/EnumerationHelpers.cs - - public static class EnumerationHelpers - { /// - /// Improves the speed of the code in a foreach loop for Il2CppSystem.Collections.Generic.List + /// Combines multiple collections into a single collection /// - /// - /// - /// - public static IEnumerable GetFastEnumerator(this Il2CppSystem.Collections.Generic.List list) where T : Il2CppSystem.Object => new Il2CppListEnumerable(list); + /// The collection to start with + /// The other collections to add to + /// The type of the elements in the collections to combine + /// A collection containing all elements of and all + public static IEnumerable CombineWith(this IEnumerable firstCollection, params IEnumerable[] collections) + { + return firstCollection.Concat(collections.SelectMany(x => x)); + } } - - public unsafe class Il2CppListEnumerable : IEnumerable, IEnumerator where T : Il2CppSystem.Object - { - // ReSharper disable once FieldCanBeMadeReadOnly.Local - private static Func _objFactory; - - private readonly IntPtr _arrayPointer; - private readonly int _count; - private int _index = -1; - - static Il2CppListEnumerable() - { - _elemSize = IntPtr.Size; - _offset = 4 * IntPtr.Size; - - var constructor = typeof(T).GetConstructor([typeof(IntPtr)]); - var ptr = Expression.Parameter(typeof(IntPtr)); - var create = Expression.New(constructor!, ptr); - var lambda = Expression.Lambda>(create, ptr); - _objFactory = lambda.Compile(); - } - - public Il2CppListEnumerable(Il2CppSystem.Collections.Generic.List list) - { - var listStruct = (Il2CppListStruct*)list.Pointer; - _count = listStruct->_size; - _arrayPointer = listStruct->_items; - } - - System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() - { - return this; - } - - IEnumerator IEnumerable.GetEnumerator() - { - return this; - } - - T IEnumerator.Current => (T)Current; - public object Current { get; private set; } - - public bool MoveNext() - { - if (++_index >= _count) return false; - var refPtr = *(IntPtr*)IntPtr.Add(IntPtr.Add(_arrayPointer, _offset), _index * _elemSize); - Current = _objFactory(refPtr); - return true; - } - - public void Reset() - { - _index = -1; - } - -#pragma warning disable CA1816 - public void Dispose() - { - } -#pragma warning restore CA1816 - private struct Il2CppListStruct - { -#pragma warning disable CS0169 // Field is never used - private IntPtr _unusedPtr1; - private IntPtr _unusedPtr2; -#pragma warning disable CS0649 // Field is never assigned to, and will always have its default value - public IntPtr _items; - public int _size; -#pragma warning restore CS0649 // Field is never assigned to, and will always have its default value -#pragma warning restore CS0169 // Field is never used - } - - // ReSharper disable StaticMemberInGenericType - private static readonly int _elemSize; - - private static readonly int _offset; - // ReSharper restore StaticMemberInGenericType - } - - */ } \ No newline at end of file