ql.h revision 84cbbcb90af44807361198baed4e74ab77715ff7
1/******************************************************************************
2 *
3 * Copyright (C) 2002 Jason Evans <jasone@canonware.com>.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 *    notice(s), this list of conditions and the following disclaimer
11 *    unmodified other than the allowable addition of one or more
12 *    copyright notices.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 *    notice(s), this list of conditions and the following disclaimer in
15 *    the documentation and/or other materials provided with the
16 *    distribution.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER(S) ``AS IS'' AND ANY
19 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT HOLDER(S) BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
25 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
27 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 *
30 ******************************************************************************/
31
32/*
33 * List definitions.
34 */
35#define ql_head(a_type)							\
36struct {								\
37	a_type *qlh_first;						\
38}
39
40#define ql_head_initializer(a_head) {NULL}
41
42#define ql_elm(a_type)	qr(a_type)
43
44/* List functions. */
45#define ql_new(a_head) do {						\
46	(a_head)->qlh_first = NULL;					\
47} while (0)
48
49#define ql_elm_new(a_elm, a_field) qr_new((a_elm), a_field)
50
51#define ql_first(a_head) ((a_head)->qlh_first)
52
53#define ql_last(a_head, a_field)					\
54	((ql_first(a_head) != NULL)					\
55	    ? qr_prev(ql_first(a_head), a_field) : NULL)
56
57#define ql_next(a_head, a_elm, a_field)					\
58	((ql_last(a_head, a_field) != (a_elm))				\
59	    ? qr_next((a_elm), a_field)	: NULL)
60
61#define ql_prev(a_head, a_elm, a_field)					\
62	((ql_first(a_head) != (a_elm)) ? qr_prev((a_elm), a_field)	\
63				       : NULL)
64
65#define ql_before_insert(a_head, a_qlelm, a_elm, a_field) do {		\
66	qr_before_insert((a_qlelm), (a_elm), a_field);			\
67	if (ql_first(a_head) == (a_qlelm)) {				\
68		ql_first(a_head) = (a_elm);				\
69	}								\
70} while (0)
71
72#define ql_after_insert(a_qlelm, a_elm, a_field)			\
73	qr_after_insert((a_qlelm), (a_elm), a_field)
74
75#define ql_head_insert(a_head, a_elm, a_field) do {			\
76	if (ql_first(a_head) != NULL) {					\
77		qr_before_insert(ql_first(a_head), (a_elm), a_field);	\
78	}								\
79	ql_first(a_head) = (a_elm);					\
80} while (0)
81
82#define ql_tail_insert(a_head, a_elm, a_field) do {			\
83	if (ql_first(a_head) != NULL) {					\
84		qr_before_insert(ql_first(a_head), (a_elm), a_field);	\
85	}								\
86	ql_first(a_head) = qr_next((a_elm), a_field);			\
87} while (0)
88
89#define ql_remove(a_head, a_elm, a_field) do {				\
90	if (ql_first(a_head) == (a_elm)) {				\
91		ql_first(a_head) = qr_next(ql_first(a_head), a_field);	\
92	}								\
93	if (ql_first(a_head) != (a_elm)) {				\
94		qr_remove((a_elm), a_field);				\
95	} else {							\
96		ql_first(a_head) = NULL;				\
97	}								\
98} while (0)
99
100#define ql_head_remove(a_head, a_type, a_field) do {			\
101	a_type *t = ql_first(a_head);					\
102	ql_remove((a_head), t, a_field);				\
103} while (0)
104
105#define ql_tail_remove(a_head, a_type, a_field) do {			\
106	a_type *t = ql_last(a_head, a_field);				\
107	ql_remove((a_head), t, a_field);				\
108} while (0)
109
110#define ql_foreach(a_var, a_head, a_field)				\
111	qr_foreach((a_var), ql_first(a_head), a_field)
112
113#define ql_reverse_foreach(a_var, a_head, a_field)			\
114	qr_reverse_foreach((a_var), ql_first(a_head), a_field)
115