| 1 | /* |
| 2 | * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com) |
| 3 | * Copyright (C) 2003-2008 Benny Prijono <[email protected]> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU 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, write to the Free Software |
| 17 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 18 | */ |
| 19 | #ifndef __PJ_LIST_H__ |
| 20 | #define __PJ_LIST_H__ |
| 21 | |
| 22 | /** |
| 23 | * @file list.h |
| 24 | * @brief Linked List data structure. |
| 25 | */ |
| 26 | |
| 27 | #include <pj/types.h> |
| 28 | |
| 29 | PJ_BEGIN_DECL |
| 30 | |
| 31 | /* |
| 32 | * @defgroup PJ_DS Data Structure. |
| 33 | */ |
| 34 | |
| 35 | /** |
| 36 | * @defgroup PJ_LIST Linked List |
| 37 | * @ingroup PJ_DS |
| 38 | * @{ |
| 39 | * |
| 40 | * List in PJLIB is implemented as doubly-linked list, and it won't require |
| 41 | * dynamic memory allocation (just as all PJLIB data structures). The list here |
| 42 | * should be viewed more like a low level C list instead of high level C++ list |
| 43 | * (which normally are easier to use but require dynamic memory allocations), |
| 44 | * therefore all caveats with C list apply here too (such as you can NOT put |
| 45 | * a node in more than one lists). |
| 46 | * |
| 47 | * \section pj_list_example_sec Examples |
| 48 | * |
| 49 | * See below for examples on how to manipulate linked list: |
| 50 | * - List sample: \src{pjlib/src/pjlib-samples/list.c} |
| 51 | * - List test: \src{pjlib/src/pjlib-test/list.c} |
| 52 | */ |
| 53 | |
| 54 | |
| 55 | /** |
| 56 | * Use this macro in the start of the structure declaration to declare that |
| 57 | * the structure can be used in the linked list operation. This macro simply |
| 58 | * declares additional member @a prev and @a next to the structure. |
| 59 | * @hideinitializer |
| 60 | */ |
| 61 | #define PJ_DECL_LIST_MEMBER(type) \ |
| 62 | /** List @a prev. */ \ |
| 63 | type *prev; \ |
| 64 | /** List @a next. */ \ |
| 65 | type *next |
| 66 | |
| 67 | |
| 68 | /** |
| 69 | * This structure describes generic list node and list. The owner of this list |
| 70 | * must initialize the 'value' member to an appropriate value (typically the |
| 71 | * owner itself). |
| 72 | */ |
| 73 | struct pj_list |
| 74 | { |
| 75 | PJ_DECL_LIST_MEMBER(void); |
| 76 | } PJ_ATTR_MAY_ALIAS; /* may_alias avoids warning with gcc-4.4 -Wall -O2 */ |
| 77 | |
| 78 | |
| 79 | /** |
| 80 | * Initialize the list. |
| 81 | * Initially, the list will have no member, and function pj_list_empty() will |
| 82 | * always return nonzero (which indicates TRUE) for the newly initialized |
| 83 | * list. |
| 84 | * |
| 85 | * @param node The list head. |
| 86 | */ |
| 87 | PJ_INLINE(void) pj_list_init(pj_list_type * node) |
| 88 | { |
| 89 | ((pj_list*)node)->next = ((pj_list*)node)->prev = node; |
| 90 | } |
| 91 | |
| 92 | |
| 93 | /** |
| 94 | * Check that the list is empty. |
| 95 | * |
| 96 | * @param node The list head. |
| 97 | * |
| 98 | * @return Non-zero if the list is empty, or zero if it is not empty. |
| 99 | * |
| 100 | */ |
| 101 | PJ_INLINE(int) pj_list_empty(const pj_list_type * node) |
| 102 | { |
| 103 | return ((pj_list*)node)->next == node; |
| 104 | } |
| 105 | |
| 106 | |
| 107 | /** |
| 108 | * Insert the node to the list before the specified element position. |
| 109 | * |
| 110 | * @param pos The element to which the node will be inserted before. |
| 111 | * @param node The element to be inserted. |
| 112 | * |
| 113 | */ |
| 114 | PJ_IDECL(void) pj_list_insert_before(pj_list_type *pos, pj_list_type *node); |
| 115 | |
| 116 | |
| 117 | /** |
| 118 | * Insert the node to the back of the list. This is just an alias for |
| 119 | * #pj_list_insert_before(). |
| 120 | * |
| 121 | * @param list The list. |
| 122 | * @param node The element to be inserted. |
| 123 | */ |
| 124 | PJ_INLINE(void) pj_list_push_back(pj_list_type *list, pj_list_type *node) |
| 125 | { |
| 126 | pj_list_insert_before(list, node); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | /** |
| 131 | * Inserts all nodes in \a nodes to the target list. |
| 132 | * |
| 133 | * @param lst The target list. |
| 134 | * @param nodes Nodes list. |
| 135 | */ |
| 136 | PJ_IDECL(void) pj_list_insert_nodes_before(pj_list_type *lst, |
| 137 | pj_list_type *nodes); |
| 138 | |
| 139 | /** |
| 140 | * Insert a node to the list after the specified element position. |
| 141 | * |
| 142 | * @param pos The element in the list which will precede the inserted |
| 143 | * element. |
| 144 | * @param node The element to be inserted after the position element. |
| 145 | * |
| 146 | */ |
| 147 | PJ_IDECL(void) pj_list_insert_after(pj_list_type *pos, pj_list_type *node); |
| 148 | |
| 149 | |
| 150 | /** |
| 151 | * Insert the node to the front of the list. This is just an alias for |
| 152 | * #pj_list_insert_after(). |
| 153 | * |
| 154 | * @param list The list. |
| 155 | * @param node The element to be inserted. |
| 156 | */ |
| 157 | PJ_INLINE(void) pj_list_push_front(pj_list_type *list, pj_list_type *node) |
| 158 | { |
| 159 | pj_list_insert_after(list, node); |
| 160 | } |
| 161 | |
| 162 | |
| 163 | /** |
| 164 | * Insert all nodes in \a nodes to the target list. |
| 165 | * |
| 166 | * @param lst The target list. |
| 167 | * @param nodes Nodes list. |
| 168 | */ |
| 169 | PJ_IDECL(void) pj_list_insert_nodes_after(pj_list_type *lst, |
| 170 | pj_list_type *nodes); |
| 171 | |
| 172 | |
| 173 | /** |
| 174 | * Insert a list to another list before the specified element position. |
| 175 | * |
| 176 | * @param pos The element to which the node will be inserted before. |
| 177 | * @param lst The list to be inserted. |
| 178 | */ |
| 179 | PJ_IDECL(void) pj_list_insert_list_before(pj_list_type *pos, |
| 180 | pj_list_type *lst); |
| 181 | |
| 182 | |
| 183 | /** |
| 184 | * Insert a list to another list after the specified element position. |
| 185 | * |
| 186 | * @param pos The element in the list which will precede the inserted |
| 187 | * list. |
| 188 | * @param lst The list to be inserted. |
| 189 | */ |
| 190 | PJ_IDECL(void) pj_list_insert_list_after(pj_list_type *pos, |
| 191 | pj_list_type *lst); |
| 192 | |
| 193 | |
| 194 | /** |
| 195 | * Remove elements from the source list, and insert them to the destination |
| 196 | * list. The elements of the source list will occupy the |
| 197 | * front elements of the target list. Note that the node pointed by \a list2 |
| 198 | * itself is not considered as a node, but rather as the list descriptor, so |
| 199 | * it will not be inserted to the \a list1. The elements to be inserted starts |
| 200 | * at \a list2->next. If \a list2 is to be included in the operation, use |
| 201 | * \a pj_list_insert_nodes_before. |
| 202 | * |
| 203 | * @param list1 The destination list. |
| 204 | * @param list2 The source list. |
| 205 | * |
| 206 | */ |
| 207 | PJ_IDECL(void) pj_list_merge_first(pj_list_type *list1, pj_list_type *list2); |
| 208 | |
| 209 | |
| 210 | /** |
| 211 | * Remove elements from the second list argument, and insert them to the list |
| 212 | * in the first argument. The elements from the second list will be appended |
| 213 | * to the first list. Note that the node pointed by \a list2 |
| 214 | * itself is not considered as a node, but rather as the list descriptor, so |
| 215 | * it will not be inserted to the \a list1. The elements to be inserted starts |
| 216 | * at \a list2->next. If \a list2 is to be included in the operation, use |
| 217 | * \a pj_list_insert_nodes_before. |
| 218 | * |
| 219 | * @param list1 The element in the list which will precede the inserted |
| 220 | * element. |
| 221 | * @param list2 The element in the list to be inserted. |
| 222 | * |
| 223 | */ |
| 224 | PJ_IDECL(void) pj_list_merge_last( pj_list_type *list1, pj_list_type *list2); |
| 225 | |
| 226 | |
| 227 | /** |
| 228 | * Erase the node from the list it currently belongs. |
| 229 | * |
| 230 | * @param node The element to be erased. |
| 231 | */ |
| 232 | PJ_IDECL(void) pj_list_erase(pj_list_type *node); |
| 233 | |
| 234 | |
| 235 | /** |
| 236 | * Find node in the list. |
| 237 | * |
| 238 | * @param list The list head. |
| 239 | * @param node The node element to be searched. |
| 240 | * |
| 241 | * @return The node itself if it is found in the list, or NULL if it is not |
| 242 | * found in the list. |
| 243 | */ |
| 244 | PJ_IDECL(pj_list_type*) pj_list_find_node(pj_list_type *list, |
| 245 | pj_list_type *node); |
| 246 | |
| 247 | |
| 248 | /** |
| 249 | * Search the list for the specified value, using the specified comparison |
| 250 | * function. This function iterates on nodes in the list, started with the |
| 251 | * first node, and call the user supplied comparison function until the |
| 252 | * comparison function returns ZERO. |
| 253 | * |
| 254 | * @param list The list head. |
| 255 | * @param value The user defined value to be passed in the comparison |
| 256 | * function |
| 257 | * @param comp The comparison function, which should return ZERO to |
| 258 | * indicate that the searched value is found. |
| 259 | * |
| 260 | * @return The first node that matched, or NULL if it is not found. |
| 261 | */ |
| 262 | PJ_IDECL(pj_list_type*) pj_list_search(pj_list_type *list, void *value, |
| 263 | int (*comp)(void *value, |
| 264 | const pj_list_type *node) |
| 265 | ); |
| 266 | |
| 267 | |
| 268 | /** |
| 269 | * Traverse the list to get the number of elements in the list. |
| 270 | * |
| 271 | * @param list The list head. |
| 272 | * |
| 273 | * @return Number of elements. |
| 274 | */ |
| 275 | PJ_IDECL(pj_size_t) pj_list_size(const pj_list_type *list); |
| 276 | |
| 277 | |
| 278 | /** |
| 279 | * @} |
| 280 | */ |
| 281 | |
| 282 | #if PJ_FUNCTIONS_ARE_INLINED |
| 283 | # include "list_i.h" |
| 284 | #endif |
| 285 | |
| 286 | PJ_END_DECL |
| 287 | |
| 288 | #endif /* __PJ_LIST_H__ */ |
| 289 |