/// <summary>
        /// Copies the contents of this builder to the specified array.
        /// </summary>
        /// <param name="position">The position in this builder to start copying from.</param>
        /// <param name="array">The destination array.</param>
        /// <param name="arrayIndex">The index in <see cref="array"/> to start copying to.</param>
        /// <param name="count">The number of items to copy.</param>
        /// <returns>The position in this builder that was copied up to.</returns>
        public CopyPosition CopyTo(CopyPosition position, T[] array, int arrayIndex, int count)
        {
            Debug.Assert(array != null);
            Debug.Assert(arrayIndex >= 0);
            Debug.Assert(count > 0 && count <= Count);
            Debug.Assert(array.Length - arrayIndex >= count);

            // Go through each buffer, which contains one 'row' of items.
            // The index in each buffer is referred to as the 'column'.

            /*
             * Visual representation:
             *
             *       C0   C1   C2 ..  C31 ..   C63
             * R0:  [0]  [1]  [2] .. [31]
             * R1: [32] [33] [34] .. [63]
             * R2: [64] [65] [66] .. [95] .. [127]
             */

            int row    = position.Row;
            int column = position.Column;

            T[] buffer = GetBuffer(row);
            int copied = CopyToCore(buffer, column);

            if (count == 0)
            {
                return(new CopyPosition(row, column + copied).Normalize(buffer.Length));
            }

            do
            {
                buffer = GetBuffer(++row);
                copied = CopyToCore(buffer, 0);
            } while(count > 0);

            return(new CopyPosition(row, copied).Normalize(buffer.Length));

            int CopyToCore(T[] sourceBuffer, int sourceIndex)
            {
                Debug.Assert(sourceBuffer.Length > sourceIndex);

                // Copy until we satisfy `count` or reach the end of the current buffer.
                int copyCount = Math.Min(sourceBuffer.Length - sourceIndex, count);

                Array.Copy(sourceBuffer, sourceIndex, array, arrayIndex, copyCount);

                arrayIndex += copyCount;
                count      -= copyCount;

                return(copyCount);
            }
        }
Example #2
0
        /// <summary>
        /// Copies the contents of this builder to the specified array.
        /// </summary>
        /// <param name="position">The position in this builder to start copying from.</param>
        /// <param name="array">The destination array.</param>
        /// <param name="arrayIndex">The index in <see cref="array"/> to start copying to.</param>
        /// <param name="count">The number of items to copy.</param>
        /// <returns>The position in this builder that was copied up to.</returns>
        public CopyPosition CopyTo(CopyPosition position, T[] array, int arrayIndex, int count)
        {
            Debug.Assert(array != null);
            Debug.Assert(arrayIndex >= 0);
            Debug.Assert(count > 0 && count <= Count);
            Debug.Assert(array.Length - arrayIndex >= count);

            // Go through each buffer, which contains one 'row' of items.
            // The index in each buffer is referred to as the 'column'.

            /*
             * Visual representation:
             *
             *       C0   C1   C2 ..  C31 ..   C63
             * R0:  [0]  [1]  [2] .. [31]
             * R1: [32] [33] [34] .. [63]
             * R2: [64] [65] [66] .. [95] .. [127]
             */

            int row    = position.Row;
            int column = position.Column;

            T[] buffer = GetBuffer(row);
            int copied =
#if __MonoCS__
                CopyToCore(buffer, column, array, ref arrayIndex, ref count);
#else
                CopyToCore(buffer, column);
#endif

            if (count == 0)
            {
                return(new CopyPosition(row, column + copied).Normalize(buffer.Length));
            }

            do
            {
                buffer = GetBuffer(++row);
                copied =
#if __MonoCS__
                    CopyToCore(buffer, 0, array, ref arrayIndex, ref count);
#else
                    CopyToCore(buffer, 0);
#endif
            } while (count > 0);

            return(new CopyPosition(row, copied).Normalize(buffer.Length));

#if __MonoCS__
        }
Example #3
0
        /// <summary>
        /// Copies the contents of this builder to the specified array.
        /// </summary>
        /// <param name="position">The position in this builder to start copying from.</param>
        /// <param name="array">The destination array.</param>
        /// <param name="arrayIndex">The index in <see cref="array"/> to start copying to.</param>
        /// <param name="count">The number of items to copy.</param>
        /// <returns>The position in this builder that was copied up to.</returns>
        public CopyPosition CopyTo(CopyPosition position, T[] array, int arrayIndex, int count)
        {
            Debug.Assert(arrayIndex >= 0);
            Debug.Assert(count >= 0 && count <= Count);
            Debug.Assert(array?.Length - arrayIndex >= count);

            // Go through each buffer, which contains one 'row' of items.
            // The index in each buffer is referred to as the 'column'.

            /*
             * Visual representation:
             *
             *       C0   C1   C2 ..  C31 ..   C63
             * R0:  [0]  [1]  [2] .. [31]
             * R1: [32] [33] [34] .. [63]
             * R2: [64] [65] [66] .. [95] .. [127]
             */

            int row    = position.Row;
            int column = position.Column;

            for (; count > 0; row++, column = 0)
            {
                T[] buffer = GetBuffer(index: row);

                // During this iteration, copy until we satisfy `count` or reach the
                // end of the current buffer.
                int copyCount = Math.Min(buffer.Length, count);

                if (copyCount > 0)
                {
                    Array.Copy(buffer, column, array, arrayIndex, copyCount);

                    arrayIndex += copyCount;
                    count      -= copyCount;
                    column     += copyCount;
                }
            }

            return(new CopyPosition(row: row, column: column));
        }
 public CopyPosition CopyTo(CopyPosition position, T[] array, int arrayIndex, int count)
 {
     Array.Copy(_builder.Buffer !, position.Column, array, arrayIndex, count);
     return(new CopyPosition(0, position.Column + count));
 }