Ejemplo n.º 1
0
        /// <summary>
        /// 排序去除Top N
        /// </summary>
        /// <param name="values">待排序数组</param>
        /// <param name="count">排序数据数量</param>
        /// <returns>排序后的数据</returns>
        private static ulong[] getRemoveTopDesc(ulong[] values, int count)
        {
            ulong[] newValues = new ulong[count];
            count = values.Length - count;
            uint sqrtMod;
            int  length = Math.Min(Math.Max(count << 2, count + (int)AutoCSer.Extensions.NumberExtension.sqrt((uint)values.Length, out sqrtMod)), values.Length);

            ulong[] removeValues = new ulong[length];
            fixed(ulong *newValueFixed = newValues, removeFixed = removeValues, valueFixed = values)
            {
                int copyCount = length - count, copyLength = copyCount * sizeof(ulong);

                ulong *readIndex = valueFixed + values.Length - length, removeStart = removeFixed + copyCount;

                Buffer.BlockCopy(values, (int)((byte *)readIndex - (byte *)valueFixed), removeValues, 0, length * sizeof(ulong));
                //unsafer.memory.Copy(readIndex, removeFixed, length * sizeof(ulong));
                ulong *removeEnd = removeFixed + --length, removeIndex = removeStart, writeIndex = newValueFixed + copyCount;

                FixedArrayQuickRangeSort.ULongRangeSorterDesc sort = new FixedArrayQuickRangeSort.ULongRangeSorterDesc {
                    SkipCount = removeStart, GetEndIndex = removeStart
                };
                sort.Sort(removeFixed, removeEnd);
                Buffer.BlockCopy(removeValues, 0, newValues, 0, copyLength);
                //unsafer.memory.Copy(removeFixed, newValueFixed, copyLength);
                for (ulong maxValue = *removeStart; readIndex != valueFixed;)
                {
                    if ((*--readIndex).CompareTo(maxValue) >= 0)
                    {
                        *writeIndex++ = *readIndex;
                    }
                    else
                    {
                        *--removeIndex = *readIndex;
                        if (removeIndex == removeFixed)
                        {
                            sort.Sort(removeFixed, removeEnd);
                            removeIndex = removeStart;
                            maxValue    = *removeStart;
                            Buffer.BlockCopy(removeValues, 0, newValues, (int)((byte *)writeIndex - (byte *)newValueFixed), copyLength);
                            //unsafer.memory.Copy(removeFixed, writeIndex, copyLength);
                            writeIndex += copyCount;
                        }
                    }
                }
                if (removeIndex != removeStart)
                {
                    sort.Sort(removeIndex, removeEnd);
                    Buffer.BlockCopy(removeValues, (int)((byte *)removeIndex - (byte *)removeFixed), newValues, (int)((byte *)writeIndex - (byte *)newValueFixed), (int)((byte *)removeStart - (byte *)removeIndex));
                    //unsafer.memory.Copy(removeIndex, writeIndex, (int)(removeStart - removeIndex) * sizeof(ulong));
                }
            }

            return(newValues);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 排序取Top N
        /// </summary>
        /// <param name="values">待排序数组</param>
        /// <param name="count">排序数据数量</param>
        /// <returns>排序后的数据</returns>
        private static LeftArray <ulong> getTopDesc(ulong[] values, int count)
        {
            uint sqrtMod;
            int  length = Math.Min(Math.Max(count << 2, count + (int)AutoCSer.Extensions.NumberExtension.sqrt((uint)values.Length, out sqrtMod)), values.Length);

            ulong[] newValues = new ulong[length];
            fixed(ulong *newValueFixed = newValues, valueFixed = values)
            {
                ulong *readIndex = valueFixed + values.Length - length;

                Buffer.BlockCopy(values, (int)((byte *)readIndex - (byte *)valueFixed), newValues, 0, length * sizeof(ulong));
                //unsafer.memory.Copy(readIndex, newValueFixed, length * sizeof(ulong));
                ulong *writeStat = newValueFixed + count, writeEnd = newValueFixed + --length, writeIndex = writeStat;

                FixedArrayQuickRangeSort.ULongRangeSorterDesc sort = new FixedArrayQuickRangeSort.ULongRangeSorterDesc {
                    SkipCount = writeStat - 1, GetEndIndex = writeStat - 1
                };
                sort.Sort(newValueFixed, writeEnd);
                for (ulong maxValue = *sort.SkipCount; readIndex != valueFixed;)
                {
                    if ((*--readIndex).CompareTo(maxValue) > 0)
                    {
                        *writeIndex = *readIndex;
                        if (writeIndex == writeEnd)
                        {
                            sort.Sort(newValueFixed, writeEnd);
                            writeIndex = writeStat;
                            maxValue   = *sort.SkipCount;
                        }
                        else
                        {
                            ++writeIndex;
                        }
                    }
                }
                if (writeIndex != writeStat)
                {
                    sort.Sort(newValueFixed, --writeIndex);
                }
            }

            return(new LeftArray <ulong> {
                Array = newValues, Length = count
            });
        }