[原创]写了个for_each宏,放出来给各位评审评审。
by mr. dave
at 2010-11-19 00:13:00
original http://www.cnblogs.com/dave_cn/archive/2010/11/19/1881332.html
摘要: 转载请注明出处。http://www.cnblogs.com/dave_cn/
之前在编码的时候遇到几次需要将这么几个值统一处理下,当时也没有想到什么好招,就每个量都写了相同的代码,一直觉得很土,加之使用python时的for...in...的美好感觉,便写了个for_each的宏。
for_each宏能够很方便遍历一组零散的元素,而且在遍历完之后将不再需要的临时申请的空间释放掉。set_list_m每次添加一个元素,set_list_f则会一次将所需要遍历的元素全部加入。
因为懒set_list_m宏中应该给变量加括号的也就懒着加了。
如果有问题的地方,还请各位多多指正!
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdarg.h>
5
6 struct list_t {
7 void *value;
8 struct list_t *next;
9 };
10
11 #define set_list_m(_p, _list) \
12 do { \
13 if (_list == NULL) { \
14 _list = (struct list_t *)malloc(sizeof(struct list_t)); \
15 _list->next = NULL; \
16 _list->value = _p; \
17 } \
18 else { \
19 struct list_t *_tmp = _list; \
20 while (_tmp->next != NULL) { \
21 _tmp = _tmp->next; \
22 } \
23 _tmp->next = (struct list_t *)malloc(sizeof(struct list_t)); \
24 _tmp = _tmp->next; \
25 _tmp->next = NULL; \
26 _tmp->value = _p; \
27 } \
28 } \
29 while (0)
30
31 /*
32 * 写for_each宏的原由是为了方便遍历一组零散的元素(这些元素并没有通过数组/列表等组织在一起)
33 * 而python中的for...in...也不停诱惑着我
34 */
35 #define for_each(_p, _list) \
36 for ( struct list_t *_tmp = (_list), *_front; \
37 (_tmp != NULL) && (((_p) = _tmp->value) || 1); \
38 _front = _tmp, _tmp = _tmp->next, free(_front) )
39
40 /*
41 * struct list_t *set_list_f(void *p, ...)
42 * @parameter: 需要要以NULL结束
43 * @return: 返回for_each可用的list
44 */
45 struct list_t *set_list_f(void *p, .../* NULL */)
46 {
47 va_list arg_ptr;
48 va_start(arg_ptr, p);
49
50 struct list_t *_tmpnode = NULL;
51 struct list_t *list = NULL;
52 void *_tmpval = p;
53 while (_tmpval != NULL) {
54 if (_tmpnode == NULL) {
55 _tmpnode = (struct list_t *)malloc(sizeof(struct list_t));
56 list = _tmpnode;
57 }
58 else {
59 _tmpnode->next = (struct list_t *)malloc(sizeof(struct list_t));
60 _tmpnode = _tmpnode->next;
61 }
62 _tmpnode->next = NULL;
63 _tmpnode->value = _tmpval;
64
65 _tmpval = va_arg(arg_ptr, void *);
66 }
67
68 va_end(arg_ptr);
69
70 return list;
71 }
72
73 int main()
74 {
75 //////////////////////////////////////////////////////
76 int a = 1, b = 2, c = 3;
77
78 struct list_t *l1 = NULL;
79 set_list_m(&a, l1);
80 set_list_m(&b, l1);
81 set_list_m(&c, l1);
82
83 int *p1 = NULL;
84 for_each(p1, l1) {
85 printf("for_each\n");
86 *p1 = 4;
87 }
88
89 printf("a = [%d], b = [%d], c = [%d]\n", a, b, c);
90
91 //////////////////////////////////////////////////////
92 a = 1, b = 2, c = 3;
93
94 struct list_t *l2 = set_list_f(&a, &b, &c, NULL);
95
96 int *p2 = NULL;
97 for_each(p2, l2) {
98 printf("for_each\n");
99 *p2 = 5;
100 }
101
102 printf("a = [%d], b = [%d], c = [%d]\n", a, b, c);
103
104 return 0;
105 }
2 #include <stdlib.h>
3 #include <string.h>
4 #include <stdarg.h>
5
6 struct list_t {
7 void *value;
8 struct list_t *next;
9 };
10
11 #define set_list_m(_p, _list) \
12 do { \
13 if (_list == NULL) { \
14 _list = (struct list_t *)malloc(sizeof(struct list_t)); \
15 _list->next = NULL; \
16 _list->value = _p; \
17 } \
18 else { \
19 struct list_t *_tmp = _list; \
20 while (_tmp->next != NULL) { \
21 _tmp = _tmp->next; \
22 } \
23 _tmp->next = (struct list_t *)malloc(sizeof(struct list_t)); \
24 _tmp = _tmp->next; \
25 _tmp->next = NULL; \
26 _tmp->value = _p; \
27 } \
28 } \
29 while (0)
30
31 /*
32 * 写for_each宏的原由是为了方便遍历一组零散的元素(这些元素并没有通过数组/列表等组织在一起)
33 * 而python中的for...in...也不停诱惑着我
34 */
35 #define for_each(_p, _list) \
36 for ( struct list_t *_tmp = (_list), *_front; \
37 (_tmp != NULL) && (((_p) = _tmp->value) || 1); \
38 _front = _tmp, _tmp = _tmp->next, free(_front) )
39
40 /*
41 * struct list_t *set_list_f(void *p, ...)
42 * @parameter: 需要要以NULL结束
43 * @return: 返回for_each可用的list
44 */
45 struct list_t *set_list_f(void *p, .../* NULL */)
46 {
47 va_list arg_ptr;
48 va_start(arg_ptr, p);
49
50 struct list_t *_tmpnode = NULL;
51 struct list_t *list = NULL;
52 void *_tmpval = p;
53 while (_tmpval != NULL) {
54 if (_tmpnode == NULL) {
55 _tmpnode = (struct list_t *)malloc(sizeof(struct list_t));
56 list = _tmpnode;
57 }
58 else {
59 _tmpnode->next = (struct list_t *)malloc(sizeof(struct list_t));
60 _tmpnode = _tmpnode->next;
61 }
62 _tmpnode->next = NULL;
63 _tmpnode->value = _tmpval;
64
65 _tmpval = va_arg(arg_ptr, void *);
66 }
67
68 va_end(arg_ptr);
69
70 return list;
71 }
72
73 int main()
74 {
75 //////////////////////////////////////////////////////
76 int a = 1, b = 2, c = 3;
77
78 struct list_t *l1 = NULL;
79 set_list_m(&a, l1);
80 set_list_m(&b, l1);
81 set_list_m(&c, l1);
82
83 int *p1 = NULL;
84 for_each(p1, l1) {
85 printf("for_each\n");
86 *p1 = 4;
87 }
88
89 printf("a = [%d], b = [%d], c = [%d]\n", a, b, c);
90
91 //////////////////////////////////////////////////////
92 a = 1, b = 2, c = 3;
93
94 struct list_t *l2 = set_list_f(&a, &b, &c, NULL);
95
96 int *p2 = NULL;
97 for_each(p2, l2) {
98 printf("for_each\n");
99 *p2 = 5;
100 }
101
102 printf("a = [%d], b = [%d], c = [%d]\n", a, b, c);
103
104 return 0;
105 }
作者: mr. dave 发表于 2010-11-19 00:13 原文链接
最新新闻:
· 如何评估软件进度(2010-11-19 07:29)
· 传谷歌曾出价25亿美元收购Twitter被指污辱(2010-11-19 07:28)
· MySpace与Facebook联合发布Mashup同步功能(2010-11-19 07:27)
· 传亚马逊1亿美元投资团购网站LivingSocial(2010-11-19 07:24)
· 扎克伯格创业初期网站Facemash拍出3万美元(2010-11-19 07:23)