<commit_before>
#region Using Directives

using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;

#endregion

namespace System.CommandLine.ValueConverters
{
    /// <summary>
    /// Represents a helper, which makes it easy to create collections of many different kinds via one, simple API.
    /// </summary>
    public static class CollectionHelper
    {
        #region Private Static Fields

        /// <summary>
        /// Contains a list of all the collection types that are supported by the collection helper.
        /// </summary>
        private static List<Type> supportedTypes = new List<Type>
        {
            // System.Collections
            typeof(ArrayList),
            typeof(Queue),
            typeof(Stack),
            typeof(ICollection),
            typeof(IEnumerable),
            typeof(IList),

            // System.Collections.Generic
            typeof(HashSet<>),
            typeof(LinkedList<>),
            typeof(List<>),
            typeof(Queue<>),
            typeof(SortedSet<>),
            typeof(Stack<>),
            typeof(ICollection<>),
            typeof(IEnumerable<>),
            typeof(IList<>),
            typeof(IReadOnlyCollection<>),
            typeof(IReadOnlyList<>),
            typeof(ISet<>),

            // System.Collections.ObjectModel
            typeof(Collection<>),
            typeof(ObservableCollection<>),
            typeof(ReadOnlyCollection<>),
            typeof(ReadOnlyObservableCollection<>)
        };

        #endregion

        #region Public Static Methods

        /// <summary>
        /// Determines if a given type is one of the supported collection types.
        /// </summary>
        /// <param name="type">The type that is to be checked for support.</param>
        /// <returns>Returns <c>true</c> if the specified type is one of the supported collection types and <c>false</c> otherwise.</returns>
        public static bool IsSupportedCollectionType<T>()
        {
            Type type = typeof(T);
            if (type.IsArray)
            {
                if (type.GetElementType().IsArray)
                    return false;
                return type.GetArrayRank() == 1;
            }
            if (type.IsConstructedGenericType)
                type = type.GetGenericTypeDefinition();
            return CollectionHelper.supportedTypes.Contains(type);
        }

        /// <summary>
        /// Merges two collection into one collection of the same type.
        /// </summary>
        /// <param name="firstCollection">The first collection.</param>
        /// <param name="secondCollection">The second collection.</param>
        /// <returns>Returns a collection that contains the elements of both collections, which is of the same type as the input collections.</returns>
        public static T Merge<T>(T firstCollection, T secondCollection)
        {
            // Validates that the two collections are of a supported collection type
            if (!CollectionHelper.IsSupportedCollectionType<T>())
                throw new InvalidOperationException("The specified type is not a supported collection type.");

            // Converts the two collections to arrays first
            Array firstArray = CollectionHelper.ToArray<T>(firstCollection);
            Array secondArray = CollectionHelper.ToArray<T>(secondCollection);

            // Combines the two arrays into a single array
            object[] mergedArray = new object[firstArray.Length + secondArray.Length];
            Array.Copy(firstArray, mergedArray, firstArray.Length);
            Array.Copy(secondArray, 0, mergedArray, firstArray.Length, secondArray.Length);

            // Converts the merged array back to the collection type and returns it
            return CollectionHelper.To<object[], T>(mergedArray);
        }

        /// <summary>
        /// Merges two collection into one collection, where the input collections and the result all may have a different collection type.
        /// </summary>
        /// <param name="firstCollection">The first collection.</param>
        /// <param name="secondCollection">The second collection.</param>
        /// <returns>Returns a collection that contains the elements of both collections, which is of the same type as the input collections.</returns>
        public static TResult Merge<TResult, TFirstInput, TSecondInput>(TFirstInput firstCollection, TSecondInput secondCollection)
        {
            // Validates that the two collections are of a supported collection type
            if (!CollectionHelper.IsSupportedCollectionType<TFirstInput>())
                throw new InvalidOperationException("The type for the first input is not a supported collection type.");
            if (!CollectionHelper.IsSupportedCollectionType<TSecondInput>())
                throw new InvalidOperationException("The type for the second input is not a supported collection type.");
            if (!CollectionHelper.IsSupportedCollectionType<TResult>())
                throw new InvalidOperationException("The type for the result is not a supported collection type.");

            // Converts the two collections to arrays first
            Array firstArray = CollectionHelper.ToArray<TFirstInput>(firstCollection);
            Array secondArray = CollectionHelper.ToArray<TSecondInput>(secondCollection);

            // Combines the two arrays into a single array
            object[] mergedArray = new object[firstArray.Length + secondArray.Length];
            Array.Copy(firstArray, mergedArray, firstArray.Length);
            Array.Copy(secondArray, 0, mergedArray, firstArray.Length, secondArray.Length);

            // Converts the merged array back to the collection type and returns it
            return CollectionHelper.To<object[], TResult>(mergedArray);
        }

        /// <summary>
        /// Converts the specified collection to a collection of another containing the same elements.
        /// </summary>
        /// <param name="inputCollection">The collection, that is to be converted to another collection type.</param>
        /// <returns>Returns the converted collection.</returns>
        public static TResult To<TInput, TResult>(TInput inputCollection)
        {
            // Checks if both collection types are supported
            if (!CollectionHelper.IsSupportedCollectionType<TInput>())
                throw new InvalidOperationException("The type of the specified collection is not a supported collection type.");
            if (!CollectionHelper.IsSupportedCollectionType<TResult>())
                throw new InvalidOperationException("The type of the result is not a supported collection type.");

            // First the input collection is converted to an array and then the array is converted to the result collection type
            Array array = CollectionHelper.ToArray(inputCollection);
            return CollectionHelper.FromArray<TResult>(array);
        }

        /// <summary>
        /// Converts the specified collection to a collection of another containing the same elements.
        /// </summary>
        /// <param name="inputCollection">The collection, that is to be converted to another collection type.</param>
        /// <returns>Returns the converted collection.</returns>
        public static TResult From<TInput, TResult>(TInput inputCollection)
        {
            // Checks if both collection types are supported
            if (!CollectionHelper.IsSupportedCollectionType<TInput>())
                throw new InvalidOperationException("The type of the specified collection is not a supported collection type.");
            if (!CollectionHelper.IsSupportedCollectionType<TResult>())
                throw new InvalidOperationException("The type of the result is not a supported collection type.");

            // First the input collection is converted to an array and then the array is converted to the result collection type
            Array array = CollectionHelper.ToArray<TInput>(inputCollection);
            return CollectionHelper.FromArray<TResult>(array);
        }

        /// <summary>
        /// Converts the specified collection into an array with the same elements.
        /// </summary>
        /// <param name="inputCollection">The collection that is to be converted into an array.</param>
        /// <returns>Returns an array that contains the same elements of the specified input collection.</returns>
        public static Array ToArray<T>(T inputCollection)
        {
            // Checks if the type of the input collection is supported
            if (!CollectionHelper.IsSupportedCollectionType<T>())
                throw new InvalidOperationException("The type of the specified collection is not a supported collection type.");

            // Checks if the collection already is an array, then nothing needs to be done
            Type type = typeof(T);<commit_msg>Removed a method, which was unnecessary from the collection helper. The To and From methods actually did the same thing xD.<commit_after>
#region Using Directives

using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reflection;

#endregion

namespace System.CommandLine.ValueConverters
{
    /// <summary>
    /// Represents a helper, which makes it easy to create collections of many different kinds via one, simple API.
    /// </summary>
    public static class CollectionHelper
    {
        #region Private Static Fields

        /// <summary>
        /// Contains a list of all the collection types that are supported by the collection helper.
        /// </summary>
        private static List<Type> supportedTypes = new List<Type>
        {
            // System.Collections
            typeof(ArrayList),
            typeof(Queue),
            typeof(Stack),
            typeof(ICollection),
            typeof(IEnumerable),
            typeof(IList),

            // System.Collections.Generic
            typeof(HashSet<>),
            typeof(LinkedList<>),
            typeof(List<>),
            typeof(Queue<>),
            typeof(SortedSet<>),
            typeof(Stack<>),
            typeof(ICollection<>),
            typeof(IEnumerable<>),
            typeof(IList<>),
            typeof(IReadOnlyCollection<>),
            typeof(IReadOnlyList<>),
            typeof(ISet<>),

            // System.Collections.ObjectModel
            typeof(Collection<>),
            typeof(ObservableCollection<>),
            typeof(ReadOnlyCollection<>),
            typeof(ReadOnlyObservableCollection<>)
        };

        #endregion

        #region Public Static Methods

        /// <summary>
        /// Determines if a given type is one of the supported collection types.
        /// </summary>
        /// <param name="type">The type that is to be checked for support.</param>
        /// <returns>Returns <c>true</c> if the specified type is one of the supported collection types and <c>false</c> otherwise.</returns>
        public static bool IsSupportedCollectionType<T>()
        {
            Type type = typeof(T);
            if (type.IsArray)
            {
                if (type.GetElementType().IsArray)
                    return false;
                return type.GetArrayRank() == 1;
            }
            if (type.IsConstructedGenericType)
                type = type.GetGenericTypeDefinition();
            return CollectionHelper.supportedTypes.Contains(type);
        }

        /// <summary>
        /// Merges two collection into one collection of the same type.
        /// </summary>
        /// <param name="firstCollection">The first collection.</param>
        /// <param name="secondCollection">The second collection.</param>
        /// <returns>Returns a collection that contains the elements of both collections, which is of the same type as the input collections.</returns>
        public static T Merge<T>(T firstCollection, T secondCollection)
        {
            // Validates that the two collections are of a supported collection type
            if (!CollectionHelper.IsSupportedCollectionType<T>())
                throw new InvalidOperationException("The specified type is not a supported collection type.");

            // Converts the two collections to arrays first
            Array firstArray = CollectionHelper.ToArray<T>(firstCollection);
            Array secondArray = CollectionHelper.ToArray<T>(secondCollection);

            // Combines the two arrays into a single array
            object[] mergedArray = new object[firstArray.Length + secondArray.Length];
            Array.Copy(firstArray, mergedArray, firstArray.Length);
            Array.Copy(secondArray, 0, mergedArray, firstArray.Length, secondArray.Length);

            // Converts the merged array back to the collection type and returns it
            return CollectionHelper.To<object[], T>(mergedArray);
        }

        /// <summary>
        /// Merges two collection into one collection, where the input collections and the result all may have a different collection type.
        /// </summary>
        /// <param name="firstCollection">The first collection.</param>
        /// <param name="secondCollection">The second collection.</param>
        /// <returns>Returns a collection that contains the elements of both collections, which is of the same type as the input collections.</returns>
        public static TResult Merge<TFirstInput, TSecondInput, TResult>(TFirstInput firstCollection, TSecondInput secondCollection)
        {
            // Validates that the two collections are of a supported collection type
            if (!CollectionHelper.IsSupportedCollectionType<TFirstInput>())
                throw new InvalidOperationException("The type for the first input is not a supported collection type.");
            if (!CollectionHelper.IsSupportedCollectionType<TSecondInput>())
                throw new InvalidOperationException("The type for the second input is not a supported collection type.");
            if (!CollectionHelper.IsSupportedCollectionType<TResult>())
                throw new InvalidOperationException("The type for the result is not a supported collection type.");

            // Converts the two collections to arrays first
            Array firstArray = CollectionHelper.ToArray<TFirstInput>(firstCollection);
            Array secondArray = CollectionHelper.ToArray<TSecondInput>(secondCollection);

            // Combines the two arrays into a single array
            object[] mergedArray = new object[firstArray.Length + secondArray.Length];
            Array.Copy(firstArray, mergedArray, firstArray.Length);
            Array.Copy(secondArray, 0, mergedArray, firstArray.Length, secondArray.Length);

            // Converts the merged array back to the collection type and returns it
            return CollectionHelper.To<object[], TResult>(mergedArray);
        }

        /// <summary>
        /// Converts the specified collection to a collection of another containing the same elements.
        /// </summary>
        /// <param name="inputCollection">The collection, that is to be converted to another collection type.</param>
        /// <returns>Returns the converted collection.</returns>
        public static TResult To<TInput, TResult>(TInput inputCollection)
        {
            // Checks if both collection types are supported
            if (!CollectionHelper.IsSupportedCollectionType<TInput>())
                throw new InvalidOperationException("The type of the specified collection is not a supported collection type.");
            if (!CollectionHelper.IsSupportedCollectionType<TResult>())
                throw new InvalidOperationException("The type of the result is not a supported collection type.");

            // First the input collection is converted to an array and then the array is converted to the result collection type
            Array array = CollectionHelper.ToArray(inputCollection);
            return CollectionHelper.FromArray<TResult>(array);
        }

        /// <summary>
        /// Converts the specified collection into an array with the same elements.
        /// </summary>
        /// <param name="inputCollection">The collection that is to be converted into an array.</param>
        /// <returns>Returns an array that contains the same elements of the specified input collection.</returns>
        public static Array ToArray<T>(T inputCollection)
        {
            // Checks if the type of the input collection is supported
            if (!CollectionHelper.IsSupportedCollectionType<T>())
                throw new InvalidOperationException("The type of the specified collection is not a supported collection type.");

            // Checks if the collection already is an array, then nothing needs to be done
            Type type = typeof(T);
{'fraction_non_alphanumeric': 0.08714571327766671, 'fraction_numerical': 0.0003528166529460191, 'mean_word_length': 2.3986810551558753, 'pattern_counts': {'":': 0, '<': 172, '<?xml version=': 0, '>': 172, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6203357', 'n_tokens_mistral': 3736, 'n_tokens_neox': 3679, 'n_words': 1381}