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

namespace NRules.RuleModel.Aggregators
{
    /// <summary>
    /// Aggregator that projects each matching fact into a collection and creates a new fact for each element in that collection.
    /// </summary>
    /// <typeparam name="TSource">Type of source element.</typeparam>
    /// <typeparam name="TResult">Type of result element.</typeparam>
    internal class FlatteningAggregator<TSource, TResult> : IAggregator
    {
        private readonly Func<TSource, IEnumerable<TResult>> _selector;
        private readonly Dictionary<TSource, object> _sourceToValue = new Dictionary<TSource, object>();
        private readonly Dictionary<TSource, IList<TResult>> _sourceToList = new Dictionary<TSource, IList<TResult>>();

        public FlatteningAggregator(Func<TSource, IEnumerable<TResult>> selector)
        {
            _selector = selector;
        }

        public IEnumerable<AggregationResult> Initial()
        {
            return AggregationResult.Empty;
        }

        public IEnumerable<AggregationResult> Add(object fact)
        {
            var source = (TSource) fact;
            var value = _selector(source);
            var list = value.ToList();
            _sourceToValue[source] = value;
            _sourceToList[source] = list;
            
            return list.Select(x => AggregationResult.Added(x)).ToArray();
        }

        public IEnumerable<AggregationResult> Modify(object fact)
        {
            var source = (TSource)fact;
            var value = _selector(source);
            var list = value.ToList();
            var oldList = _sourceToList[source];

            return oldList.Select(x => AggregationResult.Removed(x)).Union(
                list.Select(x => AggregationResult.Added(x))).ToArray();
        }

        public IEnumerable<AggregationResult> Remove(object fact)
        {
            var source = (TSource)fact;
            var oldList = _sourceToList[source];
            _sourceToValue.Remove(source);
            _sourceToList.Remove(source);
            return oldList.Select(x => AggregationResult.Removed(x)).ToArray();
        }

        public IEnumerable<object> Aggregates { get { return _sourceToList.Values.SelectMany(x => x).Cast<object>(); } }<commit_msg>Fix flattening aggregator<commit_after>using System;
using System.Collections.Generic;
using System.Linq;

namespace NRules.RuleModel.Aggregators
{
    /// <summary>
    /// Aggregator that projects each matching fact into a collection and creates a new fact for each element in that collection.
    /// </summary>
    /// <typeparam name="TSource">Type of source element.</typeparam>
    /// <typeparam name="TResult">Type of result element.</typeparam>
    internal class FlatteningAggregator<TSource, TResult> : IAggregator
    {
        private readonly Func<TSource, IEnumerable<TResult>> _selector;
        private readonly Dictionary<TSource, IList<TResult>> _sourceToList = new Dictionary<TSource, IList<TResult>>();

        public FlatteningAggregator(Func<TSource, IEnumerable<TResult>> selector)
        {
            _selector = selector;
        }

        public IEnumerable<AggregationResult> Initial()
        {
            return AggregationResult.Empty;
        }

        public IEnumerable<AggregationResult> Add(object fact)
        {
            var source = (TSource) fact;
            var value = _selector(source);
            var list = value.ToList();
            _sourceToList[source] = list;
            
            return list.Select(x => AggregationResult.Added(x)).ToArray();
        }

        public IEnumerable<AggregationResult> Modify(object fact)
        {
            var source = (TSource)fact;
            var value = _selector(source);
            var list = value.ToList();
            var oldList = _sourceToList[source];
            _sourceToList[source] = list;

            return oldList.Select(x => AggregationResult.Removed(x)).Union(
                list.Select(x => AggregationResult.Added(x))).ToArray();
        }

        public IEnumerable<AggregationResult> Remove(object fact)
        {
            var source = (TSource)fact;
            var oldList = _sourceToList[source];
            _sourceToList.Remove(source);
            return oldList.Select(x => AggregationResult.Removed(x)).ToArray();
        }

        public IEnumerable<object> Aggregates { get { return _sourceToList.Values.SelectMany(x => x).Cast<object>(); } }
{'fraction_non_alphanumeric': 0.10407440212577503, 'fraction_numerical': 0.0, 'mean_word_length': 2.4961300309597525, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 57, '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': '2768360', 'n_tokens_mistral': 1205, 'n_tokens_neox': 1185, 'n_words': 275}