File: sort_QuickSort_Impl.f

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 (288 lines) | stat: -rw-r--r-- 9,605 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
C       
C       File:          sort_QuickSort_Impl.f
C       Symbol:        sort.QuickSort-v0.1
C       Symbol Type:   class
C       Babel Version: 0.10.2
C       Description:   Server-side implementation for sort.QuickSort
C       
C       WARNING: Automatically generated; only changes within splicers preserved
C       
C       babel-version = 0.10.2
C       


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


C       DO-NOT-DELETE splicer.begin(_miscellaneous_code_start)
      logical function notEmpty(self)
      implicit none
      integer*8 self, stacks, depth
      integer*4 stackdepth
      call sort_QuickSort__get_data_f(self, stacks)
      call sidl_opaque__array_get1_f(stacks, 0, depth)
      call sidl_int__array_get1_f(depth, 0, stackdepth)
      notEmpty = (stackdepth .gt. 0)
      end

      subroutine push(self, lower, upper)
      implicit none
      integer*8 self
      integer*8 tracker
      integer*8 stacks, low, up, depth
      integer*4 lower, upper, stackdepth
      call sort_QuickSort__get_data_f(self, stacks)
      call sidl_opaque__array_get1_f(stacks, 0, depth)
      call sidl_opaque__array_get1_f(stacks, 1, low)
      call sidl_opaque__array_get1_f(stacks, 2, up)
      call sidl_int__array_get1_f(depth, 0, stackdepth)
      if (stackdepth .lt. 32) then
         call sidl_int__array_set1_f(low, stackdepth, lower)
         call sidl_int__array_set1_f(up, stackdepth, upper)
         stackdepth = stackdepth + 1
         call sidl_int__array_set1_f(depth, 0, stackdepth)
      else
         call synch_RegOut_getInstance_f(tracker)
         call synch_RegOut_writeComment_f(tracker,
     $        'stack overflow in QuickSort')
         call synch_RegOut_forceFailure_f(tracker)
         call synch_RegOut_deleteRef_f(tracker)
         stop
      endif
      end

      subroutine pop(self, lower, upper)
      implicit none
      integer*8 self
      integer*8 stacks, low, up, depth, tracker
      integer*4 lower, upper, stackdepth
      call sort_QuickSort__get_data_f(self, stacks)
      call sidl_opaque__array_get1_f(stacks, 0, depth)
      call sidl_opaque__array_get1_f(stacks, 1, low)
      call sidl_opaque__array_get1_f(stacks, 2, up)
      call sidl_int__array_get1_f(depth, 0, stackdepth)
      if (stackdepth .gt. 0) then
         stackdepth = stackdepth - 1
         call sidl_int__array_set1_f(depth, 0, stackdepth)
         call sidl_int__array_get1_f(low, stackdepth, lower)
         call sidl_int__array_get1_f(up, stackdepth, upper)
      else
         call synch_RegOut_getInstance_f(tracker)
         call synch_RegOut_writeComment_f(tracker,
     $        'stack underflow in QuickSort')
         call synch_RegOut_forceFailure_f(tracker)
         call synch_RegOut_deleteRef_f(tracker)
         stop
      endif
      end

C
C Choose the middle of the first, middle and last element of the
C list.  For small lists, return the middle without checking.
C
      integer*4 function choosePivot(elems, comp, cmp, start, end)
      implicit none
      integer*8 elems, comp, cmp
      integer*4 start, end, pivot, mid, cmpres, counter
      pivot = (start + end) / 2
      if ((end - start) .gt. 4) then
         mid = pivot
         call sort_Counter_inc_f(cmp, counter)
         call sort_Container_compare_f(elems, start, mid, comp,
     $        cmpres)
         if (cmpres .le. 0) then
            call sort_Counter_inc_f(cmp, counter)
            call sort_Container_compare_f(elems, mid, end -1,
     $           comp, cmpres)
            if (cmpres .gt. 0) then
               call sort_Counter_inc_f(cmp, counter)
               call sort_Container_compare_f(elems, start,
     $              end - 1, comp, cmpres)
               if (cmpres .lt. 0) then
                  pivot = end - 1
               else
                  pivot = start
               endif
            endif
         else
            call sort_Counter_inc_f(cmp, counter)
            call sort_Container_compare_f(elems, mid, end - 1,
     $           comp, cmpres)
            if (cmpres .lt. 0) then
               call sort_Counter_inc_f(cmp, counter)
               call sort_Container_compare_f(elems, start, end - 1,
     $              comp, cmpres)
               if (cmpres .gt. 0) then
                  pivot = end - 1
               else
                  pivot = start
               endif
            endif
         endif
      endif
      choosePivot = pivot
      end

      subroutine quickSort(self, elems, comp, cmp, swp)
      implicit none
      integer*8 self, elems, comp, cmp, swp
      integer*4 start, end, pivot, choosePivot
      integer*4 i, j, cmpres, counter
      logical notEmpty
      call sort_Container_getLength_f(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 sort_Counter_inc_f(swp, counter)
               call sort_Container_swap_f(elems, start, pivot)
            endif
 100        j = j - 1
            call sort_Counter_inc_f(cmp, counter)
            call sort_Container_compare_f(elems, start, j, comp,
     $           cmpres)
            if (cmpres .lt. 0) goto 100
            i = i + 1
            do while (i .lt. j)
               call sort_Counter_inc_f(cmp, counter)
               call sort_Container_compare_f(elems, start,
     $              i, comp, cmpres)
               if (cmpres .lt. 0) goto 200
               i = i + 1
            enddo
 200        if (i .ge. j) goto 300
            call sort_counter_inc_f(swp, counter)
            call sort_Container_swap_f(elems, i, j)
            goto 100
 300        if (j .ne. start) then
               call sort_Counter_inc_f(swp, counter)
               call sort_Container_swap_f(elems, start, j)
            endif
            call push(self, start, j)
            call push(self, j + 1, end)
         endif
      enddo
      end
      
C       DO-NOT-DELETE splicer.end(_miscellaneous_code_start)




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

        subroutine sort_QuickSort__ctor_fi(self)
        implicit none
C       in sort.QuickSort self
        integer*8 self

C       DO-NOT-DELETE splicer.begin(sort.QuickSort._ctor)
        integer*8 stacks, lower, upper, depth
        integer*4 stackdepth
        stackdepth = 0
        call sidl_int__array_create1d_f(32, lower)
        call sidl_int__array_create1d_f(32, upper)
        call sidl_int__array_create1d_f(1, depth)
        call sidl_int__array_set1_f(depth, 0, stackdepth)
        call sidl_opaque__array_create1d_f(3, stacks)
        call sidl_opaque__array_set1_f(stacks, 0, depth)
        call sidl_opaque__array_set1_f(stacks, 1, lower)
        call sidl_opaque__array_set1_f(stacks, 2, upper)
        call sort_QuickSort__set_data_f(self, stacks)
C       DO-NOT-DELETE splicer.end(sort.QuickSort._ctor)
        end


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

        subroutine sort_QuickSort__dtor_fi(self)
        implicit none
C       in sort.QuickSort self
        integer*8 self

C       DO-NOT-DELETE splicer.begin(sort.QuickSort._dtor)
        integer*8 stacks, lower, upper, depth
        call sort_QuickSort__get_data_f(self, stacks)
        call sidl_opaque__array_get1_f(stacks, 0, depth)
        call sidl_opaque__array_get1_f(stacks, 1, lower)
        call sidl_opaque__array_get1_f(stacks, 2, upper)
        call sidl_int__array_deleteRef_f(depth)
        call sidl_int__array_deleteRef_f(lower)
        call sidl_int__array_deleteRef_f(upper)
        call sidl_opaque__array_deleteRef_f(stacks)
        stacks = 0
        call sort_QuickSort__set_data_f(self, stacks)
C       DO-NOT-DELETE splicer.end(sort.QuickSort._dtor)
        end


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

        subroutine sort_QuickSort__load_fi()
        implicit none

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


C       
C       Sort elements using Quick Sort.
C       

        subroutine sort_QuickSort_sort_fi(self, elems, comp)
        implicit none
C       in sort.QuickSort self
        integer*8 self
C       in sort.Container elems
        integer*8 elems
C       in sort.Comparator comp
        integer*8 comp

C       DO-NOT-DELETE splicer.begin(sort.QuickSort.sort)
        integer*8 swp, cmp
        call sort_QuickSort_getCompareCounter_f(self, cmp)
        call sort_QuickSort_getSwapCounter_f(self, swp)
        call quickSort(self, elems, comp, cmp, swp)
        call sort_Counter_deleteRef_f(cmp)
        call sort_Counter_deleteRef_f(swp)
C       DO-NOT-DELETE splicer.end(sort.QuickSort.sort)
        end


C       
C       Return quick sort.
C       

        subroutine sort_QuickSort_getName_fi(self, retval)
        implicit none
C       in sort.QuickSort self
        integer*8 self
C       out string retval
        character*(*) retval

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


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