summaryrefslogtreecommitdiff
path: root/libs/ode-0.16.1/libccd/src/ccd/list.h
blob: 54391ed8ba02c6192956b89bfd8fe806ccc3ed94 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/***
 * libccd
 * ---------------------------------
 * Copyright (c)2010 Daniel Fiser <danfis@danfis.cz>
 *
 *
 *  This file is part of libccd.
 *
 *  Distributed under the OSI-approved BSD License (the "License");
 *  see accompanying file BDS-LICENSE for details or see
 *  <http://www.opensource.org/licenses/bsd-license.php>.
 *
 *  This software is distributed WITHOUT ANY WARRANTY; without even the
 *  implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *  See the License for more information.
 */

#ifndef __CCD_LIST_H__
#define __CCD_LIST_H__

#include <string.h>
#include <ccd/compiler.h>

#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */

struct _ccd_list_t {
    struct _ccd_list_t *next, *prev;
};
typedef struct _ccd_list_t ccd_list_t;



/**
 * Get the struct for this entry.
 * @ptr:	the &ccd_list_t pointer.
 * @type:	the type of the struct this is embedded in.
 * @member:	the name of the list_struct within the struct.
 */
#define ccdListEntry(ptr, type, member) \
    ccd_container_of(ptr, type, member)

/**
 * Iterates over list.
 */
#define ccdListForEach(list, item) \
        for (item = (list)->next; \
             _ccd_prefetch((item)->next), item != (list); \
             item = (item)->next)

/**
 * Iterates over list safe against remove of list entry
 */
#define ccdListForEachSafe(list, item, tmp) \
	    for (item = (list)->next, tmp = (item)->next; \
             item != (list); \
		     item = tmp, tmp = (item)->next)

/**
 * Iterates over list of given type.
 * @pos:	the type * to use as a loop cursor.
 * @head:	the head for your list.
 * @member:	the name of the list_struct within the struct.
 */
#define ccdListForEachEntry(head, pos, postype, member)                 \
	for (pos = ccdListEntry((head)->next, postype, member);	\
	     _ccd_prefetch(pos->member.next), &pos->member != (head); 	\
	     pos = ccdListEntry(pos->member.next, postype, member))

/**
 * Iterates over list of given type safe against removal of list entry
 * @pos:	the type * to use as a loop cursor.
 * @n:		another type * to use as temporary storage
 * @head:	the head for your list.
 * @member:	the name of the list_struct within the struct.
 */
#define ccdListForEachEntrySafe(head, pos, postype, n, ntype, member)         \
    for (pos = ccdListEntry((head)->next, postype, member),             \
		 n = ccdListEntry(pos->member.next, postype, member);	\
	     &pos->member != (head); 					\
	     pos = n, n = ccdListEntry(n->member.next, ntype, member))


/**
 * Initialize list.
 */
_ccd_inline void ccdListInit(ccd_list_t *l);

_ccd_inline ccd_list_t *ccdListNext(ccd_list_t *l);
_ccd_inline ccd_list_t *ccdListPrev(ccd_list_t *l);

/**
 * Returns true if list is empty.
 */
_ccd_inline int ccdListEmpty(const ccd_list_t *head);

/**
 * Appends item to end of the list l.
 */
_ccd_inline void ccdListAppend(ccd_list_t *l, ccd_list_t *item);

/**
 * Removes item from list.
 */
_ccd_inline void ccdListDel(ccd_list_t *item);



///
/// INLINES:
///

_ccd_inline void ccdListInit(ccd_list_t *l)
{
    l->next = l;
    l->prev = l;
}

_ccd_inline ccd_list_t *ccdListNext(ccd_list_t *l)
{
    return l->next;
}

_ccd_inline ccd_list_t *ccdListPrev(ccd_list_t *l)
{
    return l->prev;
}

_ccd_inline int ccdListEmpty(const ccd_list_t *head)
{
    return head->next == head;
}

_ccd_inline void ccdListAppend(ccd_list_t *l, ccd_list_t *newccd)
{
    newccd->prev = l->prev;
    newccd->next = l;
    l->prev->next = newccd;
    l->prev = newccd;
}

_ccd_inline void ccdListDel(ccd_list_t *item)
{
    item->next->prev = item->prev;
    item->prev->next = item->next;
    item->next = item;
    item->prev = item;
}

#ifdef __cplusplus
} /* extern "C" */
#endif /* __cplusplus */

#endif /* __CCD_LIST_H__ */