예제 #1
0
        public override void Write(JsonWriter writer, T[,,] array)
        {
            var dim0end = array.GetUpperBound(0);

            var dim1start = array.GetLowerBound(1);
            var dim1end   = array.GetUpperBound(1);

            var dim2start = array.GetLowerBound(2);
            var dim2end   = array.GetUpperBound(2);

            writer.WriteStartArray();

            for (var i0 = 0; i0 <= dim0end; ++i0)
            {
                writer.WriteStartArray();

                for (var i1 = dim1start; i1 <= dim1end; ++i1)
                {
                    writer.WriteStartArray();

                    for (var i2 = dim2start; i2 <= dim2end; ++i2)
                    {
                        writer.WriteValue(array[i0, i1, i2]);
                    }

                    writer.WriteEndArray();
                }

                writer.WriteEndArray();
            }

            writer.WriteEndArray();
        }
예제 #2
0
        public static T[, ,] Resize <T>(this T[, ,] source, int newLengthA, int newLengthB, int newLengthC)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            int upperSA = source.GetUpperBound(0),
                upperSB = source.GetUpperBound(1),
                upperSC = source.GetUpperBound(2);

            int lowerSA = source.GetLowerBound(0),
                lowerSB = source.GetLowerBound(1),
                lowerSC = source.GetLowerBound(2);

            T[, ,] result = (T[, , ])(Array.CreateInstance(typeof(T), new int[] { newLengthA, newLengthB, newLengthC }, new int[] { lowerSA, lowerSB, lowerSC }));
            int lengthSA = upperSA + 1 - lowerSA,
                lengthSB = upperSB + 1 - lowerSB,
                lengthSC = upperSC + 1 - lowerSC;
            int copyMaxA = Math.Min(newLengthA, lengthSA),
                copyMaxB = Math.Min(newLengthB, lengthSB),
                copyMaxC = Math.Min(newLengthC, lengthSC);

            source.CubicCopy(result, copyMaxA, copyMaxB, copyMaxC);
            return(result);
        }
        /// <summary>
        /// Converts to jagged array.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="threeDimensionalArray">The three dimensional array.</param>
        /// <returns></returns>
        /// <acknowledgment>
        /// https://stackoverflow.com/a/25995025
        /// </acknowledgment>
        public static T[][][] ToJaggedArray <T>(this T[,,] threeDimensionalArray)
        {
            var rowsFirstIndex = threeDimensionalArray.GetLowerBound(0);
            var rowsLastIndex  = threeDimensionalArray.GetUpperBound(0);
            var numberOfRows   = rowsLastIndex + 1;

            var columnsFirstIndex = threeDimensionalArray.GetLowerBound(1);
            var columnsLastIndex  = threeDimensionalArray.GetUpperBound(1);
            var numberOfColumns   = columnsLastIndex + 1;

            var dFirstIndex = threeDimensionalArray.GetLowerBound(2);
            var dLastIndex  = threeDimensionalArray.GetUpperBound(2);
            var numberOfD   = dLastIndex + 1;

            var jaggedArray = new T[numberOfRows][][];

            for (var i = rowsFirstIndex; i <= rowsLastIndex; i++)
            {
                jaggedArray[i] = new T[numberOfColumns][];
                for (var j = columnsFirstIndex; j <= columnsLastIndex; j++)
                {
                    jaggedArray[i][j] = new T[numberOfD];
                    for (var k = dFirstIndex; k <= dLastIndex; k++)
                    {
                        jaggedArray[i][j][k] = threeDimensionalArray[i, j, k];
                    }
                }
            }

            return(jaggedArray);
        }
예제 #4
0
        public static T[][][] ToJaggedArray <T>([NotNull] this T[,,] array)
        {
            Check.NotNull(array);

            var firstMin = array.GetLowerBound(0);
            var firstMax = array.GetUpperBound(0);
            var rows     = firstMax - firstMin + 1;

            var secondMin = array.GetLowerBound(1);
            var secondMax = array.GetUpperBound(1);
            var cols      = secondMax - secondMin + 1;

            var thirdMin = array.GetLowerBound(2);
            var thirdMax = array.GetUpperBound(2);
            var depths   = thirdMax - thirdMin + 1;

            var result = new T[rows][][];

            for (var row = 0; row < rows; row++)
            {
                result[row] = new T[cols][];
                for (var col = 0; col < cols; col++)
                {
                    result[row][col] = new T[depths];
                    for (var depth = 0; depth < depths; depth++)
                    {
                        result[row][col][depth] = array[row + firstMin, col + secondMin, depth + thirdMin];
                    }
                }
            }

            return(result);
        }
예제 #5
0
    /// <summary>
    /// Gets the inclusive lower bounds of an array.
    /// </summary>
    /// <typeparam name="T">The type of values stored in the array.</typeparam>
    /// <param name="array">The array.</param>
    /// <returns>The inclusive upper bounds.</returns>
    public static Index3D GetLowerBounds <T>(this T[,,] array)
    {
        Contracts.Requires.That(array != null);

        return(new Index3D(
                   array.GetLowerBound((int)Axis3D.X),
                   array.GetLowerBound((int)Axis3D.Y),
                   array.GetLowerBound((int)Axis3D.Z)));
    }
예제 #6
0
 public static IEnumerable <T> SliceColumn <T>(this T[,,] array, int column)
 {
     for (var i = array.GetLowerBound(0); i <= array.GetUpperBound(0); i++)
     {
         for (var j = array.GetLowerBound(1); j <= array.GetUpperBound(1); j++)
         {
             yield return(array[i, j, column]);
         }
     }
 }
예제 #7
0
 public static IEnumerable <T> SliceRow <T>(this T[,,] array, int row)
 {
     for (var i = array.GetLowerBound(1); i <= array.GetUpperBound(1); i++)
     {
         for (var j = array.GetLowerBound(2); j <= array.GetUpperBound(2); j++)
         {
             yield return(array[row, i, j]);
         }
     }
 }
예제 #8
0
 public static bool OutOfBounds <T>(this T[,,] array, Vector3 coords)
 {
     if (coords.x >= array.GetLowerBound(0) && coords.x <= array.GetUpperBound(0) &&
         coords.y >= array.GetLowerBound(1) && coords.y <= array.GetUpperBound(1) &&
         coords.z >= array.GetLowerBound(2) && coords.z <= array.GetUpperBound(2))
     {
         return(false);
     }
     else
     {
         return(true);
     }
 }
예제 #9
0
 public static bool OutOfBounds <T>(this T[,,] array, Coord3D coords)
 {
     if (coords.X >= array.GetLowerBound(0) && coords.X <= array.GetUpperBound(0) &&
         coords.Y >= array.GetLowerBound(1) && coords.Y <= array.GetUpperBound(1) &&
         coords.Z >= array.GetLowerBound(2) && coords.Z <= array.GetUpperBound(2))
     {
         return(false);
     }
     else
     {
         return(true);
     }
 }
예제 #10
0
        /// <summary>
        /// Copies data from the <paramref name="source"/> three-dimensional array
        /// to the <paramref name="destination"/> three-dimensional array.
        /// </summary>
        /// <typeparam name="T">The kind of element used within the arrays
        /// copied.</typeparam>
        /// <param name="source">The three-dimensional array from which copying occurs.</param>
        /// <param name="destination">The three-dimensional array of <typeparamref name="T"/>
        /// elements in which the elements are received.</param>
        /// <param name="lengthA">The <see cref="Int32"/>
        /// value representing the size of the lowest order dimension
        /// of the copy operation.</param>
        /// <param name="lengthB">The <see cref="Int32"/>
        /// value representing the size of the middle order dimension
        /// of the copy operation.</param>
        /// <param name="lengthC">The <see cref="Int32"/>
        /// value representing the size of the highest order dimension
        /// of the copy operation.</param>
        public static void CubicCopy <T>(this T[, ,] source, T[, ,] destination, int lengthA, int lengthB, int lengthC)
        {
            if (source == null)
            {
                throw new ArgumentNullException("source");
            }
            int upperSA  = source.GetUpperBound(0),
                upperSB  = source.GetUpperBound(1),
                upperSC  = source.GetUpperBound(2);
            int upperDA  = destination.GetUpperBound(0),
                upperDB  = destination.GetUpperBound(1),
                upperDC  = destination.GetUpperBound(2);
            int lowerSA  = source.GetLowerBound(0),
                lowerSB  = source.GetLowerBound(1),
                lowerSC  = source.GetLowerBound(2);
            int lowerDA  = destination.GetLowerBound(0),
                lowerDB  = destination.GetLowerBound(1),
                lowerDC  = destination.GetLowerBound(2);
            int lengthSA = upperSA + 1 - lowerSA,
                lengthSB = upperSB + 1 - lowerSB,
                lengthSC = upperSC + 1 - lowerSC;
            int lengthDA = upperDA + 1 - lowerDA,
                lengthDB = upperDB + 1 - lowerDB,
                lengthDC = upperDC + 1 - lowerDC;

            if (lengthA > lengthSA || lengthA > lengthDA)
            {
                throw new ArgumentOutOfRangeException("lengthA");
            }
            if (lengthB > lengthSB || lengthB > lengthDB)
            {
                throw new ArgumentOutOfRangeException("lengthB");
            }
            if (lengthC > lengthSC || lengthC > lengthDC)
            {
                throw new ArgumentOutOfRangeException("lengthC");
            }
            lock (source)
            {
                Parallel.For(0, lengthA, outterIndex =>
                {
                    Parallel.For(0, lengthB, middleIndex =>
                    {
                        for (int innerIndex = 0; innerIndex < lengthC; innerIndex++)
                        {
                            destination[lowerDA + outterIndex, lowerDB + middleIndex, lowerDC + innerIndex] = source[lowerSA + outterIndex, lowerSB + middleIndex, lowerSC + innerIndex];
                        }
                    });
                });
            }
        }
예제 #11
0
        /// <summary> Fills the specified 3D array using passed function. </summary>
        /// <typeparam name="T">The type of the elements of the array to fill.</typeparam>
        /// <param name="array">The array to fill.</param>
        /// <param name="filling">The filling function: (x, y, z, old element) => new element.</param>
        public static void Fill <T>([NotNull] this T[,,] array, [NotNull] Func <int, int, int, T, T> filling)
        {
            Check.NotNull(array)
            .NotNull(filling);

            for (var x = array.GetLowerBound(0); x <= array.GetUpperBound(0); x++)
            {
                for (var y = array.GetLowerBound(1); y <= array.GetUpperBound(1); y++)
                {
                    for (var z = array.GetLowerBound(2); z <= array.GetUpperBound(2); z++)
                    {
                        array[x, y, z] = filling(x, y, z, array[x, y, z]);
                    }
                }
            }
        }
예제 #12
0
        private static void Copy3DItems <T>(
            T[,,] sourceArray,
            T[,,] targetArray,
            MemberSettings settings,
            ReferencePairCollection referencePairs)
        {
            var copyValues = State.Copy.IsCopyValue(
                sourceArray.GetType().GetItemType(),
                settings);

            for (var i = sourceArray.GetLowerBound(0); i <= sourceArray.GetUpperBound(0); i++)
            {
                for (var j = sourceArray.GetLowerBound(1); j <= sourceArray.GetUpperBound(1); j++)
                {
                    for (var k = sourceArray.GetLowerBound(2); k <= sourceArray.GetUpperBound(2); k++)
                    {
                        if (copyValues)
                        {
                            targetArray[i, j, k] = sourceArray[i, j, k];
                            continue;
                        }

                        var  sv = sourceArray[i, j, k];
                        var  tv = targetArray[i, j, k];
                        bool created;
                        bool needsSync;
                        var  clone = State.Copy.CloneWithoutSync(sv, tv, settings, out created, out needsSync);
                        if (created)
                        {
                            targetArray[i, j, k] = clone;
                        }

                        if (needsSync)
                        {
                            State.Copy.Sync(sv, clone, settings, referencePairs);
                        }
                    }
                }
            }
        }
예제 #13
0
    /// <summary>
    /// Gets the index of the first element of the specified dimension in the array.
    /// </summary>
    /// <typeparam name="T">The type of values stored in the array.</typeparam>
    /// <param name="array">The array.</param>
    /// <param name="dimension">The dimension whose starting index needs to be determined.</param>
    /// <returns>The index of the first element of the specified dimension in the array.</returns>
    public static int GetLowerBound <T>(this T[,,] array, Axis3D dimension)
    {
        Contracts.Requires.That(array != null);

        return(array.GetLowerBound((int)dimension));
    }