// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the Apache 2.0 License. // See the LICENSE file in the project root for more information. using System.Collections.Generic; using System.Diagnostics; namespace System.Linq { internal sealed class SingleLinkedNode { public SingleLinkedNode(TSource first, TSource second) { Linked = new SingleLinkedNode(first); Item = second; Count = 2; } public SingleLinkedNode(TSource item) { Item = item; Count = 1; } private SingleLinkedNode(SingleLinkedNode linked, TSource item) { Debug.Assert(linked != null); Linked = linked; Item = item; Count = linked.Count + 1; } public TSource Item { get; } public SingleLinkedNode Linked { get; } public int Count { get; } public SingleLinkedNode Add(TSource item) => new SingleLinkedNode(this, item); public IEnumerator GetEnumerator() { var array = new TSource[Count]; var index = Count; for (var n = this; n != null; n = n.Linked) { --index; array[index] = n.Item; } Debug.Assert(index == 0); return ((IEnumerable)array).GetEnumerator(); } } }