ln.collections/ArrayStream.cs

42 lines
966 B
C#

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
namespace ln.collections
{
public class ArrayStream<T>
{
public T[] Elements { get; }
public int Position { get; set; }
public ArrayStream(T[] elements)
{
Elements = elements;
Position = 0;
}
public ArrayStream(IEnumerable<T> 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);
}
}