public static void EqualRange <T>( this ArrayIterator <T> first, ArrayIterator <T> last, T val, Func <T, T, bool> comp, out ArrayIterator <T> lower, out ArrayIterator <T> upper ) { lower = first.LowerBound(last, val, comp); upper = lower.UpperBound(last, val, comp); }
public static void InplaceMerge <T>( this ArrayIterator <T> first, ArrayIterator <T> middle, ArrayIterator <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; } ArrayIterator <T> firstCut; ArrayIterator <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); }