public void MoveBackwardTest() { int[] data = { 1, 2, 3 }; IList <int> lst = new List <int>(); int expectedCount = 3; int actualCount = 0; using (BidirectionalIterator <int> inputIterator = new BidirectionalIterator <int>(data)) { using (IOutputIterator <int> outputIterator = new BackInsertIterator <int>(lst)) { Algorithm.MoveBackward(inputIterator, outputIterator); actualCount = lst.Count(); } //whether the data is actually remove Assert.IsTrue(inputIterator.Count() == 0); } Assert.IsTrue(expectedCount == actualCount); bool isCorrectData = (lst[0] == 3 && lst[1] == 2 && lst[2] == 1); Assert.IsTrue(isCorrectData); }
/// <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 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 void ReverseTest() { int[] data = { 1, 2, 3 }; using (BidirectionalIterator <int> inputIterator = new BidirectionalIterator <int>(data)) { Algorithm.Reverse(inputIterator); Assert.IsTrue(inputIterator[0] == 3 && inputIterator[1] == 2 && inputIterator[2] == 1); } }
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); } }
public static void Advance <T>(BidirectionalIterator <T> iter, int distance) { while (distance > 0) { iter.MoveNext(); --distance; } while (distance < 0) { iter.MovePrev(); ++distance; } }
public void ReverseCopyTest() { int[] data = { 1, 2, 3 }; IList <int> lst = new List <int>(); using (IBidirectionalIterator <int> inputIterator = new BidirectionalIterator <int>(data)) { using (IOutputIterator <int> outputIterator = new BackInsertIterator <int>(lst)) { Algorithm.ReverseCopy(inputIterator, outputIterator); } Assert.IsTrue(lst[0] == 3 && lst[1] == 2 && lst[2] == 1); } }
public static void TestIEnumarable() { //Test custom simple collection with enumerator MyCollection <int> collection = new MyCollection <int>(new [] { 1, 2, 3, 4 }); Console.WriteLine("MyCollection: "); foreach (var n in collection) { Console.Write($"{n} "); } Console.WriteLine(); //Test Bidirectional iterator LinkedList <int> intList = new LinkedList <int>(); for (int i = 0; i < 6; i++) { intList.AddLast(i); } BidirectionalIterator <int> bidirectionalIterator = new BidirectionalIterator <int>(intList, intList.First, TIterationDirection.Forward); Console.WriteLine("Bidirectional iterator: "); foreach (var n in bidirectionalIterator) { Console.Write($"{n} "); if (n == 5) //change direction { bidirectionalIterator.Direction = TIterationDirection.Backward; } } Console.WriteLine(); //Test Circular iterator CircularIterator <int> circularIterator = new CircularIterator <int>(intList, intList.First); int counter = 0; Console.WriteLine("Circular iterator: "); foreach (var n in circularIterator) { Console.Write($"{n} "); if (counter++ == 20) { circularIterator.Stop(); } } }
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 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); } } }
static void Main() { LinkedList <int> intList = new LinkedList <int>(); for (int i = 1; i < 6; ++i) { intList.AddLast(i); } BidirectionalIterator <int> iter = new BidirectionalIterator <int>(intList, intList.First, TIteratorDirection.Forward); foreach (int n in iter) { Console.WriteLine(n); if (n == 5) { iter.Direction = TIteratorDirection.Backward; } } }
public static void ReverseCopy <T>(BidirectionalIterator <T> begin, BidirectionalIterator <T> end, IList <T> target) { ReverseCopy(begin, end, IteratorUtil.Begin(target)); }
/// <summary> /// Copies the items from a source IList to an output iterator in reverse order. /// </summary> /// <remarks>Note the subtle difference between CopyBackward and ReverseCopy. </remarks> /// <remarks> /// This method is a little different than its Copy counterpart. Copy takes an IEnumerable<T>, which is very loose. We require /// an IList<T>. The reason is because .NET does not provide a way to iterate an enumerable collection in reverse. We do provide /// a workaround (see the next method), but it is very ineffecient."/> /// </remarks> /// <typeparam name="T"></typeparam> /// <param name="list"></param> /// <param name="target"></param> public static void CopyBackward <T>(IList <T> list, BidirectionalIterator <T> targetEnd) { CopyBackward(IteratorUtil.Begin(list), IteratorUtil.End(list), targetEnd); }
/// <summary> /// Copies elements from a source enumerable to an output iterator in reverse order /// </summary> /// <remarks> /// Because .NET does not allow use to traverse an enumerator in reverse, we have to create a complete, temporary /// copy of the input collection. This is terribly inefficient. Beware when using this routine with large collections. /// </remarks> /// <typeparam name="T"></typeparam> /// <param name="enumerable"></param> /// <param name="target"></param> public static void CopyBackward <T>(IEnumerable <T> enumerable, BidirectionalIterator <T> targetEnd) { CopyBackward(new List <T>(enumerable), targetEnd); }
public UnitGroup(IEnumerable <UnitSelector> units) { this.units = new BidirectionalIterator <UnitSelector>(units); this.units.TryMoveNext(); }
public static bool NextPermutation <T>(BidirectionalIterator <T> begin, BidirectionalIterator <T> end) where T : IComparable <T> { return(NextPermutation(begin, end, Comparer <T> .Default)); }
public static void CopyBackward <T>(BidirectionalIterator <T> begin, BidirectionalIterator <T> end, IList <T> targetEnd) { CopyBackward(begin, end, IteratorUtil.End(targetEnd)); }
public static BidirectionalIterator <T> AdvanceCopy <T>(BidirectionalIterator <T> iter, int distance) { iter = IteratorUtil.Clone(iter); Advance(iter, distance); return(iter); }