Ejemplo n.º 1
0
        public static bool PrevPermutation <T>(BidirectionalIterator <T> begin, BidirectionalIterator <T> end,
                                               Functional.BinaryPredicate <T> func)
        {
            if (begin.Equals(end))
            {
                return(false);
            }

            if (IteratorUtil.IsOneElementRange(begin, end))
            {
                return(false);
            }

            BidirectionalIterator <T> i = IteratorUtil.Clone(end);

            i.MovePrev();

            for (;;)
            {
                BidirectionalIterator <T> ii = IteratorUtil.Clone(i);
                i.MovePrev();

                T i_value = i.Read();
                if (func(ii.Read(), i_value))
                {
                    BidirectionalIterator <T> j = IteratorUtil.Clone(end);
                    for (j.MovePrev(); !func(j.Read(), i_value); j.MovePrev())
                    {
                        ;
                    }
                    IteratorUtil.Swap(i, j);
                    Reverse(ii, end);
                    return(true);
                }
                if (i.Equals(begin))
                {
                    Reverse(begin, end);
                    return(false);
                }
            }
        }
Ejemplo n.º 2
0
        public static bool NextPermutation <T>(BidirectionalIterator <T> begin, BidirectionalIterator <T> end, IComparer <T> comparer)
        {
            if (begin.Equals(end))
            {
                return(false);
            }

            if (IteratorUtil.IsOneElementRange(begin, end))
            {
                return(false);
            }

            BidirectionalIterator <T> i = IteratorUtil.Clone(end);

            i.MovePrev();

            for (;;)
            {
                BidirectionalIterator <T> ii = IteratorUtil.Clone(i);
                i.MovePrev();

                T i_value = i.Read();
                if (comparer.Compare(i_value, ii.Read()) == -1)
                {
                    BidirectionalIterator <T> j = IteratorUtil.Clone(end);
                    for (j.MovePrev(); comparer.Compare(i_value, j.Read()) != -1; j.MovePrev())
                    {
                        ;
                    }
                    IteratorUtil.Swap(i, j);
                    Reverse(ii, end);
                    return(true);
                }
                if (i.Equals(begin))
                {
                    Reverse(begin, end);
                    return(false);
                }
            }
        }
Ejemplo n.º 3
0
        public static ForwardIterator <T> Search <T>(ForwardIterator <T> begin, ForwardIterator <T> end,
                                                     ForwardIterator <T> searchBegin, ForwardIterator <T> searchEnd,
                                                     IEqualityComparer <T> comparer)
        {
            if (begin.Equals(end))
            {
                return(null);
            }

            if (searchBegin.Equals(searchEnd))
            {
                return(null);
            }

            begin       = IteratorUtil.Clone(begin);
            searchBegin = IteratorUtil.Clone(searchBegin);

            if (IteratorUtil.IsOneElementRange(searchBegin, searchEnd))
            {
                return(Find(begin, end, searchBegin.Read(), comparer));
            }

            T firstSearchElement = searchBegin.Read();

            while (!begin.Equals(end))
            {
                begin = Find(begin, end, firstSearchElement, comparer);
                if (begin == null)
                {
                    return(null);
                }

                ForwardIterator <T> iter = IteratorUtil.Clone(begin);
                iter.MoveNext();
                if (iter.Equals(end))
                {
                    return(null);
                }

                ForwardIterator <T> searchIter = IteratorUtil.Clone(searchBegin);
                searchIter.MoveNext();

                while (comparer.Equals(iter.Read(), searchIter.Read()))
                {
                    searchIter.MoveNext();
                    if (searchIter.Equals(searchEnd))
                    {
                        return(begin);
                    }

                    iter.MoveNext();
                    if (iter.Equals(end))
                    {
                        return(null);
                    }
                }

                begin.MoveNext();
            }
            return(null);
        }
Ejemplo n.º 4
0
        public static ForwardIterator <T> Search <T>(ForwardIterator <T> begin, ForwardIterator <T> end,
                                                     ForwardIterator <T> searchBegin, ForwardIterator <T> searchEnd)
            where T : IEquatable <T>
        {
            return(Search(begin, end, searchBegin, searchEnd, EqualityComparer <T> .Default));

#if NEVER
            if (begin.Equals(end))
            {
                return(null);
            }

            if (searchBegin.Equals(searchEnd))
            {
                return(null);
            }

            begin       = IteratorUtil.Clone(begin);
            searchBegin = IteratorUtil.Clone(searchBegin);

            if (IteratorUtil.IsOneElementRange(searchBegin, searchEnd))
            {
                return(Find(begin, end, searchBegin.Read()));
            }

            T firstSearchElement = searchBegin.Read();
            while (!begin.Equals(end))
            {
                begin = Find(begin, end, firstSearchElement);
                if (begin == null)
                {
                    return(null);
                }

                ForwardIterator <T> iter = IteratorUtil.Clone(begin);
                iter.MoveNext();
                if (iter.Equals(end))
                {
                    return(null);
                }

                ForwardIterator <T> searchIter = IteratorUtil.Clone(searchBegin);
                searchIter.MoveNext();

                while (iter.Read().Equals(searchIter.Read()))
                {
                    searchIter.MoveNext();
                    if (searchIter.Equals(searchEnd))
                    {
                        return(begin);
                    }

                    iter.MoveNext();
                    if (iter.Equals(end))
                    {
                        return(null);
                    }
                }

                begin.MoveNext();
            }
            return(null);
#endif
        }