Open Model Railroad Network (OpenMRN)
Loading...
Searching...
No Matches
median.hxx
Go to the documentation of this file.
1
35#ifndef _UTILS_MEDIAN_HXX_
36#define _UTILS_MEDIAN_HXX_
37
41inline void __attribute__((__always_inline__, optimize("O3")))
42comp_swap(unsigned &a, unsigned &b)
43{
44 if (a > b)
45 {
46 unsigned tmp = a;
47 a = b;
48 b = tmp;
49 }
50}
51
54inline unsigned __attribute__((__always_inline__, optimize("O3")))
55median_5(unsigned a, unsigned b, unsigned c, unsigned d, unsigned e)
56{
57 comp_swap(a, c);
58 comp_swap(b, d);
59 comp_swap(a, b);
60 comp_swap(c, d);
61 // Now: a is the smallest of [a,b,c,d]
62 // d is the greatest of [a,b,c,d]; neither of these can be the median.
63 comp_swap(b, c);
64 // Now: [a,b,c,d] are sorted
65 if (e > c) {
66 return c;
67 }
68 if (e < b) {
69 return b;
70 }
71 return e;
72}
73
74#endif // _UTILS_MEDIAN_HXX_
void comp_swap(unsigned &a, unsigned &b)
Sorts two values.
Definition median.hxx:42
unsigned median_5(unsigned a, unsigned b, unsigned c, unsigned d, unsigned e)
Computes the median of five values.
Definition median.hxx:55