是否可以使用预处理器对数组进行排序?

发布于 2024-08-29 09:09:19 字数 733 浏览 4 评论 0原文

我有很多很长的数组。不可能进行运行时排序。手动对它们进行排序也很耗时。此外,以后可以按任意顺序添加新元素,因此我想使用 C 预处理器按值对它们进行排序,或者可能有任何编译器标志(GCC)?

例如:

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

必须排序:

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


SOLVED

好的,这是我的解决方案:

  1. 手动将每个数组复制并粘贴到名为 tobesorted.c 的临时文件中,
  2. 按第二列排序: sort -b -i --key=2 tobesorted.c
  3. 复制并粘贴输出回原始文件。

实际上,如果能够直接从预处理器调用“sort”就太好了(我希望至少 GCC 以某种方式支持此类功能,但似乎并不支持)。

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).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

樱&纷飞 2024-09-05 09:09:19

这样做。

  1. 将您的巨型数组放入文件中。

  2. 使用内置sort对文件进行排序

    使用内置

  3. 编写一个小程序来创建C代码从文件中。写C程序的AC程序就可以了。您可以使用 Python 及其一些很酷的模板包来简化这项工作。

  4. 编译由转换为 C 代码的排序文件和程序的其余部分组成的剩余程序。

    编译由转换

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.

梦幻之岛 2024-09-05 09:09:19

不,这是不可能的。您不能使用预处理器执行字符串操作(连接除外)。而且您也无法将字符串与模板元编程进行比较。

[编辑]您可以做的是将数据结构放入一个文件中,该文件将由外部构建脚本(例如unix“排序”实用程序)进行预处理,然后修改您的makefile/项目,以便在构建时,您生成一个带有(已排序的)初始化数组的 C 文件

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

疏忽 2024-09-05 09:09:19

我不认为你可以在 gcc 预处理器中做到这一点,从来没有见过可以做你正在寻找的事情的东西。

但是您可以用您最喜欢的脚本语言(python、perl、sed 等)编写自己的“预处理器”,它会在 gcc 启动之前对这些值进行排序。

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.

笑叹一世浮沉 2024-09-05 09:09:19

我认为不可能使用预处理器,但您可以使用 sort#include 的组合来实现所需的效果:

仅将值放入单独的文件中values.h 排序键位于前面(您需要为此重新排列您的struct sometype):

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

在您的 Makefile 中,使用Unix 命令 sort 将该文件排序到 values_sorted.h 中:

sort < values.h > values_sorted.h

在您的实际代码中,包含排序后的文件:

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

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"
};
那小子欠揍 2024-09-05 09:09:19

以下适用于两个和三个元素:

// 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 }

但我想说,很明显,这种方法不能推广到任意大小的数组。
我认为这是不可能的,但我仍然没有这个猜想的正式证明。

The following worked 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'd say that it is clear that this approach does not generalize to arbitrary sized arrays.
I suppose it is not possible, but I still don't have a formal proof for this conjecture.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文