/// <summary> /// 数组排序 /// </summary> /// <typeparam name="valueType">数据类型</typeparam> /// <param name="values">待排序数组</param> /// <param name="getKey">排序键值获取器</param> /// <param name="startIndex">起始位置</param> /// <param name="count">排序数据数量</param> /// <param name="fixedIndex">索引位置</param> /// <returns>排序后的数组</returns> private static valueType[] getSort/*Compare[0]*//*Compare[0]*/ <valueType>(valueType[] values, Func <valueType, /*Type[0]*/ ulong /*Type[0]*/> getKey, int startIndex, int count, /*Type[1]*/ ULongSortIndex /*Type[1]*/ *fixedIndex) { /*Type[1]*/ ULongSortIndex /*Type[1]*/.Create(fixedIndex, values, getKey, startIndex, count); sort/*Compare[0]*//*Compare[0]*/ (fixedIndex, fixedIndex + count - 1); return /*Type[1]*/ (ULongSortIndex /*Type[1]*/.Create(fixedIndex, values, count)); }
/// <summary> /// 数组排序 /// </summary> /// <typeparam name="valueType">数据类型</typeparam> /// <param name="values">待排序数组</param> /// <param name="getKey">排序键值获取器</param> /// <param name="fixedIndex">索引位置</param> /// <returns>排序后的数组</returns> private static valueType[] getSort/*Compare[0]*//*Compare[0]*/ <valueType>(valueType[] values, Func <valueType, /*Type[0]*/ ulong /*Type[0]*/> getKey, /*Type[1]*/ ULongSortIndex /*Type[1]*/ *fixedIndex) { /*Type[1]*/ ULongSortIndex /*Type[1]*/.Create(fixedIndex, values, getKey); sort/*Compare[0]*//*Compare[0]*/ (fixedIndex, fixedIndex + values.Length - 1); return /*Type[1]*/ (ULongSortIndex /*Type[1]*/.Create(fixedIndex, values, values.Length)); }
/// <summary> /// 数组排序 /// </summary> /// <typeparam name="valueType">数据类型</typeparam> /// <param name="values">待排序数组</param> /// <param name="getKey">排序键值获取器</param> /// <param name="startIndex">起始位置</param> /// <param name="count">排序数据数量</param> /// <param name="fixedIndex">索引位置</param> /// <returns>排序后的数组</returns> private static valueType[] getSortDesc <valueType>(valueType[] values, Func <valueType, ulong> getKey, int startIndex, int count, ULongSortIndex *fixedIndex) { ULongSortIndex.Create(fixedIndex, values, getKey, startIndex, count); sortDesc(fixedIndex, fixedIndex + count - 1); return(ULongSortIndex.Create(fixedIndex, values, count)); }
/// <summary> /// 数组排序 /// </summary> /// <typeparam name="valueType">数据类型</typeparam> /// <param name="values">待排序数组</param> /// <param name="getKey">排序键值获取器</param> /// <param name="fixedIndex">索引位置</param> /// <returns>排序后的数组</returns> private static valueType[] getSortDesc <valueType>(valueType[] values, Func <valueType, ulong> getKey, ULongSortIndex *fixedIndex) { ULongSortIndex.Create(fixedIndex, values, getKey); sortDesc(fixedIndex, fixedIndex + values.Length - 1); return(ULongSortIndex.Create(fixedIndex, values, values.Length)); }
/// <summary> /// 索引快速排序子过程 /// </summary> /// <param name="startIndex">起始位置</param> /// <param name="endIndex">结束位置-1</param> internal static void sortDesc(ULongSortIndex *startIndex, ULongSortIndex *endIndex) { do { ULongSortIndex leftValue = *startIndex, rightValue = *endIndex; int average = (int)(endIndex - startIndex) >> 1; if (average == 0) { if (leftValue.Value.CompareTo(rightValue.Value) < 0) { *startIndex = rightValue; *endIndex = leftValue; } break; } ULongSortIndex *leftIndex = startIndex, rightIndex = endIndex, averageIndex = startIndex + average; ULongSortIndex indexValue = *averageIndex; if (leftValue.Value.CompareTo(indexValue.Value) < 0) { if (leftValue.Value.CompareTo(rightValue.Value) < 0) { *rightIndex = leftValue; if (indexValue.Value.CompareTo(rightValue.Value) < 0) { *leftIndex = rightValue; } else { *leftIndex = indexValue; *averageIndex = indexValue = rightValue; } } else { *leftIndex = indexValue; *averageIndex = indexValue = leftValue; } } else { if (indexValue.Value.CompareTo(rightValue.Value) < 0) { *rightIndex = indexValue; if (leftValue.Value.CompareTo(rightValue.Value) < 0) { *leftIndex = rightValue; *averageIndex = indexValue = leftValue; } else { *averageIndex = indexValue = rightValue; } } } ++leftIndex; --rightIndex; ulong value = indexValue.Value; do { while ((*leftIndex).Value.CompareTo(value) > 0) { ++leftIndex; } while (value.CompareTo((*rightIndex).Value) > 0) { --rightIndex; } if (leftIndex < rightIndex) { leftValue = *leftIndex; *leftIndex = *rightIndex; *rightIndex = leftValue; } else { if (leftIndex == rightIndex) { ++leftIndex; --rightIndex; } break; } }while (++leftIndex <= --rightIndex); if (rightIndex - startIndex <= endIndex - leftIndex) { if (startIndex < rightIndex) { sortDesc(startIndex, rightIndex); } startIndex = leftIndex; } else { if (leftIndex < endIndex) { sortDesc(leftIndex, endIndex); } endIndex = rightIndex; } }while (startIndex < endIndex); }
/// <summary> /// 索引快速排序子过程 /// </summary> /// <param name="startIndex">起始位置</param> /// <param name="endIndex">结束位置-1</param> internal static void sort/*Compare[0]*//*Compare[0]*/ (/*Type[1]*/ ULongSortIndex /*Type[1]*/ *startIndex, /*Type[1]*/ ULongSortIndex /*Type[1]*/ *endIndex) { do { /*Type[1]*/ ULongSortIndex /*Type[1]*/ leftValue = *startIndex, rightValue = *endIndex; int average = (int)(endIndex - startIndex) >> 1; if (average == 0) { if (leftValue.Value /*Compare[1]*/ > /*Compare[1]*/ rightValue.Value) { *startIndex = rightValue; *endIndex = leftValue; } break; } /*Type[1]*/ ULongSortIndex /*Type[1]*/ *leftIndex = startIndex, rightIndex = endIndex, averageIndex = startIndex + average; /*Type[1]*/ ULongSortIndex /*Type[1]*/ indexValue = *averageIndex; if (leftValue.Value /*Compare[1]*/ > /*Compare[1]*/ indexValue.Value) { if (leftValue.Value /*Compare[1]*/ > /*Compare[1]*/ rightValue.Value) { *rightIndex = leftValue; if (indexValue.Value /*Compare[1]*/ > /*Compare[1]*/ rightValue.Value) { *leftIndex = rightValue; } else { *leftIndex = indexValue; *averageIndex = indexValue = rightValue; } } else { *leftIndex = indexValue; *averageIndex = indexValue = leftValue; } } else { if (indexValue.Value /*Compare[1]*/ > /*Compare[1]*/ rightValue.Value) { *rightIndex = indexValue; if (leftValue.Value /*Compare[1]*/ > /*Compare[1]*/ rightValue.Value) { *leftIndex = rightValue; *averageIndex = indexValue = leftValue; } else { *averageIndex = indexValue = rightValue; } } } ++leftIndex; --rightIndex; /*Type[0]*/ ulong /*Type[0]*/ value = indexValue.Value; do { while ((*leftIndex).Value /*Compare[2]*/ < /*Compare[2]*/ value) { ++leftIndex; } while (value /*Compare[2]*/ < /*Compare[2]*/ (*rightIndex).Value) { --rightIndex; } if (leftIndex < rightIndex) { leftValue = *leftIndex; *leftIndex = *rightIndex; *rightIndex = leftValue; } else { if (leftIndex == rightIndex) { ++leftIndex; --rightIndex; } break; } }while (++leftIndex <= --rightIndex); if (rightIndex - startIndex <= endIndex - leftIndex) { if (startIndex < rightIndex) { sort/*Compare[0]*//*Compare[0]*/ (startIndex, rightIndex); } startIndex = leftIndex; } else { if (leftIndex < endIndex) { sort/*Compare[0]*//*Compare[0]*/ (leftIndex, endIndex); } endIndex = rightIndex; } }while (startIndex < endIndex); }
/// <summary> /// 范围排序 /// </summary> /// <param name="startIndex">起始指针</param> /// <param name="endIndex">结束指针-1</param> public void Sort(/*Type[2]*/ ULongSortIndex /*Type[2]*/ *startIndex, /*Type[2]*/ ULongSortIndex /*Type[2]*/ *endIndex) { do { /*Type[2]*/ ULongSortIndex /*Type[2]*/ leftValue = *startIndex, rightValue = *endIndex; int average = (int)(endIndex - startIndex) >> 1; if (average == 0) { if (leftValue.Value /*Compare[1]*/ > /*Compare[1]*/ rightValue.Value) { *startIndex = rightValue; *endIndex = leftValue; } break; } /*Type[2]*/ ULongSortIndex /*Type[2]*/ *averageIndex = startIndex + average, leftIndex = startIndex, rightIndex = endIndex; /*Type[2]*/ ULongSortIndex /*Type[2]*/ indexValue = *averageIndex; if (leftValue.Value /*Compare[1]*/ > /*Compare[1]*/ indexValue.Value) { if (leftValue.Value /*Compare[1]*/ > /*Compare[1]*/ rightValue.Value) { *rightIndex = leftValue; if (indexValue.Value /*Compare[1]*/ > /*Compare[1]*/ rightValue.Value) { *leftIndex = rightValue; } else { *leftIndex = indexValue; *averageIndex = indexValue = rightValue; } } else { *leftIndex = indexValue; *averageIndex = indexValue = leftValue; } } else { if (indexValue.Value /*Compare[1]*/ > /*Compare[1]*/ rightValue.Value) { *rightIndex = indexValue; if (leftValue.Value /*Compare[1]*/ > /*Compare[1]*/ rightValue.Value) { *leftIndex = rightValue; *averageIndex = indexValue = leftValue; } else { *averageIndex = indexValue = rightValue; } } } ++leftIndex; --rightIndex; /*Type[0]*/ ulong /*Type[0]*/ value = indexValue.Value; do { while ((*leftIndex).Value /*Compare[2]*/ < /*Compare[2]*/ value) { ++leftIndex; } while (value /*Compare[2]*/ < /*Compare[2]*/ (*rightIndex).Value) { --rightIndex; } if (leftIndex < rightIndex) { leftValue = *leftIndex; *leftIndex = *rightIndex; *rightIndex = leftValue; } else { if (leftIndex == rightIndex) { ++leftIndex; --rightIndex; } break; } }while (++leftIndex <= --rightIndex); if (rightIndex - startIndex <= endIndex - leftIndex) { if (startIndex < rightIndex && rightIndex >= SkipCount) { Sort(startIndex, rightIndex); } if (leftIndex > GetEndIndex) { break; } startIndex = leftIndex; } else { if (leftIndex < endIndex && leftIndex <= GetEndIndex) { Sort(leftIndex, endIndex); } if (rightIndex < SkipCount) { break; } endIndex = rightIndex; } }while (startIndex < endIndex); }
/// <summary> /// 范围排序 /// </summary> /// <param name="startIndex">起始指针</param> /// <param name="endIndex">结束指针-1</param> public void Sort(ULongSortIndex *startIndex, ULongSortIndex *endIndex) { do { ULongSortIndex leftValue = *startIndex, rightValue = *endIndex; int average = (int)(endIndex - startIndex) >> 1; if (average == 0) { if (leftValue.Value.CompareTo(rightValue.Value) < 0) { *startIndex = rightValue; *endIndex = leftValue; } break; } ULongSortIndex *averageIndex = startIndex + average, leftIndex = startIndex, rightIndex = endIndex; ULongSortIndex indexValue = *averageIndex; if (leftValue.Value.CompareTo(indexValue.Value) < 0) { if (leftValue.Value.CompareTo(rightValue.Value) < 0) { *rightIndex = leftValue; if (indexValue.Value.CompareTo(rightValue.Value) < 0) { *leftIndex = rightValue; } else { *leftIndex = indexValue; *averageIndex = indexValue = rightValue; } } else { *leftIndex = indexValue; *averageIndex = indexValue = leftValue; } } else { if (indexValue.Value.CompareTo(rightValue.Value) < 0) { *rightIndex = indexValue; if (leftValue.Value.CompareTo(rightValue.Value) < 0) { *leftIndex = rightValue; *averageIndex = indexValue = leftValue; } else { *averageIndex = indexValue = rightValue; } } } ++leftIndex; --rightIndex; ulong value = indexValue.Value; do { while ((*leftIndex).Value.CompareTo(value) > 0) { ++leftIndex; } while (value.CompareTo((*rightIndex).Value) > 0) { --rightIndex; } if (leftIndex < rightIndex) { leftValue = *leftIndex; *leftIndex = *rightIndex; *rightIndex = leftValue; } else { if (leftIndex == rightIndex) { ++leftIndex; --rightIndex; } break; } }while (++leftIndex <= --rightIndex); if (rightIndex - startIndex <= endIndex - leftIndex) { if (startIndex < rightIndex && rightIndex >= SkipCount) { Sort(startIndex, rightIndex); } if (leftIndex > GetEndIndex) { break; } startIndex = leftIndex; } else { if (leftIndex < endIndex && leftIndex <= GetEndIndex) { Sort(leftIndex, endIndex); } if (rightIndex < SkipCount) { break; } endIndex = rightIndex; } }while (startIndex < endIndex); }