61 lines
1.6 KiB
C#
61 lines
1.6 KiB
C#
namespace Jellyfin.Plugin.SmartPlaylist.Util {
|
|
public interface IStream<T> {
|
|
int Available();
|
|
T Get();
|
|
int Commit();
|
|
int Rewind();
|
|
int Rewind(int n);
|
|
int Consumed();
|
|
IStream<T> Copy();
|
|
}
|
|
|
|
public class Stream<T> : IStream<T> {
|
|
private readonly IList<T> _items;
|
|
private int _cursor;
|
|
private int _ephemeralCursor;
|
|
|
|
protected Stream(IList<T> items) {
|
|
_items = items;
|
|
_cursor = 0;
|
|
_ephemeralCursor = 0;
|
|
}
|
|
|
|
private Stream(IList<T> items, int cursor, int ephemeralCursor) {
|
|
_items = items;
|
|
_cursor = cursor;
|
|
_ephemeralCursor = ephemeralCursor;
|
|
}
|
|
|
|
public int Available() {
|
|
return _items.Count - _ephemeralCursor;
|
|
}
|
|
public T Get() {
|
|
return _items[_ephemeralCursor++];
|
|
}
|
|
public int Commit() {
|
|
int diff = Consumed();
|
|
_cursor = _ephemeralCursor;
|
|
return diff;
|
|
}
|
|
public int Rewind() {
|
|
int diff = Consumed();
|
|
_ephemeralCursor = _cursor;
|
|
return diff;
|
|
}
|
|
public int Rewind(int n) {
|
|
int diff = Consumed();
|
|
if (diff < n) {
|
|
n = diff;
|
|
}
|
|
_ephemeralCursor -= n;
|
|
return n;
|
|
}
|
|
public int Consumed() {
|
|
return _ephemeralCursor - _cursor;
|
|
}
|
|
public IStream<T> Copy() {
|
|
return new Stream<T>(_items, _cursor, _ephemeralCursor);
|
|
}
|
|
}
|
|
}
|
|
|