Ejemplo n.º 1
0
        public static TPixel GetReferenceBGRP32 <TPixel>(this Pixel.BGRP32 pixel)
            where TPixel : unmanaged
        {
            if (typeof(TPixel) == typeof(Pixel.BGR24))
            {
                var pix = pixel.GetReferenceBGR24();
                return(MEMUNSAFE.As <Pixel.BGR24, TPixel>(ref pix));
            }

            if (typeof(TPixel) == typeof(Pixel.RGB24))
            {
                var pix = pixel.GetReferenceRGB24();
                return(MEMUNSAFE.As <Pixel.RGB24, TPixel>(ref pix));
            }

            if (typeof(TPixel) == typeof(Pixel.BGRA32))
            {
                var pix = pixel.GetReferenceBGRA32();
                return(MEMUNSAFE.As <Pixel.BGRA32, TPixel>(ref pix));
            }

            if (typeof(TPixel) == typeof(Pixel.RGBA32))
            {
                var pix = pixel.GetReferenceRGBA32();
                return(MEMUNSAFE.As <Pixel.RGBA32, TPixel>(ref pix));
            }

            if (typeof(TPixel) == typeof(Pixel.ARGB32))
            {
                var pix = pixel.GetReferenceARGB32();
                return(MEMUNSAFE.As <Pixel.ARGB32, TPixel>(ref pix));
            }

            throw new NotImplementedException(typeof(TPixel).Name);
        }
            //
            // Returns the cached result of a name-agnostic query on the Type's members, as if you'd passed in
            //
            //  BindingFlags == Public | NonPublic | Instance | Static | FlattenHierarchy  (immediateTypeOnly == false)
            //                  Public | NonPublic | Instance | Static | DeclaredOnly      (immediateTypeOnly == true)
            //
            public QueriedMemberList <M> GetQueriedMembers <M>(bool immediateTypeOnly) where M : MemberInfo
            {
                int    index  = MemberPolicies <M> .MemberTypeIndex;
                object result = Volatile.Read(ref _nameAgnosticQueryCaches[index]);

                if (result == null)
                {
                    QueriedMemberList <M> newResult = QueriedMemberList <M> .Create(_type, filter : null, ignoreCase : false, immediateTypeOnly : immediateTypeOnly);

                    newResult.Compact();
                    Volatile.Write(ref _nameAgnosticQueryCaches[index], newResult);
                    return(newResult);
                }
                Debug.Assert(result is QueriedMemberList <M>);
                QueriedMemberList <M> list = Unsafe.As <QueriedMemberList <M> >(result);

                if (list.ImmediateTypeOnly && !immediateTypeOnly)
                {
                    QueriedMemberList <M> newResult = QueriedMemberList <M> .Create(_type, filter : null, ignoreCase : false, immediateTypeOnly : false);

                    newResult.Compact();
                    Volatile.Write(ref _nameAgnosticQueryCaches[index], newResult);
                    return(newResult);
                }
                return(list);
            }
Ejemplo n.º 3
0
        public override string?ReadLine()
        {
            if (charPos == charLen && ReadBuffer() == 0)
            {
                return(null);
            }

            // this variable is needed to save temporary the length of characters that are candidates for line termination string
            var newLineBufferPosition = 0;
            var newLine = Environment.NewLine.AsSpan();

            var result = new BufferWriterSlim <char>(stackalloc char[MemoryRental <char> .StackallocThreshold], allocator);

            try
            {
                do
                {
                    ref var first = ref BufferHelpers.GetReference(in buffer);

                    do
                    {
                        Debug.Assert(charPos >= 0 && charPos < buffer.Length);
                        var ch = Unsafe.Add(ref first, charPos);

                        if (ch == newLine[newLineBufferPosition])
                        {
                            // skip character which is a part of line termination string
                            if (newLineBufferPosition == newLine.Length - 1)
                            {
                                charPos += 1;
                                goto exit;
                            }

                            newLineBufferPosition += 1;
                            continue;
                        }

                        if ((uint)newLineBufferPosition > 0U)
                        {
                            result.Write(newLine.Slice(0, newLineBufferPosition));
                        }

                        result.Add(ch);
                        newLineBufferPosition = 0;
                    }while (++charPos < charLen);
                }while (ReadBuffer() > 0);

                // add trailing characters recognized as a part of uncompleted line termination
                if ((uint)newLineBufferPosition > 0U)
                {
                    result.Write(newLine.Slice(0, newLineBufferPosition));
                }

exit:
                return((uint)result.WrittenCount > 0U ? new string(result.WrittenSpan) : string.Empty);
            }
Ejemplo n.º 4
0
            /// <exception cref="InvalidOperationException">When this NDArray is a slice.</exception>
            public ref byte GetPinnableReference()
            {
                if (_this.Shape.IsSliced || _this.Shape.IsBroadcasted)
                    throw new InvalidOperationException("Can't pin reference when NDArray is sliced or broadcasted.");

                unsafe
                {
                    return ref CompilerUnsafe.AsRef<byte>(Address);
                }
            }
Ejemplo n.º 5
0
            //
            // Returns the cached result of a name-specific query on the Type's members, as if you'd passed in
            //
            //  BindingFlags == Public | NonPublic | Instance | Static | FlattenHierarchy
            //
            public QueriedMemberList <M> GetQueriedMembers <M>(string name, bool ignoreCase) where M : MemberInfo
            {
                int    index = MemberPolicies <M> .MemberTypeIndex;
                object obj   = ignoreCase ? _perNameQueryCaches_CaseInsensitive[index] : _perNameQueryCaches_CaseSensitive[index];

                Debug.Assert(obj is PerNameQueryCache <M>);
                PerNameQueryCache <M> unifier = Unsafe.As <PerNameQueryCache <M> >(obj);
                QueriedMemberList <M> result  = unifier.GetOrAdd(name);

                return(result);
            }
Ejemplo n.º 6
0
        public static Pixel.BGRP32 GetReferenceBGRP32 <TPixel>(this TPixel pixel)
            where TPixel : unmanaged
        {
            if (typeof(TPixel) == typeof(Pixel.BGRP32))
            {
                return(MEMUNSAFE.As <TPixel, Pixel.BGRP32>(ref pixel));
            }

            if (typeof(TPixel) == typeof(Pixel.RGBP32))
            {
                ref var pix = ref MEMUNSAFE.As <TPixel, Pixel.RGBP32>(ref pixel);
                return(new Pixel.BGRP32(pix));
            }
        internal static int ParseLogEntryPrologue(ReadOnlySpan <byte> input, out long length, out long term, out DateTimeOffset timeStamp, out bool isSnapshot)
        {
            length = ReadInt64LittleEndian(input);
            input  = input.Slice(sizeof(long));

            term  = ReadInt64LittleEndian(input);
            input = input.Slice(sizeof(long));

            timeStamp = Span.Read <DateTimeOffset>(ref input);

            isSnapshot = ValueTypeExtensions.ToBoolean(input[0]);

            return(sizeof(long) + sizeof(long) + Unsafe.SizeOf <DateTimeOffset>() + sizeof(byte));
        }
            //
            // Returns the cached result of a name-specific query on the Type's members, as if you'd passed in
            //
            //  BindingFlags == Public | NonPublic | Instance | Static | FlattenHierarchy  (immediateTypeOnly == false)
            //                  Public | NonPublic | Instance | Static | DeclaredOnly      (immediateTypeOnly == true)
            //
            public QueriedMemberList <M> GetQueriedMembers <M>(string name, bool ignoreCase, bool immediateTypeOnly) where M : MemberInfo
            {
                int index = MemberPolicies <M> .MemberTypeIndex;

                object[] cacheArray = ignoreCase ?
                                      (immediateTypeOnly ? _perNameQueryCaches_CaseInsensitive_ImmediateTypeOnly : _perNameQueryCaches_CaseInsensitive) :
                                      (immediateTypeOnly ? _perNameQueryCaches_CaseSensitive_ImmediateTypeOnly : _perNameQueryCaches_CaseSensitive);

                object unifierAsObject = cacheArray[index];

                Debug.Assert(unifierAsObject is PerNameQueryCache <M>);
                PerNameQueryCache <M> unifier = Unsafe.As <PerNameQueryCache <M> >(unifierAsObject);
                QueriedMemberList <M> result  = unifier.GetOrAdd(name);

                return(result);
            }
Ejemplo n.º 9
0
            //
            // Returns the cached result of a name-agnostic query on the Type's members, as if you'd passed in
            //
            //  BindingFlags == Public | NonPublic | Instance | Static | FlattenHierarchy
            //
            public QueriedMemberList <M> GetQueriedMembers <M>() where M : MemberInfo
            {
                int    index  = MemberPolicies <M> .MemberTypeIndex;
                object result = Volatile.Read(ref _nameAgnosticQueryCaches[index]);

                if (result == null)
                {
                    QueriedMemberList <M> newResult = QueriedMemberList <M> .Create(_type, optionalNameFilter : null, ignoreCase : false);

                    newResult.Compact();
                    result = newResult;
                    Volatile.Write(ref _nameAgnosticQueryCaches[index], result);
                }

                Debug.Assert(result is QueriedMemberList <M>);
                return(Unsafe.As <QueriedMemberList <M> >(result));
            }
Ejemplo n.º 10
0
 /// <exception cref="InvalidOperationException">When this NDArray is a slice.</exception>
 public unsafe ref T GetPin<T>()
 {
     if (_this.Shape.IsSliced || _this.Shape.IsBroadcasted) throw new InvalidOperationException("Can't pin reference when NDArray is sliced or broadcasted.");
     return ref CompilerUnsafe.AsRef<T>(_this.Address);
 }
Ejemplo n.º 11
0
 /// <summary>
 ///     Reinterpret_cast without strings marshaling
 /// </summary>
 /// <typeparam name="T">
 ///     Type of struct to cast
 /// </typeparam>
 /// <param name="ptr">
 ///     Raw pointer to use
 /// </param>
 /// <returns>
 ///     A value of type <typeparamref name="T"/>
 /// </returns>
 public static unsafe T PtrToStructureUnsafe <T>(IntPtr ptr)
 {
     return(Unsafe.Read <T>(ptr.ToPointer()));
 }
Ejemplo n.º 12
0
 static Task FlushAsync(TWriter writer, CancellationToken token) => Unsafe.As <IFlushable>(writer).FlushAsync(token);
Ejemplo n.º 13
0
 static void Flush(TWriter writer) => Unsafe.As <IFlushable>(writer).Flush();