Пример #1
0
        /// <summary>
        /// 数组范围排序
        /// </summary>
        /// <typeparam name="valueType">数据类型</typeparam>
        /// <param name="array">待排序数组</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">排序范围数据数量</param>
        /// <param name="getKey">排序键值获取器</param>
        /// <param name="skipCount">跳过数据数量</param>
        /// <param name="getCount">排序数据数量</param>
        /// <param name="fixedIndex">索引位置</param>
        /// <returns>排序后的数组</returns>
        private static valueType[] getRangeSort/*Compare[0]*//*Compare[0]*/ <valueType>(valueType[] array, int startIndex, int count, Func <valueType, /*Type[0]*/ ulong /*Type[0]*/> getKey, int skipCount, int getCount, /*Type[2]*/ ULongSortIndex /*Type[2]*/ *fixedIndex)
        {
            /*Type[2]*/
            ULongSortIndex /*Type[2]*/ *writeIndex = fixedIndex;

            for (int index = startIndex, endIndex = startIndex + count; index != endIndex; (*writeIndex++).Set(getKey(array[index]), index++))
            {
                ;
            }
            new /*Type[3]*/ ULongRangeIndexSorter/*Type[3]*//*Compare[0]*//*Compare[0]*/
            {
                SkipCount   = fixedIndex + skipCount,
                GetEndIndex = fixedIndex + skipCount + getCount - 1
            }.Sort(fixedIndex, fixedIndex + count - 1);
            valueType[] newValues = new valueType[getCount];
            writeIndex = fixedIndex + skipCount;
            for (int index = 0; index != newValues.Length; ++index)
            {
                newValues[index] = array[(*writeIndex++).Index];
            }
            return(newValues);
        }
Пример #2
0
 /// <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);
 }
Пример #3
0
        /// <summary>
        /// 排序去除Top N
        /// </summary>
        /// <typeparam name="valueType">数据类型</typeparam>
        /// <param name="values">待排序数组</param>
        /// <param name="getKey">排序键值获取器</param>
        /// <param name="count">排序数据数量</param>
        /// <param name="newValues">目标数据数组</param>
        /// <param name="length">排序缓存区尺寸</param>
        /// <param name="removeFixed">索引位置</param>
        private static void removeTopDesc <valueType>(valueType[] values, Func <valueType, ulong> getKey, int count, valueType[] newValues, int length, ULongSortIndex *removeFixed)
        {
            int             index = 0, writeIndex = 0;
            ULongSortIndex *removeEnd = removeFixed;

            while (index != length)
            {
                (*removeEnd++).Set(getKey(values[index]), index++);
            }
            ULongSortIndex *removeStart = removeFixed + (count = length - count), removeIndex = removeFixed;

            FixedArrayQuickRangeSort.ULongRangeIndexSorterDesc sort = new FixedArrayQuickRangeSort.ULongRangeIndexSorterDesc {
                SkipCount = removeStart, GetEndIndex = removeStart
            };
            sort.Sort(removeFixed, --removeEnd);
            while (writeIndex != count)
            {
                newValues[writeIndex++] = values[(*removeIndex++).Index];
            }
            for (ulong maxValue = (*removeStart).Value; index != values.Length; ++index)
            {
                ulong value = getKey(values[index]);
                if (value.CompareTo(maxValue) >= 0)
                {
                    newValues[writeIndex++] = values[index];
                }
                else
                {
                    (*--removeIndex).Set(value, index);
                    if (removeIndex == removeFixed)
                    {
                        sort.Sort(removeFixed, removeEnd);
                        for (removeIndex = removeFixed; removeIndex != removeStart; newValues[writeIndex++] = values[(*removeIndex++).Index])
                        {
                            ;
                        }
                        maxValue = (*removeStart).Value;
                    }
                }
            }
            if (removeIndex != removeStart)
            {
                sort.Sort(removeIndex, removeEnd);
                while (removeIndex != removeStart)
                {
                    newValues[writeIndex++] = values[(*removeIndex++).Index];
                }
            }
        }
Пример #4
0
        /// <summary>
        /// 排序取Top N
        /// </summary>
        /// <typeparam name="valueType">数据类型</typeparam>
        /// <param name="values">待排序数组</param>
        /// <param name="getKey">排序键值获取器</param>
        /// <param name="count">排序数据数量</param>
        /// <param name="length">排序缓存区尺寸</param>
        /// <param name="indexFixed">索引位置</param>
        /// <returns>排序后的数据</returns>
        private static valueType[] getTopDesc <valueType>(valueType[] values, Func <valueType, ulong> getKey, int count, int length, ULongSortIndex *indexFixed)
        {
            ULongSortIndex *writeEnd = indexFixed;
            int             index    = 0;

            while (index != length)
            {
                (*writeEnd++).Set(getKey(values[index]), index++);
            }
            ULongSortIndex *writeStat = indexFixed + count, writeIndex = writeStat;

            FixedArrayQuickRangeSort.ULongRangeIndexSorterDesc sort = new FixedArrayQuickRangeSort.ULongRangeIndexSorterDesc {
                SkipCount = writeStat - 1, GetEndIndex = writeStat - 1
            };
            sort.Sort(indexFixed, --writeEnd);
            for (ulong maxValue = (*sort.SkipCount).Value; index != values.Length; ++index)
            {
                ulong value = getKey(values[index]);
                if (value.CompareTo(maxValue) > 0)
                {
                    (*writeIndex).Set(value, index);
                    if (writeIndex == writeEnd)
                    {
                        sort.Sort(indexFixed, writeEnd);
                        writeIndex = writeStat;
                        maxValue   = (*sort.SkipCount).Value;
                    }
                    else
                    {
                        ++writeIndex;
                    }
                }
            }
            if (writeIndex != writeStat)
            {
                sort.Sort(indexFixed, --writeIndex);
            }
            valueType[] newValues = new valueType[count];
            for (writeIndex = indexFixed, index = 0; index != count; ++index)
            {
                newValues[index] = values[(*writeIndex++).Index];
            }
            return(newValues);
        }
Пример #5
0
        /// <summary>
        /// 排序去除Top N
        /// </summary>
        /// <typeparam name="valueType">数据类型</typeparam>
        /// <param name="values">待排序数组</param>
        /// <param name="getKey">排序键值获取器</param>
        /// <param name="count">排序数据数量</param>
        /// <param name="newValues">目标数据数组</param>
        /// <param name="length">排序缓存区尺寸</param>
        /// <param name="removeFixed">索引位置</param>
        private static void removeTop/*Compare[0]*//*Compare[0]*/ <valueType>(valueType[] values, Func <valueType, /*Type[0]*/ ulong /*Type[0]*/> getKey, int count, valueType[] newValues, int length, /*Type[2]*/ ULongSortIndex /*Type[2]*/ *removeFixed)
        {
            int index = 0, writeIndex = 0;
            /*Type[2]*/
            ULongSortIndex /*Type[2]*/ *removeEnd = removeFixed;

            while (index != length)
            {
                (*removeEnd++).Set(getKey(values[index]), index++);
            }
            /*Type[2]*/
            ULongSortIndex /*Type[2]*/ *removeStart = removeFixed + (count = length - count), removeIndex = removeFixed;

            FixedArrayQuickRangeSort./*Type[3]*/ ULongRangeIndexSorter /*Type[3]*//*Compare[0]*//*Compare[0]*/ sort = new FixedArrayQuickRangeSort./*Type[3]*/ ULongRangeIndexSorter /*Type[3]*//*Compare[0]*//*Compare[0]*/ {
                SkipCount = removeStart, GetEndIndex = removeStart
            };
            sort.Sort(removeFixed, --removeEnd);
            while (writeIndex != count)
            {
                newValues[writeIndex++] = values[(*removeIndex++).Index];
            }
            for (/*Type[0]*/ ulong /*Type[0]*/ maxValue = (*removeStart).Value; index != values.Length; ++index)
            {
                /*Type[0]*/
                ulong /*Type[0]*/ value = getKey(values[index]);
                if (value /*Compare[2]*/ <= /*Compare[2]*/ maxValue)
                {
                    newValues[writeIndex++] = values[index];
                }
                else
                {
                    (*--removeIndex).Set(value, index);
                    if (removeIndex == removeFixed)
                    {
                        sort.Sort(removeFixed, removeEnd);
                        for (removeIndex = removeFixed; removeIndex != removeStart; newValues[writeIndex++] = values[(*removeIndex++).Index])
                        {
                            ;
                        }
                        maxValue = (*removeStart).Value;
                    }
                }
            }
            if (removeIndex != removeStart)
            {
                sort.Sort(removeIndex, removeEnd);
                while (removeIndex != removeStart)
                {
                    newValues[writeIndex++] = values[(*removeIndex++).Index];
                }
            }
        }
Пример #6
0
        /// <summary>
        /// 排序取Top N
        /// </summary>
        /// <typeparam name="valueType">数据类型</typeparam>
        /// <param name="values">待排序数组</param>
        /// <param name="getKey">排序键值获取器</param>
        /// <param name="count">排序数据数量</param>
        /// <param name="length">排序缓存区尺寸</param>
        /// <param name="indexFixed">索引位置</param>
        /// <returns>排序后的数据</returns>
        private static valueType[] getTop/*Compare[0]*//*Compare[0]*/ <valueType>(valueType[] values, Func <valueType, /*Type[0]*/ ulong /*Type[0]*/> getKey, int count, int length, /*Type[2]*/ ULongSortIndex /*Type[2]*/ *indexFixed)
        {
            /*Type[2]*/
            ULongSortIndex /*Type[2]*/ *writeEnd = indexFixed;
            int index = 0;

            while (index != length)
            {
                (*writeEnd++).Set(getKey(values[index]), index++);
            }
            /*Type[2]*/
            ULongSortIndex /*Type[2]*/ *writeStat = indexFixed + count, writeIndex = writeStat;

            FixedArrayQuickRangeSort./*Type[3]*/ ULongRangeIndexSorter /*Type[3]*//*Compare[0]*//*Compare[0]*/ sort = new FixedArrayQuickRangeSort./*Type[3]*/ ULongRangeIndexSorter /*Type[3]*//*Compare[0]*//*Compare[0]*/ {
                SkipCount = writeStat - 1, GetEndIndex = writeStat - 1
            };
            sort.Sort(indexFixed, --writeEnd);
            for (/*Type[0]*/ ulong /*Type[0]*/ maxValue = (*sort.SkipCount).Value; index != values.Length; ++index)
            {
                /*Type[0]*/
                ulong /*Type[0]*/ value = getKey(values[index]);
                if (value /*Compare[1]*/ < /*Compare[1]*/ maxValue)
                {
                    (*writeIndex).Set(value, index);
                    if (writeIndex == writeEnd)
                    {
                        sort.Sort(indexFixed, writeEnd);
                        writeIndex = writeStat;
                        maxValue   = (*sort.SkipCount).Value;
                    }
                    else
                    {
                        ++writeIndex;
                    }
                }
            }
            if (writeIndex != writeStat)
            {
                sort.Sort(indexFixed, --writeIndex);
            }
            valueType[] newValues = new valueType[count];
            for (writeIndex = indexFixed, index = 0; index != count; ++index)
            {
                newValues[index] = values[(*writeIndex++).Index];
            }
            return(newValues);
        }
Пример #7
0
 /// <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));
 }
Пример #8
0
        /// <summary>
        /// 数组范围排序
        /// </summary>
        /// <typeparam name="valueType">数据类型</typeparam>
        /// <param name="array">待排序数组</param>
        /// <param name="startIndex">起始位置</param>
        /// <param name="count">排序范围数据数量</param>
        /// <param name="getKey">排序键值获取器</param>
        /// <param name="skipCount">跳过数据数量</param>
        /// <param name="getCount">排序数据数量</param>
        /// <param name="fixedIndex">索引位置</param>
        /// <returns>排序后的数组</returns>
        private static valueType[] getRangeSortDesc <valueType>(valueType[] array, int startIndex, int count, Func <valueType, ulong> getKey, int skipCount, int getCount, ULongSortIndex *fixedIndex)
        {
            ULongSortIndex *writeIndex = fixedIndex;

            for (int index = startIndex, endIndex = startIndex + count; index != endIndex; (*writeIndex++).Set(getKey(array[index]), index++))
            {
                ;
            }
            new ULongRangeIndexSorterDesc
            {
                SkipCount   = fixedIndex + skipCount,
                GetEndIndex = fixedIndex + skipCount + getCount - 1
            }.Sort(fixedIndex, fixedIndex + count - 1);
            valueType[] newValues = new valueType[getCount];
            writeIndex = fixedIndex + skipCount;
            for (int index = 0; index != newValues.Length; ++index)
            {
                newValues[index] = array[(*writeIndex++).Index];
            }
            return(newValues);
        }
Пример #9
0
 /// <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);
 }
Пример #10
0
        /// <summary>
        /// 数组范围排序
        /// </summary>
        /// <typeparam name="valueType">数据类型</typeparam>
        /// <typeparam name="returnType">返回数据类型</typeparam>
        /// <param name="array">待排序数组</param>
        /// <param name="skipCount">跳过数据数量</param>
        /// <param name="getCount">排序数据数量</param>
        /// <param name="getValue">获取返回数据</param>
        /// <param name="fixedIndex">索引位置</param>
        /// <returns>排序后的数组</returns>
        private static returnType[] getRangeSort/*Compare[0]*//*Compare[0]*/ <valueType, returnType>(valueType[] array, int skipCount, int getCount, Func <valueType, returnType> getValue, /*Type[2]*/ ULongSortIndex /*Type[2]*/ *fixedIndex)
        {
            new /*Type[3]*/ ULongRangeIndexSorter/*Type[3]*//*Compare[0]*//*Compare[0]*/
            {
                SkipCount   = fixedIndex + skipCount,
                GetEndIndex = fixedIndex + skipCount + getCount - 1
            }.Sort(fixedIndex, fixedIndex + array.Length - 1);
            returnType[] newValues = new returnType[getCount];
            /*Type[2]*/
            ULongSortIndex /*Type[2]*/ *writeIndex = fixedIndex + skipCount;

            for (int index = 0; index != newValues.Length; ++index)
            {
                newValues[index] = getValue(array[(*writeIndex++).Index]);
            }
            return(newValues);
        }
Пример #11
0
        private static returnType[] getRangeSort/*Compare[0]*//*Compare[0]*/ <valueType, returnType>(valueType[] array, Func <valueType, /*Type[0]*/ ulong /*Type[0]*/> getKey, int skipCount, int getCount, Func <valueType, returnType> getValue, /*Type[2]*/ ULongSortIndex /*Type[2]*/ *fixedIndex)
        {
            /*Type[2]*/
            ULongSortIndex /*Type[2]*/ *writeIndex = fixedIndex;

            for (int index = 0; index != array.Length; (*writeIndex++).Set(getKey(array[index]), index++))
            {
                ;
            }
            return(getRangeSort/*Compare[0]*//*Compare[0]*/ (array, skipCount, getCount, getValue, fixedIndex));
        }
Пример #12
0
 /// <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);
 }
Пример #13
0
        /// <summary>
        /// 数组范围排序
        /// </summary>
        /// <typeparam name="valueType">数据类型</typeparam>
        /// <typeparam name="returnType">返回数据类型</typeparam>
        /// <param name="array">待排序数组</param>
        /// <param name="skipCount">跳过数据数量</param>
        /// <param name="getCount">排序数据数量</param>
        /// <param name="getValue">获取返回数据</param>
        /// <param name="fixedIndex">索引位置</param>
        /// <returns>排序后的数组</returns>
        private static returnType[] getRangeSortDesc <valueType, returnType>(valueType[] array, int skipCount, int getCount, Func <valueType, returnType> getValue, ULongSortIndex *fixedIndex)
        {
            new ULongRangeIndexSorterDesc
            {
                SkipCount   = fixedIndex + skipCount,
                GetEndIndex = fixedIndex + skipCount + getCount - 1
            }.Sort(fixedIndex, fixedIndex + array.Length - 1);
            returnType[]    newValues  = new returnType[getCount];
            ULongSortIndex *writeIndex = fixedIndex + skipCount;

            for (int index = 0; index != newValues.Length; ++index)
            {
                newValues[index] = getValue(array[(*writeIndex++).Index]);
            }
            return(newValues);
        }
Пример #14
0
        private static returnType[] getRangeSortDesc <valueType, returnType>(valueType[] array, Func <valueType, ulong> getKey, int skipCount, int getCount, Func <valueType, returnType> getValue, ULongSortIndex *fixedIndex)
        {
            ULongSortIndex *writeIndex = fixedIndex;

            for (int index = 0; index != array.Length; (*writeIndex++).Set(getKey(array[index]), index++))
            {
                ;
            }
            return(getRangeSortDesc(array, skipCount, getCount, getValue, fixedIndex));
        }
Пример #15
0
 /// <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));
 }
Пример #16
0
 /// <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));
 }
Пример #17
0
 /// <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));
 }
Пример #18
0
 /// <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);
 }