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); }
private TSource[] LazyToArray() { Debug.Assert(GetCount(onlyIfCheap: true) == -1); var builder = new SparseArrayBuilder <TSource>(initialize: true); if (_prepended != null) { builder.Reserve(_prependCount); } builder.AddRange(_source); if (_appended != null) { builder.Reserve(_appendCount); } TSource[] array = builder.ToArray(); int index = 0; for (SingleLinkedNode <TSource>?node = _prepended; node != null; node = node.Linked) { array[index++] = node.Item; } index = array.Length - 1; for (SingleLinkedNode <TSource>?node = _appended; node != null; node = node.Linked) { array[index--] = node.Item; } return(array); }
public virtual TSource[] ToArray() { var builder = new SparseArrayBuilder <TSource>(initialize: true); var deferredCopies = new ArrayBuilder <int>(); for (int i = 0; ; i++) { IEnumerable <TSource> source = GetEnumerable(i); if (source == null) { break; } int count; if (EnumerableHelpers.TryGetCount(source, out count)) { if (count > 0) { builder.Reserve(count); deferredCopies.Add(i); } continue; } builder.AddRange(source); } 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); }
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); }
public TResult[] ToArray() { var builder = new SparseArrayBuilder <TResult>(initialize: true); var deferredCopies = new ArrayBuilder <IEnumerable <TResult> >(); foreach (TSource element in _source) { IEnumerable <TResult> enumerable = _selector(element); int count; if (EnumerableHelpers.TryGetCount(enumerable, out count)) { if (count > 0) { builder.Reserve(count); deferredCopies.Add(enumerable); } continue; } builder.AddRange(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); }
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); }