示例#1
0
        public override void Swap(long fromIndex, long toIndex)
        {
            int  bytesLeft   = itemSize;
            long fromAddress = Address(fromIndex, 0);
            long toAddress   = Address(toIndex, 0);

            // piece-wise swap, as large chunks as possible: long, int, short and finally byte-wise swap
            while (bytesLeft > 0)
            {
                int chunkSize;
                if (bytesLeft >= Long.BYTES)
                {
                    chunkSize = Long.BYTES;
                    long intermediary = GetLong(fromAddress);
                    UnsafeUtil.copyMemory(toAddress, fromAddress, chunkSize);
                    PutLong(toAddress, intermediary);
                }
                else if (bytesLeft >= Integer.BYTES)
                {
                    chunkSize = Integer.BYTES;
                    int intermediary = GetInt(fromAddress);
                    UnsafeUtil.copyMemory(toAddress, fromAddress, chunkSize);
                    PutInt(toAddress, intermediary);
                }
                else if (bytesLeft >= Short.BYTES)
                {
                    chunkSize = Short.BYTES;
                    short intermediary = GetShort(fromAddress);
                    UnsafeUtil.copyMemory(toAddress, fromAddress, chunkSize);
                    PutShort(toAddress, intermediary);
                }
                else
                {
                    chunkSize = Byte.BYTES;
                    sbyte intermediary = GetByte(fromAddress);
                    UnsafeUtil.copyMemory(toAddress, fromAddress, chunkSize);
                    PutByte(toAddress, intermediary);
                }
                fromAddress += chunkSize;
                toAddress   += chunkSize;
                bytesLeft   -= chunkSize;
            }
        }
示例#2
0
        public override void Clear()
        {
            if (IsByteUniform(_defaultValue))
            {
                UnsafeUtil.setMemory(Address, LengthConflict * itemSize, _defaultValue[0]);
            }
            else
            {
                long intermediary = UnsafeUtil.allocateMemory(itemSize, AllocationTracker);
                for (int i = 0; i < _defaultValue.Length; i++)
                {
                    UnsafeUtil.putByte(intermediary + i, _defaultValue[i]);
                }

                for (long i = 0, adr = Address; i < LengthConflict; i++, adr += itemSize)
                {
                    UnsafeUtil.copyMemory(intermediary, adr, itemSize);
                }
                UnsafeUtil.free(intermediary, itemSize, AllocationTracker);
            }
        }