Exemplo n.º 1
0
        /// <summary>
        /// Copies the contents of this span into another.  The destination
        /// must be at least as big as the source, and may be bigger.
        /// </summary>
        /// <param name="destination">The span to copy items into.</param>
        public bool TryCopyTo(Span <T> destination)
        {
            // There are some benefits of making local copies. See https://github.com/dotnet/coreclr/issues/5556
            var dest = destination;
            var src  = this;

            if (src.Length > dest.Length)
            {
                return(false);
            }

            if (default(T) != null && MemoryUtils.IsPrimitiveValueType <T>())
            {
                UnsafeUtilities.CopyBlock(src.Object, src.Offset, dest.Object, dest.Offset,
                                          src.Length * UnsafeUtilities.SizeOf <T>());
            }
            else
            {
                for (int i = 0; i < src.Length; i++)
                {
                    // We don't check bounds here as we are surely within them
                    T value = UnsafeUtilities.Get <T>(src.Object, src.Offset, (UIntPtr)i);
                    UnsafeUtilities.Set(dest.Object, dest.Offset, (UIntPtr)i, value);
                }
            }

            return(true);
        }
Exemplo n.º 2
0
 /// <summary>
 /// Fetches the element at the specified index.
 /// </summary>
 /// <exception cref="System.ArgumentOutOfRangeException">
 /// Thrown when the specified index is not in range (&lt;0 or &gt;&eq;length).
 /// </exception>
 public T this[int index]
 {
     [MethodImpl(MethodImplOptions.AggressiveInlining)]
     get
     {
         Contract.RequiresInRange(index, (uint)Length);
         return(UnsafeUtilities.Get <T>(Object, Offset, (UIntPtr)index));
     }
     [MethodImpl(MethodImplOptions.AggressiveInlining)]
     set
     {
         Contract.RequiresInRange(index, (uint)Length);
         UnsafeUtilities.Set(Object, Offset, (UIntPtr)index, value);
     }
 }
Exemplo n.º 3
0
        /// <summary>
        /// Copies the contents of this span into another.  The destination
        /// must be at least as big as the source, and may be bigger.
        /// </summary>
        /// <param name="destination">The span to copy items into.</param>
        public void CopyTo(Span <T> destination)
        {
            // There are some benefits of making local copies. See https://github.com/dotnet/coreclr/issues/5556
            var dest = destination;
            var src  = this;

            Contract.Requires(src.Length <= dest.Length);

            if (default(T) != null && MemoryUtils.IsPrimitiveValueType <T>())
            {
                // review: (#848) - overflow and alignment
                UnsafeUtilities.CopyBlock(src.Object, src.Offset, dest.Object, dest.Offset,
                                          src.Length * Unsafe.SizeOf <T>());
            }
            else
            {
                for (int i = 0; i < src.Length; i++)
                {
                    // We don't check bounds here as we are surely within them
                    T value = UnsafeUtilities.Get <T>(src.Object, src.Offset, (UIntPtr)i);
                    UnsafeUtilities.Set(dest.Object, dest.Offset, (UIntPtr)i, value);
                }
            }
        }
Exemplo n.º 4
0
 public static T Read <[Primitive] T>(this ReadOnlySpan <byte> slice)
     where T : struct
 {
     Contract.RequiresInInclusiveRange(UnsafeUtilities.SizeOf <T>(), (uint)slice.Length);
     return(UnsafeUtilities.Get <T>(slice.Object, slice.Offset, (UIntPtr)0));
 }