jellyfin-smart-playlist/Jellyfin.Plugin.SmartPlaylist/Util/Stream.cs

48 lines
1.2 KiB
C#
Raw Normal View History

2024-06-27 01:47:44 +02:00
namespace Jellyfin.Plugin.SmartPlaylist.Util {
public interface IStream<T> {
int available();
T get();
int commit();
int rewind();
int rewind(int n);
}
public class Stream<T> : IStream<T> {
private readonly IList<T> _items;
private int _cursor;
private int _ephemeral_cursor;
protected Stream(IList<T> items) {
_items = items;
_cursor = 0;
_ephemeral_cursor = 0;
}
public int available() {
return _items.Count - _ephemeral_cursor;
}
public T get() {
return _items[_ephemeral_cursor++];
}
public int commit() {
int diff = _ephemeral_cursor - _cursor;
_cursor = _ephemeral_cursor;
return diff;
}
public int rewind() {
int diff = _ephemeral_cursor - _cursor;
_ephemeral_cursor = _cursor;
return diff;
}
public int rewind(int n) {
int diff = _ephemeral_cursor - _cursor;
if (diff < n) {
n = diff;
}
_ephemeral_cursor -= n;
return n;
}
}
}