-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort_tools.f90
368 lines (353 loc) · 12 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
!
! hydrogen-tunnel: Static-field tunneling in a central potential
! Copyright (C) 2018-2022 Serguei Patchkovskii, [email protected]
!
! This program is free software: you can redistribute it and/or modify
! it under the terms of the GNU General Public License as published by
! the Free Software Foundation, either version 3 of the License, or
! (at your option) any later version.
!
! This program is distributed in the hope that it will be useful,
! but WITHOUT ANY WARRANTY; without even the implied warranty of
! MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
! GNU General Public License for more details.
!
! You should have received a copy of the GNU General Public License
! along with this program. If not, see <https://www.gnu.org/licenses/>.
!
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, order_keys
public rcsid_sort_tools
character(len=clen), save :: rcsid_sort_tools = "$Id: sort_tools.f90,v 1.3 2021/04/26 15:44:44 ps Exp $"
integer(ik), parameter :: xrk = qrk
interface sort
module procedure sort_integer
module procedure sort_real
end interface sort
interface order_keys
module procedure order_integer
module procedure order_real
end interface order_keys
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
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
!
! 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
end module sort_tools