Beispiel #1
0
        public static void Rotate <T>(ForwardIterator <T> begin, ForwardIterator <T> middle, ForwardIterator <T> end)
        {
            if (begin.Equals(middle) || middle.Equals(end) || begin.Equals(end))
            {
                return;
            }

//        begin  = IteratorUtil.Clone(begin);
//        middle = IteratorUtil.Clone(middle);
            ForwardIterator <T> left  = IteratorUtil.Clone(begin);
            ForwardIterator <T> right = IteratorUtil.Clone(middle);

            while (true)
            {
                IteratorUtil.Swap(left, right);
                left.MoveNext();
                right.MoveNext();
                if (left.Equals(middle))
                {
                    if (right.Equals(end))
                    {
                        return;
                    }
                    else
                    {
                        middle = IteratorUtil.Clone(right);
                    }
                }
                else if (right.Equals(end))
                {
                    right = IteratorUtil.Clone(middle);
                }
            }
        }
Beispiel #2
0
 public static void SwapRanges <T>(ForwardIterator <T> begin, ForwardIterator <T> end, ForwardIterator <T> dest)
 {
     for (begin = IteratorUtil.Clone(begin), dest = IteratorUtil.Clone(dest); !begin.Equals(end); begin.MoveNext(), dest.MoveNext())
     {
         IteratorUtil.Swap(begin, dest);
     }
 }
Beispiel #3
0
        public static void Reverse <T>(BidirectionalIterator <T> begin, BidirectionalIterator <T> end)
        {
            end   = IteratorUtil.Clone(end);
            begin = IteratorUtil.Clone(begin);

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

            last.MovePrev();
            for (; !begin.Equals(last) && !begin.Equals(end); last.MovePrev(), end.MovePrev(), begin.MoveNext())
            {
                IteratorUtil.Swap(begin, last);
            }
        }
Beispiel #4
0
        static public void RandomShuffle <T>(RandomAccessIterator <T> begin, RandomAccessIterator <T> end, RandomShuffleFunc func)
        {
            if (begin.Equals(end))
            {
                return;
            }

            begin = IteratorUtil.Clone(begin);
            for (RandomAccessIterator <T> iter = IteratorUtil.AdvanceCopy(begin, 1); !iter.Equals(end); iter.MoveNext())
            {
                begin.Position = func(iter.Position + 1);
                IteratorUtil.Swap(iter, begin);
            }
        }
Beispiel #5
0
        public static bool NextPermutation <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(i_value, ii.Read()))
                {
                    BidirectionalIterator <T> j = IteratorUtil.Clone(end);
                    for (j.MovePrev(); !func(i_value, j.Read()); j.MovePrev())
                    {
                        ;
                    }
                    IteratorUtil.Swap(i, j);
                    Reverse(ii, end);
                    return(true);
                }
                if (i.Equals(begin))
                {
                    Reverse(begin, end);
                    return(false);
                }
            }
        }
Beispiel #6
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);
                }
            }
        }