Skip to content

Commit 496ca5e

Browse files
authored
fix (sparse) : add sort_data to coo2csr procedure (#1146)
* add sort_data optional to coo2csr * make COO inout to allow sorting * enrich doc for sort_data in coo2csr
1 parent e33b3eb commit 496ca5e

2 files changed

Lines changed: 16 additions & 4 deletions

File tree

doc/specs/stdlib_sparse.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,14 +296,16 @@ If the `diagonal` array has not been previously allocated, the `diag` subroutine
296296

297297
### Syntax
298298

299-
`call ` [[stdlib_sparse_conversion(module):coo2csr(interface)]] `(coo,csr)`
299+
`call ` [[stdlib_sparse_conversion(module):coo2csr(interface)]] `(coo,csr[,sort_data])`
300300

301301
### Arguments
302302

303303
`coo` : Shall be a `COO` type of `real` or `complex` type. It is an `intent(in)` argument.
304304

305305
`csr` : Shall be a `CSR` type of `real` or `complex` type. It is an `intent(out)` argument.
306306

307+
`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.
308+
307309
### Syntax
308310

309311
`call ` [[stdlib_sparse_conversion(module):coo2csc(interface)]] `(coo,csc)`

src/sparse/stdlib_sparse_conversion.fypp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
!!
77
! This code was modified from https://github.com/jalvesz/FSPARSE by its author: Alves Jose
88
module stdlib_sparse_conversion
9+
use stdlib_optval, only: optval
910
use stdlib_sorting, only: sort
1011
use stdlib_sparse_constants
1112
use stdlib_sparse_kinds
@@ -215,11 +216,20 @@ contains
215216
#:endfor
216217

217218
#:for k1, t1, s1 in (KINDS_TYPES)
218-
subroutine coo2csr_${s1}$(COO,CSR)
219-
type(COO_${s1}$_type), intent(in) :: COO
220-
type(CSR_${s1}$_type), intent(out) :: CSR
219+
subroutine coo2csr_${s1}$(COO, CSR, sort_data)
220+
type(COO_${s1}$_type), intent(inout) :: COO
221+
type(CSR_${s1}$_type), intent(out) :: CSR
222+
logical, intent(in), optional :: sort_data
223+
logical :: sort_data_
221224
integer(ilp) :: i
222225

226+
sort_data_ = optval(x=sort_data, default=.false.)
227+
228+
if(sort_data_) then
229+
call sort_coo_unique_${s1}$( COO%index, COO%data, COO%nnz, COO%nrows, COO%ncols )
230+
COO%is_sorted = .true.
231+
end if
232+
223233
CSR%nnz = COO%nnz; CSR%nrows = COO%nrows; CSR%ncols = COO%ncols
224234
CSR%storage = COO%storage
225235

0 commit comments

Comments
 (0)