internal static void RotateLeftInPlaceInternal <T>(this T[] array, IntT shift) { Contract.Requires(shift > 0); Contract.Requires(null != array && array.Length > shift && array.Length > 1); var c = 0; var count = array.Length; for (var m = 0; count > c; ++m) { var t = array[m]; var i = m; for (var j = m + shift; ;) { array[i] = array[j]; ++c; i = j; j += shift; if (count <= j) { j -= count; } if (j == m) { break; } } array[i] = t; ++c; } }
internal static bool CheckMatchCounts <T, TEqualityComparer>(this T[] first, IntT startFirst, IntT endExFirst, T[] second, IntT startSecond, IntT endExSecond, TEqualityComparer equalityComparer) where TEqualityComparer : IO.IFunc <T, T, bool> { var(u, v) = TrimMatchingSuffixes(first, endExFirst, second, endExSecond, equalityComparer); for (var i = startFirst; u > i; ++i) { if (i == IndexOfInternal(first, startFirst, i, first[i], equalityComparer)) { var c2 = CountInternal(first, startSecond, v, first[i], equalityComparer); if (0 == c2) { return(false); } var k = i; unchecked { ++k; } var c1 = 1 + CountInternal(first, k, u, first[i], equalityComparer); if (c1 != c2) { return(false); } } } return(true); }
internal static void RotateLeftInPlaceInternal <T>(this T[] array, IntT start, IntT count, IntT shift) { Contract.Requires(null != array && CheckSegment(array, start, count)); Contract.Requires(shift > 0); Contract.Requires(count > shift); Contract.Requires(count > 1); var c = 0; for (var m = 0; count > c; ++m) { var t = array[start + m]; var i = m; for (var j = m + shift; ;) { array[start + i] = array[start + j]; ++c; i = j; j += shift; if (count <= j) { j -= count; } if (j == m) { break; } } array[start + i] = t; ++c; } }
internal IntT IndexOfInternal <TListSource>(TListSource source, IntT start, IntT count) where TListSource : IReadOnlyList <T> { var pattern_count = this.pattern_count; if (pattern_count > 0) { if (pattern_count <= count) { var t = this.skip_table; var s = this.skip_table_start; var pattern = this.pattern; var pattern_start = this.pattern_start; var comparer = this.equalityComparer; var i = start; var j = (IntT)0; while (unchecked (start + count) > i) { while (j > -1 && !comparer.Invoke(pattern[pattern_start + j], source[i])) { j = t[s + j]; } ++j; ++i; if (pattern_count <= j) { return(i - j); } } } return(-1); } return(start); }
public static void SortHeap <T, TComparer, TValue>(T[] keys, IntT start, IntT count, TValue[] values) where TComparer : struct, IComparer <T> { Contract.Requires(null != keys); Contract.Requires(CheckSegment(keys, start, count)); Contract.Requires(null == values || CheckSegment(values, start, count)); SortHeap(keys, default(TComparer), start, count, values); }
public static void SortPartInsertion <T, TComparer, TValue>(T[] keys, TComparer comparer, IntT first, IntT last, TValue[] values) where TComparer : IComparer <T> { Contract.Requires(null != keys); Contract.Requires(first <= last); Contract.Requires(CheckIndex(keys, first)); Contract.Requires(CheckIndex(keys, last)); Contract.Requires(null != comparer); unchecked { IntT i, j; T t; TValue tValue; for (i = first; last > i; ++i) { j = i; t = keys[i + 1]; tValue = (values != null) ? values[i + 1] : default(TValue); while (j >= first && comparer.Compare(t, keys[j]) < 0) { keys[j + 1] = keys[j]; if (values != null) { values[j + 1] = values[j]; } --j; } keys[j + 1] = t; if (values != null) { values[j + 1] = tValue; } } } }
public static void SwapIfGreater <T, TComparer>(T[] keys, IntT first, IntT second) where TComparer : struct, IComparer <T> { Contract.Requires(null != keys); Contract.Requires(CheckIndex(keys, first)); Contract.Requires(CheckIndex(keys, second)); SwapIfGreater(keys, default(TComparer), first, second); }
public static void SortHeap <T, TComparer>(T[] keys, TComparer comparer, IntT start, IntT count) where TComparer : IComparer <T> { Contract.Requires(null != keys); Contract.Requires(CheckSegment(keys, start, count)); Contract.Requires(null != comparer); SortHeap(keys, comparer, start, count, ArrayModule.Null); }
public static void SortHeapWalkSiftDown <T, TComparer, TValue>(T[] keys, IntT root, IntT start, IntT count, TValue[] values) where TComparer : struct, IComparer <T> { Contract.Requires(null != keys); Contract.Requires(CheckIndex(keys, root)); Contract.Requires(CheckIndex(keys, start)); SortHeapWalkSiftDown(keys, default(TComparer), root, start, count, values); }
public static void RotateLeftInPlace_A <T>(this T[] array, IntT start, IntT count, IntT shift) { if (null != array) { if (count > 1) { if (unchecked (start + count) <= array.Length && 0 <= start) { var s = NormalizeShiftCount(count, shift); if (s > 0) { var t = unchecked (count - s); ReverseInternal(array, start, count); ReverseInternal(array, unchecked (start + t), s); ReverseInternal(array, start, t); } return; } goto L_a; } if (CheckSegment(array, start, count)) { return; } L_a: // TODO: Perf throw new ArgumentException(); } ThrowHelper.ThrowArgumentNullException_array(); }
public static void Swap <T>(T[] keys, IntT first, IntT second) { Contract.Requires(null != keys); Contract.Requires(CheckIndex(keys, first)); Contract.Requires(CheckIndex(keys, second)); Swap(keys, first, second, ArrayModule.Null); }
internal SearchProcessingUnit_A_KnuthMorrisPratt(IntT[] skip_table, IntT skip_table_start, TListPattern pattern, IntT pattern_start, IntT pattern_count, TEqualityComparer equalityComparer) { this.skip_table = skip_table; this.skip_table_start = skip_table_start; this.equalityComparer = equalityComparer; this.pattern = pattern; this.pattern_start = pattern_start; this.pattern_count = pattern_count; }
internal static void ReverseInternal <T>(this T[] array, IntT start, IntT count) { unchecked { var i = start; var j = start + count - 1; for (; i < j; ++i, --j) { var temp = array[i]; array[i] = array[j]; array[j] = temp; } } }
public IntT IndexOf <TListSource>(TListSource source, IntT start, IntT count) where TListSource : IReadOnlyList <T> { if (null != source) { if (ListModule.CheckSegment <T, TListSource>(source, start, count)) { return(IndexOfInternal(source, start, count)); } // TODO throw new ArgumentException(); } throw new ArgumentNullException("source"); }
public static void Reverse <T>(this T[] array, IntT start, IntT count) { if (null != array) { if (CheckSegment(array, start, count)) { ReverseInternal(array, start, count); return; } // TODO: Perf throw new ArgumentException(); } ThrowHelper.ThrowArgumentNullException_array(); }
public static void Swap <T, TValue>(T[] keys, IntT i, IntT j, TValue[] values) { Contract.Requires(null != keys); Contract.Requires(CheckIndex(keys, i)); Contract.Requires(CheckIndex(keys, j)); var t = keys[i]; keys[i] = keys[j]; keys[j] = t; if (null != values) { var value = values[i]; values[i] = values[j]; values[j] = value; } }
public static IntT PickPivotAndPartition <T, TComparer, TValue>(T[] keys, TComparer comparer, IntT first, IntT last, TValue[] values) where TComparer : IComparer <T> { Contract.Requires(null != keys); Contract.Requires(first <= last); Contract.Requires(CheckIndex(keys, first)); Contract.Requires(CheckIndex(keys, last)); Contract.Ensures(first <= Contract.Result <IntT>() && Contract.Result <IntT>() <= last); IntT middle = unchecked (first + ((last - first) / 2)); SwapIfGreater(keys, comparer, first, middle, values); SwapIfGreater(keys, comparer, first, last, values); SwapIfGreater(keys, comparer, middle, last, values); var pivot = keys[middle]; Swap(keys, middle, unchecked (last - 1), values); IntT left = first, right = unchecked (last - 1); while (left < right) { if (pivot == null) { while (left < unchecked (last - 1) && keys[unchecked (++left)] == null) { } while (right > first && keys[unchecked (--right)] != null) { } } else { while (comparer.Compare(pivot, keys[unchecked (++left)]) > 0) { } while (comparer.Compare(pivot, keys[unchecked (--right)]) < 0) { } } if (left >= right) { break; } Swap(keys, left, right, values); } Swap(keys, left, unchecked (last - 1), values); return(left); }
public static void RotateRightInPlace <T>(this T[] array, IntT shift) { if (null != array) { var count = array.Length; if (count > 1) { var s = NormalizeShiftCount(count, shift); if (s > 0) { RotateLeftInPlaceInternal(array, unchecked (count - s)); } } return; } ThrowHelper.ThrowArgumentNullException_array(); }
private static IntT NormalizeShiftCount(IntT count, IntT shift) { var s = shift; if (0 > s || count <= s) { unchecked { s %= count; } if (0 > s) { unchecked { s += count; } } } return(s); }
public static void RotateLeftInPlace_A <T>(this T[] array, IntT shift) { if (null != array) { var start = 0; var count = array.Length; if (count > 1) { var s = NormalizeShiftCount(count, shift); if (s > 0) { var t = unchecked (count - s); ReverseInternal(array); ReverseInternal(array, unchecked (start + t), s); ReverseInternal(array, start, t); } } return; } ThrowHelper.ThrowArgumentNullException_array(); }
internal static bool NextPermutationInternal <T, TLessThan>(this T[] array, IntT start, IntT count, TLessThan lessThan) where TLessThan : IO.IFunc <T, T, bool> { Contract.Requires(0 <= count); Contract.Requires(count > 1); var c = unchecked (start + count); var i = c; --i; for (; ;) { var j = i; --i; if (lessThan.Invoke(array[i], array[j])) { var k = c; for (; ;) { --k; { var p = array[i]; var q = array[k]; if (lessThan.Invoke(p, q)) { array[i] = q; array[k] = p; break; } } } Reverse(array, j, c - j); return(true); } if (i == start) { Reverse(array, start, count); return(false); } } }
public static bool NextPermutation <T, TLessThan>(this T[] array, IntT start, IntT count, TLessThan lessThan) where TLessThan : IO.IFunc <T, T, bool> { if (null != array) { if (count > 1) { if (unchecked (start + count) <= array.Length && 0 <= start) { return(NextPermutationInternal(array, start, count, lessThan)); } goto L_a; } if (CheckSegment(array, start, count)) { return(false); } L_a: // TODO: Perf throw new ArgumentException(); } ThrowHelper.ThrowArgumentNullException_array(); throw (ArgumentNullException)null; }
public static UIntT Decrease(UIntT value_lo, IntT value_hi, out IntT result_hi) { return(DecreaseSigned(value_lo, value_hi, out result_hi)); }
public static UIntT ShiftRight(UIntT low, IntT high, out IntT highResult) { return(ShiftRightSigned(low, high, out highResult)); }
public static SearchProcessingUnit_A_KnuthMorrisPratt <T, TListPattern, TEqualityComparer, TAllocator> Create(TListPattern pattern, IntT start, IntT count, TEqualityComparer equalityComparer, TAllocator allocator) { if (count > 0) { var(skip_table, skip_table_start) = allocator.Invoke(count); var i = (IntT)0; var j = -1; skip_table[skip_table_start] = j; while (count - 1 > i) { while (j > -1 && !equalityComparer.Invoke(pattern[start + i], pattern[start + j])) { j = skip_table[skip_table_start + j]; } ++i; ++j; if (equalityComparer.Invoke(pattern[start + i], pattern[start + j])) { skip_table[skip_table_start + i] = skip_table[skip_table_start + j]; } else { skip_table[skip_table_start + i] = j; } } return(new SearchProcessingUnit_A_KnuthMorrisPratt <T, TListPattern, TEqualityComparer, TAllocator>(skip_table, skip_table_start, pattern, start, count, equalityComparer)); } { return(new SearchProcessingUnit_A_KnuthMorrisPratt <T, TListPattern, TEqualityComparer, TAllocator>(Array_Empty <IntT> .Value, 0, pattern, start, count, equalityComparer)); } }
public static void SortIntrospective <T, TComparer, TValue>(T[] keys, TComparer comparer, IntT start, IntT count, TValue[] values) where TComparer : IComparer <T> { Contract.Requires(null != keys); Contract.Requires(0 <= start); Contract.Requires(0 <= count); Contract.Requires(count <= keys.Length); Contract.Requires(count + start <= keys.Length); Contract.Requires(null == values || count + start <= values.Length); if (count < 2) { return; } SortPartIntrospectiveRecursionLimited(keys, comparer, start, unchecked (count + start - 1), unchecked (2 * SortUtilities.FloorLog2(keys.Length)), values); }
public static void SortQuickRecursionLimited <T, TComparer, TValue>(T[] keys, TComparer comparer, IntT first, IntT last, int recLimit, TValue[] values) where TComparer : IComparer <T> { do { if (recLimit == 0) { SortHeap(keys, comparer, first, last, values); return; } var i = first; var j = last; var middle = unchecked (i + ((j - i) >> 1)); SwapIfGreater(keys, comparer, i, middle, values); SwapIfGreater(keys, comparer, i, j, values); SwapIfGreater(keys, comparer, middle, j, values); var x = keys[middle]; do { while (comparer.Compare(x, keys[i]) > 0) { ++i; } while (comparer.Compare(x, keys[j]) < 0) { --j; } Contract.Assert(first <= i && j <= last); if (i > j) { break; } if (i < j) { T key = keys[i]; keys[i] = keys[j]; keys[j] = key; if (values != null) { TValue value = values[i]; values[i] = values[j]; values[j] = value; } } unchecked { ++i; --j; } } while (i <= j); unchecked { --recLimit; } if (j - first <= unchecked (last - i)) { if (first < j) { SortQuickRecursionLimited(keys, comparer, first, j, recLimit, values); } first = i; } else { if (i < last) { SortQuickRecursionLimited(keys, comparer, i, last, recLimit, values); } last = j; } } while (first < last); }
public static void PartialSortPartQuick <T, TComparer, TValue>(T[] keys, TComparer comparer, IntT first, IntT last, int k, TValue[] values) where TComparer : IComparer <T> { if (last > first) { var pivot = PickPivotAndPartition(keys, comparer, first, last, values); if (pivot == k - 1) { return; } else if (pivot > k - 1) { PartialSortPartQuick(keys, comparer, first, pivot, k, values); } else { PartialSortPartQuick(keys, comparer, unchecked (pivot + 1), last, k, values); } } }
public static void PartialSortQuick <T, TComparer, TValue>(T[] keys, TComparer comparer, IntT start, IntT count, int k, TValue[] values) where TComparer : IComparer <T> { PartialSortPartQuick(keys, comparer, start, start + count - 1, k, values); }
public static void Sort <T, TComparer, TValue>(T[] keys, TComparer comparer, IntT index, IntT length, TValue[] values) where TComparer : IComparer <T> { Contract.Assert(null != keys); Contract.Assert(0 <= index && 0 <= length && (length <= keys.Length - index)); SortIntrospective(keys, comparer, index, length, values); }