示例#1
0
        public static void Set(NumberSystem numberSystem)
        {
            var existing = _value;

            if (existing == null)
            {
                switch (numberSystem)
                {
                case NumberSystem.Unsigned:
                case NumberSystem.OnesComplement:
                case NumberSystem.TwosComplement:
                case NumberSystem.SignBit:
                    _value = numberSystem;
                    break;

                default:
                    throw new ArgumentOutOfRangeException(nameof(numberSystem));
                }
            }
            else if (numberSystem != existing.Value)
            {
                throw new InvalidOperationException($"The number-system for '{typeof(T).Name}' has already been set and cannot be changed");
            }
        }
示例#2
0
        private static void Sort32(Span<uint> keys, Span<uint> workspace, int r, uint keyMask, bool ascending, NumberSystem numberSystem)
        {
            if ((keyMask & SortUtils.MSB32U) == 0) numberSystem = NumberSystem.Unsigned;

            if (!ascending || numberSystem != NumberSystem.Unsigned) throw new NotImplementedException("Need to do that!");
            
            if (ascending ? SortUtils.ShortSortAscending(keys, 0, (uint)keys.Length) : SortUtils.ShortSortDescending(keys, 0, (uint)keys.Length))
            {
                r = SortUtils.ChooseBitCount<uint>(r, DefaultR);
                workspace = workspace.Slice(0, keys.Length);

                if (keyMask == 0) return;

                Span<uint> offsets = stackalloc uint[1 << r];
                int bucketCount = 1 << r, groups = ((32 - 1) / r) + 1;

                uint mask = (uint)(bucketCount - 1), groupMask;
                var shift = 32;
                do
                {
                    shift -= r;
                    groupMask = (keyMask >> shift) & mask;
                    keyMask &= ~(mask << shift);
                } while (groupMask == 0);

                // no need to check groupMask is non-zero - we already checked keyMask, so we definitely expect *something*
                Sort32(keys, workspace, offsets, keyMask, groupMask, mask, r, shift, ascending, 0, keys.Length);
            }
        }
示例#3
0
        private static void Sort32(uint *keys, uint *workspace, int len, int r, uint keyMask, bool ascending, NumberSystem numberSystem)
        {
            if (len <= 1 || keyMask == 0)
            {
                return;
            }
            r = SortUtils.ChooseBitCount <uint>(r, DefaultR);

            int   countLength   = 1 << r;
            int   groups        = GroupCount <uint>(r);
            uint *countsOffsets = stackalloc uint[countLength];
            uint  mask          = (uint)(countLength - 1);

            bool reversed = false;

            if (SortCore32(keys, workspace, r, keyMask, countLength, len, countsOffsets, groups, mask, ascending, numberSystem != NumberSystem.Unsigned))
            {
                Swap(ref keys, ref workspace, ref reversed);
            }

            if (reversed)
            {
                Unsafe.CopyBlock(workspace, keys, (uint)len << 2);
            }
        }