File: sort_QuickSort_Impl.cc

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 (187 lines) | stat: -rw-r--r-- 5,190 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
// 
// File:          sort_QuickSort_Impl.cc
// 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
// 
#include "sort_QuickSort_Impl.hh"

// 
// Includes for all method dependencies.
// 
#ifndef included_sidl_BaseInterface_hh
#include "sidl_BaseInterface.hh"
#endif
#ifndef included_sidl_ClassInfo_hh
#include "sidl_ClassInfo.hh"
#endif
#ifndef included_sort_Comparator_hh
#include "sort_Comparator.hh"
#endif
#ifndef included_sort_Container_hh
#include "sort_Container.hh"
#endif
#ifndef included_sort_Counter_hh
#include "sort_Counter.hh"
#endif
#line 32 "../../../../babel/regression/sort/libUCxx/sort_QuickSort_Impl.cc"
// DO-NOT-DELETE splicer.begin(sort.QuickSort._includes)
/**
 * Choose the middle of the first, middle and last element of the
 * list.  For small lists, return the middle without checking.
 */
static int32_t
choosePivot(ucxx::sort::Container  &elems,
            ucxx::sort::Comparator &comp,
            ucxx::sort::Counter    &cmp,
            int32_t           start,
            int32_t           end)
{
  int32_t pivot = (start + end) >> 1;
  if ((end - start) > 4) {
    int32_t mid = pivot;
    cmp.inc();
    if (elems.compare(start, mid, comp) <= 0) {
      cmp.inc();
      if (elems.compare(mid, end - 1, comp) > 0) {
        cmp.inc();
        if (elems.compare( start, end - 1, comp) < 0) {
          pivot = end - 1;
        }
        else {
          pivot = start;
        }
      }
    }
    else {
      cmp.inc();
      if (elems.compare( mid, end - 1, comp) < 0) {
        cmp.inc();
        if (elems.compare( start, end - 1, comp) > 0) {
          pivot = end - 1;
        }
        else {
          pivot = start;
        }
      }
    }
  }
  return pivot;
}

static void 
quickSort(ucxx::sort::Container  &elems,
          ucxx::sort::Comparator &comp,
          ucxx::sort::Counter    &cmp,
          ucxx::sort::Counter    &swp,
          int32_t           start,
          int32_t           end)
{
  if ((end - start) > 1) {
    int32_t pivot = choosePivot(elems, comp, cmp, start, end);
    int32_t i = start;
    int32_t j = end;
    if (pivot != start) {
      swp.inc();
      elems.swap(start, pivot);
    }
    for(;;) {
      do {
        --j;
        cmp.inc();
      } while (elems.compare( start, j, comp) < 0);
      while (++i < j) {
        cmp.inc();
        if (elems.compare( start, i, comp) < 0) break;
      }
      if (i >= j) break;
      swp.inc();
      elems.swap(i, j);
    }
    if (j != start) {
      swp.inc();
      elems.swap(start, j);
    }
    quickSort(elems, comp, cmp, swp, start, j);
    quickSort(elems, comp, cmp, swp, j + 1, end);
  }
}
// DO-NOT-DELETE splicer.end(sort.QuickSort._includes)
#line 115 "sort_QuickSort_Impl.cc"

// user defined constructor
void sort::QuickSort_impl::_ctor() {
#line 117 "../../../../babel/regression/sort/libUCxx/sort_QuickSort_Impl.cc"
  // DO-NOT-DELETE splicer.begin(sort.QuickSort._ctor)
  // add construction details here
  // DO-NOT-DELETE splicer.end(sort.QuickSort._ctor)
#line 123 "sort_QuickSort_Impl.cc"
}

// user defined destructor
void sort::QuickSort_impl::_dtor() {
#line 124 "../../../../babel/regression/sort/libUCxx/sort_QuickSort_Impl.cc"
  // DO-NOT-DELETE splicer.begin(sort.QuickSort._dtor)
  // add destruction details here
  // DO-NOT-DELETE splicer.end(sort.QuickSort._dtor)
#line 132 "sort_QuickSort_Impl.cc"
}

// static class initializer
void sort::QuickSort_impl::_load() {
#line 131 "../../../../babel/regression/sort/libUCxx/sort_QuickSort_Impl.cc"
  // DO-NOT-DELETE splicer.begin(sort.QuickSort._load)
  // guaranteed to be called at most once before any other method in this class
  // DO-NOT-DELETE splicer.end(sort.QuickSort._load)
#line 141 "sort_QuickSort_Impl.cc"
}

// user defined static methods: (none)

// user defined non-static methods:
/**
 * Sort elements using Quick Sort.
 */
void
sort::QuickSort_impl::sort_impl (
  /* in */::ucxx::sort::Container elems,
  /* in */::ucxx::sort::Comparator comp ) 
throw () 
{
#line 148 "../../../../babel/regression/sort/libUCxx/sort_QuickSort_Impl.cc"
  // DO-NOT-DELETE splicer.begin(sort.QuickSort.sort)
  const int32_t num = elems.getLength();
  ucxx::sort::Counter cmp = getCompareCounter();
  ucxx::sort::Counter swp = getSwapCounter();
  quickSort(elems, comp, cmp, swp, 0, num);
  // DO-NOT-DELETE splicer.end(sort.QuickSort.sort)
#line 163 "sort_QuickSort_Impl.cc"
}

/**
 * Return quick sort.
 */
::std::string
sort::QuickSort_impl::getName_impl () 
throw () 

{
#line 164 "../../../../babel/regression/sort/libUCxx/sort_QuickSort_Impl.cc"
  // DO-NOT-DELETE splicer.begin(sort.QuickSort.getName)
  return "Quick sort";
  // DO-NOT-DELETE splicer.end(sort.QuickSort.getName)
#line 178 "sort_QuickSort_Impl.cc"
}


#line 170 "../../../../babel/regression/sort/libUCxx/sort_QuickSort_Impl.cc"
// DO-NOT-DELETE splicer.begin(sort.QuickSort._misc)
// Put miscellaneous code here
// DO-NOT-DELETE splicer.end(sort.QuickSort._misc)
#line 186 "sort_QuickSort_Impl.cc"