flac: Saner EOF handling
[cmus.git] / iter.h
blobc8b596c1229085ca026fce58782daa1631a645c2
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 <stdlib.h>
25 struct iter {
26 /* this usually points to the list head */
27 void *data0;
29 /* these point to the list item, for simple lists data2 is usually NULL */
30 void *data1;
31 void *data2;
34 static inline void iter_init(struct iter *iter)
36 iter->data0 = NULL;
37 iter->data1 = NULL;
38 iter->data2 = NULL;
41 static inline void iter_head(struct iter *iter)
43 iter->data1 = NULL;
44 iter->data2 = NULL;
47 static inline int iters_equal(struct iter *a, struct iter *b)
49 return a->data0 == b->data0 &&
50 a->data1 == b->data1 &&
51 a->data2 == b->data2;
54 static inline int iter_is_head(struct iter *iter)
56 return iter->data0 != NULL &&
57 iter->data1 == NULL &&
58 iter->data2 == NULL;
61 static inline int iter_is_null(struct iter *iter)
63 return iter->data0 == NULL &&
64 iter->data1 == NULL &&
65 iter->data2 == NULL;
68 static inline int iter_is_empty(struct iter *iter)
70 return iter->data0 == NULL || (iter->data1 == NULL && iter->data2 == NULL);
73 #define GENERIC_ITER_PREV(FUNC, TYPE, MEMBER) \
74 int FUNC(struct iter *iter) \
75 { \
76 struct list_head *head = iter->data0; \
77 TYPE *e = iter->data1; \
79 if (head == NULL) \
80 return 0; \
81 if (e == NULL) { \
82 /* head, get last */ \
83 if (head->prev == head) { \
84 /* empty, iter points to the head already */ \
85 return 0; \
86 } \
87 iter->data1 = container_of(head->prev, TYPE, MEMBER); \
88 return 1; \
89 } \
90 if (e->MEMBER.prev == head) { \
91 iter->data1 = NULL; \
92 return 0; \
93 } \
94 iter->data1 = container_of(e->MEMBER.prev, TYPE, MEMBER); \
95 return 1; \
98 #define GENERIC_ITER_NEXT(FUNC, TYPE, MEMBER) \
99 int FUNC(struct iter *iter) \
101 struct list_head *head = iter->data0; \
102 TYPE *e = iter->data1; \
104 if (head == NULL) \
105 return 0; \
106 if (e == NULL) { \
107 /* head, get first */ \
108 if (head->next == head) { \
109 /* empty, iter points to the head already */ \
110 return 0; \
112 iter->data1 = container_of(head->next, TYPE, MEMBER); \
113 return 1; \
115 if (e->MEMBER.next == head) { \
116 iter->data1 = NULL; \
117 return 0; \
119 iter->data1 = container_of(e->MEMBER.next, TYPE, MEMBER); \
120 return 1; \
123 #endif