/// <summary> /// Deletes a column. /// </summary> /// <typeparam name="TElement">The type of the element.</typeparam> /// <param name="array">The array.</param> /// <param name="columnIndex">Index of the column.</param> public static TElement[,] DeleteColumn <TElement>(TElement[,] array, int columnIndex) { var colCount = array.GetLength(0) - 1; var rowCount = array.GetLength(1); if (colCount <= 0) { colCount = 0; rowCount = 0; } var newArr = new TElement[colCount, rowCount]; for (int x = 0; x < colCount; x++) { int tmpX = x; if (tmpX >= columnIndex) { tmpX++; } for (int y = 0; y < rowCount; y++) { newArr[x, y] = array[tmpX, y]; } } return(newArr); }
/// <summary> /// Deletes the row. /// </summary> /// <typeparam name="TElement">The type of the element.</typeparam> /// <param name="array">The array.</param> /// <param name="rowIndex">Index of the row.</param> public static TElement[,] DeleteRow <TElement>(TElement[,] array, int rowIndex) { var colCount = array.GetLength(0); var rowCount = array.GetLength(1) - 1; if (rowCount <= 0) { colCount = 0; rowCount = 0; } var newArr = new TElement[colCount, rowCount]; for (int y = 0; y < rowCount; y++) { int tmpY = y; if (tmpY >= rowIndex) { tmpY++; } for (int x = 0; x < colCount; x++) { newArr[x, y] = array[x, tmpY]; } } return(newArr); }
/// <summary> /// Duplicates the column. /// </summary> /// <typeparam name="TElement">The type of the element.</typeparam> /// <param name="columnIndex">Index of the column.</param> /// <param name="array">The array.</param> public static TElement[,] DuplicateColumn <TElement>(TElement[,] array, int columnIndex) { var colCount = array.GetLength(0); var rowCount = array.GetLength(1); var newArr = new TElement[colCount + 1, Math.Max(rowCount, 1)]; for (int x = 0; x < colCount; x++) { int tmpX = x; if (tmpX >= columnIndex) { tmpX++; } for (int y = 0; y < rowCount; y++) { newArr[tmpX, y] = array[x, y]; } } for (int y = 0; y < newArr.GetLength(1); y++) { newArr[columnIndex, y] = array[columnIndex, y]; } return(newArr); }
/// <summary> /// Duplicates the row. /// </summary> /// <typeparam name="TElement">The type of the element.</typeparam> /// <param name="array">The array.</param> /// <param name="rowIndex">Index of the row.</param> public static TElement[,] DuplicateRow <TElement>(TElement[,] array, int rowIndex) { var colCount = array.GetLength(0); var rowCount = array.GetLength(1); var newArr = new TElement[Math.Max(colCount, 1), rowCount + 1]; for (int y = 0; y < rowCount; y++) { int tmpY = y; if (tmpY >= rowIndex) { tmpY++; } for (int x = 0; x < colCount; x++) { newArr[x, tmpY] = array[x, y]; } } for (int x = 0; x < newArr.GetLength(0); x++) { newArr[x, rowIndex] = array[x, rowIndex]; } return(newArr); }
private void MatrixToGrid <TElement>(TElement[,] m, DataGridView dgv) { if (m == null) { return; } if (m.GetLength(0) > dgv.RowCount) { return; } if (m.GetLength(1) > dgv.ColumnCount) { return; } // check rows columns for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { dgv1[j, i].Value = m[i, j]; } } }
public MatrixLayer(TElement[,] matrix) : this() { Width = matrix.GetLength(0); Height = matrix.GetLength(1); storage = new TElement[Width * Height]; SetStorage(matrix); }
public static void For <TElement>(this TElement[,] @this, Action <int, int, TElement> action) { for (var row = 0; row < @this.GetLength(0); row++) { for (var column = 0; column < @this.GetLength(1); column++) { action(row, column, @this[row, column]); } } }
/// <summary> /// Moves a row. /// </summary> /// <typeparam name="TElement">The type of the element.</typeparam> /// <param name="array">The array.</param> /// <param name="fromRow">From row.</param> /// <param name="toRow">To row.</param> public static TElement[,] MoveRow <TElement>(TElement[,] array, int fromRow, int toRow) { if (fromRow == toRow) { return(array); } var colCount = array.GetLength(0); var rowCount = array.GetLength(1); var newArr = new TElement[colCount, rowCount]; if (fromRow < toRow) { // Move down for (int y = 0; y < rowCount; y++) { int offset = y >= fromRow && y < toRow ? 1 : 0; for (int x = 0; x < colCount; x++) { if (y == toRow) { newArr[x, y] = array[x, fromRow]; } else { newArr[x, y] = array[x, y + offset]; } } } } else { // Move up for (int y = 0; y < rowCount; y++) { int offset = y > toRow && y <= fromRow ? 1 : 0; for (int x = 0; x < colCount; x++) { if (y == toRow + 1) { newArr[x, y] = array[x, fromRow]; } else { newArr[x, y] = array[x, y - offset]; } } } } return(newArr); }
/// <summary> /// Moves a column. /// </summary> /// <typeparam name="TElement">The type of the element.</typeparam> /// <param name="array">The array.</param> /// <param name="fromColumn">From column.</param> /// <param name="toColumn">To column.</param> public static TElement[,] MoveColumn <TElement>(TElement[,] array, int fromColumn, int toColumn) { if (fromColumn == toColumn) { return(array); } var colCount = array.GetLength(0); var rowCount = array.GetLength(1); var newArr = new TElement[colCount, rowCount]; if (fromColumn < toColumn) { // Move from left to right for (int x = 0; x < colCount; x++) { int offset = x >= fromColumn && x < toColumn ? 1 : 0; for (int y = 0; y < rowCount; y++) { if (x == toColumn) { newArr[x, y] = array[fromColumn, y]; } else { newArr[x, y] = array[x + offset, y]; } } } } else { // Move from right to left for (int x = 0; x < colCount; x++) { int offset = x > toColumn && x <= fromColumn ? 1 : 0; for (int y = 0; y < rowCount; y++) { if (x == toColumn + 1) { newArr[x, y] = array[fromColumn, y]; } else { newArr[x, y] = array[x - offset, y]; } } } } return(newArr); }
public static TElement[] ShallowFlatten <TElement>(TElement[,] array) { var newList = new TElement[array.Length]; var index = 0; foreach (var element in array) { newList[index] = element; index++; } return(newList); }
/// <summary> /// Get row from 2D array. /// </summary> public static IEnumerable <TElement> GetRow <TElement>(this TElement[,] source, int row) { _ = source ?? throw new ArgumentNullException(nameof(source)); var rowSize = source.GetLength(0); if (row < 0 || row >= rowSize) { throw new ArgumentOutOfRangeException(nameof(row), $"{nameof(row)} has to be between 0 and {rowSize - 1}."); } var columnSize = source.GetLength(1); return(Enumerable.Range(0, columnSize).Select(c => source[row, c])); }
/// <summary> /// Get column from 2D array. /// </summary> public static IEnumerable <TElement> GetColumn <TElement>(this TElement[,] source, int column) { _ = source ?? throw new ArgumentNullException(nameof(source)); var columnSize = source.GetLength(1); if (column < 0 || column >= columnSize) { throw new ArgumentOutOfRangeException(nameof(column), $"{nameof(column)} has to be between 0 and {columnSize - 1}."); } var rowSize = source.GetLength(0); return(Enumerable.Range(0, rowSize) .Select(r => source[r, column])); }
/// <summary> /// /// </summary> /// <typeparam name="TElement"></typeparam> /// <param name="source"></param> /// <returns></returns> public static TElement[][] Project <TElement>(this TElement[,] source) { int rows = source.GetLength(0), columns = source.GetLength(1); var result = new TElement[rows][]; for (int row = 0; row < rows; row++) { result[row] = new TElement[columns]; for (int column = 0; column < columns; column++) { result[row][column] = source[row, column]; } } return(result); }
/// <summary> /// Inserts one column right of the specified column index. /// </summary> /// <typeparam name="TElement">The type of the element.</typeparam> /// <param name="columnIndex">Index of the column.</param> /// <param name="arr">The arr.</param> public static TElement[,] InsertOneColumnRight <TElement>(TElement[,] arr, int columnIndex) { var colCount = arr.GetLength(0); var rowCount = arr.GetLength(1); var newArr = new TElement[colCount + 1, Math.Max(rowCount, 1)]; for (int x = 0; x < colCount; x++) { int tmpX = x; if (tmpX > columnIndex) { tmpX++; } for (int _y = 0; _y < rowCount; _y++) { newArr[tmpX, _y] = arr[x, _y]; } } return(newArr); }
public virtual void Resize(int rows, int columns) { _matrix = new TElement[rows, columns]; }
public ControlMatrix(IList <TRowUnit> rowUnitList, IList <TColumnUnit> columnUnitList, TElement[,] elements) { if (elements != null && (rowUnitList.Count != elements.GetLength(0) || columnUnitList.Count != elements.GetLength(1))) { throw new IndexOutOfRangeException("Support.Net.Util.ControlMatrix()"); } rows = Clone(rowUnitList, (idx, unit) => new MatrixRow(unit, idx)); columns = Clone(columnUnitList, (idx, unit) => new MatrixColumn(unit, idx)); matrixElements = new MatrixElement[rows.Count(), columns.Count()]; if (elements != null) { for (int i = 0; i < rows.Count(); i++) { for (int j = 0; j < columns.Count(); j++) { var element = DoGetPoint(i, j); element.Element = elements[i, j]; } } } }
/// <summary> /// Draws the property. /// </summary> protected override void DrawPropertyLayout(IPropertyValueEntry <TArray> entry, GUIContent label) { TElement[,] value = entry.Values[0] as TElement[, ]; bool rowLengthConflic = false; bool colLengthConflic = false; int colCount = value.GetLength(0); int rowCount = value.GetLength(1); for (int i = 1; i < entry.Values.Count; i++) { var arr = entry.Values[i] as TElement[, ]; colLengthConflic = colLengthConflic || arr.GetLength(0) != colCount; rowLengthConflic = rowLengthConflic || arr.GetLength(1) != rowCount; colCount = Mathf.Min(colCount, arr.GetLength(0)); rowCount = Mathf.Min(rowCount, arr.GetLength(1)); } var context = entry.Context.Get(this, "context", (Context)null); if (context.Value == null || colCount != context.Value.ColCount || rowCount != context.Value.RowCount) { context.Value = new Context(); context.Value.Value = value; context.Value.ColCount = colCount; context.Value.RowCount = rowCount; context.Value.Attribute = entry.Property.Info.GetAttribute <TableMatrixAttribute>() ?? this.GetDefaultTableMatrixAttributeSettings(); if (context.Value.Attribute.DrawElementMethod != null) { string error; var drawElementMethod = entry.ParentType.FindMember() .IsMethod() .IsStatic() .HasReturnType <TElement>() .IsNamed(context.Value.Attribute.DrawElementMethod) .HasParameters <Rect, TElement>() .GetMember <MethodInfo>(out error); if (error != null) { context.Value.ErrorMessage += error + "\n\n"; } else { context.Value.DrawElement = (Func <Rect, TElement, TElement>)Delegate.CreateDelegate(typeof(Func <Rect, TElement, TElement>), drawElementMethod); } } context.Value.HorizontalTitleGetter = new StringMemberHelper(entry.ParentType, context.Value.Attribute.HorizontalTitle); context.Value.VerticalTitleGetter = new StringMemberHelper(entry.ParentType, context.Value.Attribute.VerticalTitle); context.Value.Table = GUITable.Create( Mathf.Max(colCount, 1) + (colLengthConflic ? 1 : 0), Mathf.Max(rowCount, 1) + (rowLengthConflic ? 1 : 0), (rect, x, y) => this.DrawElement(rect, entry, context.Value, x, y), context.Value.HorizontalTitleGetter.GetString(entry), context.Value.Attribute.HideColumnIndices ? (Action <Rect, int>)null : (rect, x) => this.DrawColumn(rect, entry, context.Value, x), context.Value.VerticalTitleGetter.GetString(entry), context.Value.Attribute.HideRowIndices ? (Action <Rect, int>)null : (rect, y) => this.DrawRows(rect, entry, context.Value, y), context.Value.Attribute.ResizableColumns ); if (context.Value.Attribute.RowHeight != 0) { for (int y = 0; y < context.Value.RowCount; y++) { int _y = context.Value.Table.RowCount - 1 - y; for (int x = 0; x < context.Value.Table.ColumnCount; x++) { var cell = context.Value.Table[x, _y]; if (cell != null) { cell.Height = context.Value.Attribute.RowHeight; } } } } if (colLengthConflic) { context.Value.Table[context.Value.Table.ColumnCount - 1, 1].Width = 15; } if (colLengthConflic) { for (int x = 0; x < context.Value.Table.ColumnCount; x++) { context.Value.Table[x, context.Value.Table.RowCount - 1].Height = 15; } } } if (context.Value.Attribute.SquareCells) { SetSquareRowHeights(context); } this.TableMatrixAttribute = context.Value.Attribute; context.Value.Value = value; var prev = EditorGUI.showMixedValue; this.OnBeforeDrawTable(entry, context.Value, label); if (context.Value.ErrorMessage != null) { SirenixEditorGUI.ErrorMessageBox(context.Value.ErrorMessage); } else { try { context.Value.Table.DrawTable(); GUILayout.Space(3); } catch (ExitGUIException ex) { throw ex; } catch (Exception ex) { Debug.LogException(ex); } } EditorGUI.showMixedValue = prev; }