<commit_before>﻿//-----------------------------------------------------------------------
// <copyright file="GeneratorBase.cs" company="NJsonSchema">
//     Copyright (c) <NAME>. All rights reserved.
// </copyright>
// <license>https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md</license>
// <author><NAME>, <EMAIL></author>
//-----------------------------------------------------------------------

using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace NJsonSchema.CodeGeneration
{
    /// <summary>The base class of the code generators</summary>
    public abstract class GeneratorBase
    {
        private readonly TypeResolverBase _resolver;
        private readonly CodeGeneratorSettingsBase _settings;

        /// <summary>Initializes a new instance of the <see cref="GeneratorBase"/> class.</summary>
        /// <param name="rootObject">The root object.</param>
        /// <param name="typeResolver">The type resolver.</param>
        /// <param name="settings">The settings.</param>
        protected GeneratorBase(object rootObject, TypeResolverBase typeResolver, CodeGeneratorSettingsBase settings)
        {
            RootObject = rootObject;
            _resolver = typeResolver;
            _settings = settings;
        }

        /// <summary>Gets the root object.</summary>
        protected object RootObject { get; }

        /// <summary>Generates the the whole file containing all needed types.</summary>
        /// <returns>The code</returns>
        public string GenerateFile(string typeNameHint)
        {
            var schema = (JsonSchema)RootObject;
            return GenerateFile(schema, typeNameHint);
        }

        /// <summary>Generates the the whole file containing all needed types.</summary>
        /// <returns>The code</returns>
        public string GenerateFile()
        {
            var schema = (JsonSchema)RootObject;
            return GenerateFile(schema, schema.Title != null && Regex.IsMatch(schema.Title, "^[a-zA-Z0-9_]*$") ? schema.Title : null);
        }

        /// <summary>Generates the type from the schema and all types from the resolver.</summary>
        /// <param name="schema">The schema</param>
        /// <param name="typeNameHint">The type name hint.</param>
        /// <returns>The code.</returns>
        public IEnumerable<CodeArtifact> GenerateTypes(JsonSchema schema, string typeNameHint)
        {
            _resolver.Resolve(schema, false, typeNameHint); // register root type
            return GenerateTypes();
        }

        /// <summary>Generates the the whole file containing all needed types.</summary>
        /// <returns>The code</returns>
        public string GenerateFile(JsonSchema schema, string typeNameHint)
        {
            var artifacts = GenerateTypes(schema, typeNameHint);
            return GenerateFile(artifacts);
        }

        /// <summary>Generates all types from the resolver.</summary>
        /// <returns>The code.</returns>
        public virtual IEnumerable<CodeArtifact> GenerateTypes()
        {
            var processedTypes = new List<string>();
            var types = new Dictionary<string, CodeArtifact>();
            while (_resolver.Types.Any(t => !processedTypes.Contains(t.Value)))
            {
                foreach (var pair in _resolver.Types.ToList())
                {
                    processedTypes.Add(pair.Value);
                    var result = GenerateType(pair.Key, pair.Value);
                    types[result.TypeName] = result;
                }
            }

            var artifacts = types.Values
                .Where(p => !_settings.ExcludedTypeNames.Contains(p.TypeName));<commit_msg>remove a call ToList from dictionary in a loop (#1436)<commit_after>﻿//-----------------------------------------------------------------------
// <copyright file="GeneratorBase.cs" company="NJsonSchema">
//     Copyright (c) <NAME>. All rights reserved.
// </copyright>
// <license>https://github.com/RicoSuter/NJsonSchema/blob/master/LICENSE.md</license>
// <author><NAME>, <EMAIL></author>
//-----------------------------------------------------------------------

using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

namespace NJsonSchema.CodeGeneration
{
    /// <summary>The base class of the code generators</summary>
    public abstract class GeneratorBase
    {
        private readonly TypeResolverBase _resolver;
        private readonly CodeGeneratorSettingsBase _settings;

        /// <summary>Initializes a new instance of the <see cref="GeneratorBase"/> class.</summary>
        /// <param name="rootObject">The root object.</param>
        /// <param name="typeResolver">The type resolver.</param>
        /// <param name="settings">The settings.</param>
        protected GeneratorBase(object rootObject, TypeResolverBase typeResolver, CodeGeneratorSettingsBase settings)
        {
            RootObject = rootObject;
            _resolver = typeResolver;
            _settings = settings;
        }

        /// <summary>Gets the root object.</summary>
        protected object RootObject { get; }

        /// <summary>Generates the the whole file containing all needed types.</summary>
        /// <returns>The code</returns>
        public string GenerateFile(string typeNameHint)
        {
            var schema = (JsonSchema)RootObject;
            return GenerateFile(schema, typeNameHint);
        }

        /// <summary>Generates the the whole file containing all needed types.</summary>
        /// <returns>The code</returns>
        public string GenerateFile()
        {
            var schema = (JsonSchema)RootObject;
            return GenerateFile(schema, schema.Title != null && Regex.IsMatch(schema.Title, "^[a-zA-Z0-9_]*$") ? schema.Title : null);
        }

        /// <summary>Generates the type from the schema and all types from the resolver.</summary>
        /// <param name="schema">The schema</param>
        /// <param name="typeNameHint">The type name hint.</param>
        /// <returns>The code.</returns>
        public IEnumerable<CodeArtifact> GenerateTypes(JsonSchema schema, string typeNameHint)
        {
            _resolver.Resolve(schema, false, typeNameHint); // register root type
            return GenerateTypes();
        }

        /// <summary>Generates the the whole file containing all needed types.</summary>
        /// <returns>The code</returns>
        public string GenerateFile(JsonSchema schema, string typeNameHint)
        {
            var artifacts = GenerateTypes(schema, typeNameHint);
            return GenerateFile(artifacts);
        }

        /// <summary>Generates all types from the resolver.</summary>
        /// <returns>The code.</returns>
        public virtual IEnumerable<CodeArtifact> GenerateTypes()
        {
            var processedTypes = new List<string>();
            var types = new Dictionary<string, CodeArtifact>();
            while (_resolver.Types.Any(t => !processedTypes.Contains(t.Value)))
            {
                foreach (var pair in _resolver.Types)
                {
                    processedTypes.Add(pair.Value);
                    var result = GenerateType(pair.Key, pair.Value);
                    types[result.TypeName] = result;
                }
            }

            var artifacts = types.Values
                .Where(p => !_settings.ExcludedTypeNames.Contains(p.TypeName));
{'fraction_non_alphanumeric': 0.1492656875834446, 'fraction_numerical': 0.001068090787716956, 'mean_word_length': 2.041412911084044, 'pattern_counts': {'":': 0, '<': 103, '<?xml version=': 0, '>': 107, 'https://': 2, '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': '5706100', 'n_tokens_mistral': 1826, 'n_tokens_neox': 1781, 'n_words': 464}