-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsort_tools.f90
491 lines (474 loc) · 16 KB
/
sort_tools.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
module sort_tools
!
! Very simple sorting tools. We are using merge sort, which has guaranteed
! O(N log(N)) scaling and optimal (sequential) memory access pattern.
! The code is modification of a subroutine from "sparse.f90"
!
use accuracy
implicit none
private
public sort, sort_unique, order_keys
public rcsid_sort_tools
interface sort
module procedure sort_integer
module procedure sort_real
module procedure sort_and_order_integer
module procedure sort_and_order_real
!*qd module procedure sort_quad
end interface sort
interface sort_unique
module procedure sort_unique_integer
end interface sort_unique
interface order_keys
module procedure order_integer
module procedure order_real
!*qd module procedure order_quad
end interface order_keys
character(len=clen), save :: rcsid_sort_tools = "$Id: sort_tools.f90,v 1.4 2021/09/29 13:43:22 ps Exp $"
integer(ik), parameter :: verbose = 0
contains
!
! Externally callable subroutines
!
subroutine sort_integer(key)
integer(ik), intent(inout) :: key(:) ! List of keys to sort
!
integer(ik) :: n_in
integer(ik) :: order(size(key)) ! The contents of the index array are immaterial
!
n_in = size(key)
order = 0
call sort_integer2(n_in,key,order)
end subroutine sort_integer
subroutine sort_real(key)
real(rk), intent(inout) :: key(:) ! List of keys to sort
!
integer(ik) :: n_in
integer(ik) :: order(size(key)) ! The contents of the index array are immaterial
!
n_in = size(key)
order = 0
call sort_real2(n_in,key,order)
end subroutine sort_real
subroutine sort_quad(key)
real(xrk), intent(inout) :: key(:) ! List of keys to sort
!
integer(ik) :: n_in
integer(ik) :: order(size(key)) ! The contents of the index array are immaterial
!
n_in = size(key)
order = 0
call sort_quad2(n_in,key,order)
end subroutine sort_quad
function sort_unique_integer(key) result (nkey)
integer(ik), intent(inout) :: key(:) ! In: List of keys to sort
! Out: List of keys in numerically ascending oreder,
! with duplicates removed.
integer(ik) :: nkey ! Number of unique keys present in the key() array
!
nkey = sort_unique_integer2(size(key),key)
end function sort_unique_integer
subroutine order_integer(key,order)
integer(ik), intent(in) :: key (:) ! List of keys to sort
integer(ik), intent(out) :: order(:) ! Sorting order of the keys
!
integer(ik) :: n_in, i
integer(ik) :: key_copy(size(key))
!
n_in = size(key)
if (n_in/=size(order)) stop 'sort_tools%order_integer - called with inconsistent array sizes'
fill_order: do i=1,n_in
order(i) = i
end do fill_order
key_copy = key
call sort_integer2(n_in,key_copy,order)
end subroutine order_integer
subroutine order_real(key,order)
real(rk), intent(in) :: key (:) ! List of keys to sort
integer(ik), intent(out) :: order(:) ! Sorting order of the keys
!
integer(ik) :: n_in, i
real(rk) :: key_copy(size(key))
!
n_in = size(key)
if (n_in/=size(order)) stop 'sort_tools%order_real - called with inconsistent array sizes'
fill_order: do i=1,n_in
order(i) = i
end do fill_order
key_copy = key
call sort_real2(n_in,key_copy,order)
end subroutine order_real
subroutine order_quad(key,order)
real(xrk), intent(in) :: key (:) ! List of keys to sort
integer(ik), intent(out) :: order(:) ! Sorting order of the keys
!
integer(ik) :: n_in, i
real(xrk) :: key_copy(size(key))
!
n_in = size(key)
if (n_in/=size(order)) stop 'sort_tools%order_quad - called with inconsistent array sizes'
fill_order: do i=1,n_in
order(i) = i
end do fill_order
key_copy = key
call sort_quad2(n_in,key_copy,order)
end subroutine order_quad
subroutine sort_and_order_integer(key,order)
integer(ik), intent(inout) :: key (:) ! List of keys to sort
integer(ik), intent(out) :: order(:) ! Sorting order of the keys
!
integer(ik) :: n_in, i
!
n_in = size(key)
if (n_in/=size(order)) stop 'sort_tools%sort_and_order_integer - called with inconsistent array sizes'
fill_order: do i=1,n_in
order(i) = i
end do fill_order
call sort_integer2(n_in,key,order)
end subroutine sort_and_order_integer
subroutine sort_and_order_real(key,order)
real(rk), intent(inout) :: key (:) ! List of keys to sort
integer(ik), intent(out) :: order(:) ! Sorting order of the keys
!
integer(ik) :: n_in, i
!
n_in = size(key)
if (n_in/=size(order)) stop 'sort_tools%sort_and_order_real - called with inconsistent array sizes'
fill_order: do i=1,n_in
order(i) = i
end do fill_order
call sort_real2(n_in,key,order)
end subroutine sort_and_order_real
!
! Internal subroutines beyond this point
!
recursive subroutine sort_real2(n_in,key,val)
integer(ik), intent(in) :: n_in ! Size of the
real(rk), intent(inout) :: key(:) ! List of keys to sort
integer(ik), intent(out) :: val(:) ! Payload to sort
!
real(rk) :: k_left (2+n_in/2) ! Temporary buffers for keys
real(rk) :: k_right(2+n_in/2)
integer(ik) :: v_left (2+n_in/2) ! Temporary buffers for payload
integer(ik) :: v_right(2+n_in/2)
integer(ik) :: n_left, n_right ! Number of left/right keys
integer(ik) :: p_left, p_right ! Positions of the left/right sections
integer(ik) :: p_out ! Position of the output key
!
if (n_in<=1) then
return ! Already sorted
end if
!
! Partition the input array
!
p_left = 1
n_left = n_in/2
p_right = p_left + n_left
n_right = n_in - n_left
!
if (verbose>=2) then
write (out,"('sort_real2: Asked to sort ',i10,' elements')") n_in
write (out,"((t4,8(g16.8,1x)))") key(1:n_in)
write (out,"('sort_real2: Left: ',i10,' elements from ',i10)") n_left, p_left
write (out,"('sort_real2: Right: ',i10,' elements from ',i10)") n_right, p_right
end if
!
! Copy out the keys and values
!
k_left (1:n_left ) = key( p_left:p_left+n_left-1 )
v_left (1:n_left ) = val( p_left:p_left+n_left-1 )
k_right(1:n_right) = key(p_right:p_right+n_right-1)
v_right(1:n_right) = val(p_right:p_right+n_right-1)
!
! Merge sort left and right arrays
!
call sort_real2(n_left, k_left, v_left )
call sort_real2(n_right,k_right,v_right)
!
! Add impossibly large, key values as sentinels at the right.
! Having the sentinels lets us simplify merging logic.
!
k_left (n_left+1) = huge(key)
k_right(n_right+1) = huge(key)
!
! Merge pre-sorted arrays.
! At this point, left and right sections are already sorted.
!
p_out = 0
p_left = 1
p_right = 1
merge_left_right: do while (p_left<=n_left .or. p_right<=n_right)
if (k_left(p_left)<=k_right(p_right)) then
!
! Left key is smaller or equal; copy it
!
p_out = p_out + 1
key(p_out) = k_left(p_left)
val(p_out) = v_left(p_left)
p_left = p_left + 1
else
!
! Right key is smaller; copy it
!
p_out = p_out + 1
key(p_out) = k_right(p_right)
val(p_out) = v_right(p_right)
p_right = p_right + 1
end if
end do merge_left_right
!
if (p_out/=n_in) stop 'sort_tools%sort_real2 - Logical error in merge'
end subroutine sort_real2
recursive subroutine sort_quad2(n_in,key,val)
integer(ik), intent(in) :: n_in ! Size of the
real(xrk), intent(inout) :: key(:) ! List of keys to sort
integer(ik), intent(out) :: val(:) ! Payload to sort
!
real(xrk) :: k_left (2+n_in/2) ! Temporary buffers for keys
real(xrk) :: k_right(2+n_in/2)
integer(ik) :: v_left (2+n_in/2) ! Temporary buffers for payload
integer(ik) :: v_right(2+n_in/2)
integer(ik) :: n_left, n_right ! Number of left/right keys
integer(ik) :: p_left, p_right ! Positions of the left/right sections
integer(ik) :: p_out ! Position of the output key
!
if (n_in<=1) then
return ! Already sorted
end if
!
! Partition the input array
!
p_left = 1
n_left = n_in/2
p_right = p_left + n_left
n_right = n_in - n_left
!
if (verbose>=2) then
write (out,"('sort_quad2: Asked to sort ',i10,' elements')") n_in
write (out,"((t4,8(g16.8,1x)))") key(1:n_in)
write (out,"('sort_quad2: Left: ',i10,' elements from ',i10)") n_left, p_left
write (out,"('sort_quad2: Right: ',i10,' elements from ',i10)") n_right, p_right
end if
!
! Copy out the keys and values
!
k_left (1:n_left ) = key( p_left:p_left+n_left-1 )
v_left (1:n_left ) = val( p_left:p_left+n_left-1 )
k_right(1:n_right) = key(p_right:p_right+n_right-1)
v_right(1:n_right) = val(p_right:p_right+n_right-1)
!
! Merge sort left and right arrays
!
call sort_quad2(n_left, k_left, v_left )
call sort_quad2(n_right,k_right,v_right)
!
! Add impossibly large, key values as sentinels at the right.
! Having the sentinels lets us simplify merging logic.
!
k_left (n_left+1) = huge(key)
k_right(n_right+1) = huge(key)
!
! Merge pre-sorted arrays.
! At this point, left and right sections are already sorted.
!
p_out = 0
p_left = 1
p_right = 1
merge_left_right: do while (p_left<=n_left .or. p_right<=n_right)
if (k_left(p_left)<=k_right(p_right)) then
!
! Left key is smaller or equal; copy it
!
p_out = p_out + 1
key(p_out) = k_left(p_left)
val(p_out) = v_left(p_left)
p_left = p_left + 1
else
!
! Right key is smaller; copy it
!
p_out = p_out + 1
key(p_out) = k_right(p_right)
val(p_out) = v_right(p_right)
p_right = p_right + 1
end if
end do merge_left_right
!
if (p_out/=n_in) stop 'sort_tools%sort_quad2 - Logical error in merge'
end subroutine sort_quad2
recursive subroutine sort_integer2(n_in,key,val)
integer(ik), intent(in) :: n_in ! Size of the
integer(ik), intent(inout) :: key(:) ! List of keys to sort
integer(ik), intent(out) :: val(:) ! Payload to sort
!
integer(ik) :: k_left (2+n_in/2) ! Temporary buffers for keys
integer(ik) :: k_right(2+n_in/2)
integer(ik) :: v_left (2+n_in/2) ! Temporary buffers for payload
integer(ik) :: v_right(2+n_in/2)
integer(ik) :: n_left, n_right ! Number of left/right keys
integer(ik) :: p_left, p_right ! Positions of the left/right sections
integer(ik) :: p_out ! Position of the output key
!
if (n_in<=1) then
return ! Already sorted
end if
!
! Partition the input array
!
p_left = 1
n_left = n_in/2
p_right = p_left + n_left
n_right = n_in - n_left
!
if (verbose>=2) then
write (out,"('sort_integer2: Asked to sort ',i10,' elements')") n_in
write (out,"((t4,8(i12,1x)))") key(1:n_in)
write (out,"('sort_integer2: Left: ',i10,' elements from ',i10)") n_left, p_left
write (out,"('sort_integer2: Right: ',i10,' elements from ',i10)") n_right, p_right
end if
!
! Copy out the keys and values
!
k_left (1:n_left ) = key( p_left:p_left+n_left-1 )
v_left (1:n_left ) = val( p_left:p_left+n_left-1 )
k_right(1:n_right) = key(p_right:p_right+n_right-1)
v_right(1:n_right) = val(p_right:p_right+n_right-1)
!
! Merge sort left and right arrays
!
call sort_integer2(n_left, k_left, v_left )
call sort_integer2(n_right,k_right,v_right)
!
! Add impossibly large, key values as sentinels at the right.
! Having the sentinels lets us simplify merging logic.
!
k_left (n_left+1) = huge(key)
k_right(n_right+1) = huge(key)
!
! Merge pre-sorted arrays.
! At this point, left and right sections are already sorted.
!
p_out = 0
p_left = 1
p_right = 1
merge_left_right: do while (p_left<=n_left .or. p_right<=n_right)
if (k_left(p_left)<=k_right(p_right)) then
!
! Left key is smaller or equal; copy it
!
p_out = p_out + 1
key(p_out) = k_left(p_left)
val(p_out) = v_left(p_left)
p_left = p_left + 1
else
!
! Right key is smaller; copy it
!
p_out = p_out + 1
key(p_out) = k_right(p_right)
val(p_out) = v_right(p_right)
p_right = p_right + 1
end if
end do merge_left_right
!
if (p_out/=n_in) stop 'sort_tools%sort_integer2 - Logical error in merge'
end subroutine sort_integer2
!
! sort_unique_integer2 is a modification of mergeSort2 from sparse.f90, with
! payload deleted.
!
recursive function sort_unique_integer2(n_in,key) result(n_out)
integer(ik), intent(in) :: n_in ! Number of elements to sort
integer(ik), intent(inout) :: key(:) ! Keys
integer(ik) :: n_out ! Number of surviving keys
!
integer(ik) :: k_left (2+n_in/2) ! Temporary buffers for keys
integer(ik) :: k_right(2+n_in/2)
integer(ik) :: c_left, c_right ! Initial number of left/right keys
integer(ik) :: n_left, n_right ! Number of surviving keys in left/right arrays
integer(ik) :: p_left, p_right ! Positions of the left/right sections
!
if (n_in<=1) then
n_out = n_in
return ! Already sorted
end if
!
! Partition the input array
!
p_left = 1
c_left = n_in/2
p_right = p_left + c_left
c_right = n_in - c_left
!
if (verbose>3) then
write (out,"('sort_unique_integer2: Asked to sort ',i10,' elements')") n_in
write (out,"((t12,8(i10,1x)))") key(1:n_in)
write (out,"('sort_unique_integer2: Left: ',i10,' elements from ',i10)") c_left, p_left
write (out,"('sort_unique_integer2: Right: ',i10,' elements from ',i10)") c_right, p_right
call flush(out)
end if
!
! Copy out the keys and values
!
k_left (1:c_left ) = key( p_left:p_left+c_left-1 )
k_right(1:c_right) = key(p_right:p_right+c_right-1)
!
! Merge sort left and right arrays
!
n_left = sort_unique_integer2(c_left, k_left )
n_right = sort_unique_integer2(c_right,k_right)
!
if (verbose>3) then
write (out,"('sort_unique_integer2: Survivors left: ',i10,' right: ',i10)") n_left, n_right
call flush(out)
end if
!
! Add impossibly large, key values as sentinels at the right
!
k_left (n_left+1) = huge(1_ik)
k_right(n_right+1) = huge(1_ik)
!
! Merge pre-sorted arrays. At this point, left and right sections are already
! sorted, and contain no duplicate keys.
!
n_out = 0
p_left = 1
p_right = 1
merge_left_right: do
if (k_left(p_left)==k_right(p_right)) then
!
! Have we reached the end of both arrays? Thanks to sentinels, only
! one index need to be tested.
!
if (p_left>n_left) exit merge_left_right
!
! Key are identical - merge and consume both values
!
n_out = n_out + 1
key(n_out) = k_left(p_left)
p_left = p_left + 1
p_right = p_right + 1
cycle merge_left_right
end if
if (k_left(p_left)<k_right(p_right)) then
!
! Left key is smaller - copy the left value
!
n_out = n_out + 1
key(n_out) = k_left(p_left)
p_left = p_left + 1
else
!
! Right key is smaller - copy the right value
!
n_out = n_out + 1
key(n_out) = k_right(p_right)
p_right = p_right + 1
end if
end do merge_left_right
!
if (verbose>3) then
write (out,"('sort_unique_integer2: Final merge gave ',i10)") n_out
end if
end function sort_unique_integer2
end module sort_tools