コード例 #1
0
        /// <summary>
        /// Copies data from the <paramref name="source"/> pointer to the <paramref name="destination"/> array.
        /// </summary>
        /// <typeparam name="T">The array element type. Must be a <see cref="Extensions.IsBlittable">blittable</see> struct.</typeparam>
        /// <param name="source">The pointer to the source data. Must not be <see cref="IntPtr.Zero"/>.</param>
        /// <param name="destination">The destination array to which the data will be copied. This method will copy
        /// <see cref="ArraySlice{T}.Length"/> elements' worth of data, starting at the <see cref="ArraySlice{T}.Offset"/>.</param>
        /// <param name="arrayElementSizeBytes">The size of <typeparamref name="T"/>. Use either sizeof() (preferred), or
        /// <see cref="SizeOf{T}"/> if the type is not known at compile-time.</param>
        public static unsafe void CopyGenericArray <T>(IntPtr source, ArraySlice <T> destination, uint arrayElementSizeBytes) where T : struct
        {
            Assure.False(source == IntPtr.Zero);
            Assure.True(typeof(T).IsBlittable());
            Assure.Equal((int)arrayElementSizeBytes, SizeOf <T>());
            GCHandle pinnedArrayHandle = GCHandle.Alloc(destination.ContainingArray, GCHandleType.Pinned);

            try {
                MemCopy(source,
                        pinnedArrayHandle.AddrOfPinnedObject() + (int)(arrayElementSizeBytes * destination.Offset), destination.Length * arrayElementSizeBytes);
            }
            finally {
                pinnedArrayHandle.Free();
            }
        }
コード例 #2
0
        public void TestConstructors()
        {
            // Define variables and constants
            int[] testArray = Enumerable.Range(0, 10).ToArray();

            // Set up context


            // Execute
            var fullSlice           = new ArraySlice <int>(testArray);
            var offset3Slice        = new ArraySlice <int>(testArray, 3);
            var offset3Length4Slice = new ArraySlice <int>(testArray, 3, 4);

#if !DEVELOPMENT && !RELEASE
            try {
                new ArraySlice <int>(null);
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
            try {
                new ArraySlice <int>(testArray, 40);
                Assert.Fail();
            }
            catch (OverflowException) { }
            try {
                new ArraySlice <int>(testArray, 2, 9);
                Assert.Fail();
            }
            catch (AssuranceFailedException) { }
#endif

            // Assert outcome
            Assert.IsTrue(fullSlice.IsFullArraySlice);
            Assert.IsFalse(offset3Slice.IsFullArraySlice);
            Assert.IsFalse(offset3Length4Slice.IsFullArraySlice);

            Assert.AreEqual((uint)testArray.Length, fullSlice.Length);
            Assert.AreEqual((uint)testArray.Length - 3, offset3Slice.Length);
            Assert.AreEqual((uint)4, offset3Length4Slice.Length);

            Assert.AreEqual(0U, fullSlice.Offset);
            Assert.AreEqual(3U, offset3Slice.Offset);
            Assert.AreEqual(3U, offset3Length4Slice.Offset);
        }
コード例 #3
0
 public ArraySliceEnumerator(ArraySlice <T> arrSlice)
 {
     this.arrSlice = arrSlice;
     curIndex      = arrSlice.Offset - 1U;
 }
コード例 #4
0
 /// <summary>
 /// Indicates whether the current object is equal to another object of the same type.
 /// </summary>
 /// <returns>
 /// true if the current object is equal to the <paramref name="other"/> parameter; otherwise, false.
 /// </returns>
 /// <param name="other">An object to compare with this object.</param>
 public bool Equals(ArraySlice <T> other)
 {
     return(Equals(ContainingArray, other.ContainingArray) && Offset == other.Offset && Length == other.Length);
 }