<commit_before>﻿using System;
using System.Collections;
using System.Collections.Generic;


//-----------------------------------------------------------------------
// <copyright file="EnumerableCache.cs" company="<NAME>">
//     Copyright (c) <NAME>. All rights reserved.
//     This code is released under the Microsoft Public License (Ms-PL).
// </copyright>
//-----------------------------------------------------------------------

namespace GeneratorAPI {
    /// <summary>
    ///     Extension methods for <see cref="IEnumerable{T}" /> types.
    /// </summary>
    internal static class EnumerableCacheExtensions {
        /// <summary>
        ///     Caches the results of enumerating over a given object so that subsequence enumerations
        ///     don't require interacting with the object a second time.
        /// </summary>
        /// <typeparam name="T">The type of element found in the enumeration.</typeparam>
        /// <param name="sequence">The enumerable object.</param>
        /// <returns>
        ///     Either a new enumerable object that caches enumerated results, or the original, <paramref name="sequence" />
        ///     object if no caching is necessary to avoid additional CPU work.
        /// </returns>
        /// <remarks>
        ///     <para>
        ///         This is designed for use on the results of generator methods (the ones with <c>yield return</c> in them)
        ///         so that only those elements in the sequence that are needed are ever generated, while not requiring
        ///         regeneration of elements that are enumerated over multiple times.
        ///     </para>
        ///     <para>This can be a huge performance gain if enumerating multiple times over an expensive generator method.</para>
        ///     <para>
        ///         Some enumerable types such as collections, lists, and already-cached generators do not require
        ///         any (additional) caching, and this method will simply return those objects rather than caching them
        ///         to avoid double-caching.
        ///     </para>
        /// </remarks>
        internal static IEnumerable<T> CacheGeneratedResults<T>(this IEnumerable<T> sequence) {
            // Don't create a cache for types that don't need it.
            if (sequence is IList<T> ||
                sequence is ICollection<T> ||
                sequence is EnumerableCache<T>) return sequence;

            return new EnumerableCache<T>(sequence);
        }

        /// <summary>
        ///     A wrapper for <see cref="IEnumerable&lt;T&gt;" /> types and returns a caching <see cref="IEnumerator&lt;T&gt;" />
        ///     from its <see cref="IEnumerable&lt;T&gt;.GetEnumerator" /> method.
        /// </summary>
        /// <typeparam name="T">The type of element in the sequence.</typeparam>
        private class EnumerableCache<T> : IEnumerable<T> {<commit_msg>Turn EnumerableCache into partial class.<commit_after>﻿using System;
using System.Collections;
using System.Collections.Generic;


//-----------------------------------------------------------------------
// <copyright file="EnumerableCache.cs" company="<NAME>">
//     Copyright (c) <NAME>. All rights reserved.
//     This code is released under the Microsoft Public License (Ms-PL).
// </copyright>
//-----------------------------------------------------------------------

namespace GeneratorAPI {
    /// <summary>
    ///     Extension methods for <see cref="IEnumerable{T}" /> types.
    /// </summary>
    public static partial class Generator {
        /// <summary>
        ///     Caches the results of enumerating over a given object so that subsequence enumerations
        ///     don't require interacting with the object a second time.
        /// </summary>
        /// <typeparam name="T">The type of element found in the enumeration.</typeparam>
        /// <param name="sequence">The enumerable object.</param>
        /// <returns>
        ///     Either a new enumerable object that caches enumerated results, or the original, <paramref name="sequence" />
        ///     object if no caching is necessary to avoid additional CPU work.
        /// </returns>
        /// <remarks>
        ///     <para>
        ///         This is designed for use on the results of generator methods (the ones with <c>yield return</c> in them)
        ///         so that only those elements in the sequence that are needed are ever generated, while not requiring
        ///         regeneration of elements that are enumerated over multiple times.
        ///     </para>
        ///     <para>This can be a huge performance gain if enumerating multiple times over an expensive generator method.</para>
        ///     <para>
        ///         Some enumerable types such as collections, lists, and already-cached generators do not require
        ///         any (additional) caching, and this method will simply return those objects rather than caching them
        ///         to avoid double-caching.
        ///     </para>
        /// </remarks>
        private static IEnumerable<T> CacheGeneratedResults<T>(this IEnumerable<T> sequence) {
            // Don't create a cache for types that don't need it.
            if (sequence is IList<T> ||
                sequence is ICollection<T> ||
                sequence is EnumerableCache<T>) return sequence;

            return new EnumerableCache<T>(sequence);
        }

        /// <summary>
        ///     A wrapper for <see cref="IEnumerable&lt;T&gt;" /> types and returns a caching <see cref="IEnumerator&lt;T&gt;" />
        ///     from its <see cref="IEnumerable&lt;T&gt;.GetEnumerator" /> method.
        /// </summary>
        /// <typeparam name="T">The type of element in the sequence.</typeparam>
        private class EnumerableCache<T> : IEnumerable<T> {
{'fraction_non_alphanumeric': 0.15687948105155344, 'fraction_numerical': 0.0, 'mean_word_length': 1.7993311036789297, 'pattern_counts': {'":': 0, '<': 87, '<?xml version=': 0, '>': 87, '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': '7545372', 'n_tokens_mistral': 1398, 'n_tokens_neox': 1359, 'n_words': 531}