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 (169 lines) | stat: -rw-r--r-- 4,709 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
// 
// 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"

#line 14 "../../../../babel/regression/sort/libCxx/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(sort::Container  &elems,
            sort::Comparator &comp,
            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(sort::Container  &elems,
          sort::Comparator &comp,
          sort::Counter    &cmp,
          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 97 "sort_QuickSort_Impl.cc"

// user-defined constructor.
void sort::QuickSort_impl::_ctor() {
#line 99 "../../../../babel/regression/sort/libCxx/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 105 "sort_QuickSort_Impl.cc"
}

// user-defined destructor.
void sort::QuickSort_impl::_dtor() {
#line 106 "../../../../babel/regression/sort/libCxx/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 114 "sort_QuickSort_Impl.cc"
}

// static class initializer.
void sort::QuickSort_impl::_load() {
#line 113 "../../../../babel/regression/sort/libCxx/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 123 "sort_QuickSort_Impl.cc"
}

// user-defined static methods: (none)

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

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

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


#line 152 "../../../../babel/regression/sort/libCxx/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 168 "sort_QuickSort_Impl.cc"