Is it possible to sort elements at compile time in C?
Syntax is of secondary importance, I was thinking of a macro like this:
SORT(9, -1, 12, 4) // expands to: -1, 4, 9, 12
SORT(dog, cat, cow) // expands to: cat, cow, dog
but I won't frown at any API as long as it sorts without issuing a single CPU instruction.
The requirements are pretty lax:
sort
or home-brewed code generators are not welcome as they complicate the build step.SORT_5
to sort 5 elements is OK.I realize the solution will probably resort to some language voodoo that barely compiles, I just want to know if it's possible.
Here is a macro approach:
#define GET_1_of_3(a, b, c) ((a) < (b) ? ((c) < (a) ? (c) : (a)) : ((c) < (b) ? (c) : (b)))
#define GET_2_of_3(a, b, c) ((c) > (a) && (c) < (b) || (c) < (a) && (c) > (b) ? (c) : ((b) > (a) && (b) < (c) || (b) < (a) && (b) > (c) ? (b) : (a)))
#define GET_3_of_3(a, b, c) ((a) > (b) ? ((c) > (a) ? (c) : (a)) : ((c) > (b) ? (c) : (b)))
#define SORT_3(a, b, c) GET_1_of_3(a, b, c),GET_2_of_3(a, b, c),GET_3_of_3(a, b, c)
void main(){
int x[3] = { SORT_3(6,2,3) };
printf("%d, %d, %d", x[0], x[1], x[2]);
}
This works for int
and works in C, but it's not possible for strings without const_expr
from C++. Obviously, you're in for a lot of macro-writing to support a large number of SORT_X
.
Let's consider sorting numbers that can only be 0 or 1. For two numbers, SORT2
in the following code can sort them:
#define SORT2(a,b) SORT2_##a##b
#define SORT2_00 0,0
#define SORT2_01 0,1
#define SORT2_10 0,1
#define SORT2_11 1,1
This can of course be expanded to larger ranges and more arguments.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With