Is it possible to sort arrays using a preprocessor?

I have some very long arrays. Sorting at runtime is possible. Sorting them also takes a lot of time. Moreover, new elements can be added in any order later, so I would like to sort them by value using the C preprocessor, or maybe there is some compiler flag (GCC)?

For example:

sometype S[] = { {somevals, "BOB", someothervals}, {somevals, "ALICE", someothervals}, {somevals, "TIM", someothervals}, } 

should be sorted like this:

 sometype S[] = { {somevals, "ALICE", someothervals}, {somevals, "BOB", someothervals}, {somevals, "TIM", someothervals}, } 


solvable

Ok, here is my solution:

  • Manually copy and paste each array into a temporary tobesorted.c file
  • Column 2 sort -b -i --key=2 tobesorted.c : sort -b -i --key=2 tobesorted.c
  • Copy and paste the output back into the source file.

Actually, it would be nice to be able to call “sorting” directly from the preprocessor (I had the hope that at least GCC somehow supported such functions, but it seems that this is not so).

+6
c gcc sorting c-preprocessor
source share
5 answers

No, It is Immpossible. You cannot perform string operations (other than concatenation) with the preprocessor. And you also cannot compare strings with the template metaprogram.

[edit] What you can do is put your data structure in a file that needs to be pre-processed by an external build script (like the unix sorting utility), and then change your makefile / project so that at build time, you create a C file with (sorted) initialized arrays

+5
source share

Do it.

  • Put your giant array in a file.

  • Sort file with built-in sort

  • Write a small program to create C code from a file. Program C, which records C programs, is great. You can use Python and some of its cool template packages to simplify this work.

  • Compile the rest of the program, consisting of a sorted file converted to C code, as well as the rest of your program.

+11
source share

I don’t think you can do it in the gcc preprocessor, you have never seen something that could do what you are looking for.

But you can write your own “preprocessor” in your favorite scripting language (python, perl, sed, etc.) that will sort these values ​​before gcc starts working.

+1
source share

I cannot think of the possibility of using a preprocessor, but you can use a combination of sort and #include to achieve the desired effect:

Put only the values ​​in a separate values.h file with the sort key in front (for this you will need to change your struct sometype ):

 {"BOB", somevals, someothervals}, {"ALICE", somevals, someothervals}, {"TIM", somevals, someothervals}, 

In your Makefile, use the Unix sort command to sort this file in values_sorted.h :

 sort < values.h > values_sorted.h 

In your actual code, include the sorted file:

 sometype S[] = { #include "values_sorted.h" }; 
+1
source share

The following actions were performed for two and three elements:

 // Experiment: static sort: #define STATIC_SORT2(CMP, a, b) CMP(a,b) <= 0 ?(a):(b), CMP(a,b) <= 0 ? (b):(a), #define STATIC_SORT3(CMP, a, b, c) \ (CMP(a,b) <= 0 && CMP(a,c) <= 0 ? (a) : \ CMP(b,a) <= 0 && CMP(b,c) <= 0 ? (b) : \ (c)), \ (CMP(a,b) <= 0 && CMP(a,c) <= 0 ? ( CMP(b,c) <= 0 ? (b) : (c) ) : \ CMP(b,a) <= 0 && CMP(b,c) <= 0 ? ( CMP(a,c) <= 0 ? (a) : (c) ) : \ (CMP(a,b) <= 0 ? (a) : (b))), \ (CMP(a,c) <= 0 && CMP(b,c) <= 0 ? (c) : \ CMP(a,b) <= 0 && CMP(c,b) <= 0 ? (b) : \ (a)) // Example: // #define STATIC_INT_CMP(a,b) ((int)(a) - (int)(b)) // int sorted[] = { STATIC_SORT3(STATIC_INT_CMP, 2, 3, 1 } // gives { 1, 2, 3 } // #define STATIC_INT_COCMP(a,b) ((int)(b) - (int)(a)) // int cosorted[] = { STATIC_SORT3(STATIC_INT_COCMP, 2, 3, 1 } // gives { 3, 2, 1 } 

But I would say that it is clear that this approach does not generalize to arrays of arbitrary size. I suppose this is impossible, but I still have no formal proof of this hypothesis.

0
source share

All Articles