<commit_before>﻿namespace AngleSharp.DOM.Collections
{
    using AngleSharp.DOM.Html;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;

    sealed class OptionsCollection : IHtmlOptionsCollection
    {
        #region Fields

        readonly Node _parent;
        readonly List<IHtmlElement> _elements;

        #endregion

        #region ctor

        public OptionsCollection(Node parent)
        {
            _parent = parent;
            _elements = new List<IHtmlElement>();
        }

        #endregion

        #region Index

        public IHtmlOptionElement this[UInt32 index]
        {
            get { return _elements[(int)index] as IHtmlOptionElement; }
            set { _elements[(int)index] = value; }
        }

        public Element this[Int32 index]
        {
            get { return _elements[index] as Element; }
        }

        public Element this[String name]
        {
            get 
            {
                for (var i = 0; i < _elements.Count; i++)
                {
                    if (_elements[i].Id == name)
                        return _elements[i] as Element;

                    var fce = _elements[i] as HTMLFormControlElement;

                    if (fce != null && fce.Name == name)
                        return fce;
                }

                return null;
            }
        }

        #endregion

        #region Properties

        public Int32 SelectedIndex
        {
            get
            {
                var index = 0;
                
                foreach (var element in _elements.OfType<IHtmlOptionElement>())
                {
                    if (element.IsSelected)
                        return index;

                    index++;
                }

                return -1;
            }
            set
            {
                var index = 0;

                foreach (var element in _elements.OfType<IHtmlOptionElement>())
                    element.IsSelected = index++ == value;
            }
        }

        public Int32 Length
        {
            get { return _elements.Count; }
        }

        #endregion

        #region Methods

        public void Add(IHtmlOptionElement element, IHtmlElement before = null)
        {
            var index = _elements.IndexOf(before);

            if (index >= 0)
                _elements.Insert(index, element);
            else
                _elements.Add(element);
        }

        public void Add(IHtmlOptionsGroupElement element, IHtmlElement before = null)
        {
            var index = _elements.IndexOf(before);

            if (index >= 0)
                _elements.Insert(index, element);
            else
                _elements.Add(element);
        }

        public void Remove(Int32 index)
        {
            _elements.RemoveAt(index);
        }

        #endregion

        #region Enumerator

        public IEnumerator<Element> GetEnumerator()
        {
            return _elements.OfType<Element>().GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion
    }
}<commit_msg>Manipulates the DOM instead of using its own list<commit_after>﻿namespace AngleSharp.DOM.Collections
{
    using AngleSharp.DOM.Html;
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;

    sealed class OptionsCollection : IHtmlOptionsCollection
    {
        #region Fields

        readonly Node _parent;

        #endregion

        #region ctor

        public OptionsCollection(Node parent)
        {
            _parent = parent;
        }

        #endregion

        #region Index

        public IHtmlOptionElement this[UInt32 index]
        {
            get { return _parent.ChildNodes.OfType<IHtmlOptionElement>().Skip((int)index).FirstOrDefault(); }
            set { _parent.ReplaceChild(value, this[index]); }
        }

        public Element this[Int32 index]
        {
            get { return _parent.ChildNodes.OfType<Element>().Skip(index).FirstOrDefault(); }
        }

        public Element this[String name]
        {
            get 
            {
                foreach (var element in _parent.ChildNodes.OfType<Element>())
                {
                    if (element.Id == name)
                        return element;

                    var fce = element as HTMLFormControlElement;

                    if (fce != null && fce.Name == name)
                        return fce;
                }

                return null;
            }
        }

        #endregion

        #region Properties

        public Int32 SelectedIndex
        {
            get
            {
                var index = 0;

                foreach (var element in _parent.ChildNodes.OfType<IHtmlOptionElement>())
                {
                    if (element.IsSelected)
                        return index;

                    index++;
                }

                return -1;
            }
            set
            {
                var index = 0;

                foreach (var element in _parent.ChildNodes.OfType<IHtmlOptionElement>())
                    element.IsSelected = index++ == value;
            }
        }

        public Int32 Length
        {
            get { return _parent.ChildNodes.OfType<IHtmlOptionElement>().Count(); }
        }

        #endregion

        #region Methods

        public void Add(IHtmlOptionElement element, IHtmlElement before = null)
        {
            _parent.InsertBefore(element, before);
        }

        public void Add(IHtmlOptionsGroupElement element, IHtmlElement before = null)
        {
            _parent.InsertBefore(element, before);
        }

        public void Remove(Int32 index)
        {
            var child = this[(uint)index];
            _parent.RemoveChild(child);
        }

        #endregion

        #region Enumerator

        public IEnumerator<Element> GetEnumerator()
        {
            return _parent.ChildNodes.OfType<Element>().GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return GetEnumerator();
        }

        #endregion
    }
}
{'fraction_non_alphanumeric': 0.07779012541673282, 'fraction_numerical': 0.004603905381806636, 'mean_word_length': 1.2826086956521738, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 19, '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': '1879818', 'n_tokens_mistral': 1624, 'n_tokens_neox': 1514, 'n_words': 355}