1
/*
2
 * Copyright 2008-2011 Various Authors
3
 * Copyright 2005 Timo Hirvonen
4
 *
5
 * This program is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU General Public License as
7
 * published by the Free Software Foundation; either version 2 of the
8
 * License, or (at your option) any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful, but
11
 * WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
#ifndef _ITER_H
20
#define _ITER_H
21
22
#include <stddef.h> /* NULL */
23
24
struct iter {
25
	/* this usually points to the list head */
26
	void *data0;
27
28
	/* these point to the list item, for simple lists data2 is usually NULL */
29
	void *data1;
30
	void *data2;
31
};
32
33
static inline void iter_init(struct iter *iter)
34
{
35
	iter->data0 = NULL;
36
	iter->data1 = NULL;
37
	iter->data2 = NULL;
38
}
39
40
static inline void iter_head(struct iter *iter)
41
{
42
	iter->data1 = NULL;
43
	iter->data2 = NULL;
44
}
45
46
static inline int iters_equal(struct iter *a, struct iter *b)
47
{
48
	return a->data0 == b->data0 &&
49
		a->data1 == b->data1 &&
50
		a->data2 == b->data2;
51
}
52
53
static inline int iter_is_head(struct iter *iter)
54
{
55
	return iter->data0 != NULL &&
56
		iter->data1 == NULL &&
57
		iter->data2 == NULL;
58
}
59
60
static inline int iter_is_null(struct iter *iter)
61
{
62
	return iter->data0 == NULL &&
63
		iter->data1 == NULL &&
64
		iter->data2 == NULL;
65
}
66
67
static inline int iter_is_empty(struct iter *iter)
68
{
69
	return iter->data0 == NULL || (iter->data1 == NULL && iter->data2 == NULL);
70
}
71
72
#define GENERIC_ITER_PREV(FUNC, TYPE, MEMBER)				\
73
int FUNC(struct iter *iter)						\
74
{									\
75
	struct list_head *head = iter->data0;				\
76
	TYPE *e = iter->data1;						\
77
									\
78
	if (head == NULL)						\
79
		return 0;						\
80
	if (e == NULL) {						\
81
		/* head, get last */					\
82
		if (head->prev == head) {				\
83
			/* empty, iter points to the head already */	\
84
			return 0;					\
85
		}							\
86
		iter->data1 = container_of(head->prev, TYPE, MEMBER);	\
87
		return 1;						\
88
	}								\
89
	if (e->MEMBER.prev == head) {					\
90
		iter->data1 = NULL;					\
91
		return 0;						\
92
	}								\
93
	iter->data1 = container_of(e->MEMBER.prev, TYPE, MEMBER);	\
94
	return 1;							\
95
}
96
97
#define GENERIC_ITER_NEXT(FUNC, TYPE, MEMBER)				\
98
int FUNC(struct iter *iter)						\
99
{									\
100
	struct list_head *head = iter->data0;				\
101
	TYPE *e = iter->data1;						\
102
									\
103
	if (head == NULL)						\
104
		return 0;						\
105
	if (e == NULL) {						\
106
		/* head, get first */					\
107
		if (head->next == head) {				\
108
			/* empty, iter points to the head already */	\
109
			return 0;					\
110
		}							\
111
		iter->data1 = container_of(head->next, TYPE, MEMBER);	\
112
		return 1;						\
113
	}								\
114
	if (e->MEMBER.next == head) {					\
115
		iter->data1 = NULL;					\
116
		return 0;						\
117
	}								\
118
	iter->data1 = container_of(e->MEMBER.next, TYPE, MEMBER);	\
119
	return 1;							\
120
}
121
122
#define GENERIC_TREE_ITER_PREV(FUNC, TYPE, MEMBER)				\
123
int FUNC(struct iter *iter)							\
124
{										\
125
	struct rb_root *root = iter->data0;					\
126
	TYPE *e = iter->data1;							\
127
										\
128
	if (root == NULL)							\
129
		return 0;							\
130
	if (e == NULL) {							\
131
		/* head, get last */						\
132
		if (rb_root_empty(root)) {					\
133
			/* empty, iter points to the head already */		\
134
			return 0;						\
135
		}								\
136
		iter->data1 = container_of(rb_last(root), TYPE, MEMBER);	\
137
		return 1;							\
138
	}									\
139
	if (rb_prev(&e->MEMBER) == NULL) {					\
140
		iter->data1 = NULL;						\
141
		return 0;							\
142
	}									\
143
	iter->data1 = container_of(rb_prev(&e->MEMBER), TYPE, MEMBER);		\
144
	return 1;								\
145
}
146
147
#define GENERIC_TREE_ITER_NEXT(FUNC, TYPE, MEMBER)				\
148
int FUNC(struct iter *iter)							\
149
{										\
150
	struct rb_root *root = iter->data0;					\
151
	TYPE *e = iter->data1;							\
152
										\
153
	if (root == NULL)							\
154
		return 0;							\
155
	if (e == NULL) {							\
156
		/* head, get first */						\
157
		if (rb_root_empty(root)) {					\
158
			/* empty, iter points to the head already */		\
159
			return 0;						\
160
		}								\
161
		iter->data1 = container_of(rb_first(root), TYPE, MEMBER);	\
162
		return 1;							\
163
	}									\
164
	if (rb_next(&e->MEMBER) == NULL) {					\
165
		iter->data1 = NULL;						\
166
		return 0;							\
167
	}									\
168
	iter->data1 = container_of(rb_next(&e->MEMBER), TYPE, MEMBER);		\
169
	return 1;								\
170
}
171
172
#endif