Skip to content

Commit

Permalink
adding empty enumerator
Browse files Browse the repository at this point in the history
  • Loading branch information
ipjohnson committed May 25, 2018
1 parent aae345f commit 4632ce5
Showing 1 changed file with 41 additions and 1 deletion.
42 changes: 41 additions & 1 deletion src/Grace/Data/Immutable/ImmutableLinkedList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ public class ImmutableLinkedList<T> : IEnumerable<T>, IReadOnlyList<T>
/// </summary>
public static readonly ImmutableLinkedList<T> Empty = new ImmutableLinkedList<T>(default(T), null, 0);

/// <summary>
/// Empty enumerator
/// </summary>
private static readonly EmptyLinkedListEnumerator EmptyEnumerator = new EmptyLinkedListEnumerator();

/// <summary>
/// Default constructor
/// </summary>
Expand Down Expand Up @@ -187,7 +192,7 @@ public ImmutableLinkedList<T> AddRange(IEnumerable<T> range)
/// <returns></returns>
public IEnumerator<T> GetEnumerator()
{
return new LinkedListEnumerator(this);
return Count == 0 ? (IEnumerator<T>)EmptyEnumerator : new LinkedListEnumerator(this);
}

/// <summary>
Expand Down Expand Up @@ -267,6 +272,41 @@ public bool Contains(T value)

private string DebuggerDisplayString => $"Count: {Count}";

/// <summary>
/// Empty enumerator
/// </summary>
private class EmptyLinkedListEnumerator : IEnumerator<T>
{
/// <summary>Advances the enumerator to the next element of the collection.</summary>
/// <returns>true if the enumerator was successfully advanced to the next element; false if the enumerator has passed the end of the collection.</returns>
/// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception>
public bool MoveNext()
{
return false;
}

/// <summary>Sets the enumerator to its initial position, which is before the first element in the collection.</summary>
/// <exception cref="T:System.InvalidOperationException">The collection was modified after the enumerator was created. </exception>
public void Reset()
{

}

/// <summary>Gets the element in the collection at the current position of the enumerator.</summary>
/// <returns>The element in the collection at the current position of the enumerator.</returns>
public T Current { get; }

/// <summary>Gets the current element in the collection.</summary>
/// <returns>The current element in the collection.</returns>
object IEnumerator.Current => Current;

/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public void Dispose()
{

}
}

private class LinkedListEnumerator : IEnumerator<T>
{
private readonly ImmutableLinkedList<T> _start;
Expand Down

0 comments on commit 4632ce5

Please sign in to comment.