| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419 | // Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.using System;using System.Collections.Generic;using System.Diagnostics;using System.Linq;using System.Reactive.Disposables;using System.Reactive.Subjects;namespace System.Reactive.Linq.ObservableImpl{    class OrderBy<TSource, TKey> : OrderedProducer<TSource>, IOrderedObservable<TSource>    {        private readonly Func<TSource, IObservable<TKey>> _timeSelector;        private readonly Func<TSource, TKey> _keySelector;        private readonly IComparer<TKey> _comparer;        private readonly bool _descending;        public OrderBy(IObservable<TSource> source, Func<TSource, IObservable<TKey>> timeSelector, bool descending)            : base(source, null)        {            _timeSelector = timeSelector;            _descending = descending;        }        public OrderBy(IObservable<TSource> source, Func<TSource, IObservable<TKey>> timeSelector, bool descending, OrderedProducer<TSource> previous)            : base(source, previous)        {            _timeSelector = timeSelector;            _descending = descending;        }        public OrderBy(IObservable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer, bool descending)            : base(source, null)        {            _keySelector = keySelector;            _comparer = comparer ?? Comparer<TKey>.Default;            _descending = descending;        }        public OrderBy(IObservable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer, bool descending, OrderedProducer<TSource> previous)            : base(source, previous)        {            _keySelector = keySelector;            _comparer = comparer ?? Comparer<TKey>.Default;            _descending = descending;        }        public IOrderedObservable<TSource> CreateOrderedObservable<TTKey>(Func<TSource, TTKey> keySelector, IComparer<TTKey> comparer, bool descending)        {            return new OrderBy<TSource, TTKey>(base._source, keySelector, comparer, descending, previous: this);        }        public IOrderedObservable<TSource> CreateOrderedObservable<TOther>(Func<TSource, IObservable<TOther>> timeSelector, bool descending)        {            return new OrderBy<TSource, TOther>(base._source, timeSelector, descending, previous: this);        }        protected override SortSink Sort(IObserver<TSource> observer, IDisposable cancel)        {            if (_timeSelector != null)            {                if (_descending)                {                    return new Descending(this, observer, cancel);                }                else                {                    return new Ascending(this, observer, cancel);                }            }            else            {                var sink = observer as ι;                if (sink != null)                {                    /* This optimization exists for 2 reasons:                      *                      * 1. To avoid having to use multiple buffers in consecutive ordering operations.                     * 2. To take advantage of Enumerable's optimizations for consecutive ordering operations.                     */                    sink.OrderBy(this);                    return sink;                }                else                {                    if (_descending)                    {                        return new Descending_(this, observer, cancel);                    }                    else                    {                        return new Ascending_(this, observer, cancel);                    }                }            }        }        class Ascending : ρ        {            public Ascending(OrderBy<TSource, TKey> parent, IObserver<TSource> observer, IDisposable cancel)                : base(parent, observer, cancel)            {            }            protected override void Consume(TSource value)            {                base._observer.OnNext(value);            }            protected override void Complete()            {                base._observer.OnCompleted();            }        }        class Descending : ρ        {            public Descending(OrderBy<TSource, TKey> parent, IObserver<TSource> observer, IDisposable cancel)                : base(parent, observer, cancel)            {            }            private IList<TSource> _list;            public override IDisposable Initialize()            {                _list = new List<TSource>();                return base.Initialize();            }            protected override void Consume(TSource value)            {                _list.Add(value);            }            protected override void Complete()            {                foreach (var value in _list.Reverse())                {                    base._observer.OnNext(value);                }                base._observer.OnCompleted();            }        }        class Ascending_ : ι        {            public Ascending_(OrderBy<TSource, TKey> parent, IObserver<TSource> observer, IDisposable cancel)                : base(parent, observer, cancel)            {            }            protected override IOrderedEnumerable<TSource> OrderBy(IEnumerable<TSource> source)            {                return source.OrderBy(_parent._keySelector, _parent._comparer);            }            protected override IOrderedEnumerable<TSource> ThenBy(IOrderedEnumerable<TSource> source)            {                return source.ThenBy(_parent._keySelector, _parent._comparer);            }        }        class Descending_ : ι        {            public Descending_(OrderBy<TSource, TKey> parent, IObserver<TSource> observer, IDisposable cancel)                : base(parent, observer, cancel)            {            }            protected override IOrderedEnumerable<TSource> OrderBy(IEnumerable<TSource> source)            {                return source.OrderByDescending(base._parent._keySelector, base._parent._comparer);            }            protected override IOrderedEnumerable<TSource> ThenBy(IOrderedEnumerable<TSource> source)            {                return source.ThenByDescending(base._parent._keySelector, base._parent._comparer);            }        }        /// <summary>        /// Reactive sorting.  This code is based on the code from the SelectMany operator (8/11/2013).        /// </summary>        abstract class ρ : SortSink        {            protected readonly OrderBy<TSource, TKey> _parent;            public ρ(OrderBy<TSource, TKey> parent, IObserver<TSource> observer, IDisposable cancel)                : base(observer, cancel)            {                _parent = parent;            }            private object _gate;            private bool _isStopped;            private CompositeDisposable _group;            private SingleAssignmentDisposable _sourceSubscription;            public override IDisposable Initialize()            {                _gate = new object();                _isStopped = false;                _group = new CompositeDisposable();                _sourceSubscription = new SingleAssignmentDisposable();                _group.Add(_sourceSubscription);                return _group;            }            public override void Run(IObservable<TSource> source)            {                _sourceSubscription.Disposable = source.SubscribeSafe(this);            }            public override void OnNext(TSource value)            {                var collection = default(IObservable<TKey>);                try                {                    collection = _parent._timeSelector(value);                }                catch (Exception ex)                {                    lock (_gate)                    {                        base._observer.OnError(ex);                        base.Dispose();                    }                    return;                }                var innerSubscription = new SingleAssignmentDisposable();                _group.Add(innerSubscription);                innerSubscription.Disposable = collection.SubscribeSafe(new ι(this, value, innerSubscription));            }            public override void OnError(Exception error)            {                lock (_gate)                {                    base._observer.OnError(error);                    base.Dispose();                }            }            public override void OnCompleted()            {                _isStopped = true;                if (_group.Count == 1)                {                    //                    // Notice there can be a race between OnCompleted of the source and any                    // of the inner sequences, where both see _group.Count == 1, and one is                    // waiting for the lock. There won't be a double OnCompleted observation                    // though, because the call to Dispose silences the observer by swapping                    // in a NopObserver<T>.                    //                    lock (_gate)                    {                        Complete();                        base.Dispose();                    }                }                else                {                    _sourceSubscription.Dispose();                }            }            protected abstract void Complete();            protected abstract void Consume(TSource value);            class ι : IObserver<TKey>            {                private readonly ρ _parent;                private readonly TSource _value;                private readonly IDisposable _self;                public ι(ρ parent, TSource value, IDisposable self)                {                    _parent = parent;                    _value = value;                    _self = self;                }                public void OnNext(TKey value)                {                    OnCompleted();                }                public void OnError(Exception error)                {                    lock (_parent._gate)                    {                        _parent._observer.OnError(error);                        _parent.Dispose();                    }                }                public void OnCompleted()                {                    lock (_parent._gate)                    {                        _parent.Consume(_value);                    }                    _parent._group.Remove(_self);                    if (_parent._isStopped && _parent._group.Count == 1)                    {                        //                        // Notice there can be a race between OnCompleted of the source and any                        // of the inner sequences, where both see _group.Count == 1, and one is                        // waiting for the lock. There won't be a double OnCompleted observation                        // though, because the call to Dispose silences the observer by swapping                        // in a NopObserver<T>.                        //                        lock (_parent._gate)                        {                            _parent.Complete();                            _parent.Dispose();                        }                    }                }            }        }        /// <summary>        /// Aggregates before sorting.  This code is based on the code from the ToList operator (8/11/2013).        /// </summary>        abstract class ι : SortSink        {            protected readonly OrderBy<TSource, TKey> _parent;            public ι(OrderBy<TSource, TKey> parent, IObserver<TSource> observer, IDisposable cancel)                : base(observer, cancel)            {                _parent = parent;            }            private List<TSource> _list;            private Stack<OrderBy<TSource, TKey>> _orderBy;            private SingleAssignmentDisposable _sourceSubscription;            public override IDisposable Initialize()            {                _list = new List<TSource>();                _orderBy = new Stack<OrderBy<TSource, TKey>>();                _sourceSubscription = new SingleAssignmentDisposable();                return _sourceSubscription;            }            public override void Run(IObservable<TSource> source)            {                _sourceSubscription.Disposable = source.SubscribeSafe(this);            }            public override void OnNext(TSource value)            {                _list.Add(value);            }            public override void OnError(Exception error)            {                base._observer.OnError(error);                base.Dispose();            }            public override void OnCompleted()            {                foreach (var value in OrderAll(_list))                {                    base._observer.OnNext(value);                }                base._observer.OnCompleted();                base.Dispose();            }            protected abstract IOrderedEnumerable<TSource> OrderBy(IEnumerable<TSource> source);            protected abstract IOrderedEnumerable<TSource> ThenBy(IOrderedEnumerable<TSource> source);            internal void OrderBy(OrderBy<TSource, TKey> parent)            {                _orderBy.Push(parent);            }            private IEnumerable<TSource> OrderAll(IEnumerable<TSource> source)            {                IOrderedEnumerable<TSource> ordered = null;                foreach (var parent in _orderBy)                {                    if (ordered == null)                    {                        ordered = parent._descending                                ? source.OrderByDescending(parent._keySelector, parent._comparer)                                : source.OrderBy(parent._keySelector, parent._comparer);                    }                    else                    {                        ordered = parent._descending                                ? ordered.ThenByDescending(parent._keySelector, parent._comparer)                                : ordered.ThenBy(parent._keySelector, parent._comparer);                    }                }                return ordered == null ? OrderBy(source) : ThenBy(ordered);            }        }    }}
 |