Sort an array in increasing order, and remove values that
are within tol of the previous one.
| Type | Intent | Optional | Attributes | Name | ||
|---|---|---|---|---|---|---|
| real(kind=wp), | intent(inout), | dimension(:), allocatable | :: | x |
the array |
|
| real(kind=wp), | intent(in) | :: | tol |
duplicate tolerance |
pure subroutine sort_unique(x,tol) implicit none real(wp),dimension(:),allocatable,intent(inout) :: x !! the array real(wp),intent(in) :: tol !! duplicate tolerance integer(ip) :: i !! counter integer(ip) :: j !! counter integer(ip) :: nu !! number of unique values real(wp) :: tmp !! the value being inserted ! insertion sort (the arrays are small and nearly sorted) do i = 2_ip, size(x,kind=ip) tmp = x(i) j = i - 1_ip do while (j>=1_ip) if (x(j)<=tmp) exit x(j+1_ip) = x(j) j = j - 1_ip end do x(j+1_ip) = tmp end do nu = min(1_ip,size(x,kind=ip)) do i = 2_ip, size(x,kind=ip) if (x(i)-x(nu)>tol) then nu = nu + 1_ip x(nu) = x(i) end if end do x = x(1_ip:nu) end subroutine sort_unique