views:

187

answers:

4

I have a number of very long arrays. No run-time sort is possible. It is also time consuming to sort them manually. Moreover, new elements can be added in any order later, so I would like to sort them by value using C preprocessor or maybe there is any compilers flag (GCC)?

For example:

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

must be sorted so:

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


SOLVED

Ok, here is my solution:

1) Manually copy&paste each array into a temporary file called tobesorted.c

2) Sort it by 2nd column: sort -b -i --key=2 tobesorted.c

3) Copy&paste output back into original file.

Actually, it would be nice to have some possibility to call "sort" directly from the preprocessor (I had a hope that at least GCC somehow support such features, but it seems that it doesn't).

+4  A: 

No, it is not possible. You cannot do string operations (other than concatenation) with the preprocessor. And you can't compare strings with template metaprograming, either.

[edit] What you could do is put your datastructure in a file that is meant to be preprocessed by an external build script (e.g. the unix "sort" utility), and then modify your makefile/project so that at build time, you generate a C file with the (sorted) initialized arrays

Virgil
+1  A: 

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

But you could write your own "preprocessor" in your favourite scripting language (python, perl, sed etc...) that would sort those values before gcc kicks in.

hhafez
+11  A: 

Do this.

  1. Put your giant array in a file.

  2. Sort the file with the built-in sort

  3. Write a small program to create C code from the file. A C program that writes C programs is fine. You can use Python and some of it's cool template packages to make this job simpler.

  4. Compile the remaining program consisting of the sorted file transformed into C code plus the rest of your program.

S.Lott
And modify your Makefile to do all this automatically.
anon
3 alt. load the file and build the array in the program at startup, no sorting required.
Tobias Wärre
@Tobias: I cannot use run-time sort (because it is an embedded system and very fast startup is required)
psihodelia
I cannot use external applications to sort, because of the specific revision control system. I can make all this for myself, but I have to update files in Revision System (and I cannot insert any py/sh file).
psihodelia
@psihodelia: Ok, I merely suggested that you'd build the array from the file at the beginning, and that the file should be sorted before it was opened. But perhaps that would be too slow too.
Tobias Wärre
+1  A: 

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

Put just the values into a seperate file values.h with the sort key being in front (you will need to rearrange your struct sometype for this):

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

In your Makefile, use the Unix command sort to sort that file into values_sorted.h:

sort < values.h > values_sorted.h

In your actual code, include the sorted file:

sometype S[] = {
#include "values_sorted.h"
};
digitalarbeiter