Buffer<TSource>
using System.Collections.Generic;
using System.Reactive.Concurrency;
using System.Reactive.Disposables;
using System.Runtime.CompilerServices;
namespace System.Reactive.Linq.ObservableImpl
{
    [System.Runtime.CompilerServices.NullableContext(1)]
    [System.Runtime.CompilerServices.Nullable(0)]
    internal static class Buffer<[System.Runtime.CompilerServices.Nullable(2)] TSource>
    {
        [System.Runtime.CompilerServices.Nullable(new byte[] {
            0,
            1,
            1,
            1,
            0
        })]
        internal sealed class CountExact : Producer<IList<TSource>, CountExact.ExactSink>
        {
            [System.Runtime.CompilerServices.Nullable(new byte[] {
                0,
                1,
                1,
                1
            })]
            internal sealed class ExactSink : Sink<TSource, IList<TSource>>
            {
                private readonly int _count;
                private int _index;
                [System.Runtime.CompilerServices.Nullable(new byte[] {
                    2,
                    1
                })]
                private IList<TSource> _buffer;
                internal ExactSink(IObserver<IList<TSource>> observer, int count)
                    : base(observer)
                {
                    _count = count;
                }
                public override void OnNext(TSource value)
                {
                    IList<TSource> list = _buffer;
                    if (list == null)
                        list = (_buffer = new List<TSource>());
                    list.Add(value);
                    int num = _index + 1;
                    if (num == _count) {
                        _buffer = null;
                        _index = 0;
                        ForwardOnNext(list);
                    } else
                        _index = num;
                }
                public override void OnError(Exception error)
                {
                    _buffer = null;
                    ForwardOnError(error);
                }
                public override void OnCompleted()
                {
                    IList<TSource> buffer = _buffer;
                    _buffer = null;
                    if (buffer != null)
                        ForwardOnNext(buffer);
                    ForwardOnCompleted();
                }
            }
            private readonly IObservable<TSource> _source;
            private readonly int _count;
            public CountExact(IObservable<TSource> source, int count)
            {
                _source = source;
                _count = count;
            }
            [return: System.Runtime.CompilerServices.Nullable(new byte[] {
                1,
                0
            })]
            protected override ExactSink CreateSink(IObserver<IList<TSource>> observer)
            {
                return new ExactSink(observer, _count);
            }
            protected override void Run([System.Runtime.CompilerServices.Nullable(new byte[] {
                1,
                0
            })] ExactSink sink)
            {
                sink.Run(_source);
            }
        }
        [System.Runtime.CompilerServices.Nullable(new byte[] {
            0,
            1,
            1,
            1,
            0
        })]
        internal sealed class CountSkip : Producer<IList<TSource>, CountSkip.SkipSink>
        {
            [System.Runtime.CompilerServices.Nullable(new byte[] {
                0,
                1,
                1,
                1
            })]
            internal sealed class SkipSink : Sink<TSource, IList<TSource>>
            {
                private readonly int _count;
                private readonly int _skip;
                private int _index;
                [System.Runtime.CompilerServices.Nullable(new byte[] {
                    2,
                    1
                })]
                private IList<TSource> _buffer;
                internal SkipSink(IObserver<IList<TSource>> observer, int count, int skip)
                    : base(observer)
                {
                    _count = count;
                    _skip = skip;
                }
                public override void OnNext(TSource value)
                {
                    int index = _index;
                    IList<TSource> list = _buffer;
                    if (index == 0)
                        list = (_buffer = new List<TSource>());
                    list?.Add(value);
                    if (++index == _count) {
                        _buffer = null;
                        ForwardOnNext(list);
                    }
                    if (index == _skip)
                        _index = 0;
                    else
                        _index = index;
                }
                public override void OnError(Exception error)
                {
                    _buffer = null;
                    ForwardOnError(error);
                }
                public override void OnCompleted()
                {
                    IList<TSource> buffer = _buffer;
                    _buffer = null;
                    if (buffer != null)
                        ForwardOnNext(buffer);
                    ForwardOnCompleted();
                }
            }
            private readonly IObservable<TSource> _source;
            private readonly int _count;
            private readonly int _skip;
            public CountSkip(IObservable<TSource> source, int count, int skip)
            {
                _source = source;
                _count = count;
                _skip = skip;
            }
            [return: System.Runtime.CompilerServices.Nullable(new byte[] {
                1,
                0
            })]
            protected override SkipSink CreateSink(IObserver<IList<TSource>> observer)
            {
                return new SkipSink(observer, _count, _skip);
            }
            protected override void Run([System.Runtime.CompilerServices.Nullable(new byte[] {
                1,
                0
            })] SkipSink sink)
            {
                sink.Run(_source);
            }
        }
        [System.Runtime.CompilerServices.Nullable(new byte[] {
            0,
            1,
            1,
            1,
            0
        })]
        internal sealed class CountOverlap : Producer<IList<TSource>, CountOverlap.OverlapSink>
        {
            [System.Runtime.CompilerServices.Nullable(new byte[] {
                0,
                1,
                1,
                1
            })]
            internal sealed class OverlapSink : Sink<TSource, IList<TSource>>
            {
                private readonly Queue<IList<TSource>> _queue;
                private readonly int _count;
                private readonly int _skip;
                private int _n;
                public OverlapSink(IObserver<IList<TSource>> observer, int count, int skip)
                    : base(observer)
                {
                    _queue = new Queue<IList<TSource>>();
                    _count = count;
                    _skip = skip;
                    CreateWindow();
                }
                private void CreateWindow()
                {
                    List<TSource> item = new List<TSource>();
                    _queue.Enqueue(item);
                }
                public override void OnNext(TSource value)
                {
                    foreach (IList<TSource> item in _queue) {
                        item.Add(value);
                    }
                    int num = _n - _count + 1;
                    if (num >= 0 && num % _skip == 0) {
                        IList<TSource> list = _queue.Dequeue();
                        if (list.Count > 0)
                            ForwardOnNext(list);
                    }
                    _n++;
                    if (_n % _skip == 0)
                        CreateWindow();
                }
                public override void OnError(Exception error)
                {
                    _queue.Clear();
                    ForwardOnError(error);
                }
                public override void OnCompleted()
                {
                    while (_queue.Count > 0) {
                        IList<TSource> list = _queue.Dequeue();
                        if (list.Count > 0)
                            ForwardOnNext(list);
                    }
                    ForwardOnCompleted();
                }
            }
            private readonly IObservable<TSource> _source;
            private readonly int _count;
            private readonly int _skip;
            public CountOverlap(IObservable<TSource> source, int count, int skip)
            {
                _source = source;
                _count = count;
                _skip = skip;
            }
            [return: System.Runtime.CompilerServices.Nullable(new byte[] {
                1,
                0
            })]
            protected override OverlapSink CreateSink(IObserver<IList<TSource>> observer)
            {
                return new OverlapSink(observer, _count, _skip);
            }
            protected override void Run([System.Runtime.CompilerServices.Nullable(new byte[] {
                1,
                0
            })] OverlapSink sink)
            {
                sink.Run(_source);
            }
        }
        [System.Runtime.CompilerServices.Nullable(new byte[] {
            0,
            1,
            1,
            1,
            0
        })]
        internal sealed class TimeSliding : Producer<IList<TSource>, TimeSliding._>
        {
            [System.Runtime.CompilerServices.Nullable(new byte[] {
                0,
                1,
                1,
                1
            })]
            internal sealed class _ : Sink<TSource, IList<TSource>>
            {
                private readonly TimeSpan _timeShift;
                private readonly IScheduler _scheduler;
                private readonly object _gate = new object();
                private readonly Queue<List<TSource>> _q = new Queue<List<TSource>>();
                private SerialDisposableValue _timerSerial;
                private TimeSpan _totalTime;
                private TimeSpan _nextShift;
                private TimeSpan _nextSpan;
                public _([System.Runtime.CompilerServices.Nullable(new byte[] {
                    1,
                    0
                })] TimeSliding parent, IObserver<IList<TSource>> observer)
                    : base(observer)
                {
                    _timeShift = parent._timeShift;
                    _scheduler = parent._scheduler;
                }
                public void Run([System.Runtime.CompilerServices.Nullable(new byte[] {
                    1,
                    0
                })] TimeSliding parent)
                {
                    _totalTime = TimeSpan.Zero;
                    _nextShift = parent._timeShift;
                    _nextSpan = parent._timeSpan;
                    CreateWindow();
                    CreateTimer();
                    Run(parent._source);
                }
                protected override void Dispose(bool disposing)
                {
                    if (disposing)
                        _timerSerial.Dispose();
                    base.Dispose(disposing);
                }
                private void CreateWindow()
                {
                    List<TSource> item = new List<TSource>();
                    _q.Enqueue(item);
                }
                private void CreateTimer()
                {
                    SingleAssignmentDisposable singleAssignmentDisposable = new SingleAssignmentDisposable();
                    _timerSerial.Disposable = singleAssignmentDisposable;
                    bool flag = false;
                    bool flag2 = false;
                    if (_nextSpan == _nextShift) {
                        flag = true;
                        flag2 = true;
                    } else if (_nextSpan < _nextShift) {
                        flag = true;
                    } else {
                        flag2 = true;
                    }
                    TimeSpan timeSpan = flag ? _nextSpan : _nextShift;
                    TimeSpan dueTime = timeSpan - _totalTime;
                    _totalTime = timeSpan;
                    if (flag)
                        _nextSpan += _timeShift;
                    if (flag2)
                        _nextShift += _timeShift;
                    singleAssignmentDisposable.Disposable = Scheduler.ScheduleAction<(_, bool, bool)>(_scheduler, (this, flag, flag2), dueTime, (Action<(_, bool, bool)>)delegate((_ this, bool isSpan, bool isShift) tuple) {
                        tuple.this.Tick(tuple.isSpan, tuple.isShift);
                    });
                }
                private void Tick(bool isSpan, bool isShift)
                {
                    lock (_gate) {
                        if (isSpan && _q.Count > 0) {
                            List<TSource> value = _q.Dequeue();
                            ForwardOnNext(value);
                        }
                        if (isShift)
                            CreateWindow();
                    }
                    CreateTimer();
                }
                public override void OnNext(TSource value)
                {
                    lock (_gate) {
                        foreach (List<TSource> item in _q) {
                            item.Add(value);
                        }
                    }
                }
                public override void OnError(Exception error)
                {
                    lock (_gate) {
                        while (_q.Count > 0) {
                            _q.Dequeue().Clear();
                        }
                        ForwardOnError(error);
                    }
                }
                public override void OnCompleted()
                {
                    lock (_gate) {
                        while (_q.Count > 0) {
                            ForwardOnNext(_q.Dequeue());
                        }
                        ForwardOnCompleted();
                    }
                }
            }
            private readonly IObservable<TSource> _source;
            private readonly TimeSpan _timeSpan;
            private readonly TimeSpan _timeShift;
            private readonly IScheduler _scheduler;
            public TimeSliding(IObservable<TSource> source, TimeSpan timeSpan, TimeSpan timeShift, IScheduler scheduler)
            {
                _source = source;
                _timeSpan = timeSpan;
                _timeShift = timeShift;
                _scheduler = scheduler;
            }
            [return: System.Runtime.CompilerServices.Nullable(new byte[] {
                1,
                0
            })]
            protected override _ CreateSink(IObserver<IList<TSource>> observer)
            {
                return new _(this, observer);
            }
            protected override void Run([System.Runtime.CompilerServices.Nullable(new byte[] {
                1,
                0
            })] _ sink)
            {
                sink.Run(this);
            }
        }
        [System.Runtime.CompilerServices.Nullable(new byte[] {
            0,
            1,
            1,
            1,
            0
        })]
        internal sealed class TimeHopping : Producer<IList<TSource>, TimeHopping._>
        {
            [System.Runtime.CompilerServices.Nullable(new byte[] {
                0,
                1,
                1,
                1
            })]
            internal sealed class _ : Sink<TSource, IList<TSource>>
            {
                private readonly object _gate = new object();
                private List<TSource> _list = new List<TSource>();
                private SingleAssignmentDisposableValue _periodicDisposable;
                public _(IObserver<IList<TSource>> observer)
                    : base(observer)
                {
                }
                public void Run([System.Runtime.CompilerServices.Nullable(new byte[] {
                    1,
                    0
                })] TimeHopping parent)
                {
                    _periodicDisposable.Disposable = Scheduler.SchedulePeriodic<_>(parent._scheduler, this, parent._timeSpan, (Action<_>)delegate(_ this) {
                        this.Tick();
                    });
                    Run(parent._source);
                }
                protected override void Dispose(bool disposing)
                {
                    if (disposing)
                        _periodicDisposable.Dispose();
                    base.Dispose(disposing);
                }
                private void Tick()
                {
                    lock (_gate) {
                        ForwardOnNext(_list);
                        _list = new List<TSource>();
                    }
                }
                public override void OnNext(TSource value)
                {
                    lock (_gate) {
                        _list.Add(value);
                    }
                }
                public override void OnError(Exception error)
                {
                    lock (_gate) {
                        _list.Clear();
                        ForwardOnError(error);
                    }
                }
                public override void OnCompleted()
                {
                    lock (_gate) {
                        ForwardOnNext(_list);
                        ForwardOnCompleted();
                    }
                }
            }
            private readonly IObservable<TSource> _source;
            private readonly TimeSpan _timeSpan;
            private readonly IScheduler _scheduler;
            public TimeHopping(IObservable<TSource> source, TimeSpan timeSpan, IScheduler scheduler)
            {
                _source = source;
                _timeSpan = timeSpan;
                _scheduler = scheduler;
            }
            [return: System.Runtime.CompilerServices.Nullable(new byte[] {
                1,
                0
            })]
            protected override _ CreateSink(IObserver<IList<TSource>> observer)
            {
                return new _(observer);
            }
            protected override void Run([System.Runtime.CompilerServices.Nullable(new byte[] {
                1,
                0
            })] _ sink)
            {
                sink.Run(this);
            }
        }
        [System.Runtime.CompilerServices.Nullable(new byte[] {
            0,
            1,
            1,
            1,
            0
        })]
        internal sealed class Ferry : Producer<IList<TSource>, Ferry._>
        {
            [System.Runtime.CompilerServices.Nullable(new byte[] {
                0,
                1,
                1,
                1
            })]
            internal sealed class _ : Sink<TSource, IList<TSource>>
            {
                [System.Runtime.CompilerServices.Nullable(new byte[] {
                    1,
                    0
                })]
                private readonly Ferry _parent;
                private readonly object _gate = new object();
                private List<TSource> _s = new List<TSource>();
                private SerialDisposableValue _timerSerial;
                private int _n;
                private int _windowId;
                public _([System.Runtime.CompilerServices.Nullable(new byte[] {
                    1,
                    0
                })] Ferry parent, IObserver<IList<TSource>> observer)
                    : base(observer)
                {
                    _parent = parent;
                }
                public void Run()
                {
                    _n = 0;
                    _windowId = 0;
                    CreateTimer(0);
                    SetUpstream(ObservableExtensions.SubscribeSafe<TSource>(_parent._source, (IObserver<TSource>)this));
                }
                protected override void Dispose(bool disposing)
                {
                    if (disposing)
                        _timerSerial.Dispose();
                    base.Dispose(disposing);
                }
                private void CreateTimer(int id)
                {
                    SingleAssignmentDisposable singleAssignmentDisposable = new SingleAssignmentDisposable();
                    _timerSerial.Disposable = singleAssignmentDisposable;
                    singleAssignmentDisposable.Disposable = Scheduler.ScheduleAction<(_, int)>(_parent._scheduler, (this, id), _parent._timeSpan, (Action<(_, int)>)delegate((_ this, int id) tuple) {
                        tuple.this.Tick(tuple.id);
                    });
                }
                private void Tick(int id)
                {
                    lock (_gate) {
                        if (id == _windowId) {
                            _n = 0;
                            int id2 = ++_windowId;
                            List<TSource> s = _s;
                            _s = new List<TSource>();
                            ForwardOnNext(s);
                            CreateTimer(id2);
                        }
                    }
                }
                public override void OnNext(TSource value)
                {
                    bool flag = false;
                    int id = 0;
                    lock (_gate) {
                        _s.Add(value);
                        _n++;
                        if (_n == _parent._count) {
                            flag = true;
                            _n = 0;
                            id = ++_windowId;
                            List<TSource> s = _s;
                            _s = new List<TSource>();
                            ForwardOnNext(s);
                        }
                        if (flag)
                            CreateTimer(id);
                    }
                }
                public override void OnError(Exception error)
                {
                    lock (_gate) {
                        _s.Clear();
                        ForwardOnError(error);
                    }
                }
                public override void OnCompleted()
                {
                    lock (_gate) {
                        ForwardOnNext(_s);
                        ForwardOnCompleted();
                    }
                }
            }
            private readonly IObservable<TSource> _source;
            private readonly int _count;
            private readonly TimeSpan _timeSpan;
            private readonly IScheduler _scheduler;
            public Ferry(IObservable<TSource> source, TimeSpan timeSpan, int count, IScheduler scheduler)
            {
                _source = source;
                _timeSpan = timeSpan;
                _count = count;
                _scheduler = scheduler;
            }
            [return: System.Runtime.CompilerServices.Nullable(new byte[] {
                1,
                0
            })]
            protected override _ CreateSink(IObserver<IList<TSource>> observer)
            {
                return new _(this, observer);
            }
            protected override void Run([System.Runtime.CompilerServices.Nullable(new byte[] {
                1,
                0
            })] _ sink)
            {
                sink.Run();
            }
        }
    }
}