Statistiques
| Révision :

root / src / m_mrgrnk.f90 @ 3

Historique | Voir | Annoter | Télécharger (17,68 ko)

1 1 pfleura2
!!! Downloaded from http://www.fortran-2000.com/rank/index.html
2 1 pfleura2
!!! on October 13th, 2010
3 1 pfleura2
4 1 pfleura2
Module m_mrgrnk
5 1 pfleura2
! PFL 2010 Oct 13 ->
6 1 pfleura2
!Integer, Parameter :: kdp = selected_real_kind(15)
7 1 pfleura2
Integer, Parameter :: kdp = KIND(1.0D0)
8 1 pfleura2
! <- PFL 2010 Oct 13
9 1 pfleura2
public :: mrgrnk
10 1 pfleura2
private :: kdp
11 1 pfleura2
private :: R_mrgrnk, I_mrgrnk, D_mrgrnk
12 1 pfleura2
interface mrgrnk
13 1 pfleura2
  module procedure D_mrgrnk, R_mrgrnk, I_mrgrnk
14 1 pfleura2
end interface mrgrnk
15 1 pfleura2
contains
16 1 pfleura2
17 1 pfleura2
 Subroutine D_mrgrnk (XDONT, IRNGT)
18 1 pfleura2
! Subroutine Dmrgrnk (XDONT, IRNGT)
19 1 pfleura2
! __________________________________________________________
20 1 pfleura2
!   MRGRNK = Merge-sort ranking of an array
21 1 pfleura2
!   For performance reasons, the first 2 passes are taken
22 1 pfleura2
!   out of the standard loop, and use dedicated coding.
23 1 pfleura2
! __________________________________________________________
24 1 pfleura2
! __________________________________________________________
25 1 pfleura2
26 1 pfleura2
   use VarTypes
27 1 pfleura2
28 1 pfleura2
   IMPLICIT NONE
29 1 pfleura2
30 1 pfleura2
31 1 pfleura2
  interface
32 1 pfleura2
     function valid(string) result (isValid)
33 1 pfleura2
       logical                  :: isValid
34 1 pfleura2
       character(*), intent(in) :: string
35 1 pfleura2
     end function valid
36 1 pfleura2
  end interface
37 1 pfleura2
38 1 pfleura2
39 1 pfleura2
40 1 pfleura2
      Real (KREAL), Dimension (:), Intent (In) :: XDONT
41 1 pfleura2
      Integer(KINT), Dimension (:), Intent (Out) :: IRNGT
42 1 pfleura2
! __________________________________________________________
43 1 pfleura2
      Real (KREAL) :: XVALA, XVALB
44 1 pfleura2
!
45 1 pfleura2
      Integer(KINT), ALLOCATABLE :: JWRKT(:) ! SIZE(IRNGT)
46 1 pfleura2
      Integer(KINT) :: LMTNA, LMTNC, IRNG1, IRNG2
47 1 pfleura2
      Integer(KINT) :: NVAL, IIND, IWRKD, IWRK, IWRKF, JINDA, IINDA, IINDB
48 1 pfleura2
49 1 pfleura2
      LOGICAL :: Debug
50 1 pfleura2
51 1 pfleura2
      Debug=Valid("MRGRNK")
52 1 pfleura2
53 1 pfleura2
  if (debug) WRITE(*,*) "================================= Entering DMRGRNK ==============="
54 1 pfleura2
!
55 1 pfleura2
      NVAL = Min (SIZE(XDONT), SIZE(IRNGT))
56 1 pfleura2
      if (debug) WRITE(*,*) "D_mrgrnk",NVAL, Size(XDONT),SIZE(IRNGT)
57 1 pfleura2
      Select Case (NVAL)
58 1 pfleura2
      Case (:0)
59 1 pfleura2
         Return
60 1 pfleura2
      Case (1)
61 1 pfleura2
         IRNGT (1) = 1
62 1 pfleura2
         Return
63 1 pfleura2
      Case Default
64 1 pfleura2
         Continue
65 1 pfleura2
      End Select
66 1 pfleura2
67 1 pfleura2
      if (debug) WRITE(*,*) "D_mrgrnk, XDONT",XDONT
68 1 pfleura2
      if (debug) WRITE(*,*) "D_mrgrnk, IRNGT",IRNGT
69 1 pfleura2
70 1 pfleura2
      ALLOCATE(JWRKT(SIZE(IRNGT)))
71 1 pfleura2
!
72 1 pfleura2
!  Fill-in the index array, creating ordered couples
73 1 pfleura2
!
74 1 pfleura2
      Do IIND = 2, NVAL, 2
75 1 pfleura2
         If (XDONT(IIND-1) <= XDONT(IIND)) Then
76 1 pfleura2
            IRNGT (IIND-1) = IIND - 1
77 1 pfleura2
            IRNGT (IIND) = IIND
78 1 pfleura2
         Else
79 1 pfleura2
            IRNGT (IIND-1) = IIND
80 1 pfleura2
            IRNGT (IIND) = IIND - 1
81 1 pfleura2
         End If
82 1 pfleura2
      End Do
83 1 pfleura2
      If (Modulo(NVAL, 2) /= 0) Then
84 1 pfleura2
         IRNGT (NVAL) = NVAL
85 1 pfleura2
      End If
86 1 pfleura2
!
87 1 pfleura2
!  We will now have ordered subsets A - B - A - B - ...
88 1 pfleura2
!  and merge A and B couples into     C   -   C   - ...
89 1 pfleura2
!
90 1 pfleura2
      LMTNA = 2
91 1 pfleura2
      LMTNC = 4
92 1 pfleura2
!
93 1 pfleura2
!  First iteration. The length of the ordered subsets goes from 2 to 4
94 1 pfleura2
!
95 1 pfleura2
      Do
96 1 pfleura2
         If (NVAL <= 2) Exit
97 1 pfleura2
!
98 1 pfleura2
!   Loop on merges of A and B into C
99 1 pfleura2
!
100 1 pfleura2
         Do IWRKD = 0, NVAL - 1, 4
101 1 pfleura2
            If ((IWRKD+4) > NVAL) Then
102 1 pfleura2
               If ((IWRKD+2) >= NVAL) Exit
103 1 pfleura2
!
104 1 pfleura2
!   1 2 3
105 1 pfleura2
!
106 1 pfleura2
               If (XDONT(IRNGT(IWRKD+2)) <= XDONT(IRNGT(IWRKD+3))) Exit
107 1 pfleura2
!
108 1 pfleura2
!   1 3 2
109 1 pfleura2
!
110 1 pfleura2
               If (XDONT(IRNGT(IWRKD+1)) <= XDONT(IRNGT(IWRKD+3))) Then
111 1 pfleura2
                  IRNG2 = IRNGT (IWRKD+2)
112 1 pfleura2
                  IRNGT (IWRKD+2) = IRNGT (IWRKD+3)
113 1 pfleura2
                  IRNGT (IWRKD+3) = IRNG2
114 1 pfleura2
!
115 1 pfleura2
!   3 1 2
116 1 pfleura2
!
117 1 pfleura2
               Else
118 1 pfleura2
                  IRNG1 = IRNGT (IWRKD+1)
119 1 pfleura2
                  IRNGT (IWRKD+1) = IRNGT (IWRKD+3)
120 1 pfleura2
                  IRNGT (IWRKD+3) = IRNGT (IWRKD+2)
121 1 pfleura2
                  IRNGT (IWRKD+2) = IRNG1
122 1 pfleura2
               End If
123 1 pfleura2
               Exit
124 1 pfleura2
            End If
125 1 pfleura2
!
126 1 pfleura2
!   1 2 3 4
127 1 pfleura2
!
128 1 pfleura2
            If (XDONT(IRNGT(IWRKD+2)) <= XDONT(IRNGT(IWRKD+3))) Cycle
129 1 pfleura2
!
130 1 pfleura2
!   1 3 x x
131 1 pfleura2
!
132 1 pfleura2
            If (XDONT(IRNGT(IWRKD+1)) <= XDONT(IRNGT(IWRKD+3))) Then
133 1 pfleura2
               IRNG2 = IRNGT (IWRKD+2)
134 1 pfleura2
               IRNGT (IWRKD+2) = IRNGT (IWRKD+3)
135 1 pfleura2
               If (XDONT(IRNG2) <= XDONT(IRNGT(IWRKD+4))) Then
136 1 pfleura2
!   1 3 2 4
137 1 pfleura2
                  IRNGT (IWRKD+3) = IRNG2
138 1 pfleura2
               Else
139 1 pfleura2
!   1 3 4 2
140 1 pfleura2
                  IRNGT (IWRKD+3) = IRNGT (IWRKD+4)
141 1 pfleura2
                  IRNGT (IWRKD+4) = IRNG2
142 1 pfleura2
               End If
143 1 pfleura2
!
144 1 pfleura2
!   3 x x x
145 1 pfleura2
!
146 1 pfleura2
            Else
147 1 pfleura2
               IRNG1 = IRNGT (IWRKD+1)
148 1 pfleura2
               IRNG2 = IRNGT (IWRKD+2)
149 1 pfleura2
               IRNGT (IWRKD+1) = IRNGT (IWRKD+3)
150 1 pfleura2
               If (XDONT(IRNG1) <= XDONT(IRNGT(IWRKD+4))) Then
151 1 pfleura2
                  IRNGT (IWRKD+2) = IRNG1
152 1 pfleura2
                  If (XDONT(IRNG2) <= XDONT(IRNGT(IWRKD+4))) Then
153 1 pfleura2
!   3 1 2 4
154 1 pfleura2
                     IRNGT (IWRKD+3) = IRNG2
155 1 pfleura2
                  Else
156 1 pfleura2
!   3 1 4 2
157 1 pfleura2
                     IRNGT (IWRKD+3) = IRNGT (IWRKD+4)
158 1 pfleura2
                     IRNGT (IWRKD+4) = IRNG2
159 1 pfleura2
                  End If
160 1 pfleura2
               Else
161 1 pfleura2
!   3 4 1 2
162 1 pfleura2
                  IRNGT (IWRKD+2) = IRNGT (IWRKD+4)
163 1 pfleura2
                  IRNGT (IWRKD+3) = IRNG1
164 1 pfleura2
                  IRNGT (IWRKD+4) = IRNG2
165 1 pfleura2
               End If
166 1 pfleura2
            End If
167 1 pfleura2
         End Do
168 1 pfleura2
!
169 1 pfleura2
!  The Cs become As and Bs
170 1 pfleura2
!
171 1 pfleura2
         LMTNA = 4
172 1 pfleura2
         Exit
173 1 pfleura2
      End Do
174 1 pfleura2
!
175 1 pfleura2
!  Iteration loop. Each time, the length of the ordered subsets
176 1 pfleura2
!  is doubled.
177 1 pfleura2
!
178 1 pfleura2
      Do
179 1 pfleura2
         If (LMTNA >= NVAL) Exit
180 1 pfleura2
         IWRKF = 0
181 1 pfleura2
         LMTNC = 2 * LMTNC
182 1 pfleura2
!
183 1 pfleura2
!   Loop on merges of A and B into C
184 1 pfleura2
!
185 1 pfleura2
         Do
186 1 pfleura2
            IWRK = IWRKF
187 1 pfleura2
            IWRKD = IWRKF + 1
188 1 pfleura2
            JINDA = IWRKF + LMTNA
189 1 pfleura2
            IWRKF = IWRKF + LMTNC
190 1 pfleura2
            If (IWRKF >= NVAL) Then
191 1 pfleura2
               If (JINDA >= NVAL) Exit
192 1 pfleura2
               IWRKF = NVAL
193 1 pfleura2
            End If
194 1 pfleura2
            IINDA = 1
195 1 pfleura2
            IINDB = JINDA + 1
196 1 pfleura2
!
197 1 pfleura2
!   Shortcut for the case when the max of A is smaller
198 1 pfleura2
!   than the min of B. This line may be activated when the
199 1 pfleura2
!   initial set is already close to sorted.
200 1 pfleura2
!
201 1 pfleura2
!          IF (XDONT(IRNGT(JINDA)) <= XDONT(IRNGT(IINDB))) CYCLE
202 1 pfleura2
!
203 1 pfleura2
!  One steps in the C subset, that we build in the final rank array
204 1 pfleura2
!
205 1 pfleura2
!  Make a copy of the rank array for the merge iteration
206 1 pfleura2
!
207 1 pfleura2
            JWRKT (1:LMTNA) = IRNGT (IWRKD:JINDA)
208 1 pfleura2
!
209 1 pfleura2
            XVALA = XDONT (JWRKT(IINDA))
210 1 pfleura2
            XVALB = XDONT (IRNGT(IINDB))
211 1 pfleura2
!
212 1 pfleura2
            Do
213 1 pfleura2
               IWRK = IWRK + 1
214 1 pfleura2
!
215 1 pfleura2
!  We still have unprocessed values in both A and B
216 1 pfleura2
!
217 1 pfleura2
               If (XVALA > XVALB) Then
218 1 pfleura2
                  IRNGT (IWRK) = IRNGT (IINDB)
219 1 pfleura2
                  IINDB = IINDB + 1
220 1 pfleura2
                  If (IINDB > IWRKF) Then
221 1 pfleura2
!  Only A still with unprocessed values
222 1 pfleura2
                     IRNGT (IWRK+1:IWRKF) = JWRKT (IINDA:LMTNA)
223 1 pfleura2
                     Exit
224 1 pfleura2
                  End If
225 1 pfleura2
                  XVALB = XDONT (IRNGT(IINDB))
226 1 pfleura2
               Else
227 1 pfleura2
                  IRNGT (IWRK) = JWRKT (IINDA)
228 1 pfleura2
                  IINDA = IINDA + 1
229 1 pfleura2
                  If (IINDA > LMTNA) Exit! Only B still with unprocessed values
230 1 pfleura2
                  XVALA = XDONT (JWRKT(IINDA))
231 1 pfleura2
               End If
232 1 pfleura2
!
233 1 pfleura2
            End Do
234 1 pfleura2
         End Do
235 1 pfleura2
!
236 1 pfleura2
!  The Cs become As and Bs
237 1 pfleura2
!
238 1 pfleura2
         LMTNA = 2 * LMTNA
239 1 pfleura2
      End Do
240 1 pfleura2
241 1 pfleura2
      DEALLOCATE(JWRKT)
242 1 pfleura2
  if (debug) WRITE(*,*) "================================= Exiting DMRGRNK ==============="
243 1 pfleura2
!
244 1 pfleura2
      Return
245 1 pfleura2
!
246 1 pfleura2
    End Subroutine D_mrgrnk
247 1 pfleura2
248 1 pfleura2
Subroutine R_mrgrnk (XDONT, IRNGT)
249 1 pfleura2
! __________________________________________________________
250 1 pfleura2
!   MRGRNK = Merge-sort ranking of an array
251 1 pfleura2
!   For performance reasons, the first 2 passes are taken
252 1 pfleura2
!   out of the standard loop, and use dedicated coding.
253 1 pfleura2
! __________________________________________________________
254 1 pfleura2
! _________________________________________________________
255 1 pfleura2
      Real, Dimension (:), Intent (In) :: XDONT
256 1 pfleura2
      Integer, Dimension (:), Intent (Out) :: IRNGT
257 1 pfleura2
! __________________________________________________________
258 1 pfleura2
      Real :: XVALA, XVALB
259 1 pfleura2
!
260 1 pfleura2
      Integer, Dimension (SIZE(IRNGT)) :: JWRKT
261 1 pfleura2
      Integer :: LMTNA, LMTNC, IRNG1, IRNG2
262 1 pfleura2
      Integer :: NVAL, IIND, IWRKD, IWRK, IWRKF, JINDA, IINDA, IINDB
263 1 pfleura2
!
264 1 pfleura2
265 1 pfleura2
      NVAL = Min (SIZE(XDONT), SIZE(IRNGT))
266 1 pfleura2
!      if (debug) WRITE(*,*) "R_MRGRNK:", NVAL
267 1 pfleura2
      Select Case (NVAL)
268 1 pfleura2
      Case (:0)
269 1 pfleura2
         Return
270 1 pfleura2
      Case (1)
271 1 pfleura2
         IRNGT (1) = 1
272 1 pfleura2
         Return
273 1 pfleura2
      Case Default
274 1 pfleura2
         Continue
275 1 pfleura2
      End Select
276 1 pfleura2
!
277 1 pfleura2
!  Fill-in the index array, creating ordered couples
278 1 pfleura2
!
279 1 pfleura2
      Do IIND = 2, NVAL, 2
280 1 pfleura2
         If (XDONT(IIND-1) <= XDONT(IIND)) Then
281 1 pfleura2
            IRNGT (IIND-1) = IIND - 1
282 1 pfleura2
            IRNGT (IIND) = IIND
283 1 pfleura2
         Else
284 1 pfleura2
            IRNGT (IIND-1) = IIND
285 1 pfleura2
            IRNGT (IIND) = IIND - 1
286 1 pfleura2
         End If
287 1 pfleura2
      End Do
288 1 pfleura2
      If (Modulo(NVAL, 2) /= 0) Then
289 1 pfleura2
         IRNGT (NVAL) = NVAL
290 1 pfleura2
      End If
291 1 pfleura2
!
292 1 pfleura2
!  We will now have ordered subsets A - B - A - B - ...
293 1 pfleura2
!  and merge A and B couples into     C   -   C   - ...
294 1 pfleura2
!
295 1 pfleura2
      LMTNA = 2
296 1 pfleura2
      LMTNC = 4
297 1 pfleura2
!
298 1 pfleura2
!  First iteration. The length of the ordered subsets goes from 2 to 4
299 1 pfleura2
!
300 1 pfleura2
      Do
301 1 pfleura2
         If (NVAL <= 2) Exit
302 1 pfleura2
!
303 1 pfleura2
!   Loop on merges of A and B into C
304 1 pfleura2
!
305 1 pfleura2
         Do IWRKD = 0, NVAL - 1, 4
306 1 pfleura2
            If ((IWRKD+4) > NVAL) Then
307 1 pfleura2
               If ((IWRKD+2) >= NVAL) Exit
308 1 pfleura2
!
309 1 pfleura2
!   1 2 3
310 1 pfleura2
!
311 1 pfleura2
               If (XDONT(IRNGT(IWRKD+2)) <= XDONT(IRNGT(IWRKD+3))) Exit
312 1 pfleura2
!
313 1 pfleura2
!   1 3 2
314 1 pfleura2
!
315 1 pfleura2
               If (XDONT(IRNGT(IWRKD+1)) <= XDONT(IRNGT(IWRKD+3))) Then
316 1 pfleura2
                  IRNG2 = IRNGT (IWRKD+2)
317 1 pfleura2
                  IRNGT (IWRKD+2) = IRNGT (IWRKD+3)
318 1 pfleura2
                  IRNGT (IWRKD+3) = IRNG2
319 1 pfleura2
!
320 1 pfleura2
!   3 1 2
321 1 pfleura2
!
322 1 pfleura2
               Else
323 1 pfleura2
                  IRNG1 = IRNGT (IWRKD+1)
324 1 pfleura2
                  IRNGT (IWRKD+1) = IRNGT (IWRKD+3)
325 1 pfleura2
                  IRNGT (IWRKD+3) = IRNGT (IWRKD+2)
326 1 pfleura2
                  IRNGT (IWRKD+2) = IRNG1
327 1 pfleura2
               End If
328 1 pfleura2
               Exit
329 1 pfleura2
            End If
330 1 pfleura2
!
331 1 pfleura2
!   1 2 3 4
332 1 pfleura2
!
333 1 pfleura2
            If (XDONT(IRNGT(IWRKD+2)) <= XDONT(IRNGT(IWRKD+3))) Cycle
334 1 pfleura2
!
335 1 pfleura2
!   1 3 x x
336 1 pfleura2
!
337 1 pfleura2
            If (XDONT(IRNGT(IWRKD+1)) <= XDONT(IRNGT(IWRKD+3))) Then
338 1 pfleura2
               IRNG2 = IRNGT (IWRKD+2)
339 1 pfleura2
               IRNGT (IWRKD+2) = IRNGT (IWRKD+3)
340 1 pfleura2
               If (XDONT(IRNG2) <= XDONT(IRNGT(IWRKD+4))) Then
341 1 pfleura2
!   1 3 2 4
342 1 pfleura2
                  IRNGT (IWRKD+3) = IRNG2
343 1 pfleura2
               Else
344 1 pfleura2
!   1 3 4 2
345 1 pfleura2
                  IRNGT (IWRKD+3) = IRNGT (IWRKD+4)
346 1 pfleura2
                  IRNGT (IWRKD+4) = IRNG2
347 1 pfleura2
               End If
348 1 pfleura2
!
349 1 pfleura2
!   3 x x x
350 1 pfleura2
!
351 1 pfleura2
            Else
352 1 pfleura2
               IRNG1 = IRNGT (IWRKD+1)
353 1 pfleura2
               IRNG2 = IRNGT (IWRKD+2)
354 1 pfleura2
               IRNGT (IWRKD+1) = IRNGT (IWRKD+3)
355 1 pfleura2
               If (XDONT(IRNG1) <= XDONT(IRNGT(IWRKD+4))) Then
356 1 pfleura2
                  IRNGT (IWRKD+2) = IRNG1
357 1 pfleura2
                  If (XDONT(IRNG2) <= XDONT(IRNGT(IWRKD+4))) Then
358 1 pfleura2
!   3 1 2 4
359 1 pfleura2
                     IRNGT (IWRKD+3) = IRNG2
360 1 pfleura2
                  Else
361 1 pfleura2
!   3 1 4 2
362 1 pfleura2
                     IRNGT (IWRKD+3) = IRNGT (IWRKD+4)
363 1 pfleura2
                     IRNGT (IWRKD+4) = IRNG2
364 1 pfleura2
                  End If
365 1 pfleura2
               Else
366 1 pfleura2
!   3 4 1 2
367 1 pfleura2
                  IRNGT (IWRKD+2) = IRNGT (IWRKD+4)
368 1 pfleura2
                  IRNGT (IWRKD+3) = IRNG1
369 1 pfleura2
                  IRNGT (IWRKD+4) = IRNG2
370 1 pfleura2
               End If
371 1 pfleura2
            End If
372 1 pfleura2
         End Do
373 1 pfleura2
!
374 1 pfleura2
!  The Cs become As and Bs
375 1 pfleura2
!
376 1 pfleura2
         LMTNA = 4
377 1 pfleura2
         Exit
378 1 pfleura2
      End Do
379 1 pfleura2
!
380 1 pfleura2
!  Iteration loop. Each time, the length of the ordered subsets
381 1 pfleura2
!  is doubled.
382 1 pfleura2
!
383 1 pfleura2
      Do
384 1 pfleura2
         If (LMTNA >= NVAL) Exit
385 1 pfleura2
         IWRKF = 0
386 1 pfleura2
         LMTNC = 2 * LMTNC
387 1 pfleura2
!
388 1 pfleura2
!   Loop on merges of A and B into C
389 1 pfleura2
!
390 1 pfleura2
         Do
391 1 pfleura2
            IWRK = IWRKF
392 1 pfleura2
            IWRKD = IWRKF + 1
393 1 pfleura2
            JINDA = IWRKF + LMTNA
394 1 pfleura2
            IWRKF = IWRKF + LMTNC
395 1 pfleura2
            If (IWRKF >= NVAL) Then
396 1 pfleura2
               If (JINDA >= NVAL) Exit
397 1 pfleura2
               IWRKF = NVAL
398 1 pfleura2
            End If
399 1 pfleura2
            IINDA = 1
400 1 pfleura2
            IINDB = JINDA + 1
401 1 pfleura2
!
402 1 pfleura2
!   Shortcut for the case when the max of A is smaller
403 1 pfleura2
!   than the min of B. This line may be activated when the
404 1 pfleura2
!   initial set is already close to sorted.
405 1 pfleura2
!
406 1 pfleura2
!          IF (XDONT(IRNGT(JINDA)) <= XDONT(IRNGT(IINDB))) CYCLE
407 1 pfleura2
!
408 1 pfleura2
!  One steps in the C subset, that we build in the final rank array
409 1 pfleura2
!
410 1 pfleura2
!  Make a copy of the rank array for the merge iteration
411 1 pfleura2
!
412 1 pfleura2
            JWRKT (1:LMTNA) = IRNGT (IWRKD:JINDA)
413 1 pfleura2
!
414 1 pfleura2
            XVALA = XDONT (JWRKT(IINDA))
415 1 pfleura2
            XVALB = XDONT (IRNGT(IINDB))
416 1 pfleura2
!
417 1 pfleura2
            Do
418 1 pfleura2
               IWRK = IWRK + 1
419 1 pfleura2
!
420 1 pfleura2
!  We still have unprocessed values in both A and B
421 1 pfleura2
!
422 1 pfleura2
               If (XVALA > XVALB) Then
423 1 pfleura2
                  IRNGT (IWRK) = IRNGT (IINDB)
424 1 pfleura2
                  IINDB = IINDB + 1
425 1 pfleura2
                  If (IINDB > IWRKF) Then
426 1 pfleura2
!  Only A still with unprocessed values
427 1 pfleura2
                     IRNGT (IWRK+1:IWRKF) = JWRKT (IINDA:LMTNA)
428 1 pfleura2
                     Exit
429 1 pfleura2
                  End If
430 1 pfleura2
                  XVALB = XDONT (IRNGT(IINDB))
431 1 pfleura2
               Else
432 1 pfleura2
                  IRNGT (IWRK) = JWRKT (IINDA)
433 1 pfleura2
                  IINDA = IINDA + 1
434 1 pfleura2
                  If (IINDA > LMTNA) Exit! Only B still with unprocessed values
435 1 pfleura2
                  XVALA = XDONT (JWRKT(IINDA))
436 1 pfleura2
               End If
437 1 pfleura2
!
438 1 pfleura2
            End Do
439 1 pfleura2
         End Do
440 1 pfleura2
!
441 1 pfleura2
!  The Cs become As and Bs
442 1 pfleura2
!
443 1 pfleura2
         LMTNA = 2 * LMTNA
444 1 pfleura2
      End Do
445 1 pfleura2
!
446 1 pfleura2
      Return
447 1 pfleura2
!
448 1 pfleura2
End Subroutine R_mrgrnk
449 1 pfleura2
Subroutine I_mrgrnk (XDONT, IRNGT)
450 1 pfleura2
! __________________________________________________________
451 1 pfleura2
!   MRGRNK = Merge-sort ranking of an array
452 1 pfleura2
!   For performance reasons, the first 2 passes are taken
453 1 pfleura2
!   out of the standard loop, and use dedicated coding.
454 1 pfleura2
! __________________________________________________________
455 1 pfleura2
! __________________________________________________________
456 1 pfleura2
      Integer, Dimension (:), Intent (In)  :: XDONT
457 1 pfleura2
      Integer, Dimension (:), Intent (Out) :: IRNGT
458 1 pfleura2
! __________________________________________________________
459 1 pfleura2
      Integer :: XVALA, XVALB
460 1 pfleura2
!
461 1 pfleura2
      Integer, Dimension (SIZE(IRNGT)) :: JWRKT
462 1 pfleura2
      Integer :: LMTNA, LMTNC, IRNG1, IRNG2
463 1 pfleura2
      Integer :: NVAL, IIND, IWRKD, IWRK, IWRKF, JINDA, IINDA, IINDB
464 1 pfleura2
!
465 1 pfleura2
      NVAL = Min (SIZE(XDONT), SIZE(IRNGT))
466 1 pfleura2
!      WRITE(*,*) "I_mrgrnk",NVAL
467 1 pfleura2
      Select Case (NVAL)
468 1 pfleura2
      Case (:0)
469 1 pfleura2
         Return
470 1 pfleura2
      Case (1)
471 1 pfleura2
         IRNGT (1) = 1
472 1 pfleura2
         Return
473 1 pfleura2
      Case Default
474 1 pfleura2
         Continue
475 1 pfleura2
      End Select
476 1 pfleura2
!
477 1 pfleura2
!  Fill-in the index array, creating ordered couples
478 1 pfleura2
!
479 1 pfleura2
      Do IIND = 2, NVAL, 2
480 1 pfleura2
         If (XDONT(IIND-1) <= XDONT(IIND)) Then
481 1 pfleura2
            IRNGT (IIND-1) = IIND - 1
482 1 pfleura2
            IRNGT (IIND) = IIND
483 1 pfleura2
         Else
484 1 pfleura2
            IRNGT (IIND-1) = IIND
485 1 pfleura2
            IRNGT (IIND) = IIND - 1
486 1 pfleura2
         End If
487 1 pfleura2
      End Do
488 1 pfleura2
      If (Modulo(NVAL, 2) /= 0) Then
489 1 pfleura2
         IRNGT (NVAL) = NVAL
490 1 pfleura2
      End If
491 1 pfleura2
!
492 1 pfleura2
!  We will now have ordered subsets A - B - A - B - ...
493 1 pfleura2
!  and merge A and B couples into     C   -   C   - ...
494 1 pfleura2
!
495 1 pfleura2
      LMTNA = 2
496 1 pfleura2
      LMTNC = 4
497 1 pfleura2
!
498 1 pfleura2
!  First iteration. The length of the ordered subsets goes from 2 to 4
499 1 pfleura2
!
500 1 pfleura2
      Do
501 1 pfleura2
         If (NVAL <= 2) Exit
502 1 pfleura2
!
503 1 pfleura2
!   Loop on merges of A and B into C
504 1 pfleura2
!
505 1 pfleura2
         Do IWRKD = 0, NVAL - 1, 4
506 1 pfleura2
            If ((IWRKD+4) > NVAL) Then
507 1 pfleura2
               If ((IWRKD+2) >= NVAL) Exit
508 1 pfleura2
!
509 1 pfleura2
!   1 2 3
510 1 pfleura2
!
511 1 pfleura2
               If (XDONT(IRNGT(IWRKD+2)) <= XDONT(IRNGT(IWRKD+3))) Exit
512 1 pfleura2
!
513 1 pfleura2
!   1 3 2
514 1 pfleura2
!
515 1 pfleura2
               If (XDONT(IRNGT(IWRKD+1)) <= XDONT(IRNGT(IWRKD+3))) Then
516 1 pfleura2
                  IRNG2 = IRNGT (IWRKD+2)
517 1 pfleura2
                  IRNGT (IWRKD+2) = IRNGT (IWRKD+3)
518 1 pfleura2
                  IRNGT (IWRKD+3) = IRNG2
519 1 pfleura2
!
520 1 pfleura2
!   3 1 2
521 1 pfleura2
!
522 1 pfleura2
               Else
523 1 pfleura2
                  IRNG1 = IRNGT (IWRKD+1)
524 1 pfleura2
                  IRNGT (IWRKD+1) = IRNGT (IWRKD+3)
525 1 pfleura2
                  IRNGT (IWRKD+3) = IRNGT (IWRKD+2)
526 1 pfleura2
                  IRNGT (IWRKD+2) = IRNG1
527 1 pfleura2
               End If
528 1 pfleura2
               Exit
529 1 pfleura2
            End If
530 1 pfleura2
!
531 1 pfleura2
!   1 2 3 4
532 1 pfleura2
!
533 1 pfleura2
            If (XDONT(IRNGT(IWRKD+2)) <= XDONT(IRNGT(IWRKD+3))) Cycle
534 1 pfleura2
!
535 1 pfleura2
!   1 3 x x
536 1 pfleura2
!
537 1 pfleura2
            If (XDONT(IRNGT(IWRKD+1)) <= XDONT(IRNGT(IWRKD+3))) Then
538 1 pfleura2
               IRNG2 = IRNGT (IWRKD+2)
539 1 pfleura2
               IRNGT (IWRKD+2) = IRNGT (IWRKD+3)
540 1 pfleura2
               If (XDONT(IRNG2) <= XDONT(IRNGT(IWRKD+4))) Then
541 1 pfleura2
!   1 3 2 4
542 1 pfleura2
                  IRNGT (IWRKD+3) = IRNG2
543 1 pfleura2
               Else
544 1 pfleura2
!   1 3 4 2
545 1 pfleura2
                  IRNGT (IWRKD+3) = IRNGT (IWRKD+4)
546 1 pfleura2
                  IRNGT (IWRKD+4) = IRNG2
547 1 pfleura2
               End If
548 1 pfleura2
!
549 1 pfleura2
!   3 x x x
550 1 pfleura2
!
551 1 pfleura2
            Else
552 1 pfleura2
               IRNG1 = IRNGT (IWRKD+1)
553 1 pfleura2
               IRNG2 = IRNGT (IWRKD+2)
554 1 pfleura2
               IRNGT (IWRKD+1) = IRNGT (IWRKD+3)
555 1 pfleura2
               If (XDONT(IRNG1) <= XDONT(IRNGT(IWRKD+4))) Then
556 1 pfleura2
                  IRNGT (IWRKD+2) = IRNG1
557 1 pfleura2
                  If (XDONT(IRNG2) <= XDONT(IRNGT(IWRKD+4))) Then
558 1 pfleura2
!   3 1 2 4
559 1 pfleura2
                     IRNGT (IWRKD+3) = IRNG2
560 1 pfleura2
                  Else
561 1 pfleura2
!   3 1 4 2
562 1 pfleura2
                     IRNGT (IWRKD+3) = IRNGT (IWRKD+4)
563 1 pfleura2
                     IRNGT (IWRKD+4) = IRNG2
564 1 pfleura2
                  End If
565 1 pfleura2
               Else
566 1 pfleura2
!   3 4 1 2
567 1 pfleura2
                  IRNGT (IWRKD+2) = IRNGT (IWRKD+4)
568 1 pfleura2
                  IRNGT (IWRKD+3) = IRNG1
569 1 pfleura2
                  IRNGT (IWRKD+4) = IRNG2
570 1 pfleura2
               End If
571 1 pfleura2
            End If
572 1 pfleura2
         End Do
573 1 pfleura2
!
574 1 pfleura2
!  The Cs become As and Bs
575 1 pfleura2
!
576 1 pfleura2
         LMTNA = 4
577 1 pfleura2
         Exit
578 1 pfleura2
      End Do
579 1 pfleura2
!
580 1 pfleura2
!  Iteration loop. Each time, the length of the ordered subsets
581 1 pfleura2
!  is doubled.
582 1 pfleura2
!
583 1 pfleura2
      Do
584 1 pfleura2
         If (LMTNA >= NVAL) Exit
585 1 pfleura2
         IWRKF = 0
586 1 pfleura2
         LMTNC = 2 * LMTNC
587 1 pfleura2
!
588 1 pfleura2
!   Loop on merges of A and B into C
589 1 pfleura2
!
590 1 pfleura2
         Do
591 1 pfleura2
            IWRK = IWRKF
592 1 pfleura2
            IWRKD = IWRKF + 1
593 1 pfleura2
            JINDA = IWRKF + LMTNA
594 1 pfleura2
            IWRKF = IWRKF + LMTNC
595 1 pfleura2
            If (IWRKF >= NVAL) Then
596 1 pfleura2
               If (JINDA >= NVAL) Exit
597 1 pfleura2
               IWRKF = NVAL
598 1 pfleura2
            End If
599 1 pfleura2
            IINDA = 1
600 1 pfleura2
            IINDB = JINDA + 1
601 1 pfleura2
!
602 1 pfleura2
!   Shortcut for the case when the max of A is smaller
603 1 pfleura2
!   than the min of B. This line may be activated when the
604 1 pfleura2
!   initial set is already close to sorted.
605 1 pfleura2
!
606 1 pfleura2
!          IF (XDONT(IRNGT(JINDA)) <= XDONT(IRNGT(IINDB))) CYCLE
607 1 pfleura2
!
608 1 pfleura2
!  One steps in the C subset, that we build in the final rank array
609 1 pfleura2
!
610 1 pfleura2
!  Make a copy of the rank array for the merge iteration
611 1 pfleura2
!
612 1 pfleura2
            JWRKT (1:LMTNA) = IRNGT (IWRKD:JINDA)
613 1 pfleura2
!
614 1 pfleura2
            XVALA = XDONT (JWRKT(IINDA))
615 1 pfleura2
            XVALB = XDONT (IRNGT(IINDB))
616 1 pfleura2
!
617 1 pfleura2
            Do
618 1 pfleura2
               IWRK = IWRK + 1
619 1 pfleura2
!
620 1 pfleura2
!  We still have unprocessed values in both A and B
621 1 pfleura2
!
622 1 pfleura2
               If (XVALA > XVALB) Then
623 1 pfleura2
                  IRNGT (IWRK) = IRNGT (IINDB)
624 1 pfleura2
                  IINDB = IINDB + 1
625 1 pfleura2
                  If (IINDB > IWRKF) Then
626 1 pfleura2
!  Only A still with unprocessed values
627 1 pfleura2
                     IRNGT (IWRK+1:IWRKF) = JWRKT (IINDA:LMTNA)
628 1 pfleura2
                     Exit
629 1 pfleura2
                  End If
630 1 pfleura2
                  XVALB = XDONT (IRNGT(IINDB))
631 1 pfleura2
               Else
632 1 pfleura2
                  IRNGT (IWRK) = JWRKT (IINDA)
633 1 pfleura2
                  IINDA = IINDA + 1
634 1 pfleura2
                  If (IINDA > LMTNA) Exit! Only B still with unprocessed values
635 1 pfleura2
                  XVALA = XDONT (JWRKT(IINDA))
636 1 pfleura2
               End If
637 1 pfleura2
!
638 1 pfleura2
            End Do
639 1 pfleura2
         End Do
640 1 pfleura2
!
641 1 pfleura2
!  The Cs become As and Bs
642 1 pfleura2
!
643 1 pfleura2
         LMTNA = 2 * LMTNA
644 1 pfleura2
      End Do
645 1 pfleura2
!
646 1 pfleura2
      Return
647 1 pfleura2
!
648 1 pfleura2
End Subroutine I_mrgrnk
649 1 pfleura2
end module m_mrgrnk