// It would be nice to have this routine, but resolution based on delegate type doesn't work very well in C# public static void Sort <T>(RandomAccessIterator <T> begin, RandomAccessIterator <T> end, Comparison <T> func) { if (begin.Equals(end)) { return; } T[] array = new T[end.Position - begin.Position]; Copy(begin, end, array); Array.Sort(array, func); Copy(array, begin); }
public static void Sort <T>(RandomAccessIterator <T> begin, RandomAccessIterator <T> end, Functional.BinaryPredicate <T> func) { if (begin.Equals(end)) { return; } T[] array = new T[end.Position - begin.Position]; Copy(begin, end, array); Array.Sort(array, new Functional.BinaryPredicateComparison <T>(func).Compare); Copy(array, begin); }
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); } }
public static void Sort <T>(RandomAccessIterator <T> begin, RandomAccessIterator <T> end, IComparer <T> comparer) { if (begin.Equals(end)) { return; } // TODO: This is probably the worst sorting algorithm ever written. I'm lazy, and just want to get the interface // and tests for this algorithm in place. Implement a real sorting function later. // // If we are going to do something this lame, it would probably be better to stuff the items one by one into a // sorted contain, and then copy objects back into [begin,end). T[] array = new T[end.Position - begin.Position]; Copy(begin, end, array); Array.Sort(array, comparer); Copy(array, begin); }
public static bool Equal <T>(RandomAccessIterator <T> begin, RandomAccessIterator <T> end, RandomAccessIterator <T> cmpBegin, RandomAccessIterator <T> cmpEnd, IComparer <T> comparer) { if (IteratorUtil.Distance(begin, end) != IteratorUtil.Distance(cmpBegin, cmpEnd)) { return(false); } for (; !begin.Equals(end) && !cmpBegin.Equals(cmpEnd); begin.MoveNext(), cmpBegin.MoveNext()) { T t1 = begin.Read(); T t2 = cmpBegin.Read(); if (comparer.Compare(t1, t2) != 0) { return(false); } } return(true); }