<commit_before>﻿// Copyright (c) The Perspex Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Subjects;
using Perspex.Controls.Templates;

namespace Perspex.Controls.Generators
{
    /// <summary>
    /// Creates containers for items and maintains a list of created containers.
    /// </summary>
    public class ItemContainerGenerator : IItemContainerGenerator
    {
        private List<ItemContainer> _containers = new List<ItemContainer>();

        /// <summary>
        /// Initializes a new instance of the <see cref="ItemContainerGenerator"/> class.
        /// </summary>
        /// <param name="owner">The owner control.</param>
        public ItemContainerGenerator(IControl owner)
        {
            Contract.Requires<ArgumentNullException>(owner != null);

            Owner = owner;
        }

        /// <inheritdoc/>
        public IEnumerable<ItemContainer> Containers => _containers;

        /// <inheritdoc/>
        public event EventHandler<ItemContainerEventArgs> Materialized;

        /// <inheritdoc/>
        public event EventHandler<ItemContainerEventArgs> Dematerialized;

        /// <summary>
        /// Gets the owner control.
        /// </summary>
        public IControl Owner { get; }

        /// <inheritdoc/>
        public IEnumerable<ItemContainer> Materialize(
            int startingIndex,
            IEnumerable items,
            IMemberSelector selector)
        {
            Contract.Requires<ArgumentNullException>(items != null);

            int index = startingIndex;
            var result = new List<ItemContainer>();

            foreach (var item in items)
            {
                var i = selector != null ? selector.Select(item) : item;
                var container = new ItemContainer(CreateContainer(i), item, index++);
                result.Add(container);
            }

            AddContainers(result);
            Materialized?.Invoke(this, new ItemContainerEventArgs(startingIndex, result));

            return result.Where(x => x != null).ToList();
        }

        /// <inheritdoc/>
        public virtual IEnumerable<ItemContainer> Dematerialize(int startingIndex, int count)
        {
            var result = new List<ItemContainer>();

            for (int i = startingIndex; i < startingIndex + count; ++i)
            {
                if (i < _containers.Count)
                {
                    result.Add(_containers[i]);
                    _containers[i] = null;
                }
            }

            Dematerialized?.Invoke(this, new ItemContainerEventArgs(startingIndex, result));

            return result;
        }

        /// <inheritdoc/>
        public virtual void InsertSpace(int index, int count)
        {
            _containers.InsertRange(index, Enumerable.Repeat<ItemContainer>(null, count));
        }

        /// <inheritdoc/>
        public virtual IEnumerable<ItemContainer> RemoveRange(int startingIndex, int count)
        {
            var result = _containers.GetRange(startingIndex, count);
            _containers.RemoveRange(startingIndex, count);
            Dematerialized?.Invoke(this, new ItemContainerEventArgs(startingIndex, result));
            return result;
        }

        /// <inheritdoc/>
        public virtual IEnumerable<ItemContainer> Clear()
        {
            var result = _containers;
            _containers = new List<ItemContainer>();

            if (result.Count > 0)
            {
                Dematerialized?.Invoke(this, new ItemContainerEventArgs(0, result));
            }

            return result;
        }

        /// <inheritdoc/>
        public IControl ContainerFromIndex(int index)
        {
            if (index < _containers.Count)<commit_msg>Filter out null entries in item containers.<commit_after>﻿// Copyright (c) The Perspex Project. All rights reserved.
// Licensed under the MIT license. See licence.md file in the project root for full license information.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reactive.Subjects;
using Perspex.Controls.Templates;

namespace Perspex.Controls.Generators
{
    /// <summary>
    /// Creates containers for items and maintains a list of created containers.
    /// </summary>
    public class ItemContainerGenerator : IItemContainerGenerator
    {
        private List<ItemContainer> _containers = new List<ItemContainer>();

        /// <summary>
        /// Initializes a new instance of the <see cref="ItemContainerGenerator"/> class.
        /// </summary>
        /// <param name="owner">The owner control.</param>
        public ItemContainerGenerator(IControl owner)
        {
            Contract.Requires<ArgumentNullException>(owner != null);

            Owner = owner;
        }

        /// <inheritdoc/>
        public IEnumerable<ItemContainer> Containers => _containers;

        /// <inheritdoc/>
        public event EventHandler<ItemContainerEventArgs> Materialized;

        /// <inheritdoc/>
        public event EventHandler<ItemContainerEventArgs> Dematerialized;

        /// <summary>
        /// Gets the owner control.
        /// </summary>
        public IControl Owner { get; }

        /// <inheritdoc/>
        public IEnumerable<ItemContainer> Materialize(
            int startingIndex,
            IEnumerable items,
            IMemberSelector selector)
        {
            Contract.Requires<ArgumentNullException>(items != null);

            int index = startingIndex;
            var result = new List<ItemContainer>();

            foreach (var item in items)
            {
                var i = selector != null ? selector.Select(item) : item;
                var container = new ItemContainer(CreateContainer(i), item, index++);
                result.Add(container);
            }

            AddContainers(result);
            Materialized?.Invoke(this, new ItemContainerEventArgs(startingIndex, result));

            return result.Where(x => x != null).ToList();
        }

        /// <inheritdoc/>
        public virtual IEnumerable<ItemContainer> Dematerialize(int startingIndex, int count)
        {
            var result = new List<ItemContainer>();

            for (int i = startingIndex; i < startingIndex + count; ++i)
            {
                if (i < _containers.Count)
                {
                    result.Add(_containers[i]);
                    _containers[i] = null;
                }
            }

            Dematerialized?.Invoke(this, new ItemContainerEventArgs(startingIndex, result));

            return result;
        }

        /// <inheritdoc/>
        public virtual void InsertSpace(int index, int count)
        {
            _containers.InsertRange(index, Enumerable.Repeat<ItemContainer>(null, count));
        }

        /// <inheritdoc/>
        public virtual IEnumerable<ItemContainer> RemoveRange(int startingIndex, int count)
        {
            var result = _containers.GetRange(startingIndex, count);
            _containers.RemoveRange(startingIndex, count);
            Dematerialized?.Invoke(this, new ItemContainerEventArgs(startingIndex, result));
            return result;
        }

        /// <inheritdoc/>
        public virtual IEnumerable<ItemContainer> Clear()
        {
            var result = _containers.Where(x => x != null).ToList();
            _containers = new List<ItemContainer>();

            if (result.Count > 0)
            {
                Dematerialized?.Invoke(this, new ItemContainerEventArgs(0, result));
            }

            return result;
        }

        /// <inheritdoc/>
        public IControl ContainerFromIndex(int index)
        {
            if (index < _containers.Count)
{'fraction_non_alphanumeric': 0.1001890359168242, 'fraction_numerical': 0.0005040957781978576, 'mean_word_length': 1.958985831469053, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 76, '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': '253804', 'n_tokens_mistral': 1946, 'n_tokens_neox': 1858, 'n_words': 497}