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); } }
/// <summary> /// Copies elements from [begin,end) to [target-(end-begin), target), starting with *(target-1) = *(end-1), /// and progressing backwards towards begin. Note that although elements are copied in reverse order, the order /// of elements is not changed. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="begin"></param> /// <param name="end"></param> /// <param name="target"></param> public static void CopyBackward <T>(BidirectionalIterator <T> begin, BidirectionalIterator <T> end, BidirectionalIterator <T> targetEnd) { targetEnd = IteratorUtil.Clone(targetEnd); end = IteratorUtil.Clone(end); while (!begin.Equals(end)) { end.MovePrev(); targetEnd.MovePrev(); targetEnd.Write(end.Read()); } }
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); } } }
public static void ReverseCopy <T>(BidirectionalIterator <T> begin, BidirectionalIterator <T> end, OutputIterator <T> dest) { end = IteratorUtil.Clone(end); dest = IteratorUtil.Clone(dest); while (!begin.Equals(end)) { end.MovePrev(); dest.Write(end.Read()); dest.MoveNext(); } }
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); } } }
public static void Advance <T>(BidirectionalIterator <T> iter, int distance) { while (distance > 0) { iter.MoveNext(); --distance; } while (distance < 0) { iter.MovePrev(); ++distance; } }