/// <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>())
            {
                PtrUtils.CopyBlock(src.Object, src.Offset, dest.Object, dest.Offset,
                                   src.Length * PtrUtils.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 = PtrUtils.Get <T>(src.Object, src.Offset, (UIntPtr)i);
                    PtrUtils.Set(dest.Object, dest.Offset, (UIntPtr)i, value);
                }
            }

            return(true);
        }
Exemple #2
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);
                }
            }
        }
 /// <summary>
 /// Determines whether two spans are equal (byte-wise) by comparing the elements by using memcmp
 /// </summary>
 /// <param name="first">A span of integers to compare to second.</param>
 /// <param name="second">A span of integers T to compare to first.</param>
 public static bool SequenceEqual(this Span <int> first, Span <int> second)
 {
     return(first.Length >= 256
         ? MemoryUtils.MemCmp(first, second)
         : SequenceEqual <int>(first, second));
 }
 /// <summary>
 /// Determines whether two read-only spans are equal (byte-wise) by comparing the elements by using memcmp
 /// </summary>
 /// <param name="first">A span of shorts to compare to second.</param>
 /// <param name="second">A span of shorts T to compare to first.</param>
 public static bool SequenceEqual(this ReadOnlySpan <short> first, ReadOnlySpan <short> second)
 {
     return(first.Length >= 512
         ? MemoryUtils.MemCmp(first, second)
         : SequenceEqual <short>(first, second));
 }
 /// <summary>
 /// Determines whether two spans are equal (byte-wise) by comparing the elements by using memcmp
 /// </summary>
 /// <param name="first">A span of characters to compare to second.</param>
 /// <param name="second">A span of characters T to compare to first.</param>
 public static bool SequenceEqual(this Span <char> first, Span <char> second)
 {
     return(first.Length >= 512
         ? MemoryUtils.MemCmp(first, second)
         : SequenceEqual <char>(first, second));
 }
 /// <summary>
 /// Determines whether two read-only spans are equal (byte-wise) by comparing the elements by using memcmp
 /// </summary>
 /// <param name="first">A span of bytes to compare to second.</param>
 /// <param name="second">A span of bytes T to compare to first.</param>
 public static bool SequenceEqual(this ReadOnlySpan <byte> first, ReadOnlySpan <byte> second)
 {
     return(first.Length >= 320
         ? MemoryUtils.MemCmp(first, second)
         : SequenceEqual <byte>(first, second));
 }
 /// <summary>
 /// Determines whether two read-only spans are equal (byte-wise) by comparing the elements by using memcmp
 /// </summary>
 /// <param name="first">A span of long integers to compare to second.</param>
 /// <param name="second">A span of long integers T to compare to first.</param>
 public static bool SequenceEqual(this ReadOnlySpan <long> first, ReadOnlySpan <long> second)
 {
     return(first.Length >= 256
         ? MemoryUtils.MemCmp(first, second)
         : SequenceEqual <long>(first, second));
 }