Пример #1
0
        public static uint NextUInt(this Random rnd)
        {
            Span <byte> bytes = stackalloc byte[4];

            rnd.NextBytes(bytes);
            return(MemoryMarshal.AsRef <uint>(bytes));
        }
Пример #2
0
        private SomeData transformBytesToData(byte[] arg)
        {
            if (!this.StopWatch.IsRunning)
            {
                this.StopWatch.Restart();
            }

            if (arg.Length != this.dataSize)
            {
                return(new SomeData(1));
            }

            var      dataSpan    = new Span <byte>(arg);
            SomeData data        = new SomeData();
            var      globalSlice = dataSpan.Slice(this.sizeOfSeperator, this.sizeOfGlobal);
            var      gdata       = MemoryMarshal.AsRef <Global>(globalSlice);

            data.Global = gdata;

            var contentSilce = dataSpan.Slice(this.sizeOfGlobal + this.sizeOfSeperator);

            data.Content = new AmpTof[this.numberOfItems];

            for (var i = 0; i < this.numberOfItems; i++)
            {
                var amp_silice = contentSilce.Slice(i * sizeOfAmp, sizeOfAmp);
                var ampdata    = MemoryMarshal.AsRef <AmpTof>(amp_silice);
                data.Content[i] = ampdata;
            }

            return(data);
        }
        /// <summary>
        /// 读取训练用的数据
        /// </summary>
        unsafe static void ReadTrainData()
        {
            var filePath = Path.Combine(Utils.DefaultDataFolder, "TrainData/bilibili.tags");

            var stream = File.OpenRead(filePath);

            byte[] intBuffer = new byte[4];

            stream.Read(intBuffer);
            var picNum = BitConverter.ToInt32(intBuffer);

            stream.Read(intBuffer);
            var width = BitConverter.ToInt32(intBuffer);

            stream.Read(intBuffer);
            var height = BitConverter.ToInt32(intBuffer);

            stream.Read(intBuffer);
            var channels = BitConverter.ToInt32(intBuffer);

            var buffer = new byte[width * height * channels];
            var v3     = MemoryMarshal.AsRef <Vec3b>(buffer);

            for (var i = 0; i < picNum; i++)
            {
                stream.Read(buffer);
                var mat = new Mat(height, width, MatType.CV_8UC3, buffer);

                // mat.SaveImage($"r:/t/{i}.png"); 输出图片demo
            }

            // 图片标签数量:int
            // 图片标签数组:图片数量*图片标签数量(多标签的处理,1表示带有对应的tag)
            // 图片标签名称:utf8字符串剩下的直接,出字符串后,用逗号分隔

            stream.Read(intBuffer);
            var tagNum = BitConverter.ToInt32(intBuffer);

            List <byte[]> imageTags = new List <byte[]>();

            for (var i = 0; i < picNum; i++)
            {
                var tagbuffer = new byte[tagNum];
                stream.Read(tagbuffer);
                imageTags.Add(tagbuffer);
            }

            var lessLen   = stream.Length - stream.Position;
            var strBuffer = new byte[lessLen];

            stream.Read(strBuffer);
            string names = Encoding.UTF8.GetString(strBuffer);

            Console.WriteLine(names);
        }
Пример #4
0
        /// <summary>
        ///     Encrypts the input in place, its length has to be dividable by 8.
        /// </summary>
        /// <param name="input">The buffer to be encrypted in place.</param>
        /// <exception cref="ArgumentOutOfRangeException">input length is not dividable by 8.</exception>
        public void Encrypt(Span <byte> input)
        {
            if (input.Length % 8 != 0 || input.Length == 0)
            {
                throw new ArgumentOutOfRangeException(nameof(input.Length));
            }

            for (var i = 0; i < input.Length; i += 8)
            {
                ref var left  = ref MemoryMarshal.AsRef <uint>(input.Slice(i));
                ref var right = ref MemoryMarshal.AsRef <uint>(input.Slice(i + 4));
Пример #5
0
        /// <summary>
        /// Calculates a Guid hash value based on the consistent identity a string.
        /// </summary>
        /// <param name="text">The string to hash.</param>
        /// <returns>An integer hash for the string.</returns>
        internal static Guid CalculateGuidHash(string text)
        {
            var input = BitConverter.IsLittleEndian ? MemoryMarshal.AsBytes(text.AsSpan()) : Encoding.Unicode.GetBytes(text);

            Span <byte> result = stackalloc byte[256 / 8];
            var         sha    = SHA256.Create();

            sha.TryComputeHash(input, result, out _);
            sha.Dispose();

            MemoryMarshal.AsRef <long>(result)          ^= MemoryMarshal.Read <long>(result.Slice(16));
            MemoryMarshal.AsRef <long>(result.Slice(8)) ^= MemoryMarshal.Read <long>(result.Slice(24));
            return(BitConverter.IsLittleEndian ? MemoryMarshal.Read <Guid>(result) : new Guid(result.Slice(0, 16)));
        }
Пример #6
0
        public static BinaryDateTime FromSpan(ReadOnlySpan <byte> span)
        {
            if (span == null)
            {
                throw new ArgumentException(nameof(span));
            }

            if (span.Length != Marshal.SizeOf(typeof(BinaryDateTime)))
            {
                throw new BinaryDateTimeLengthException("The size of span not equal to the size of Request struct.");
            }

            return(MemoryMarshal.AsRef <BinaryDateTime>(span));
        }
        /// <summary>
        /// Reads data from the stream and write to the given value.
        /// </summary>
        /// <typeparam name="T">Type of the value.</typeparam>
        /// <param name="stream">Stream to read from.</param>
        /// <param name="value">Outout value to write into.</param>
        public static unsafe void Read <T>(this Stream stream, out T value) where T : unmanaged
        {
            if (stream == null)
            {
                throw new ArgumentNullException("stream");
            }
            var valueBytes = new byte[sizeof(T)];

            if (stream.Read(valueBytes, 0, valueBytes.Length) < valueBytes.Length)
            {
                throw new ArgumentException("Not enough data to build the value");
            }
            value = MemoryMarshal.AsRef <T>(valueBytes);
        }
Пример #8
0
        static int ParseVarintUInt32_MoveMask_Impl(ReadOnlySpan <byte> span, int offset, out uint value)
        {
            var stopbits = ~(uint)Sse2.MoveMask(MemoryMarshal.AsRef <Vector128 <byte> >(span.Slice(offset)));

            switch (BitOperations.TrailingZeroCount(stopbits))
            {
            case 1:
                value = (uint)(
                    (span[offset++] & 0x7F) |
                    (span[offset] << 7)
                    );
                return(2);

            case 2:
                value = (uint)(
                    (span[offset++] & 0x7F) |
                    ((span[offset++] & 0x7F) << 7) |
                    (span[offset] << 14)
                    );
                return(3);

            case 3:
                value = (uint)(
                    (span[offset++] & 0x7F) |
                    ((span[offset++] & 0x7F) << 7) |
                    ((span[offset++] & 0x7F) << 14) |
                    (span[offset] << 21)
                    );
                return(4);

            case 4:
                value = (uint)(
                    (span[offset++] & 0x7F) |
                    ((span[offset++] & 0x7F) << 7) |
                    ((span[offset++] & 0x7F) << 14) |
                    ((span[offset++] & 0x7F) << 21) |
                    (span[offset] << 28)
                    );
                return(5);

            default:
                ThrowOverflow();
                value = default;
                return(-1);
            }
        }
Пример #9
0
        private async Task ReadNotificationsAsync(CancellationToken cancellationToken)
        {
            Debug.Assert(Marshal.SizeOf <ChildExitNotification>() == ChildExitNotification.Size);
            int carriedOverBytes = 0;
            var buf = new byte[256];

            while (!cancellationToken.IsCancellationRequested)
            {
                int bytes = carriedOverBytes + await _mainChannel.ReadAsync(buf.AsMemory(carriedOverBytes), cancellationToken).ConfigureAwait(false);

                int elementCount = bytes / ChildExitNotification.Size;
                for (int i = 0; i < elementCount; i++)
                {
                    ProcessNotification(ref MemoryMarshal.AsRef <ChildExitNotification>(buf.AsSpan(i * ChildExitNotification.Size, ChildExitNotification.Size)));
                }

                carriedOverBytes = bytes % ChildExitNotification.Size;
                if (carriedOverBytes != 0)
                {
                    Array.Copy(buf, bytes - carriedOverBytes, buf, 0, carriedOverBytes);
                }
            }
        }
Пример #10
0
        /// <summary>
        /// Calculates a Guid hash value based on the consistent identity a string.
        /// </summary>
        /// <param name="text">The string to hash.</param>
        /// <returns>An integer hash for the string.</returns>
        internal static Guid CalculateGuidHash(string text)
        {
#if NETCOREAPP
            var input = BitConverter.IsLittleEndian ? MemoryMarshal.AsBytes(text.AsSpan()) : Encoding.Unicode.GetBytes(text);

            Span <byte> result = stackalloc byte[256 / 8];
            var         sha    = SHA256.Create();
            sha.TryComputeHash(input, result, out _);
            sha.Dispose();

            MemoryMarshal.AsRef <long>(result)          ^= MemoryMarshal.Read <long>(result.Slice(16));
            MemoryMarshal.AsRef <long>(result.Slice(8)) ^= MemoryMarshal.Read <long>(result.Slice(24));
            return(BitConverter.IsLittleEndian ? MemoryMarshal.Read <Guid>(result) : new Guid(result.Slice(0, 16)));
#else
            using var sha = SHA256.Create();
            var result = sha.ComputeHash(Encoding.Unicode.GetBytes(text));
            var hash   = new byte[16];
            for (int i = 0; i < result.Length; i++)
            {
                hash[i & 15] ^= result[i];
            }
            return(new Guid(hash));
#endif
        }
Пример #11
0
 public static ref readonly T TakeSingle <S, T>(ReadOnlySpan <S> src, int offset = 0, int?length = null)
     where S : struct
     where T : struct
 => ref MemoryMarshal.AsRef <T>(src.AsBytes(offset, length));
Пример #12
0
Файл: SpanEx.cs Проект: sgf/mkcp
 public static ref T Read <T>(this Span <byte> buff, int offset) where T : struct => ref MemoryMarshal.AsRef <T>(buff.Slice(offset));
Пример #13
0
Файл: SpanEx.cs Проект: sgf/mkcp
 public static ref T Read <T>(this Span <byte> buff) where T : struct => ref MemoryMarshal.AsRef <T>(buff);
Пример #14
0
 /// <summary>
 /// Creates a readonly reference to a <typeparamref name="TStruct"/> created using the bytes
 /// starting at the provided <paramref name="position"/>.  The endianess of the various data
 /// types defined within the <typeparamref name="TStruct"/> is determiend based on the endianess
 /// of this machine.
 /// </summary>
 /// <typeparam name="TStruct">The type of struct to create; this should be
 /// a readonly struct.</typeparam>
 /// <param name="span"></param>
 /// <param name="position">The byte position to create the <typeparamref name="TStruct"/> from.</param>
 /// <returns></returns>
 /// <seealso cref="BitConverter.IsLittleEndian"/>
 public static ref readonly TStruct CreateStruct <TStruct>(this ReadOnlySpan <byte> span, int position)
     where TStruct : struct =>
 ref MemoryMarshal.AsRef <TStruct>(span.Slice(position, Marshal.SizeOf <TStruct>()));
Пример #15
0
 public ref BoundPrimitive AsRawData() => ref MemoryMarshal.AsRef <BoundPrimitive>(MemoryMarshal.AsBytes(MemoryMarshal.CreateSpan(ref this, 1)));
Пример #16
0
 public static BitBlock <T> FromSpan(Span <byte> src)
 => new BitBlock <T>(MemoryMarshal.AsRef <T>(src));
Пример #17
0
 public static BitBlock32 FromSpan(Span <byte> src)
 => MemoryMarshal.AsRef <BitBlock32>(src);