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); }
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); }