15 #ifndef TLX_SORT_NETWORKS_BOSE_NELSON_PARAMETER_HEADER 16 #define TLX_SORT_NETWORKS_BOSE_NELSON_PARAMETER_HEADER 25 namespace sort_networks {
34 namespace bose_nelson_parameter {
37 template <
typename ValueType>
43 template <
typename ValueType,
typename CSwap>
45 void merge_1_1(ValueType& a0, ValueType& b0, CSwap cswap) {
50 template <
typename ValueType,
typename CSwap>
52 void merge_1_2(ValueType& a0, ValueType& b0, ValueType& b1, CSwap cswap) {
58 template <
typename ValueType,
typename CSwap>
60 void merge_2_1(ValueType& a0, ValueType& a1, ValueType& b0, CSwap cswap) {
66 template <
typename ValueType,
typename CSwap>
68 void merge_2_2(ValueType& a0, ValueType& a1, ValueType& b0, ValueType& b1,
76 template <
typename ValueType,
typename CSwap>
78 void merge_2_3(ValueType& a0, ValueType& a1, ValueType& b0, ValueType& b1,
79 ValueType& b2, CSwap cswap) {
86 template <
typename ValueType,
typename CSwap>
88 void merge_3_2(ValueType& a0, ValueType& a1, ValueType& a2, ValueType& b0,
89 ValueType& b1, CSwap cswap) {
96 template <
typename ValueType,
typename CSwap>
98 void merge_3_3(ValueType& a0, ValueType& a1, ValueType& a2, ValueType& b0,
99 ValueType& b1, ValueType& b2, CSwap cswap) {
106 template <
typename ValueType,
typename CSwap>
108 void merge_3_4(ValueType& a0, ValueType& a1, ValueType& a2, ValueType& b0,
109 ValueType& b1, ValueType& b2, ValueType& b3, CSwap cswap) {
116 template <
typename ValueType,
typename CSwap>
118 void merge_4_3(ValueType& a0, ValueType& a1, ValueType& a2, ValueType& a3,
119 ValueType& b0, ValueType& b1, ValueType& b2, CSwap cswap) {
126 template <
typename ValueType,
typename CSwap>
128 void merge_4_4(ValueType& a0, ValueType& a1, ValueType& a2, ValueType& a3,
129 ValueType& b0, ValueType& b1, ValueType& b2, ValueType& b3,
137 template <
typename ValueType,
typename CSwap>
139 void merge_4_5(ValueType& a0, ValueType& a1, ValueType& a2, ValueType& a3,
140 ValueType& b0, ValueType& b1, ValueType& b2, ValueType& b3,
141 ValueType& b4, CSwap cswap) {
148 template <
typename ValueType,
typename CSwap>
150 void merge_5_5(ValueType& a0, ValueType& a1, ValueType& a2, ValueType& a3,
151 ValueType& a4, ValueType& b0, ValueType& b1, ValueType& b2,
152 ValueType& b3, ValueType& b4, CSwap cswap) {
154 merge_3_3(a2, a3, a4, b2, b3, b4, cswap);
159 template <
typename ValueType,
typename CSwap>
161 void merge_5_6(ValueType& a0, ValueType& a1, ValueType& a2, ValueType& a3,
162 ValueType& a4, ValueType& b0, ValueType& b1, ValueType& b2,
163 ValueType& b3, ValueType& b4, ValueType& b5, CSwap cswap) {
165 merge_3_3(a2, a3, a4, b3, b4, b5, cswap);
166 merge_3_3(a2, a3, a4, b0, b1, b2, cswap);
170 template <
typename ValueType,
typename CSwap>
172 void merge_6_6(ValueType& a0, ValueType& a1, ValueType& a2, ValueType& a3,
173 ValueType& a4, ValueType& a5, ValueType& b0, ValueType& b1,
174 ValueType& b2, ValueType& b3, ValueType& b4, ValueType& b5,
176 merge_3_3(a0, a1, a2, b0, b1, b2, cswap);
177 merge_3_3(a3, a4, a5, b3, b4, b5, cswap);
178 merge_3_3(a3, a4, a5, b0, b1, b2, cswap);
182 template <
typename ValueType,
typename CSwap>
184 void merge_6_7(ValueType& a0, ValueType& a1, ValueType& a2, ValueType& a3,
185 ValueType& a4, ValueType& a5, ValueType& b0, ValueType& b1,
186 ValueType& b2, ValueType& b3, ValueType& b4, ValueType& b5,
187 ValueType& b6, CSwap cswap) {
188 merge_3_4(a0, a1, a2, b0, b1, b2, b3, cswap);
189 merge_3_3(a3, a4, a5, b4, b5, b6, cswap);
190 merge_3_4(a3, a4, a5, b0, b1, b2, b3, cswap);
194 template <
typename ValueType,
typename CSwap>
196 void merge_7_7(ValueType& a0, ValueType& a1, ValueType& a2, ValueType& a3,
197 ValueType& a4, ValueType& a5, ValueType& a6, ValueType& b0,
198 ValueType& b1, ValueType& b2, ValueType& b3, ValueType& b4,
199 ValueType& b5, ValueType& b6, CSwap cswap) {
200 merge_3_3(a0, a1, a2, b0, b1, b2, cswap);
201 merge_4_4(a3, a4, a5, a6, b3, b4, b5, b6, cswap);
202 merge_4_3(a3, a4, a5, a6, b0, b1, b2, cswap);
206 template <
typename ValueType,
typename CSwap>
208 void merge_7_8(ValueType& a0, ValueType& a1, ValueType& a2, ValueType& a3,
209 ValueType& a4, ValueType& a5, ValueType& a6, ValueType& b0,
210 ValueType& b1, ValueType& b2, ValueType& b3, ValueType& b4,
211 ValueType& b5, ValueType& b6, ValueType& b7, CSwap cswap) {
212 merge_3_4(a0, a1, a2, b0, b1, b2, b3, cswap);
213 merge_4_4(a3, a4, a5, a6, b4, b5, b6, b7, cswap);
214 merge_4_4(a3, a4, a5, a6, b0, b1, b2, b3, cswap);
218 template <
typename ValueType,
typename CSwap>
220 void merge_8_8(ValueType& a0, ValueType& a1, ValueType& a2, ValueType& a3,
221 ValueType& a4, ValueType& a5, ValueType& a6, ValueType& a7,
222 ValueType& b0, ValueType& b1, ValueType& b2, ValueType& b3,
223 ValueType& b4, ValueType& b5, ValueType& b6, ValueType& b7,
225 merge_4_4(a0, a1, a2, a3, b0, b1, b2, b3, cswap);
226 merge_4_4(a4, a5, a6, a7, b4, b5, b6, b7, cswap);
227 merge_4_4(a4, a5, a6, a7, b0, b1, b2, b3, cswap);
233 template <
typename ValueType,
typename CSwap = DefaultCSwap<ValueType> >
235 void sort2(ValueType& x0, ValueType& x1, CSwap cswap = CSwap()) {
240 template <
typename ValueType,
typename CSwap = DefaultCSwap<ValueType> >
242 void sort3(ValueType& x0, ValueType& x1, ValueType& x2, CSwap cswap = CSwap()) {
243 sort2(x1, x2, cswap);
248 template <
typename ValueType,
typename CSwap = DefaultCSwap<ValueType> >
250 void sort4(ValueType& x0, ValueType& x1, ValueType& x2, ValueType& x3,
251 CSwap cswap = CSwap()) {
252 sort2(x0, x1, cswap);
253 sort2(x2, x3, cswap);
258 template <
typename ValueType,
typename CSwap = DefaultCSwap<ValueType> >
260 void sort5(ValueType& x0, ValueType& x1, ValueType& x2, ValueType& x3,
261 ValueType& x4, CSwap cswap = CSwap()) {
262 sort2(x0, x1, cswap);
263 sort3(x2, x3, x4, cswap);
268 template <
typename ValueType,
typename CSwap = DefaultCSwap<ValueType> >
270 void sort6(ValueType& x0, ValueType& x1, ValueType& x2, ValueType& x3,
271 ValueType& x4, ValueType& x5, CSwap cswap = CSwap()) {
272 sort3(x0, x1, x2, cswap);
273 sort3(x3, x4, x5, cswap);
274 merge_3_3(x0, x1, x2, x3, x4, x5, cswap);
278 template <
typename ValueType,
typename CSwap = DefaultCSwap<ValueType> >
280 void sort7(ValueType& x0, ValueType& x1, ValueType& x2, ValueType& x3,
281 ValueType& x4, ValueType& x5, ValueType& x6, CSwap cswap = CSwap()) {
282 sort3(x0, x1, x2, cswap);
283 sort4(x3, x4, x5, x6, cswap);
285 x3, x4, x5, x6, cswap);
289 template <
typename ValueType,
typename CSwap = DefaultCSwap<ValueType> >
291 void sort8(ValueType& x0, ValueType& x1, ValueType& x2, ValueType& x3,
292 ValueType& x4, ValueType& x5, ValueType& x6, ValueType& x7,
293 CSwap cswap = CSwap()) {
294 sort4(x0, x1, x2, x3, cswap);
295 sort4(x4, x5, x6, x7, cswap);
297 x4, x5, x6, x7, cswap);
301 template <
typename ValueType,
typename CSwap = DefaultCSwap<ValueType> >
303 void sort9(ValueType& x0, ValueType& x1, ValueType& x2, ValueType& x3,
304 ValueType& x4, ValueType& x5, ValueType& x6, ValueType& x7,
305 ValueType& x8, CSwap cswap = CSwap()) {
306 sort4(x0, x1, x2, x3, cswap);
307 sort5(x4, x5, x6, x7, x8, cswap);
309 x4, x5, x6, x7, x8, cswap);
313 template <
typename ValueType,
typename CSwap = DefaultCSwap<ValueType> >
315 void sort10(ValueType& x0, ValueType& x1, ValueType& x2, ValueType& x3,
316 ValueType& x4, ValueType& x5, ValueType& x6, ValueType& x7,
317 ValueType& x8, ValueType& x9, CSwap cswap = CSwap()) {
318 sort5(x0, x1, x2, x3, x4, cswap);
319 sort5(x5, x6, x7, x8, x9, cswap);
321 x5, x6, x7, x8, x9, cswap);
325 template <
typename ValueType,
typename CSwap = DefaultCSwap<ValueType> >
327 void sort11(ValueType& x0, ValueType& x1, ValueType& x2, ValueType& x3,
328 ValueType& x4, ValueType& x5, ValueType& x6, ValueType& x7,
329 ValueType& x8, ValueType& x9, ValueType& x10,
330 CSwap cswap = CSwap()) {
331 sort5(x0, x1, x2, x3, x4, cswap);
332 sort6(x5, x6, x7, x8, x9, x10, cswap);
334 x5, x6, x7, x8, x9, x10, cswap);
338 template <
typename ValueType,
typename CSwap = DefaultCSwap<ValueType> >
340 void sort12(ValueType& x0, ValueType& x1, ValueType& x2, ValueType& x3,
341 ValueType& x4, ValueType& x5, ValueType& x6, ValueType& x7,
342 ValueType& x8, ValueType& x9, ValueType& x10, ValueType& x11,
343 CSwap cswap = CSwap()) {
344 sort6(x0, x1, x2, x3, x4, x5, cswap);
345 sort6(x6, x7, x8, x9, x10, x11, cswap);
347 x6, x7, x8, x9, x10, x11, cswap);
351 template <
typename ValueType,
typename CSwap = DefaultCSwap<ValueType> >
353 void sort13(ValueType& x0, ValueType& x1, ValueType& x2, ValueType& x3,
354 ValueType& x4, ValueType& x5, ValueType& x6, ValueType& x7,
355 ValueType& x8, ValueType& x9, ValueType& x10, ValueType& x11,
356 ValueType& x12, CSwap cswap = CSwap()) {
357 sort6(x0, x1, x2, x3, x4, x5, cswap);
358 sort7(x6, x7, x8, x9, x10, x11, x12, cswap);
360 x6, x7, x8, x9, x10, x11, x12, cswap);
364 template <
typename ValueType,
typename CSwap = DefaultCSwap<ValueType> >
366 void sort14(ValueType& x0, ValueType& x1, ValueType& x2, ValueType& x3,
367 ValueType& x4, ValueType& x5, ValueType& x6, ValueType& x7,
368 ValueType& x8, ValueType& x9, ValueType& x10, ValueType& x11,
369 ValueType& x12, ValueType& x13, CSwap cswap = CSwap()) {
370 sort7(x0, x1, x2, x3, x4, x5, x6, cswap);
371 sort7(x7, x8, x9, x10, x11, x12, x13, cswap);
373 x7, x8, x9, x10, x11, x12, x13, cswap);
377 template <
typename ValueType,
typename CSwap = DefaultCSwap<ValueType> >
379 void sort15(ValueType& x0, ValueType& x1, ValueType& x2, ValueType& x3,
380 ValueType& x4, ValueType& x5, ValueType& x6, ValueType& x7,
381 ValueType& x8, ValueType& x9, ValueType& x10, ValueType& x11,
382 ValueType& x12, ValueType& x13, ValueType& x14,
383 CSwap cswap = CSwap()) {
384 sort7(x0, x1, x2, x3, x4, x5, x6, cswap);
385 sort8(x7, x8, x9, x10, x11, x12, x13, x14, cswap);
387 x7, x8, x9, x10, x11, x12, x13, x14, cswap);
391 template <
typename ValueType,
typename CSwap = DefaultCSwap<ValueType> >
393 void sort16(ValueType& x0, ValueType& x1, ValueType& x2, ValueType& x3,
394 ValueType& x4, ValueType& x5, ValueType& x6, ValueType& x7,
395 ValueType& x8, ValueType& x9, ValueType& x10, ValueType& x11,
396 ValueType& x12, ValueType& x13, ValueType& x14, ValueType& x15,
397 CSwap cswap = CSwap()) {
398 sort8(x0, x1, x2, x3, x4, x5, x6, x7, cswap);
399 sort8(x8, x9, x10, x11, x12, x13, x14, x15, cswap);
400 merge_8_8(x0, x1, x2, x3, x4, x5, x6, x7,
401 x8, x9, x10, x11, x12, x13, x14, x15, cswap);
408 template <
typename Iterator,
typename Comparator =
409 std::less<typename std::iterator_traits<Iterator>::value_type> >
410 static void sort(Iterator a, Iterator b, Comparator cmp = Comparator()) {
419 sort2(a[0], a[1], cswap);
422 sort3(a[0], a[1], a[2], cswap);
425 sort4(a[0], a[1], a[2], a[3], cswap);
428 sort5(a[0], a[1], a[2], a[3], a[4], cswap);
431 sort6(a[0], a[1], a[2], a[3], a[4], a[5], cswap);
434 sort7(a[0], a[1], a[2], a[3], a[4], a[5], a[6], cswap);
437 sort8(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], cswap);
440 sort9(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], cswap);
443 sort10(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9],
447 sort11(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9],
451 sort12(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9],
452 a[10], a[11], cswap);
455 sort13(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9],
456 a[10], a[11], a[12], cswap);
459 sort14(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9],
460 a[10], a[11], a[12], a[13], cswap);
463 sort15(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9],
464 a[10], a[11], a[12], a[13], a[14], cswap);
467 sort16(a[0], a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], a[9],
468 a[10], a[11], a[12], a[13], a[14], a[15], cswap);
486 #endif // !TLX_SORT_NETWORKS_BOSE_NELSON_PARAMETER_HEADER
static void merge_3_3(ValueType &a0, ValueType &a1, ValueType &a2, ValueType &b0, ValueType &b1, ValueType &b2, CSwap cswap)
merge network for element arrays length three and three
static void merge_3_4(ValueType &a0, ValueType &a1, ValueType &a2, ValueType &b0, ValueType &b1, ValueType &b2, ValueType &b3, CSwap cswap)
merge network for element arrays length three and four
static void sort14(ValueType &x0, ValueType &x1, ValueType &x2, ValueType &x3, ValueType &x4, ValueType &x5, ValueType &x6, ValueType &x7, ValueType &x8, ValueType &x9, ValueType &x10, ValueType &x11, ValueType &x12, ValueType &x13, CSwap cswap=CSwap())
Bose-Nelson sorting network for fourteen elements.
static void merge_4_5(ValueType &a0, ValueType &a1, ValueType &a2, ValueType &a3, ValueType &b0, ValueType &b1, ValueType &b2, ValueType &b3, ValueType &b4, CSwap cswap)
merge network for element arrays length four and five
static void sort16(ValueType &x0, ValueType &x1, ValueType &x2, ValueType &x3, ValueType &x4, ValueType &x5, ValueType &x6, ValueType &x7, ValueType &x8, ValueType &x9, ValueType &x10, ValueType &x11, ValueType &x12, ValueType &x13, ValueType &x14, ValueType &x15, CSwap cswap=CSwap())
Bose-Nelson sorting network for sixteen elements.
static void merge_2_2(ValueType &a0, ValueType &a1, ValueType &b0, ValueType &b1, CSwap cswap)
merge network for element arrays length two and two
static void merge_8_8(ValueType &a0, ValueType &a1, ValueType &a2, ValueType &a3, ValueType &a4, ValueType &a5, ValueType &a6, ValueType &a7, ValueType &b0, ValueType &b1, ValueType &b2, ValueType &b3, ValueType &b4, ValueType &b5, ValueType &b6, ValueType &b7, CSwap cswap)
merge network for element arrays length eight and eight
static void merge_7_7(ValueType &a0, ValueType &a1, ValueType &a2, ValueType &a3, ValueType &a4, ValueType &a5, ValueType &a6, ValueType &b0, ValueType &b1, ValueType &b2, ValueType &b3, ValueType &b4, ValueType &b5, ValueType &b6, CSwap cswap)
merge network for element arrays length seven and seven
static void merge_7_8(ValueType &a0, ValueType &a1, ValueType &a2, ValueType &a3, ValueType &a4, ValueType &a5, ValueType &a6, ValueType &b0, ValueType &b1, ValueType &b2, ValueType &b3, ValueType &b4, ValueType &b5, ValueType &b6, ValueType &b7, CSwap cswap)
merge network for element arrays length seven and eight
static void sort4(ValueType &x0, ValueType &x1, ValueType &x2, ValueType &x3, CSwap cswap=CSwap())
Bose-Nelson sorting network for four elements.
static void merge_6_6(ValueType &a0, ValueType &a1, ValueType &a2, ValueType &a3, ValueType &a4, ValueType &a5, ValueType &b0, ValueType &b1, ValueType &b2, ValueType &b3, ValueType &b4, ValueType &b5, CSwap cswap)
merge network for element arrays length six and six
static void merge_1_2(ValueType &a0, ValueType &b0, ValueType &b1, CSwap cswap)
merge network for element arrays length one and two
static void sort12(ValueType &x0, ValueType &x1, ValueType &x2, ValueType &x3, ValueType &x4, ValueType &x5, ValueType &x6, ValueType &x7, ValueType &x8, ValueType &x9, ValueType &x10, ValueType &x11, CSwap cswap=CSwap())
Bose-Nelson sorting network for twelve elements.
static void sort5(ValueType &x0, ValueType &x1, ValueType &x2, ValueType &x3, ValueType &x4, CSwap cswap=CSwap())
Bose-Nelson sorting network for five elements.
Conditional swap implementation used for sorting networks: trivial portable C++ implementation with c...
static void sort10(ValueType &x0, ValueType &x1, ValueType &x2, ValueType &x3, ValueType &x4, ValueType &x5, ValueType &x6, ValueType &x7, ValueType &x8, ValueType &x9, CSwap cswap=CSwap())
Bose-Nelson sorting network for ten elements.
static void merge_2_1(ValueType &a0, ValueType &a1, ValueType &b0, CSwap cswap)
merge network for element arrays length two and one
static void merge_5_6(ValueType &a0, ValueType &a1, ValueType &a2, ValueType &a3, ValueType &a4, ValueType &b0, ValueType &b1, ValueType &b2, ValueType &b3, ValueType &b4, ValueType &b5, CSwap cswap)
merge network for element arrays length five and six
static void merge_2_3(ValueType &a0, ValueType &a1, ValueType &b0, ValueType &b1, ValueType &b2, CSwap cswap)
merge network for element arrays length two and three
static void sort3(ValueType &x0, ValueType &x1, ValueType &x2, CSwap cswap=CSwap())
Bose-Nelson sorting network for three elements.
static void sort9(ValueType &x0, ValueType &x1, ValueType &x2, ValueType &x3, ValueType &x4, ValueType &x5, ValueType &x6, ValueType &x7, ValueType &x8, CSwap cswap=CSwap())
Bose-Nelson sorting network for nine elements.
static void merge_5_5(ValueType &a0, ValueType &a1, ValueType &a2, ValueType &a3, ValueType &a4, ValueType &b0, ValueType &b1, ValueType &b2, ValueType &b3, ValueType &b4, CSwap cswap)
merge network for element arrays length five and five
static void sort7(ValueType &x0, ValueType &x1, ValueType &x2, ValueType &x3, ValueType &x4, ValueType &x5, ValueType &x6, CSwap cswap=CSwap())
Bose-Nelson sorting network for seven elements.
static void merge_4_3(ValueType &a0, ValueType &a1, ValueType &a2, ValueType &a3, ValueType &b0, ValueType &b1, ValueType &b2, CSwap cswap)
merge network for element arrays length four and three
static void sort15(ValueType &x0, ValueType &x1, ValueType &x2, ValueType &x3, ValueType &x4, ValueType &x5, ValueType &x6, ValueType &x7, ValueType &x8, ValueType &x9, ValueType &x10, ValueType &x11, ValueType &x12, ValueType &x13, ValueType &x14, CSwap cswap=CSwap())
Bose-Nelson sorting network for fifteen elements.
static void merge_1_1(ValueType &a0, ValueType &b0, CSwap cswap)
merge network for element arrays length one and one
static void merge_6_7(ValueType &a0, ValueType &a1, ValueType &a2, ValueType &a3, ValueType &a4, ValueType &a5, ValueType &b0, ValueType &b1, ValueType &b2, ValueType &b3, ValueType &b4, ValueType &b5, ValueType &b6, CSwap cswap)
merge network for element arrays length six and seven
static void sort8(ValueType &x0, ValueType &x1, ValueType &x2, ValueType &x3, ValueType &x4, ValueType &x5, ValueType &x6, ValueType &x7, CSwap cswap=CSwap())
Bose-Nelson sorting network for eight elements.
static void merge_4_4(ValueType &a0, ValueType &a1, ValueType &a2, ValueType &a3, ValueType &b0, ValueType &b1, ValueType &b2, ValueType &b3, CSwap cswap)
merge network for element arrays length four and four
static void sort13(ValueType &x0, ValueType &x1, ValueType &x2, ValueType &x3, ValueType &x4, ValueType &x5, ValueType &x6, ValueType &x7, ValueType &x8, ValueType &x9, ValueType &x10, ValueType &x11, ValueType &x12, CSwap cswap=CSwap())
Bose-Nelson sorting network for thirteen elements.
static void sort2(ValueType &x0, ValueType &x1, CSwap cswap=CSwap())
Bose-Nelson sorting network for two elements.
static void sort6(ValueType &x0, ValueType &x1, ValueType &x2, ValueType &x3, ValueType &x4, ValueType &x5, CSwap cswap=CSwap())
Bose-Nelson sorting network for six elements.
static void sort(Iterator a, Iterator b, Comparator cmp=Comparator())
Call Bose-Network sorting network for up to sixteen elements with given comparison method...
static void merge_3_2(ValueType &a0, ValueType &a1, ValueType &a2, ValueType &b0, ValueType &b1, CSwap cswap)
merge network for element arrays length three and two
static void sort11(ValueType &x0, ValueType &x1, ValueType &x2, ValueType &x3, ValueType &x4, ValueType &x5, ValueType &x6, ValueType &x7, ValueType &x8, ValueType &x9, ValueType &x10, CSwap cswap=CSwap())
Bose-Nelson sorting network for eleven elements.