24 CHARACTER(len=*),
PARAMETER,
PRIVATE :: moduleN =
'util'
33 sort_cv, sort_im, sort_cm
37 MODULE PROCEDURE sort_unique1
41 MODULE PROCEDURE find_boundary1, find_boundary2
60 PURE FUNCTION locate(array, x)
RESULT(x_index)
61 INTEGER,
DIMENSION(:),
INTENT(IN) :: array
62 INTEGER,
INTENT(IN) :: x
65 INTEGER :: jl, jm, ju, n
69 IF (x < array(1))
RETURN
71 IF (x > array(n))
RETURN
74 DO WHILE (ju - jl > 1)
76 IF (x >= array(jm))
THEN
82 IF (x == array(jl)) x_index = jl
92 SUBROUTINE sort_unique1(arr, unique)
93 INTEGER,
DIMENSION(:),
INTENT(INOUT) :: arr
94 LOGICAL,
INTENT(OUT) :: unique
97 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: wrk
102 CALL sort(arr, n, wrk)
104 IF (arr(i) == arr(i - 1))
THEN
110 END SUBROUTINE sort_unique1
119 SUBROUTINE sort_cv(arr, n, index)
120 INTEGER,
INTENT(IN) :: n
121 CHARACTER(LEN=*),
INTENT(INOUT) :: arr(1:n)
122 INTEGER,
INTENT(OUT) :: INDEX(1:n)
124 INTEGER :: i, j, max_length
125 INTEGER,
ALLOCATABLE,
DIMENSION(:, :) :: entries
129 max_length = max(max_length, len_trim(arr(i)))
131 ALLOCATE (entries(max_length,
SIZE(arr)))
133 DO j = 1, len_trim(arr(i))
134 entries(j, i) = ichar(arr(i) (j:j))
136 IF (j <= max_length)
THEN
137 entries(j:max_length, i) = ichar(
" ")
140 CALL sort_im(entries, istart=1, iend=n, j=1, jsize=max_length, index=index)
144 arr(i) (j:j) = char(entries(j, i))
148 END SUBROUTINE sort_cv
160 RECURSIVE SUBROUTINE sort_im(matrix, istart, iend, j, jsize, index)
161 INTEGER,
DIMENSION(:, :),
INTENT(INOUT) :: matrix
162 INTEGER,
INTENT(IN) :: istart, iend, j, jsize
163 INTEGER,
DIMENSION(:),
INTENT(INOUT) :: index
167 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: work, work2
168 INTEGER :: i, ind, isize, k, kend, kstart
169 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: bck_index, tmp_index
171 isize = iend - istart + 1
180 ALLOCATE (work(isize), work2(isize), tmp_index(isize), bck_index(isize))
184 work(ind) = matrix(j, i)
185 bck_index(ind) = index(i)
189 CALL sort(work, isize, tmp_index)
195 index(i) = bck_index(tmp_index(ind))
196 matrix(j, i) = work(ind)
204 work2(ind) = matrix(k, i)
209 matrix(k, i) = work2(tmp_index(ind))
220 IF (item /= work(ind))
THEN
222 IF (kstart /= kend)
THEN
223 CALL sort(matrix, kstart, kend, j + 1, jsize, index)
230 IF (kstart /= kend)
THEN
231 CALL sort(matrix, kstart, kend, j + 1, jsize, index)
234 DEALLOCATE (work, work2, tmp_index, bck_index)
236 END SUBROUTINE sort_im
247 RECURSIVE SUBROUTINE sort_cm(matrix, istart, iend, j, jsize, index)
248 CHARACTER(LEN=*),
DIMENSION(:, :),
INTENT(INOUT) :: matrix
249 INTEGER,
INTENT(IN) :: istart, iend, j, jsize
250 INTEGER,
DIMENSION(:),
INTENT(INOUT) :: index
253 CHARACTER(LEN=LEN(matrix)) :: item
254 CHARACTER(LEN=LEN(matrix)),
ALLOCATABLE,
DIMENSION(:) :: work, work2
255 INTEGER :: i, ind, isize, k, kend, kstart
256 INTEGER,
ALLOCATABLE,
DIMENSION(:) :: bck_index, tmp_index
258 isize = iend - istart + 1
267 ALLOCATE (work(isize), work2(isize), tmp_index(isize), bck_index(isize))
271 work(ind) = matrix(j, i)
272 bck_index(ind) = index(i)
276 CALL sort(work, isize, tmp_index)
282 index(i) = bck_index(tmp_index(ind))
283 matrix(j, i) = work(ind)
291 work2(ind) = matrix(k, i)
296 matrix(k, i) = work2(tmp_index(ind))
307 IF (item /= work(ind))
THEN
309 IF (kstart /= kend)
THEN
310 CALL sort(matrix, kstart, kend, j + 1, jsize, index)
317 IF (kstart /= kend)
THEN
318 CALL sort(matrix, kstart, kend, j + 1, jsize, index)
321 DEALLOCATE (work, work2, tmp_index, bck_index)
323 END SUBROUTINE sort_cm
333 INTEGER,
INTENT(IN) :: m, n, me
337 REAL(kind=
dp) :: part
339 part = real(m, kind=
dp)/real(n, kind=
dp)
340 nl = nint(real(me, kind=
dp)*part) + 1
341 nu = nint(real(me + 1, kind=
dp)*part)
358 PURE SUBROUTINE find_boundary1(num_array, ntot, first, last, search)
359 INTEGER,
POINTER :: num_array(:)
360 INTEGER,
INTENT(IN) :: ntot
361 INTEGER,
INTENT(OUT) :: first, last
362 INTEGER,
INTENT(IN) :: search
372 IF (num_array(i) == search)
THEN
373 IF (.NOT. found)
THEN
386 END SUBROUTINE find_boundary1
403 PURE SUBROUTINE find_boundary2(num_array1, num_array2, ntot, first, last, search1, search2)
404 INTEGER,
POINTER :: num_array1(:), num_array2(:)
405 INTEGER,
INTENT(IN) :: ntot
406 INTEGER,
INTENT(OUT) :: first, last
407 INTEGER,
INTENT(IN) :: search1, search2
409 INTEGER :: i, tfirst, tlast
419 IF (num_array2(i) == search2)
THEN
420 IF (.NOT. found)
THEN
433 END SUBROUTINE find_boundary2
Routine for sorting an array.
subroutine, public cp_1d_i4_sort(arr, n, indices)
Sorts an array inplace using a combination of merge- and bubble-sort. It also returns the indices,...
subroutine, public cp_1d_i8_sort(arr, n, indices)
Sorts an array inplace using a combination of merge- and bubble-sort. It also returns the indices,...
subroutine, public cp_1d_s_sort(arr, n, indices)
Sorts an array inplace using a combination of merge- and bubble-sort. It also returns the indices,...
subroutine, public cp_1d_r_sort(arr, n, indices)
Sorts an array inplace using a combination of merge- and bubble-sort. It also returns the indices,...
Defines the basic variable types.
integer, parameter, public dp
All kind of helpful little routines.
pure integer function, public locate(array, x)
Purpose: Given an array array(1:n), and given a value x, a value x_index is returned which is the ind...
pure integer function, dimension(2), public get_limit(m, n, me)
divide m entries into n parts, return size of part me