Tab complete only interesting files
[cmus.git] / iter.h
blobc22944bedd7b07bdf6136eb267b05ba3ea035961
1 /*
2 * Copyright 2005 Timo Hirvonen
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
17 * 02111-1307, USA.
20 #ifndef _ITER_H
21 #define _ITER_H
23 #include "debug.h"
25 #include <stdlib.h>
27 struct iter {
28 /* this usually points to the list head */
29 void *data0;
31 /* these point to the list item, for simple lists data2 is usually NULL */
32 void *data1;
33 void *data2;
36 static inline void iter_init(struct iter *iter)
38 iter->data0 = NULL;
39 iter->data1 = NULL;
40 iter->data2 = NULL;
43 static inline void iter_head(struct iter *iter)
45 iter->data1 = NULL;
46 iter->data2 = NULL;
49 static inline int iters_equal(struct iter *a, struct iter *b)
51 return a->data0 == b->data0 &&
52 a->data1 == b->data1 &&
53 a->data2 == b->data2;
56 static inline int iter_is_head(struct iter *iter)
58 return iter->data0 != NULL &&
59 iter->data1 == NULL &&
60 iter->data2 == NULL;
63 static inline int iter_is_null(struct iter *iter)
65 return iter->data0 == NULL &&
66 iter->data1 == NULL &&
67 iter->data2 == NULL;
70 static inline int iter_is_empty(struct iter *iter)
72 return iter->data0 == NULL || (iter->data1 == NULL && iter->data2 == NULL);
75 #define GENERIC_ITER_PREV(FUNC, TYPE, MEMBER) \
76 int FUNC(struct iter *iter) \
77 { \
78 struct list_head *head = iter->data0; \
79 TYPE *e = iter->data1; \
81 BUG_ON(iter->data2); \
82 if (head == NULL) \
83 return 0; \
84 if (e == NULL) { \
85 /* head, get last */ \
86 if (head->prev == head) { \
87 /* empty, iter points to the head already */ \
88 return 0; \
89 } \
90 iter->data1 = container_of(head->prev, TYPE, MEMBER); \
91 return 1; \
92 } \
93 if (e->MEMBER.prev == head) { \
94 iter->data1 = NULL; \
95 return 0; \
96 } \
97 iter->data1 = container_of(e->MEMBER.prev, TYPE, MEMBER); \
98 return 1; \
101 #define GENERIC_ITER_NEXT(FUNC, TYPE, MEMBER) \
102 int FUNC(struct iter *iter) \
104 struct list_head *head = iter->data0; \
105 TYPE *e = iter->data1; \
107 BUG_ON(iter->data2); \
108 if (head == NULL) \
109 return 0; \
110 if (e == NULL) { \
111 /* head, get first */ \
112 if (head->next == head) { \
113 /* empty, iter points to the head already */ \
114 return 0; \
116 iter->data1 = container_of(head->next, TYPE, MEMBER); \
117 return 1; \
119 if (e->MEMBER.next == head) { \
120 iter->data1 = NULL; \
121 return 0; \
123 iter->data1 = container_of(e->MEMBER.next, TYPE, MEMBER); \
124 return 1; \
127 #endif