Insertion-sort one row segment lo:hi by column index.
Insertion sort is deliberate: CSR rows are short in practice, the segment is already contiguous, and a stable in-place sort keeps equal-column duplicates in their original relative order so the subsequent summation is a plain left-to-right sweep.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=default_int), | intent(inout) | :: | cols(:) | |||
| real(kind=dp), | intent(inout) | :: | vals(:) | |||
| integer(kind=default_int), | intent(in) | :: | lo | |||
| integer(kind=default_int), | intent(in) | :: | hi |
| Type | Visibility | Attributes | Name | Initial | |||
|---|---|---|---|---|---|---|---|
| integer(kind=default_int), | private | :: | i | ||||
| integer(kind=default_int), | private | :: | j | ||||
| integer(kind=default_int), | private | :: | key_col | ||||
| real(kind=dp), | private | :: | key_val |
pure subroutine sort_row(cols, vals, lo, hi) !! Insertion-sort one row segment `lo:hi` by column index. !! !! Insertion sort is deliberate: CSR rows are short in practice, the !! segment is already contiguous, and a stable in-place sort keeps !! equal-column duplicates in their original relative order so the !! subsequent summation is a plain left-to-right sweep. integer(default_int), intent(inout) :: cols(:) real(dp), intent(inout) :: vals(:) integer(default_int), intent(in) :: lo integer(default_int), intent(in) :: hi integer(default_int) :: i, j, key_col real(dp) :: key_val do i = lo + 1, hi key_col = cols(i) key_val = vals(i) j = i - 1 do while (j >= lo) if (cols(j) <= key_col) exit cols(j + 1) = cols(j) vals(j + 1) = vals(j) j = j - 1 end do cols(j + 1) = key_col vals(j + 1) = key_val end do end subroutine sort_row