csharp/71/Cometary/src/Cometary.Core/Internal/FlatteningList.cs

FlatteningList.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;

namespace Cometary
{
    /// 
    /// 
    ///   Represents a read-only list whose content is that of multiple
    ///   other read-only lists flattened.
    /// 
    /// 
    ///   For enumeration, consider using  instead of a
    ///    loop, the former being specifically optimized for flattening.
    /// 
    /// 
    internal sealed clast FlatteningList : IReadOnlyList
    {
        private readonly LightList underlyingLists = new LightList();

        /// 
        ///   Adds the given read-only  to the flattening list.
        /// 
        public void AddRange(IReadOnlyList items) => underlyingLists.Add(items);

        /// 
        ///   Removes the given read-only list of  to the flattening list.
        /// 
        public void RemoveRange(IReadOnlyList items) => underlyingLists.Remove(items);

        /// 
        public T this[int index]
        {
            get
            {
                IReadOnlyList[] lists = underlyingLists.UnderlyingArray;
                int total = 0;

                for (int i = 0; i < lists.Length; i++)
                {
                    var list = lists[i];

                    if (index  underlyingLists.SelectMany(x => x).GetEnumerator();

        /// 
        IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();

        private struct Enumerator : IEnumerator
        {
            private readonly LightList innerList;
            private int pos;
            private int posInList;

            /// 
            public T Current => innerList[pos][posInList];

            /// 
            object IEnumerator.Current => Current;

            internal Enumerator(FlatteningList list)
            {
                this.innerList = list.underlyingLists;
                this.posInList = -1;
                this.pos = 0;
            }

            /// 
            public bool MoveNext()
            {
                int pos = this.pos;
                var lists = this.innerList;

                Console.WriteLine($"Pos: {pos} / {lists.Count}");

                TryAgain:

                if (lists.Count == pos)
                    return false;

                IReadOnlyList list = lists[pos];
                int posInList = this.posInList;

                Console.WriteLine($"Pos in list: {posInList} / {list.Count}");

                // TODO: Fix this
                if (++posInList >= list.Count)
                {
                    pos = ++this.pos;
                    posInList = this.posInList = -1;

                    goto TryAgain;
                }

                return true;
            }

            /// 
            public void Reset()
            {
                posInList = -1;
                pos = 0;
            }

            /// 
            public void Dispose()
            {
            }
        }
    }
}