diff --git a/doc/specs/stdlib_sparse.md b/doc/specs/stdlib_sparse.md index 4d0b6df5b..1b1cf253d 100644 --- a/doc/specs/stdlib_sparse.md +++ b/doc/specs/stdlib_sparse.md @@ -296,7 +296,7 @@ If the `diagonal` array has not been previously allocated, the `diag` subroutine ### Syntax -`call ` [[stdlib_sparse_conversion(module):coo2csr(interface)]] `(coo,csr)` +`call ` [[stdlib_sparse_conversion(module):coo2csr(interface)]] `(coo,csr[,sort_data])` ### Arguments @@ -304,6 +304,8 @@ If the `diagonal` array has not been previously allocated, the `diag` subroutine `csr` : Shall be a `CSR` type of `real` or `complex` type. It is an `intent(out)` argument. +`sort_data`, `optional` : Shall be a `logical` argument to determine whether data in the COO graph should be sorted before obtaining the CSR representation. The transformation from COO to CSR depends on the former being sorted in row-major order and not having duplicate pairs. Using this boolean will call a sorting routine at the cost of extra runtime, default `.false.`. It is an `intent(in)` argument. + ### Syntax `call ` [[stdlib_sparse_conversion(module):coo2csc(interface)]] `(coo,csc)` diff --git a/src/sparse/stdlib_sparse_conversion.fypp b/src/sparse/stdlib_sparse_conversion.fypp index 6ada279c2..141948c06 100644 --- a/src/sparse/stdlib_sparse_conversion.fypp +++ b/src/sparse/stdlib_sparse_conversion.fypp @@ -6,6 +6,7 @@ !! ! This code was modified from https://github.com/jalvesz/FSPARSE by its author: Alves Jose module stdlib_sparse_conversion + use stdlib_optval, only: optval use stdlib_sorting, only: sort use stdlib_sparse_constants use stdlib_sparse_kinds @@ -215,11 +216,20 @@ contains #:endfor #:for k1, t1, s1 in (KINDS_TYPES) - subroutine coo2csr_${s1}$(COO,CSR) - type(COO_${s1}$_type), intent(in) :: COO - type(CSR_${s1}$_type), intent(out) :: CSR + subroutine coo2csr_${s1}$(COO, CSR, sort_data) + type(COO_${s1}$_type), intent(inout) :: COO + type(CSR_${s1}$_type), intent(out) :: CSR + logical, intent(in), optional :: sort_data + logical :: sort_data_ integer(ilp) :: i + sort_data_ = optval(x=sort_data, default=.false.) + + if(sort_data_) then + call sort_coo_unique_${s1}$( COO%index, COO%data, COO%nnz, COO%nrows, COO%ncols ) + COO%is_sorted = .true. + end if + CSR%nnz = COO%nnz; CSR%nrows = COO%nrows; CSR%ncols = COO%ncols CSR%storage = COO%storage