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
|
/*
* File: sort_QuickSort_Impl.c
* 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
*/
/*
* DEVELOPERS ARE EXPECTED TO PROVIDE IMPLEMENTATIONS
* FOR THE FOLLOWING METHODS BETWEEN SPLICER PAIRS.
*/
/*
* Symbol "sort.QuickSort" (version 0.1)
*
* Quick sort
*/
#include "sort_QuickSort_Impl.h"
#line 26 "../../../../babel/regression/sort/libC/sort_QuickSort_Impl.c"
/* DO-NOT-DELETE splicer.begin(sort.QuickSort._includes) */
#include "sort_Container.h"
#include "sort_Counter.h"
#include "sort_Comparator.h"
#include "sidl_String.h"
#include <stdio.h>
/**
* 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;
sort_Counter_inc(cmp);
if (sort_Container_compare(elems, start, mid, comp) <= 0) {
sort_Counter_inc(cmp);
if (sort_Container_compare(elems, mid, end - 1, comp) > 0) {
sort_Counter_inc(cmp);
if (sort_Container_compare(elems, start, end - 1, comp) < 0) {
pivot = end - 1;
}
else {
pivot = start;
}
}
}
else {
sort_Counter_inc(cmp);
if (sort_Container_compare(elems, mid, end - 1, comp) < 0) {
sort_Counter_inc(cmp);
if (sort_Container_compare(elems, 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) {
sort_Counter_inc(swp);
sort_Container_swap(elems, start, pivot);
}
for(;;) {
do {
--j;
sort_Counter_inc(cmp);
} while (sort_Container_compare(elems, start, j, comp) < 0);
while (++i < j) {
sort_Counter_inc(cmp);
if (sort_Container_compare(elems, start, i, comp) < 0) break;
}
if (i >= j) break;
sort_Counter_inc(swp);
sort_Container_swap(elems, i, j);
}
if (j != start) {
sort_Counter_inc(swp);
sort_Container_swap(elems, 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.c"
/*
* Static class initializer called exactly once before any user-defined method is dispatched
*/
#undef __FUNC__
#define __FUNC__ "impl_sort_QuickSort__load"
void
impl_sort_QuickSort__load(
void)
{
#line 126 "../../../../babel/regression/sort/libC/sort_QuickSort_Impl.c"
/* DO-NOT-DELETE splicer.begin(sort.QuickSort._load) */
/* Insert the implementation of the static class initializer method here... */
/* DO-NOT-DELETE splicer.end(sort.QuickSort._load) */
#line 132 "sort_QuickSort_Impl.c"
}
/*
* Class constructor called when the class is created.
*/
#undef __FUNC__
#define __FUNC__ "impl_sort_QuickSort__ctor"
void
impl_sort_QuickSort__ctor(
/* in */ sort_QuickSort self)
{
#line 141 "../../../../babel/regression/sort/libC/sort_QuickSort_Impl.c"
/* DO-NOT-DELETE splicer.begin(sort.QuickSort._ctor) */
/* Insert the implementation of the constructor method here... */
/* DO-NOT-DELETE splicer.end(sort.QuickSort._ctor) */
#line 149 "sort_QuickSort_Impl.c"
}
/*
* Class destructor called when the class is deleted.
*/
#undef __FUNC__
#define __FUNC__ "impl_sort_QuickSort__dtor"
void
impl_sort_QuickSort__dtor(
/* in */ sort_QuickSort self)
{
#line 157 "../../../../babel/regression/sort/libC/sort_QuickSort_Impl.c"
/* DO-NOT-DELETE splicer.begin(sort.QuickSort._dtor) */
/* Insert the implementation of the destructor method here... */
/* DO-NOT-DELETE splicer.end(sort.QuickSort._dtor) */
#line 167 "sort_QuickSort_Impl.c"
}
/*
* Sort elements using Quick Sort.
*/
#undef __FUNC__
#define __FUNC__ "impl_sort_QuickSort_sort"
void
impl_sort_QuickSort_sort(
/* in */ sort_QuickSort self,
/* in */ sort_Container elems,
/* in */ sort_Comparator comp)
{
#line 175 "../../../../babel/regression/sort/libC/sort_QuickSort_Impl.c"
/* DO-NOT-DELETE splicer.begin(sort.QuickSort.sort) */
const int32_t num = sort_Container_getLength(elems);
sort_Counter cmp = sort_QuickSort_getCompareCounter(self);
sort_Counter swp = sort_QuickSort_getSwapCounter(self);
quickSort(elems, comp, cmp, swp, 0, num);
sort_Counter_deleteRef(cmp);
sort_Counter_deleteRef(swp);
/* DO-NOT-DELETE splicer.end(sort.QuickSort.sort) */
#line 192 "sort_QuickSort_Impl.c"
}
/*
* Return quick sort.
*/
#undef __FUNC__
#define __FUNC__ "impl_sort_QuickSort_getName"
char*
impl_sort_QuickSort_getName(
/* in */ sort_QuickSort self)
{
#line 196 "../../../../babel/regression/sort/libC/sort_QuickSort_Impl.c"
/* DO-NOT-DELETE splicer.begin(sort.QuickSort.getName) */
return sidl_String_strdup("Quick sort");
/* DO-NOT-DELETE splicer.end(sort.QuickSort.getName) */
#line 210 "sort_QuickSort_Impl.c"
}
|