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); }
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(); }
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); }
/// <summary> /// Converts a 3 dimensional dataSet to a 2 dimensional dataSet /// </summary> /// <param name="dataSet">The dataSet to fold</param> /// <returns></returns> public T[,] ConvertTo2D(T[, ,] dataSet) { //A 2d matrix of only days and hours int dayEnd = dataSet.GetUpperBound(0) + 1; int hourEnd = dataSet.GetUpperBound(1) + 1; T[,] dMatrix = new T[dayEnd, hourEnd]; T HoursTotal; for (int Day = 0; Day < dayEnd; Day++) { for (int Hour = 0; Hour < hourEnd; Hour++) { HoursTotal = default(T); for (int Minute = 0, minuteEnd = dataSet.GetUpperBound(2) + 1; Minute < minuteEnd; Minute++) { HoursTotal = Operator.Add(HoursTotal, dataSet[Day, Hour, Minute]); }//Minutes if (!Operator.IsDefault(HoursTotal)) { HoursTotal = Operator.DivideInt32(HoursTotal, 60);//minutes in a hour } dMatrix[Day, Hour] = HoursTotal; } //Hours } //Days return(dMatrix); }
/// <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); }
/// <summary> /// Gets the inclusive upper 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 GetUpperBounds <T>(this T[,,] array) { Contracts.Requires.That(array != null); return(new Index3D( array.GetUpperBound((int)Axis3D.X), array.GetUpperBound((int)Axis3D.Y), array.GetUpperBound((int)Axis3D.Z))); }
public void SetData(T[,,] data) { _Data = data; _Length = data.Length; _SizeX = data.GetUpperBound(0) + 1; _SizeY = data.GetUpperBound(1) + 1; _SizeZ = data.GetUpperBound(2) + 1; _ElementSize = Marshal.SizeOf <T>(); }
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]); } } }
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]); } } }
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); } }
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); } }
/// <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]; } }); }); } }
/// <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]); } } } }
public void Write <T>(T[,,] param, ES2Type type) { if (settings.encrypt) { WriteEncrypted(param, type); return; } // If multidimensional, write length of dimensions. if (param.Rank > 2) { // Write no of dimensions, followed by length of each dimension. writer.Write(param.Rank); for (int i = 0; i < param.Rank; i++) { writer.Write(param.GetUpperBound(i)); } } // Else, just write length. else { writer.Write(param.Length); } // Write each object of array sequentially. foreach (System.Object obj in param) { Write(obj, type); } }
public List <T> SliceRow(int row, int depth) { // TODO: implement a fast method for slicing List <T> result = new List <T>(); for (int i = 0; i <= data.GetUpperBound(0); ++i) { result.Add(data[row, i, depth]); } return(result); }
public static T[] Flatten <T>(T[, ,] source) { int d1 = source.GetUpperBound(0) + 1; int d2 = source.GetUpperBound(1) + 1; int d3 = source.GetUpperBound(2) + 1; T[] flat = new T[d1 * d2 * d3]; for (int x = 0; x < d1; x++) { for (int y = 0; y < d2; y++) { for (int z = 0; z < d3; z++) { flat[y + d1 * (x + d2 * z)] = source[x, y, z]; } } } return(flat); }
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); } } } } }
public static bool WithinBounds <T>(this T[,,] environment, ElementPoint point) { if (point.waterPosition < 0 || point.woodPosition < 0 || point.windPosition < 0) { return(false); } if (point.waterPosition > environment.GetUpperBound(0) || point.woodPosition > environment.GetUpperBound(1) || point.windPosition > environment.GetUpperBound(2)) { return(false); } return(true); }
/// <summary> /// Extension method to flatten a 3D array to a 1D array /// </summary> /// <typeparam name="T">Array Type</typeparam> /// <param name="input">3D array to be flattened</param> /// <returns>1D array</returns> public static T[] To1DArray <T>(this T[,,] input) { // Step 1: get total size of 3D array, and allocate 1D array. int size = input.Length; T[] result = new T[size]; // Step 2: copy 3D array elements into a 1D array. int write = 0; for (int i = 0; i <= input.GetUpperBound(0); i++) { for (int j = 0; j <= input.GetUpperBound(1); j++) { for (int k = 0; k < input.GetUpperBound(2); k++) { result[write++] = input[i, j, k]; } } } // Step 3: return the new array. return(result); }
/// <summary> /// Converts a 3 dimensional dataSet to a 2 dimensional dataSet /// </summary> /// <param name="dataSet">The dataSet to fold</param> /// <param name="day">The day to focus</param> /// <returns></returns> public T[,] ConvertTo2D(T[, ,] dataSet, DayOfWeek day) { //A 2d matrix of only days and hours int hourEnd = dataSet.GetUpperBound(1) + 1; T[,] dMatrix = new T[1, hourEnd]; T HoursTotal = default(T); for (int Hour = 0; Hour < hourEnd; Hour++) { HoursTotal = default(T); for (int Minute = 0; Minute < 60; Minute++) { HoursTotal = Operator.Add(HoursTotal, dataSet[(int)day, Hour, Minute]); }//Minutes if (!Operator.IsDefault(HoursTotal)) { HoursTotal = Operator.DivideInt32(HoursTotal, 60);//minutes in a hour } dMatrix[0, Hour] = HoursTotal; }//Hours return(dMatrix); }
/// <summary> /// Gets the index of the last 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 upper bound needs to be determined.</param> /// <returns> /// The index of the last element of the specified dimension in the array, or -1 if the specified dimension is empty. /// </returns> public static int GetUpperBound <T>(this T[,,] array, Axis3D dimension) { Contracts.Requires.That(array != null); return(array.GetUpperBound((int)dimension)); }