From d07d0e3daea8260caca39fbcd87d4518f7424c7a Mon Sep 17 00:00:00 2001 From: Harald Wolff-Thobaben Date: Thu, 19 Nov 2020 20:39:27 +0100 Subject: [PATCH] Added ArrayStream class --- ArrayStream.cs | 41 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 ArrayStream.cs diff --git a/ArrayStream.cs b/ArrayStream.cs new file mode 100644 index 0000000..dfbaf67 --- /dev/null +++ b/ArrayStream.cs @@ -0,0 +1,41 @@ +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Text; + +namespace ln.collections +{ + public class ArrayStream + { + public T[] Elements { get; } + public int Position { get; set; } + + public ArrayStream(T[] elements) + { + Elements = elements; + Position = 0; + } + public ArrayStream(IEnumerable elements) + { + Elements = elements.ToArray(); + Position = 0; + } + + public T Current => Elements[Position]; + public T Next() => Next(1); + public T Next(int forward) => Elements[Position + 1 + forward]; + + public bool MoveNext() + { + if (Position >= Elements.Length) + throw new EndOfStreamException(); + + Position++; + return (Position < Elements.Length); + } + + public bool EndOfStream => (Position < Elements.Length); + + } +}