File: sort_QuickSort_Impl.F90

package info (click to toggle)
babel 0.10.2-1
  • links: PTS
  • area: contrib
  • in suites: sarge
  • size: 43,932 kB
  • ctags: 29,707
  • sloc: java: 74,695; ansic: 73,142; cpp: 40,649; sh: 18,411; f90: 10,062; fortran: 6,727; python: 6,406; makefile: 3,866; xml: 118; perl: 48
file content (311 lines) | stat: -rw-r--r-- 9,002 bytes parent folder | download
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
! 
! File:          sort_QuickSort_Impl.F90
! Symbol:        sort.QuickSort-v0.1
! Symbol Type:   class
! Babel Version: 0.10.2
! Description:   Server-side implementation for sort.QuickSort
! 
! WARNING: Automatically generated; only changes within splicers preserved
! 
! babel-version = 0.10.2
! 


! 
! Symbol "sort.QuickSort" (version 0.1)
! 
! Quick sort
! 


#include "sort_QuickSort_fAbbrev.h"
#include "sort_SortingAlgorithm_fAbbrev.h"
#include "sidl_ClassInfo_fAbbrev.h"
#include "sort_Comparator_fAbbrev.h"
#include "sidl_BaseInterface_fAbbrev.h"
#include "sort_Container_fAbbrev.h"
#include "sort_Counter_fAbbrev.h"
#include "sidl_BaseClass_fAbbrev.h"
! DO-NOT-DELETE splicer.begin(_miscellaneous_code_start)
#include "sort_MergeSort_fAbbrev.h"
#include "synch_RegOut_fAbbrev.h"


logical function notEmpty(self)
  use sort_QuickSort
  use sort_QuickSort_impl
  implicit none
  type(sort_QuickSort_t) :: self
  type(sort_QuickSort_wrap) :: dp
  call sort_QuickSort__get_data_m(self, dp)
  notEmpty = (dp%d_private_data%depth .gt. 0)
end function notEmpty

subroutine push(self, low, up)
  use sort_QuickSort
  use sort_QuickSort_impl
  use synch_RegOut
  implicit none
  type(sort_QuickSort_t) :: self
  type(sort_QuickSort_wrap) :: dp
  integer(selected_int_kind(9)) low, up
  type(synch_RegOut_t) :: tracker
  call sort_QuickSort__get_data_m(self, dp)
  if (dp%d_private_data%depth .lt. 32) then
     call set(dp%d_private_data%lower, dp%d_private_data%depth, low)
     call set(dp%d_private_data%upper, dp%d_private_data%depth, up)
     dp%d_private_data%depth = dp%d_private_data%depth + 1
  else
     call getInstance(tracker)
     call writeComment(tracker, &
          'stack overflow in QuickSort')
     call forceFailure(tracker)
     stop
  endif
end subroutine push

subroutine pop(self, low, up)
  use sort_QuickSort
  use sort_QuickSort_impl
  use synch_RegOut
  implicit none
  type(sort_QuickSort_t) :: self
  integer(selected_int_kind(9)) low, up, stackdepth
  type(sort_QuickSort_wrap) :: dp
  type(synch_RegOut_t) :: tracker
  call sort_QuickSort__get_data_m(self, dp)
  if (dp%d_private_data%depth .gt. 0) then
     dp%d_private_data%depth = dp%d_private_data%depth - 1
     stackdepth = dp%d_private_data%depth
     call get(dp%d_private_data%lower, stackdepth, low)
     call get(dp%d_private_data%upper, stackdepth, up)
  else
     call getInstance(tracker)
     call writeComment(tracker, &
          'stack underflow in QuickSort')
     call forceFailure(tracker)
     stop
  endif
end subroutine pop

!
! Choose the middle of the first, middle and last element of the
! list.  For small lists, return the middle without checking.
!
integer(selected_int_kind(9)) function choosePivot(elems, comp, cmp, start, end)
  use sort_Container
  use sort_Comparator
  use sort_Counter
  implicit none
  type(sort_Container_t) :: elems
  type(sort_Comparator_t) :: comp
  type(sort_Counter_t) cmp
  integer(selected_int_kind(9)) start, end, pivot, mid, cmpres, counter
  pivot = (start + end) / 2
  if ((end - start) .gt. 4) then
     mid = pivot
     call inc(cmp, counter)
     call compare(elems, start, mid, comp, cmpres)
     if (cmpres .le. 0) then
        call inc(cmp, counter)
        call compare(elems, mid, end -1, comp, cmpres)
        if (cmpres .gt. 0) then
           call inc(cmp, counter)
           call compare(elems, start, end - 1, comp, &
                cmpres)
           if (cmpres .lt. 0) then
              pivot = end - 1
           else
              pivot = start
           endif
        endif
     else
        call inc(cmp, counter)
        call compare(elems, mid, end - 1, comp, cmpres)
        if (cmpres .lt. 0) then
           call inc(cmp, counter)
           call compare(elems, start, end - 1, comp, &
                cmpres)
           if (cmpres .gt. 0) then
              pivot = end - 1
           else
              pivot = start
           endif
        endif
     endif
  endif
  choosePivot = pivot
end function choosePivot

subroutine quickSort(self, elems, comp, cmp, swp)
  use sort_QuickSort
  use sort_Container
  use sort_Comparator
  use sort_Counter
  implicit none
  type(sort_QuickSort_t) :: self
  type(sort_Container_t) :: elems
  type(sort_Comparator_t) :: comp
  type(sort_Counter_t) :: cmp, swp
  integer(selected_int_kind(9)) start, end, pivot, choosePivot
  integer(selected_int_kind(9)) i, j, cmpres, counter
  logical notEmpty
  call getLength(elems, end)
  start = 0
  call push(self, start, end)
  do while (notEmpty(self))
     call pop(self, start, end)
     if ((end - start) .gt. 1) then
        pivot = choosePivot(elems, comp, cmp, start, end)
        i = start
        j = end
        if (pivot .ne. start) then
           call inc(swp, counter)
           call swap(elems, start, pivot)
        endif
100     j = j - 1
        call inc(cmp, counter)
        call compare(elems, start, j, comp, cmpres)
        if (cmpres .lt. 0) goto 100
        i = i + 1
        do while (i .lt. j)
           call inc(cmp, counter)
           call compare(elems, start, i, comp, cmpres)
           if (cmpres .lt. 0) goto 200
           i = i + 1
        enddo
200     if (i .ge. j) goto 300
        call inc(swp, counter)
        call swap(elems, i, j)
        goto 100
300     if (j .ne. start) then
           call inc(swp, counter)
           call swap(elems, start, j)
        endif
        call push(self, start, j)
        call push(self, j + 1, end)
     endif
  enddo
end subroutine quickSort

! DO-NOT-DELETE splicer.end(_miscellaneous_code_start)




! 
! Class constructor called when the class is created.
! 

recursive subroutine sort_QuickSort__ctor_mi(self)
  use sort_QuickSort
  use sort_QuickSort_impl
  ! DO-NOT-DELETE splicer.begin(sort.QuickSort._ctor.use)
  ! Insert use statements here...
  ! DO-NOT-DELETE splicer.end(sort.QuickSort._ctor.use)
  implicit none
  type(sort_QuickSort_t) :: self ! in

! DO-NOT-DELETE splicer.begin(sort.QuickSort._ctor)
  type(sort_QuickSort_wrap) :: dp
  allocate(dp%d_private_data)
  call create1d(32, dp%d_private_data%lower)
  call create1d(32, dp%d_private_data%upper)
  dp%d_private_data%depth = 0
  call sort_QuickSort__set_data_m(self, dp)
! DO-NOT-DELETE splicer.end(sort.QuickSort._ctor)
end subroutine sort_QuickSort__ctor_mi


! 
! Class destructor called when the class is deleted.
! 

recursive subroutine sort_QuickSort__dtor_mi(self)
  use sort_QuickSort
  use sort_QuickSort_impl
  ! DO-NOT-DELETE splicer.begin(sort.QuickSort._dtor.use)
  ! Insert use statements here...
  ! DO-NOT-DELETE splicer.end(sort.QuickSort._dtor.use)
  implicit none
  type(sort_QuickSort_t) :: self ! in

! DO-NOT-DELETE splicer.begin(sort.QuickSort._dtor)
  type(sort_QuickSort_wrap) :: dp
  call sort_QuickSort__get_data_m(self, dp)
  call deleteRef(dp%d_private_data%lower)
  call deleteRef(dp%d_private_data%upper)
  deallocate(dp%d_private_data)
! DO-NOT-DELETE splicer.end(sort.QuickSort._dtor)
end subroutine sort_QuickSort__dtor_mi


! 
! Static class initializer called exactly once before any user-defined method is dispatched
! 

recursive subroutine sort_QuickSort__load_mi()
  use sort_QuickSort
  use sort_QuickSort_impl
  ! DO-NOT-DELETE splicer.begin(sort.QuickSort._load.use)
  ! Insert use statements here...
  ! DO-NOT-DELETE splicer.end(sort.QuickSort._load.use)
  implicit none

! DO-NOT-DELETE splicer.begin(sort.QuickSort._load)
! Insert the implementation here...
! DO-NOT-DELETE splicer.end(sort.QuickSort._load)
end subroutine sort_QuickSort__load_mi


! 
! Sort elements using Quick Sort.
! 

recursive subroutine sort_QuickSort_sort_mi(self, elems, comp)
  use sort_Container
  use sort_Comparator
  use sort_QuickSort
  use sort_QuickSort_impl
  ! DO-NOT-DELETE splicer.begin(sort.QuickSort.sort.use)
  use sort_Counter
  ! DO-NOT-DELETE splicer.end(sort.QuickSort.sort.use)
  implicit none
  type(sort_QuickSort_t) :: self ! in
  type(sort_Container_t) :: elems ! in
  type(sort_Comparator_t) :: comp ! in

! DO-NOT-DELETE splicer.begin(sort.QuickSort.sort)
  type (sort_Counter_t) :: swp, cmp
  call getCompareCounter(self, cmp)
  call getSwapCounter(self, swp)
  call quickSort(self, elems, comp, cmp, swp)
  call deleteRef(cmp)
  call deleteRef(swp)
! DO-NOT-DELETE splicer.end(sort.QuickSort.sort)
end subroutine sort_QuickSort_sort_mi


! 
! Return quick sort.
! 

recursive subroutine sort_QuickSort_getName_mi(self, retval)
  use sort_QuickSort
  use sort_QuickSort_impl
  ! DO-NOT-DELETE splicer.begin(sort.QuickSort.getName.use)
  ! Insert use statements here...
  ! DO-NOT-DELETE splicer.end(sort.QuickSort.getName.use)
  implicit none
  type(sort_QuickSort_t) :: self ! in
  character (len=*) :: retval ! out

! DO-NOT-DELETE splicer.begin(sort.QuickSort.getName)
  retval = 'Quick sort'
! DO-NOT-DELETE splicer.end(sort.QuickSort.getName)
end subroutine sort_QuickSort_getName_mi


! DO-NOT-DELETE splicer.begin(_miscellaneous_code_end)
! Insert extra code here...
! DO-NOT-DELETE splicer.end(_miscellaneous_code_end)