Added ArrayStream class
parent
f8ccd0d505
commit
d07d0e3dae
|
@ -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<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);
|
||||
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue