private IEnumerable <TVal> _col(int j, RangeWrapper iRange) { foreach (int i in iRange.Range(Rows)) { yield return(_matrix[i, j]); } }
private IEnumerable <TVal> _row(int i, RangeWrapper jRange) { foreach (int j in jRange.Range(Cols)) { yield return(_matrix[i, j]); } }
public IEnumerable <IEnumerable <TVal> > this[RangeWrapper iRangeW, RangeWrapper jRangeW] { get { foreach (int i in iRangeW.Range(Rows)) { yield return(Row(i, jRangeW)); } } set { if (value is Matrix <TVal> && iRangeW.WrappedRange is IContinousRange && jRangeW.WrappedRange is IContinousRange) { Matrix <TVal> B = (Matrix <TVal>)value; IContinousRange iRangeC = (IContinousRange)iRangeW.WrappedRange; IContinousRange jRangeC = (IContinousRange)jRangeW.WrappedRange; int iMin = iRangeC.Min(Rows), iMax = iRangeC.Max(Rows); int jMin = jRangeC.Min(Cols), jMax = jRangeC.Max(Cols); //iMax = Math.Min(iMax, iMin + B.Rows - 1); if (B.Rows > iMax - iMin + 1 || B.Cols > jMax - jMin + 1) { throw new InvalidOperationException("Source is larger than destination, [" + (iMax - iMin + 1) + ", " + (jMax - jMin + 1) + "] = [" + B.Rows + ", " + B.Cols + "]"); } int nRows = Math.Min(iMax - iMin + 1, B.Rows); if (Cols == B.Cols && jMin == 0 && jMax == Cols - 1) { // We can copy the entire array in one go, since both src and dst spans entire rows (and has the same length) Array.Copy(B._matrix, 0, _matrix, Cols * iMin, Cols * nRows); return; } int rowSize = Math.Min(jMax - jMin + 1, B.Cols); // Don't copy more than what exists in a src row // Copy row by row for (int i = 0; i < nRows; i++) { Array.Copy(B._matrix, B.Cols * i, _matrix, Cols * (iMin + i) + jMin, rowSize); } return; } IEnumerator <IEnumerable <TVal> > iEnumerator = value.GetEnumerator(); var iRange = iRangeW.Range(Rows); var jRange = jRangeW.Range(Cols); foreach (int i in iRange) { if (!iEnumerator.MoveNext()) { return; // Not enough values in src } IEnumerator <TVal> jEnumerator = iEnumerator.Current.GetEnumerator(); foreach (int j in jRange) { if (!jEnumerator.MoveNext()) { break; // Not enough values in src row } _matrix[i, j] = jEnumerator.Current; } if (jEnumerator.MoveNext()) { throw new InvalidOperationException("Source is larger than destination, [" + iRange.Count() + ", " + jRange.Count() + "] = [" + value.Count() + ", " + iEnumerator.Current.Count() + "]"); } } if (iEnumerator.MoveNext()) { throw new InvalidOperationException("Source is larger than destination, [" + iRange.Count() + ", " + jRange.Count() + "] = [" + value.Count() + ", ?]"); } } }