/// <summary> Tries to find the row-index in the given column. If it is not found, /// a reallocation is done, and a new index is returned.</summary> private int GetRowIndex(int row, int col) { int[] curRow = _rowIndex[col]; double[] curDat = _data[col]; // Try to find column index int ind = ArraySupport.BinarySearchGreater(curRow, row, 0, _used[col]); // Found if (ind < _used[col] && curRow[ind] == row) { return(ind); } // Not found, try to make room if (row < 0 || row >= row_count) { throw new IndexOutOfRangeException(" Row " + row + " Column " + col); } _used[col]++; // Check available memory if (_used[col] > curDat.Length) { // If zero-length, use new length of 1, else double the bandwidth int newLength = 1; if (curDat.Length != 0) { newLength = 2 * curDat.Length; } // Copy existing data into new arrays int[] newRow = new int[newLength]; double[] newDat = new double[newLength]; Array.Copy(curRow, 0, newRow, 0, curDat.Length); Array.Copy(curDat, 0, newDat, 0, curDat.Length); // Update pointers _rowIndex[col] = newRow; _data[col] = newDat; curRow = newRow; curDat = newDat; } // All ok, make room for insertion for (int i = _used[col] - 1; i >= ind + 1; --i) { curRow[i] = curRow[i - 1]; curDat[i] = curDat[i - 1]; } // Put in new structure curRow[ind] = row; curDat[ind] = 0.0; _isCompact = false; return(ind); }
/// <summary> Tries to find the index. If it is not found, a reallocation is done, and /// a new index is returned.</summary> private int GetIndex(int index) { // Try to find column index int i = ArraySupport.BinarySearchGreater(_ind, index, 0, _used); // Found if (i < _used && _ind[i] == index) { return(i); } // Not found, try to make room if (index < 0 || index >= _length) { throw new IndexOutOfRangeException("Index " + index); } _used++; // Check available memory if (_used > _data.Length) { // If zero-length, use new length of 1, else double the bandwidth int newLength = 1; if (_data.Length != 0) { newLength = 2 * _data.Length; } // Copy existing data into new arrays int[] newInd = new int[newLength]; double[] newDat = new double[newLength]; Array.Copy(_ind, 0, newInd, 0, _data.Length); Array.Copy(_data, 0, newDat, 0, _data.Length); // Update pointers _ind = newInd; _data = newDat; } // All ok, make room for insertion for (int j = _used - 1; j >= i + 1; --j) { _ind[j] = _ind[j - 1]; _data[j] = _data[j - 1]; } // Put in new structure _ind[i] = index; _data[i] = 0.0; return(i); }
/// <summary> Tries to find the col-index in the given row. If it is not found, /// a reallocation is done, and a new index is returned. If there is no more /// space for further allocation, an exception is thrown.</summary> private int GetColumnIndex(int col, int row) { int rowOffset = _rowIndices[row]; int ind = ArraySupport.BinarySearchGreater(_columnIndices, col, rowOffset, rowOffset + _used[row]) - rowOffset; // Found if (ind < _used[row] && _columnIndices[rowOffset + ind] == col) { return(ind); } // Need to insert // Check column index if (col < 0 || col >= column_count) { throw new IndexOutOfRangeException("Row " + row + " Column " + col); } // Is the row full? if (rowOffset + _used[row] >= _data.Length || ((row + 1) < _rowIndices.Length && rowOffset + _used[row] >= _rowIndices[row + 1])) { throw new IndexOutOfRangeException("Too many non-zeros on row " + row); } // Make room for insertion for (int i = _used[row]; i >= ind + 1; --i) { _columnIndices[rowOffset + i] = _columnIndices[rowOffset + i - 1]; _data[rowOffset + i] = _data[rowOffset + i - 1]; } // Put in new structure _used[row]++; _columnIndices[rowOffset + ind] = col; _data[rowOffset + ind] = 0; _isCompact = false; return(ind); }
/// <summary> Tries to find the col-index in the given row. If it is not found, /// a reallocation is done, and a new index is returned. If there is no more /// space for further allocation, an exception is thrown. /// </summary> private int GetRowIndex(int row, int col) { int columnOffset = _columnIndex[col]; int ind = ArraySupport.BinarySearchGreater(_rowIndex, row, columnOffset, columnOffset + _used[col]) - columnOffset; // Found if (ind < _used[col] && _rowIndex[columnOffset + ind] == row) { return(ind); } // Need to insert // Check row index if (row < 0 || row >= row_count) { throw new IndexOutOfRangeException("Row " + row + " Column " + col); } // Is the column full? if (columnOffset + _used[col] >= _data.Length || ((col + 1) < _columnIndex.Length && columnOffset + _used[col] >= _columnIndex[col + 1])) { throw new IndexOutOfRangeException("Too many non-zeros on column " + col); } // Make room for insertion for (int i = _used[col]; i >= ind + 1; --i) { _rowIndex[columnOffset + i] = _rowIndex[columnOffset + i - 1]; _data[columnOffset + i] = _data[columnOffset + i - 1]; } // Put in new structure _used[col]++; _rowIndex[columnOffset + ind] = row; _data[columnOffset + ind] = 0; _isCompact = false; return(ind); }