File: sort_MergeSort_Impl.c

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 (172 lines) | stat: -rw-r--r-- 4,566 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
/*
 * File:          sort_MergeSort_Impl.c
 * Symbol:        sort.MergeSort-v0.1
 * Symbol Type:   class
 * Babel Version: 0.10.2
 * Description:   Server-side implementation for sort.MergeSort
 * 
 * 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.MergeSort" (version 0.1)
 * 
 * Merge sort
 */

#include "sort_MergeSort_Impl.h"

#line 26 "../../../../babel/regression/sort/libC/sort_MergeSort_Impl.c"
/* DO-NOT-DELETE splicer.begin(sort.MergeSort._includes) */
#include "sort_Container.h"
#include "sort_Comparator.h"
#include "sort_Counter.h"
#include "sidl_String.h"

static void
mergeLists(sort_Container  elems,
           sort_Comparator comp,
           sort_Counter    cmp,
           sort_Counter    swp,
           int32_t         start,
           int32_t         mid,
           const int32_t   end)
{
  int32_t j;
  while ((start < mid) && (mid < end)) {
    sort_Counter_inc(cmp);
    if (sort_Container_compare(elems, start, mid, comp) > 0) {
      /* move first element of upper list into place */
      for(j = mid;j > start; --j) {
        sort_Counter_inc(swp);
        sort_Container_swap(elems, j, j - 1);
      }
      ++mid;
    }
    ++start;
  }
}

/**
 * end is one past the end
 */
static void
mergeSort(sort_Container  elems,
          sort_Comparator comp,
          sort_Counter    cmp,
          sort_Counter    swp,
          const int32_t   start,
          const int32_t   end)
{
  if ((end - start) > 1) {
    int32_t mid = (start + end) >> 1;
    mergeSort(elems, comp, cmp, swp, start, mid);
    mergeSort(elems, comp, cmp, swp, mid, end);
    mergeLists(elems, comp, cmp, swp, start, mid, end);
  }
}
/* DO-NOT-DELETE splicer.end(sort.MergeSort._includes) */
#line 76 "sort_MergeSort_Impl.c"

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

#undef __FUNC__
#define __FUNC__ "impl_sort_MergeSort__load"

void
impl_sort_MergeSort__load(
  void)
{
#line 87 "../../../../babel/regression/sort/libC/sort_MergeSort_Impl.c"
  /* DO-NOT-DELETE splicer.begin(sort.MergeSort._load) */
  /* Insert the implementation of the static class initializer method here... */
  /* DO-NOT-DELETE splicer.end(sort.MergeSort._load) */
#line 93 "sort_MergeSort_Impl.c"
}
/*
 * Class constructor called when the class is created.
 */

#undef __FUNC__
#define __FUNC__ "impl_sort_MergeSort__ctor"

void
impl_sort_MergeSort__ctor(
  /* in */ sort_MergeSort self)
{
#line 102 "../../../../babel/regression/sort/libC/sort_MergeSort_Impl.c"
  /* DO-NOT-DELETE splicer.begin(sort.MergeSort._ctor) */
  /* Insert the implementation of the constructor method here... */
  /* DO-NOT-DELETE splicer.end(sort.MergeSort._ctor) */
#line 110 "sort_MergeSort_Impl.c"
}

/*
 * Class destructor called when the class is deleted.
 */

#undef __FUNC__
#define __FUNC__ "impl_sort_MergeSort__dtor"

void
impl_sort_MergeSort__dtor(
  /* in */ sort_MergeSort self)
{
#line 118 "../../../../babel/regression/sort/libC/sort_MergeSort_Impl.c"
  /* DO-NOT-DELETE splicer.begin(sort.MergeSort._dtor) */
  /* Insert the implementation of the destructor method here... */
  /* DO-NOT-DELETE splicer.end(sort.MergeSort._dtor) */
#line 128 "sort_MergeSort_Impl.c"
}

/*
 * Sort elements using Merge Sort.
 */

#undef __FUNC__
#define __FUNC__ "impl_sort_MergeSort_sort"

void
impl_sort_MergeSort_sort(
  /* in */ sort_MergeSort self,
  /* in */ sort_Container elems,
  /* in */ sort_Comparator comp)
{
#line 136 "../../../../babel/regression/sort/libC/sort_MergeSort_Impl.c"
  /* DO-NOT-DELETE splicer.begin(sort.MergeSort.sort) */
  const int32_t num = sort_Container_getLength(elems);
  sort_Counter cmp = sort_MergeSort_getCompareCounter(self);
  sort_Counter swp = sort_MergeSort_getSwapCounter(self);
  mergeSort(elems, comp, cmp, swp, 0, num);
  sort_Counter_deleteRef(cmp);
  sort_Counter_deleteRef(swp);
  /* DO-NOT-DELETE splicer.end(sort.MergeSort.sort) */
#line 153 "sort_MergeSort_Impl.c"
}

/*
 * Return merge sort.
 */

#undef __FUNC__
#define __FUNC__ "impl_sort_MergeSort_getName"

char*
impl_sort_MergeSort_getName(
  /* in */ sort_MergeSort self)
{
#line 157 "../../../../babel/regression/sort/libC/sort_MergeSort_Impl.c"
  /* DO-NOT-DELETE splicer.begin(sort.MergeSort.getName) */
  return sidl_String_strdup("Merge sort");
  /* DO-NOT-DELETE splicer.end(sort.MergeSort.getName) */
#line 171 "sort_MergeSort_Impl.c"
}