FairRoot/PandaRoot
PndTrkMergeSort.cxx
Go to the documentation of this file.
1 #include "PndTrkMergeSort.h"
2 #include <iostream>
3 #include <cmath>
4 
5 
6 // Root includes
7 #include "TROOT.h"
8 
9 
10 using namespace std;
11 
12 
13 //----------begin of function PndTrkMergeSort::Merge
14 
16  Short_t nl,
17  Double_t *left,
18  Int_t *ind_left,
19  Short_t nr,
20  Double_t *right,
21  Int_t *ind_right,
22  Double_t *result,
23  Int_t *ind
24  )
25 {
26  Short_t i =0, j, nl_curr=0, nr_curr=0;
27 
28  while( nl > 0 && nr >0){
29  if( left[nl_curr] <= right[nr_curr]){
30  result[i] = left[nl_curr];
31  ind[i] = ind_left[nl_curr];
32  nl--;
33  nl_curr++;
34  } else {
35  result[i] = right [nr_curr];
36  ind[i] = ind_right [nr_curr];
37  nr--;
38  nr_curr++;
39  }
40  i++;
41  }
42 //--------------------
43  if( nl ==0) {
44  for(j=0; j<nr; j++){
45  result[i+j]= right[nr_curr+j];
46  ind[i+j]= ind_right[nr_curr+j];
47  }
48  } else {
49  for(j=0; j<nl; j++){
50  result[i+j]= left[nl_curr+j];
51  ind[i+j]= ind_left[nl_curr+j];
52  }
53  }
54 
55 
56 
57 
58 
59 }
60 
61 //----------end of function PndTrkMergeSort::Merge
62 
63 
64 
65 //----------begin of function PndTrkMergeSort::Merge2
66 // the only difference with Merge is that in Merge2 only Short_t are used (NO Int_t);
67 
69  Short_t nl,
70  Double_t *left,
71  Short_t *ind_left,
72  Short_t nr,
73  Double_t *right,
74  Short_t *ind_right,
75  Double_t *result,
76  Short_t *ind
77  )
78 {
79  Short_t i =0, j, nl_curr=0, nr_curr=0;
80 
81  while( nl > 0 && nr >0){
82  if( left[nl_curr] <= right[nr_curr]){
83  result[i] = left[nl_curr];
84  ind[i] = ind_left[nl_curr];
85  nl--;
86  nl_curr++;
87  } else {
88  result[i] = right [nr_curr];
89  ind[i] = ind_right [nr_curr];
90  nr--;
91  nr_curr++;
92  }
93  i++;
94  }
95 //--------------------
96  if( nl ==0) {
97  for(j=0; j<nr; j++){
98  result[i+j]= right[nr_curr+j];
99  ind[i+j]= ind_right[nr_curr+j];
100  }
101  } else {
102  for(j=0; j<nl; j++){
103  result[i+j]= left[nl_curr+j];
104  ind[i+j]= ind_left[nl_curr+j];
105  }
106  }
107 
108 }
109 
110 //----------end of function PndTrkMergeSort::Merge2
111 
112 
113 
114 //----------begin of function PndTrkMergeSort::Merge3
115 // the only difference with Merge is that in Merge3 only Short_t are used (NO Int_t); and instead
116 // of Double_t *left, Double_t *right, Double_t *result, [the arrays on which the ordering is performed]
117 // rhere are Int_t *right, *left, *result; namely the ordering is performed over arrays of Int_t;
118 
120  Short_t nl,
121  Int_t *left,
122  Short_t *ind_left,
123  Short_t nr,
124  Int_t *right,
125  Short_t *ind_right,
126  Int_t *result,
127  Short_t *ind
128  )
129 {
130  Short_t i =0, j, nl_curr=0, nr_curr=0;
131 
132  while( nl > 0 && nr >0){
133  if( left[nl_curr] <= right[nr_curr]){
134  result[i] = left[nl_curr];
135  ind[i] = ind_left[nl_curr];
136  nl--;
137  nl_curr++;
138  } else {
139  result[i] = right [nr_curr];
140  ind[i] = ind_right [nr_curr];
141  nr--;
142  nr_curr++;
143  }
144  i++;
145  }
146 //--------------------
147  if( nl ==0) {
148  for(j=0; j<nr; j++){
149  result[i+j]= right[nr_curr+j];
150  ind[i+j]= ind_right[nr_curr+j];
151  }
152  } else {
153  for(j=0; j<nl; j++){
154  result[i+j]= left[nl_curr+j];
155  ind[i+j]= ind_left[nl_curr+j];
156  }
157  }
158 
159 }
160 
161 //----------end of function PndTrkMergeSort::Merge3
162 
163 
164 //----------begin of function PndTrkMergeSort::Merge_Sort
165 
167  Short_t n_ele,
168  Double_t *array,
169  Int_t *ind)
170 {
171 
172  Int_t middle, i,//nr, nl, //[R.K. 01/2017] unused variable?
173  ind_left[n_ele], ind_right[n_ele];
174 
175  Double_t left[n_ele], right[n_ele];
176 
177  if( n_ele <= 1) return;
178 
179  middle = n_ele/2 ;
180  for(i=0; i<middle; i++){
181  left[i]=array[i];
182  ind_left[i]= ind[i];
183  }
184  for(i=middle; i<n_ele; i++){
185  right[i-middle]=array[i];
186  ind_right[i-middle]= ind[i];
187  }
188 
189  Merge_Sort( middle, left, ind_left);
190  Merge_Sort(n_ele-middle, right, ind_right);
191 
192  if( left[middle-1] > right[0]) {
193  Merge(middle, left,ind_left, n_ele-middle, right, ind_right, array, ind);
194  } else {
195  // do the appending
196  for(i=0; i<middle; i++){
197  array[i]=left[i];
198  ind[i]=ind_left[i];
199 
200  }
201  for(i=middle; i<n_ele; i++){
202  array[i]=right[i-middle];
203  ind[i]=ind_right[i-middle];
204  }
205  }
206 
207 
208 }
209 
210 
211 //----------end of function PndTrkMergeSort::Merge_Sort
212 
213 
214 
215 
216 //----------begin of function PndTrkMergeSort::Merge_Sort2
217 
218 // the only difference with Merge_Sort is that *ind is a Short_t (and NOT an Int_t);
219 
221  Short_t n_ele,
222  Double_t *array,
223  Short_t *ind)
224 {
225 
226  Short_t middle, i,//nr, nl, //[R.K. 01/2017] unused variable?
227  ind_left[n_ele], ind_right[n_ele];
228 
229  Double_t left[n_ele], right[n_ele];
230 
231  if( n_ele <= 1) return;
232 
233  middle = n_ele/2 ;
234  for(i=0; i<middle; i++){
235  left[i]=array[i];
236  ind_left[i]= ind[i];
237  }
238  for(i=middle; i<n_ele; i++){
239  right[i-middle]=array[i];
240  ind_right[i-middle]= ind[i];
241  }
242 
243  Merge_Sort2( middle, left, ind_left);
244  Merge_Sort2(n_ele-middle, right, ind_right);
245 
246  if( left[middle-1] > right[0]) {
247  Merge2(middle, left,ind_left, n_ele-middle, right, ind_right, array, ind);
248  } else {
249  // do the appending
250  for(i=0; i<middle; i++){
251  array[i]=left[i];
252  ind[i]=ind_left[i];
253 
254  }
255  for(i=middle; i<n_ele; i++){
256  array[i]=right[i-middle];
257  ind[i]=ind_right[i-middle];
258  }
259  }
260 
261 
262 }
263 
264 
265 //----------end of function PndTrkMergeSort::Merge_Sort2
266 
267 
268 
269 //----------begin of function PndTrkMergeSort::Merge_Sort3
270 
271 // the only difference with Merge_Sort is that *ind is a Short_t (and NOT an Int_t) and the *array is
272 // an Int_t (and not a Double_t), namely the merge-sorting is based on an array of Int_t;
273 
275  Short_t n_ele,
276  Int_t *array,// the array to be ordered;
277  Short_t *ind)
278 {
279 
280  Short_t middle, i,// nr, nl, //[R.K. 01/2017] unused variable?
281  ind_left[n_ele], ind_right[n_ele];
282 
283  Int_t left[n_ele], right[n_ele];
284 
285  if( n_ele <= 1) return;
286 
287  middle = n_ele/2 ;
288  for(i=0; i<middle; i++){
289  left[i]=array[i];
290  ind_left[i]= ind[i];
291  }
292  for(i=middle; i<n_ele; i++){
293  right[i-middle]=array[i];
294  ind_right[i-middle]= ind[i];
295  }
296 
297  Merge_Sort3( middle, left, ind_left);
298  Merge_Sort3(n_ele-middle, right, ind_right);
299 
300  if( left[middle-1] > right[0]) {
301  Merge3(middle, left,ind_left, n_ele-middle, right, ind_right, array, ind);
302  } else {
303  // do the appending
304  for(i=0; i<middle; i++){
305  array[i]=left[i];
306  ind[i]=ind_left[i];
307 
308  }
309  for(i=middle; i<n_ele; i++){
310  array[i]=right[i-middle];
311  ind[i]=ind_right[i-middle];
312  }
313  }
314 
315 
316 }
317 
318 
319 //----------end of function PndTrkMergeSort::Merge_Sort3
320 
void Merge(Short_t nl, Double_t *left, Int_t *ind_left, Short_t nr, Double_t *right, Int_t *ind_right, Double_t *result, Int_t *ind)
Int_t i
Definition: run_full.C:25
void Merge_Sort3(Short_t n_ele, Int_t *array, Short_t *ind)
void Merge3(Short_t nl, Int_t *left, Short_t *ind_left, Short_t nr, Int_t *right, Short_t *ind_right, Int_t *result, Short_t *ind)
Double_t
void Merge_Sort2(Short_t n_ele, Double_t *array, Short_t *ind)
void Merge_Sort(Short_t n_ele, Double_t *array, Int_t *ind)
void Merge2(Short_t nl, Double_t *left, Short_t *ind_left, Short_t nr, Double_t *right, Short_t *ind_right, Double_t *result, Short_t *ind)
ClassImp(PndTrkMergeSort)