Takes an input unsorted integer array and returns a new array of sorted, unique values (I4B version)
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| integer(kind=I4B), | intent(in), | dimension(:) | :: | input_array | Unsorted input array |
|
| integer(kind=I4B), | intent(out), | dimension(:), allocatable | :: | output_array | Sorted array of unique values |
|
| integer(kind=I4B), | intent(out), | dimension(:), allocatable | :: | index_map | An array of the same size as input_array that such that any for any index i, |
subroutine base_util_unique_I4B(input_array, output_array, index_map)
!! author: David A. Minton
!!
!! Takes an input unsorted integer array and returns a new array of sorted, unique values (I4B version)
implicit none
! Arguments
integer(I4B), dimension(:), intent(in) :: input_array
!! Unsorted input array
integer(I4B), dimension(:), allocatable, intent(out) :: output_array
!! Sorted array of unique values
integer(I4B), dimension(:), allocatable, intent(out) :: index_map
!! An array of the same size as input_array that such that any for any index i,
!! output_array(index_map(i)) = input_array(i)
! Internals
integer(I4B), dimension(:), allocatable :: unique_array
integer(I4B) :: n, lo, hi
allocate(unique_array, mold=input_array)
allocate(index_map, mold=input_array)
lo = minval(input_array) - 1
hi = maxval(input_array)
n = 0
do
n = n + 1
lo = minval(input_array(:), mask=input_array(:) > lo)
unique_array(n) = lo
where(input_array(:) == lo) index_map(:) = n
if (lo >= hi) exit
enddo
allocate(output_array(n), source=unique_array(1:n))
return
end subroutine base_util_unique_I4B