写了个for_each宏,放出来给各位评审评审。

摘要: 转载请注明出处。http://www.cnblogs.com/dave_cn/ 

之前在编码的时候遇到几次需要将这么几个值统一处理下,当时也没有想到什么好招,就每个量都写了相同的代码,一直觉得很土,加之使用python时的for...in...的美好感觉,便写了个for_each的宏。

for_each宏能够很方便遍历一组零散的元素,而且在遍历完之后将不再需要的临时申请的空间释放掉。

set_list_m每次添加一个元素,set_list_f则会一次将所需要遍历的元素全部加入。

因为懒set_list_m宏中应该给变量加括号的也就懒着加了。

如果有问题的地方,还请各位多多指正!


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
 
struct list_t {
        void                 *value;
        struct list_t         *next;
};
 
#define set_list_m(_p, _list) \
        do { \
                if (_list == NULL) { \
                        _list = (struct list_t *)malloc(sizeof(struct list_t)); \
                        _list->next = NULL; \
                        _list->value = _p; \
                } \
                else { \
                        struct list_t *_tmp = _list; \
                        while (_tmp->next != NULL) { \
                                _tmp = _tmp->next; \
                        } \
                        _tmp->next = (struct list_t *)malloc(sizeof(struct list_t)); \
                        _tmp = _tmp->next; \
                        _tmp->next = NULL; \
                        _tmp->value = _p; \
                } \
        } \
        while (0)
 
/*
 * 写for_each宏的原由是为了方便遍历一组零散的元素(这些元素并没有通过数组/列表等组织在一起)
 * 而python中的for...in...也不停诱惑着我
 */
#define for_each(_p, _list) \
        for ( struct list_t *_tmp = (_list), *_front; \
              (_tmp != NULL) && (((_p) = _tmp->value) || 1); \
              _front = _tmp, _tmp = _tmp->next, free(_front) )
 
/*
 * struct list_t *set_list_f(void *p, ...)
 * @parameter: 需要要以NULL结束
 * @return:    返回for_each可用的list
 */
struct list_t *set_list_f(void *p, .../* NULL */)
{
        va_list arg_ptr;
        va_start(arg_ptr, p);
 
        struct list_t *_tmpnode = NULL;
        struct list_t *list = NULL;
        void *_tmpval = p;
        while (_tmpval != NULL) {
                if (_tmpnode == NULL) {
                        _tmpnode = (struct list_t *)malloc(sizeof(struct list_t));
                        list = _tmpnode;
                }
                else {
                        _tmpnode->next = (struct list_t *)malloc(sizeof(struct list_t));
                        _tmpnode = _tmpnode->next;
                }
                _tmpnode->next = NULL;
                _tmpnode->value = _tmpval;
 
                _tmpval = va_arg(arg_ptr, void *);
        }
 
        va_end(arg_ptr);
 
        return list;
}
 
int main()
{
        //////////////////////////////////////////////////////
        int a = 1, b = 2, c = 3;
 
        struct list_t *l1 = NULL;
        set_list_m(&a, l1);
        set_list_m(&b, l1);
        set_list_m(&c, l1);
 
        int *p1 = NULL;
        for_each(p1, l1) {
                printf("for_each\n");
                *p1 = 4;
        }
 
        printf("a = [%d], b = [%d], c = [%d]\n", a, b, c);
         
        //////////////////////////////////////////////////////
        a = 1, b = 2, c = 3;
 
        struct list_t *l2 = set_list_f(&a, &b, &c, NULL);
 
        int *p2 = NULL;
        for_each(p2, l2) {
                printf("for_each\n");
                *p2 = 5;
        }
 
        printf("a = [%d], b = [%d], c = [%d]\n", a, b, c);
         
        return 0;
}

posted @   mr. dave  阅读(2943)  评论(3编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
点击右上角即可分享
微信分享提示