SingleLinkedNode.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. // Copied from https://github.com/dotnet/corefx/blob/f17f1e847aeab830de77f8a46656339d7b0f1b43/src/System.Linq/src/System/Linq/SingleLinkedNode.cs
  5. using System.Collections.Generic;
  6. using System.Diagnostics;
  7. namespace System.Linq
  8. {
  9. /// <summary>
  10. /// An immutable node in a singly-linked list of items.
  11. /// </summary>
  12. /// <typeparam name="TSource">The type of the node's item.</typeparam>
  13. internal sealed class SingleLinkedNode<TSource>
  14. {
  15. /// <summary>
  16. /// Constructs a tail node.
  17. /// </summary>
  18. /// <param name="item">The item to place in the tail node.</param>
  19. public SingleLinkedNode(TSource item)
  20. {
  21. Item = item;
  22. }
  23. /// <summary>
  24. /// Constructs a node linked to the specified node.
  25. /// </summary>
  26. /// <param name="linked">The linked node.</param>
  27. /// <param name="item">The item to place in this node.</param>
  28. private SingleLinkedNode(SingleLinkedNode<TSource> linked, TSource item)
  29. {
  30. Debug.Assert(linked != null);
  31. Linked = linked;
  32. Item = item;
  33. }
  34. /// <summary>
  35. /// The item held by this node.
  36. /// </summary>
  37. public TSource Item { get; }
  38. /// <summary>
  39. /// The next node in the singly-linked list.
  40. /// </summary>
  41. public SingleLinkedNode<TSource>? Linked { get; }
  42. /// <summary>
  43. /// Creates a new node that holds the specified item and is linked to this node.
  44. /// </summary>
  45. /// <param name="item">The item to place in the new node.</param>
  46. public SingleLinkedNode<TSource> Add(TSource item) => new SingleLinkedNode<TSource>(this, item);
  47. /// <summary>
  48. /// Gets the number of items in this and subsequent nodes by walking the linked list.
  49. /// </summary>
  50. public int GetCount()
  51. {
  52. var count = 0;
  53. for (SingleLinkedNode<TSource>? node = this; node != null; node = node.Linked)
  54. {
  55. count++;
  56. }
  57. return count;
  58. }
  59. /// <summary>
  60. /// Gets an <see cref="IEnumerator{TSource}"/> that enumerates the items of this node's singly-linked list in reverse.
  61. /// </summary>
  62. /// <param name="count">The number of items in this node.</param>
  63. public IEnumerator<TSource> GetEnumerator(int count)
  64. {
  65. return ((IEnumerable<TSource>)ToArray(count)).GetEnumerator();
  66. }
  67. /// <summary>
  68. /// Gets the node at a logical index by walking the linked list.
  69. /// </summary>
  70. /// <param name="index">The logical index.</param>
  71. /// <remarks>
  72. /// The caller should make sure <paramref name="index"/> is less than this node's count.
  73. /// </remarks>
  74. public SingleLinkedNode<TSource> GetNode(int index)
  75. {
  76. Debug.Assert(index >= 0 && index < GetCount());
  77. SingleLinkedNode<TSource>? node = this;
  78. for (; index > 0; index--)
  79. {
  80. node = node!.Linked;
  81. }
  82. Debug.Assert(node != null);
  83. return node;
  84. }
  85. /// <summary>
  86. /// Returns an <see cref="T:TSource[]"/> that contains the items of this node's singly-linked list in reverse.
  87. /// </summary>
  88. /// <param name="count">The number of items in this node.</param>
  89. private TSource[] ToArray(int count)
  90. {
  91. Debug.Assert(count == GetCount());
  92. var array = new TSource[count];
  93. var index = count;
  94. for (SingleLinkedNode<TSource>? node = this; node != null; node = node.Linked)
  95. {
  96. --index;
  97. array[index] = node.Item;
  98. }
  99. Debug.Assert(index == 0);
  100. return array;
  101. }
  102. }
  103. }