public TResult[] ToArray()
            {
                var builder = new SparseArrayBuilder <TResult>(initialize: true);
                ArrayBuilder <IEnumerable <TResult> > deferredCopies = default;

                foreach (TSource element in _source)
                {
                    IEnumerable <TResult> enumerable = _selector(element);

                    if (builder.ReserveOrAdd(enumerable))
                    {
                        deferredCopies.Add(enumerable);
                    }
                }

                TResult[] array = builder.ToArray();

                ArrayBuilder <Marker> markers = builder.Markers;

                for (int i = 0; i < markers.Count; i++)
                {
                    Marker marker = markers[i];
                    IEnumerable <TResult> enumerable = deferredCopies[i];
                    EnumerableHelpers.Copy(enumerable, array, marker.Index, marker.Count);
                }

                return(array);
            }
Esempio n. 2
0
            public override TSource[] ToArray()
            {
                var builder = new SparseArrayBuilder <TSource>(initialize: true);

                bool reservedFirst  = builder.ReserveOrAdd(_first);
                bool reservedSecond = builder.ReserveOrAdd(_second);

                TSource[] array = builder.ToArray();

                if (reservedFirst)
                {
                    Marker marker = builder.Markers.First();
                    Debug.Assert(marker.Index == 0);
                    EnumerableHelpers.Copy(_first, array, 0, marker.Count);
                }

                if (reservedSecond)
                {
                    Marker marker = builder.Markers.Last();
                    EnumerableHelpers.Copy(_second, array, marker.Index, marker.Count);
                }

                return(array);
            }
Esempio n. 3
0
            private TSource[] LazyToArray()
            {
                Debug.Assert(!_hasOnlyCollections);

                var builder = new SparseArrayBuilder <TSource>(initialize: true);
                ArrayBuilder <int> deferredCopies = default;

                for (int i = 0; ; i++)
                {
                    // Unfortunately, we can't escape re-walking the linked list for each source, which has
                    // quadratic behavior, because we need to add the sources in order.
                    // On the bright side, the bottleneck will usually be iterating, buffering, and copying
                    // each of the enumerables, so this shouldn't be a noticeable perf hit for most scenarios.

                    IEnumerable <TSource>?source = GetEnumerable(i);
                    if (source == null)
                    {
                        break;
                    }

                    if (builder.ReserveOrAdd(source))
                    {
                        deferredCopies.Add(i);
                    }
                }

                TSource[] array = builder.ToArray();

                ArrayBuilder <Marker> markers = builder.Markers;

                for (int i = 0; i < markers.Count; i++)
                {
                    Marker marker = markers[i];
                    IEnumerable <TSource> source = GetEnumerable(deferredCopies[i]) !;
                    EnumerableHelpers.Copy(source, array, marker.Index, marker.Count);
                }

                return(array);
            }