2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief double ended queue of generic pointers.
23 * @author Christian von Roques
25 #ifndef FIRM_ADT_PDEQ_H
26 #define FIRM_ADT_PDEQ_H
33 * The type of the pointer compare function.
35 * @param elem The list element.
36 * @param key The user supplied key.
38 * @return 0 if the element matches the key, non-zero else.
40 typedef int (*cmp_fun)(const void *elem, const void *key);
43 * The pointer double ended queue (list).
45 typedef struct pdeq pdeq;
48 * Creates a new double ended pointer list.
52 FIRM_API pdeq *new_pdeq(void);
55 * Creates a new double ended pointer list and puts an initial pointer element in.
57 * @param x The pointer element to put in.
59 * @return The new list.
61 FIRM_API pdeq *new_pdeq1(const void *x);
64 * Delete a double ended pointer list.
66 * @param dq The list to be deleted.
68 FIRM_API void del_pdeq(pdeq *dq);
71 * Returns the length of a double ended pointer list.
75 FIRM_API size_t pdeq_len(pdeq *dq);
78 * Checks if a list is empty.
82 * @return non-zero if the list is empty.
84 FIRM_API int pdeq_empty(pdeq *dq);
87 * Returns non-zero if a double ended pointer list
88 * contains a pointer x.
91 * @param x The pointer to be searched for.
93 FIRM_API int pdeq_contains(pdeq *dq, const void *x);
96 * Search a key in a double ended pointer list, the search
97 * is controlled by a compare function.
98 * An element is found, if the compare function returns 0.
99 * The search is started from the left site of the list.
101 * @param qp The list.
102 * @param cmp The compare function.
103 * @param key The search key.
105 * @return The address of the element entry if the key was found,
108 FIRM_API void *pdeq_search(pdeq *qp, cmp_fun cmp, const void *key);
111 * Convert the double ended pointer list into a linear array beginning from
112 * left, the first element in the linear array will be the left one.
114 * @param qp The list.
115 * @param dst A pointer to a pointer array with must be at least
116 * pdeq_len(dq) * sizeof(void *)
120 FIRM_API void **pdeq_copyl(pdeq *qp, const void **dst);
123 * Convert the double ended pointer list into a linear array beginning from
124 * right, the first element in the linear array will be the right one.
126 * @param qp The list.
127 * @param dst A pointer to a pointer array with must be at least
128 * pdeq_len(dq) * sizeof(void *)
132 FIRM_API void **pdeq_copyr(pdeq *qp, const void **dst);
135 * Add a pointer to the left side of a double ended pointer list.
137 * @param dq The list to add a pointer to.
138 * @param x The pointer element to be added
142 FIRM_API pdeq *pdeq_putl(pdeq *dq, const void *x);
145 * Add a pointer to the right side of a double ended pointer list.
147 * @param dq The list to add a pointer to.
148 * @param x The pointer element to be added
152 FIRM_API pdeq *pdeq_putr(pdeq *dq, const void *x);
155 * Retrieve (and remove) a pointer from the left site of a double ended pointer
159 * @return The pointer element.
160 * @remark This function will fail if the list is empty.
162 FIRM_API void *pdeq_getl(pdeq *dq);
165 * Retrieve (and remove) a pointer from the right site of a double ended pointer
169 * @return The pointer element.
170 * @remark This function will fail if the list is empty.
172 FIRM_API void *pdeq_getr(pdeq *dq);
175 * The pdeq is often used as a wait queue. A helper
176 * type to support this.
181 * Creates a new pointer wait queue (fifo).
183 * @return A new queue.
185 #define new_waitq() new_pdeq()
188 * Delete a wait queue (fifo)
190 * @param wq The wait queue.
192 #define del_waitq(wq) del_pdeq(wq)
195 * Retrieve a pointer from the wait queue (fifo).
197 * @param wq The wait queue.
199 * @return The pointer element.
201 * @remark This function will fail if the queue is empty.
203 #define waitq_get(wq) pdeq_getl(wq)
206 * Add a pointer to the wait queue (fifo).
208 * @param wq The wait queue
209 * @param x The pointer element to be added
211 * @return The wait queue.
213 #define waitq_put(wq, x) pdeq_putr((wq), (x))
216 * Checks if a wait queue is empty.
218 * @param wq The wait queue.
220 * @return non-zero if the queue is empty.
222 #define waitq_empty(wq) pdeq_empty(wq)
225 * The pdeq can be used as a stack. A helper
226 * type to support this.
231 * Creates a new pointer stack (lifo).
233 * @return A new stack.
235 #define new_stack() new_pdeq()
238 * Pop a pointer from the stack (lifo).
240 * @param st The stack.
242 * @return The pointer element.
244 * @remark This function will fail if the stack is empty.
246 #define stack_pop(st) pdeq_getr(st)
249 * Push a pointer to the stack (lifo).
251 * @param st The stack.
252 * @param x The pointer element to be added
256 #define stack_push(st, x) pdeq_putr((st), (x))
259 * Checks if a stack is empty.
261 * @param st The stack.
263 * @return non-zero if the stack is empty.
265 #define stack_empty(st) pdeq_empty(wq)