/// <summary> /// Initializes a new instance of the <see cref="T:Academy.Collections.Generic.Matrix`1"/> class /// with the specified dimensions and order. /// </summary> /// <param name="rowCount">The number of rows in the matrix.</param> /// <param name="columnCount">The number of columns in the matrix.</param> /// <param name="dataOrder">The order the matrix will store elements in memory. 'Default' and 'Merged' values are used as 'Row' value.</param> public Matrix(int rowCount, int columnCount, MatrixDataOrder dataOrder) { if (rowCount < 0) { rowCount = 0; } if (columnCount < 0) { columnCount = 0; } if (dataOrder != MatrixDataOrder.Column) { dataOrder = MatrixDataOrder.Row; } int num = MaxArraySize / rowCount; if (rowCount > num) { rowCount = num; } this._rowDim = rowCount; this._colDim = columnCount; this._dataOrder = dataOrder; this._version = 0; this.CalculateCount(); this._elements = new T[_count]; }
/// <summary> /// Creates a new instance of <see cref="T:Academy.Collections.Generic.MatrixEntryCollection`1"/> that contains /// elements copied from the specified collection and sorted by the specified order. /// </summary> /// <param name="collection">The collection whose elements are copied to the new <see cref="T:Academy.Collections.Generic.MatrixEntryCollection`1"/>.</param> /// <param name="order">The order the collection presents the entries.</param> /// <returns>a new instance of <see cref="T:Academy.Collections.Generic.MatrixEntryCollection`1"/> containing elements copied from the specified collection /// and provides the specified order.</returns> public static MatrixEntryCollection <T> Create(IEnumerable <MatrixEntry <T> > collection, MatrixDataOrder order) { if (collection == null) { Thrower.ArgumentNullException(ArgumentType.collection); } if (order == MatrixDataOrder.Default) { order = MatrixDataOrder.Row; } return(new EntryFixedCollection(collection, order)); }
/// <summary> /// Returns an <see cref="T:System.Collections.Generic.IComparer`1"/> that can be used to /// compare <see cref="T:Academy.Collections.Generic.MatrixEntry`1"/> instances based on the specified order. /// </summary> /// <param name="order">The order used to create the comparer.</param> /// <returns>A comparer used to compare <see cref="T:Academy.Collections.Generic.MatrixEntry`1"/> instances.</returns> public static IComparer <MatrixEntry <T> > CreateEntryComparer(MatrixDataOrder order) { if (order == MatrixDataOrder.Column) { return(new ColumnFirstComparer()); } if (order == MatrixDataOrder.Merged) { return(new MergedComparer()); } return(new RowFirstComparer()); }
private static IComparer <Index> GetIndexComparer(MatrixDataOrder order) { IComparer <Index> comparer; if (order == MatrixDataOrder.Row) { comparer = new RowFirstIndexComparer(); } else { comparer = new ColumnFirstIndexComparer(); } return(comparer); }
public EntryFixedCollection(IEnumerable <MatrixEntry <T> > collection, MatrixDataOrder order) : base(order) { list = new List <MatrixEntry <T> >(collection); if (order != MatrixDataOrder.Merged) { IComparer <MatrixEntry <T> > comparer = MatrixEntry <T> .CreateEntryComparer(order); list.Sort(comparer); } else { comparer = null; } }
/// <summary> /// Resizes the matrix to the specified new dimension, specifying new dimensions and order. If new dimension provokes a smaller matrix, out-bounds values will be ignored. /// Otherwise, if new dimension provokes a bigger matrix, new elements will have its type default value. /// </summary> /// <param name="newRowCount">The new number of rows.</param> /// <param name="newColumnCount">The new number of columns.</param> /// <param name="dataOrder">The order the matrix will use to store the elements in the operation.</param> public void Resize(int newRowCount, int newColumnCount, MatrixDataOrder dataOrder) { if (newRowCount < 0) { newRowCount = 0; } if (newColumnCount < 0) { newColumnCount = 0; } if (dataOrder == MatrixDataOrder.Default) { dataOrder = this.DataOrder; } if ((newRowCount != _rowDim) || (newColumnCount != _colDim) || (_dataOrder != dataOrder)) { int newSize = newRowCount * newColumnCount; T[] local = new T[newSize]; int rowMin = Math.Min(_rowDim, newRowCount); int colMin = Math.Min(_colDim, newColumnCount); for (int i = 0; i < rowMin; i++) { for (int j = 0; j < colMin; j++) { int num = GetIndex(i, j); int num2 = GetIndex(i, j, newRowCount, newColumnCount, dataOrder); local[num2] = _elements[num]; } } this._elements = local; this._dataOrder = dataOrder; this._rowDim = newRowCount; this._colDim = newColumnCount; this.CalculateCount(); } }
/// <summary> /// Initializes a new instance of the <see cref="T:Academy.Collections.Generic.SparseMatrix`1"/> class with specified dimensions, data order and default value. /// </summary> /// <param name="rowCount">The number of rows in the matrix.</param> /// <param name="columnCount">The number of columns in the matrix.</param> /// <param name="defaultValue">The value the matrix will assume as the default value.</param> /// <param name="dataOrder">The order the matrix will store elements in memory. 'Default' and 'Merged' values are used as 'Row' value.</param> public SparseMatrix(int rowCount, int columnCount, T defaultValue, MatrixDataOrder dataOrder) { if (dataOrder == MatrixDataOrder.Default) { dataOrder = MatrixDataOrder.Row; } this.order = dataOrder; IComparer <Index> comparer; if (dataOrder == MatrixDataOrder.Row) { comparer = new RowFirstIndexComparer(); } else { comparer = new ColumnFirstIndexComparer(); } dict = new SortedDictionary <Index, T>(comparer); this.comparer = new ValueEqualityComparer(); this.rowDim = rowCount; this.colDim = columnCount; this.count = (long)rowCount * (long)columnCount; this.def = defaultValue; }
/// <summary> /// Initializes a new instance of the <see cref="T:Academy.Collections.Generic.SparseMatrix`1"/> class with specified dimensions, data order, /// default value and elements copied from the specified collection and positioned in the indices specified by the respective entries. /// </summary> /// <param name="entries">The collection whose elements are copied to the new matrix.</param> /// <param name="rowCount">The amount of rows of the matrix.</param> /// <param name="columnDimension">The amount of columns of the matrix.</param> /// <param name="defaultValue">The value the matrix will assume as the default value.</param> /// <param name="order">The order the matrix will store elements in memory. 'Default' and 'Merged' values are used as 'Row' value.</param> public SparseMatrix(IEnumerable <MatrixEntry <T> > entries, int rowCount, int columnDimension, T defaultValue, MatrixDataOrder order) { if (order != MatrixDataOrder.Column) { order = MatrixDataOrder.Row; } this.order = order; IComparer <Index> comparer; if (order == MatrixDataOrder.Row) { comparer = new RowFirstIndexComparer(); } else { comparer = new ColumnFirstIndexComparer(); } dict = new SortedDictionary <Index, T>(comparer); MatrixEntry <T> upperEntry = new MatrixEntry <T>(rowCount - 1, columnDimension - 1, default(T)); this.def = defaultValue; this.comparer = new ValueEqualityComparer(); this.rowDim = rowCount; this.colDim = columnDimension; this.count = (long)rowCount * (long)columnDimension; foreach (MatrixEntry <T> entry in entries) { int num = entry.CompareTo(lowerEntry); int num2 = entry.CompareTo(upperEntry); if ((num < 0) || (num2 > 0)) { Thrower.ArgumentOutOfRangeException(ArgumentType.empty, Resources.ArgumentOutOfRange_EntryOutOfBounds); } DirectlySet(entry.RowIndex, entry.ColumnIndex, entry.Value); } }
private static int GetIndex(int rowIndex, int columnIndex, int rowCount, int columnCount, MatrixDataOrder order) { if (order == MatrixDataOrder.Column) { return((int)((long)(rowCount) * columnIndex + rowIndex)); } return((int)((long)columnCount * rowIndex + columnIndex)); }
public NonDefaultEntriesCollection(SparseMatrix <T> matrix, MatrixDataOrder order) : base(order) { this.matrix = matrix; }
/// <summary> /// Initializes a new instance of the <see cref="T:Academy.Collections.Generic.SparseMatrix`1"/> class with specified dimensions and data order. /// </summary> /// <param name="rowCount">The number of rows in the matrix.</param> /// <param name="columnCount">The number of columns in the matrix.</param> /// <param name="order">The order the matrix will store elements in memory. 'Default' and 'Merged' values are used as 'Row' value.</param> public SparseMatrix(int rowCount, int columnCount, MatrixDataOrder order) : this(rowCount, columnCount, default(T), order) { }
/// <summary> /// Initializes a new instance of the <see cref="T:Academy.Collections.Generic.MatrixEntryCollection`1"/> class with the specified order. /// </summary> /// <param name="order">The order used to enumerate through the elements contained in the collection.</param> protected MatrixEntryCollection(MatrixDataOrder order) { this.order = order; }