Пример #1
0
        public TElement LastOrDefault(Func <TElement, bool> predicate)
        {
            CachingComparer <TElement> comparer = GetComparer();

            using (IEnumerator <TElement> e = source.GetEnumerator())
            {
                TElement value;
                do
                {
                    if (!e.MoveNext())
                    {
                        return(default(TElement));
                    }
                    value = e.Current;
                } while (!predicate(value));
                comparer.SetElement(value);
                while (e.MoveNext())
                {
                    TElement x = e.Current;
                    if (predicate(x) && comparer.Compare(x, false) > 0)
                    {
                        value = x;
                    }
                }
                return(value);
            }
        }
Пример #2
0
        internal override int Compare(TElement element, bool cacheLower)
        {
            TKey newKey = KeySelector(element);
            int  cmp    = Descending ? Comparer.Compare(LastKey, newKey) : Comparer.Compare(newKey, LastKey);

            if (cmp == 0)
            {
                return(_child.Compare(element, cacheLower));
            }
            if (cacheLower == cmp < 0)
            {
                LastKey = newKey;
                _child.SetElement(element);
            }
            return(cmp);
        }
Пример #3
0
        public TElement First(Func <TElement, bool> predicate)
        {
            CachingComparer <TElement> comparer = GetComparer();

            using (IEnumerator <TElement> e = source.GetEnumerator())
            {
                TElement value;
                do
                {
                    if (!e.MoveNext())
                    {
                        throw Error.NoMatch();
                    }
                    value = e.Current;
                } while (!predicate(value));
                comparer.SetElement(value);
                while (e.MoveNext())
                {
                    TElement x = e.Current;
                    if (predicate(x) && comparer.Compare(x, true) < 0)
                    {
                        value = x;
                    }
                }
                return(value);
            }
        }
Пример #4
0
        private bool TryGetFirst(out TElement result)
        {
            CachingComparer <TElement> comparer = GetComparer();

            using (IEnumerator <TElement> e = source.GetEnumerator())
            {
                if (!e.MoveNext())
                {
                    result = default(TElement);
                    return(false);
                }
                TElement value = e.Current;
                comparer.SetElement(value);
                while (e.MoveNext())
                {
                    TElement x = e.Current;
                    if (comparer.Compare(x, true) < 0)
                    {
                        value = x;
                    }
                }
                result = value;
                return(true);
            }
        }
Пример #5
0
        public TElement TryGetLast(Func <TElement, bool> predicate, out bool found)
        {
            CachingComparer <TElement> comparer = GetComparer();

            using (IEnumerator <TElement> e = _source.GetEnumerator())
            {
                TElement value;
                do
                {
                    if (!e.MoveNext())
                    {
                        found = false;
                        return(default(TElement));
                    }

                    value = e.Current;
                }while (!predicate(value));

                comparer.SetElement(value);
                while (e.MoveNext())
                {
                    TElement x = e.Current;
                    if (predicate(x) && comparer.Compare(x, false) >= 0)
                    {
                        value = x;
                    }
                }

                found = true;
                return(value);
            }
        }
Пример #6
0
        public TElement TryGetLast(out bool found)
        {
            using (IEnumerator <TElement> e = _source.GetEnumerator())
            {
                if (!e.MoveNext())
                {
                    found = false;
                    return(default(TElement));
                }

                CachingComparer <TElement> comparer = GetComparer();
                TElement value = e.Current;
                comparer.SetElement(value);
                while (e.MoveNext())
                {
                    TElement current = e.Current;
                    if (comparer.Compare(current, false) >= 0)
                    {
                        value = current;
                    }
                }

                found = true;
                return(value);
            }
        }
Пример #7
0
        public TElement TryGetFirst(out bool found)
        {
            CachingComparer <TElement> comparer = GetComparer();

            using (IEnumerator <TElement> e = _source.GetEnumerator())
            {
                if (!e.MoveNext())
                {
                    found = false;
                    return(default(TElement));
                }

                TElement value = e.Current;
                comparer.SetElement(value);
                while (e.MoveNext())
                {
                    TElement x = e.Current;
                    if (comparer.Compare(x, true) < 0)
                    {
                        value = x;
                    }
                }

                found = true;
                return(value);
            }
        }
Пример #8
0
        private TElement Last(Buffer <TElement> buffer)
        {
            CachingComparer <TElement> comparer = GetComparer();

            TElement[] items = buffer.items;
            int        count = buffer.count;
            TElement   value = items[0];

            comparer.SetElement(value);
            for (int i = 1; i != count; ++i)
            {
                TElement x = items[i];
                if (comparer.Compare(x, false) >= 0)
                {
                    value = x;
                }
            }
            return(value);
        }
Пример #9
0
        public TElement LastOrDefault()
        {
            CachingComparer <TElement> comparer = GetComparer();

            using (IEnumerator <TElement> e = source.GetEnumerator())
            {
                if (!e.MoveNext())
                {
                    return(default(TElement));
                }
                TElement value = e.Current;
                comparer.SetElement(value);
                while (e.MoveNext())
                {
                    TElement x = e.Current;
                    if (comparer.Compare(x, false) >= 0)
                    {
                        value = x;
                    }
                }
                return(value);
            }
        }