public static int CountIf <T>( this ListIterator <T> first, ListIterator <T> last, Func <T, bool> pred ) { var count = 0; while (first.NotEqual(last)) { if (pred(first.GetCurrent())) { count++; } first = first.GetNext(); } return(count); }
public static void Mismatch <T>( this ListIterator <T> first1, ListIterator <T> last1, ListIterator <T> first2, Func <T, T, bool> pred, out ListIterator <T> mismatch1, out ListIterator <T> mismatch2 ) { while (first1.NotEqual(last1)) { if (pred(first1.GetCurrent(), first2.GetCurrent()) == false) { break; } first1 = first1.GetNext(); first2 = first2.GetNext(); } mismatch1 = first1; mismatch2 = first2; }
public static ListIterator <T> FindFirstOf <T>( this ListIterator <T> first1, ListIterator <T> last1, ListIterator <T> first2, ListIterator <T> last2, Func <T, T, bool> pred ) { while (first1.NotEqual(last1)) { for (var it = first2; it.NotEqual(last2); it = it.GetNext()) { if (pred(it.GetCurrent(), first1.GetCurrent())) { return(first1); } } first1 = first1.GetNext(); } return(last1); }
public static ListIterator <T> MaxElement <T>( this ListIterator <T> first, ListIterator <T> last, Func <T, T, bool> comp ) { if (first.IsEqual(last)) { return(last); } var largest = first; while ((first = first.GetNext()).NotEqual(last)) { if (comp(largest.GetCurrent(), first.GetCurrent())) { largest = first; } } return(largest); }
public static ListIterator <T> IsSortedUntil <T>( this ListIterator <T> first, ListIterator <T> last, Func <T, T, bool> comp ) { if (first.IsEqual(last)) { return(first); } var next = first; while ((next = next.GetNext()).NotEqual(last)) { if (comp(next.GetCurrent(), first.GetCurrent())) { return(next); } first = first.GetNext(); } return(last); }
public static ListIterator <T> AdjacentFind <T>( this ListIterator <T> first, ListIterator <T> last, Func <T, T, bool> pred ) { if (first.NotEqual(last)) { var next = first; next = next.GetNext(); while (next.NotEqual(last)) { if (pred(first.GetCurrent(), next.GetCurrent())) { return(first); } first = first.GetNext(); next = next.GetNext(); } } return(last); }
public static void PushHeap <T>( this ListIterator <T> first, ListIterator <T> last, Func <T, T, bool> comp ) { if (first.Distance(last) < 2) { return; } last = last.GetPrev(); var temp = last.GetCurrent(); var parent = first.GetAdvanced((first.Distance(last) - 1) / 2); while (first.Distance(last) > 0 && comp(parent.GetCurrent(), temp)) { last.SetCurrent(parent.GetCurrent()); last = parent; parent = first.GetAdvanced((first.Distance(last) - 1) / 2); } last.SetCurrent(temp); }
public static void InplaceMerge <T>( this ListIterator <T> first, ListIterator <T> middle, ListIterator <T> last, Func <T, T, bool> comp ) { if (first.Index >= middle.Index || middle.Index >= last.Index) { return; } if (last.Index - first.Index == 2) { if (comp(middle.GetCurrent(), first.GetCurrent())) { Swap(first, middle); } return; } ListIterator <T> firstCut; ListIterator <T> secondCut; if (middle.Index - first.Index > last.Index - middle.Index) { firstCut = first.GetAdvanced(first.Distance(middle) / 2); secondCut = middle.LowerBound(last, firstCut.GetCurrent(), comp); } else { secondCut = middle.GetAdvanced(middle.Distance(last) / 2); firstCut = first.UpperBound(middle, secondCut.GetCurrent(), comp); } Rotate(firstCut, middle, secondCut); middle = firstCut.GetAdvanced(middle.Distance(secondCut)); InplaceMerge(first, firstCut, middle, comp); InplaceMerge(middle, secondCut, last, comp); }