Changeset 0c2d9bb in mainline for uspace/lib/c/include/adt/list.h
- Timestamp:
- 2013-12-25T22:54:29Z (10 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- b51cf2c
- Parents:
- f7a33de (diff), ac36aed (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/c/include/adt/list.h
rf7a33de r0c2d9bb 38 38 39 39 #include <assert.h> 40 #include <stdbool.h> 40 41 #include <unistd.h> 41 42 … … 67 68 ((type *) (((void *)(link)) - list_link_to_void(&(((type *) NULL)->member)))) 68 69 69 #define list_foreach(list, iterator) \ 70 for (link_t *iterator = (list).head.next; \ 71 iterator != &(list).head; iterator = iterator->next) 70 #define list_foreach(list, member, itype, iterator) \ 71 for (itype *iterator = NULL; iterator == NULL; iterator = (itype *) 1) \ 72 for (link_t *_link = (list).head.next; \ 73 iterator = list_get_instance(_link, itype, member), \ 74 _link != &(list).head; _link = _link->next) 75 76 #define list_foreach_rev(list, member, itype, iterator) \ 77 for (itype *iterator = NULL; iterator == NULL; iterator = (itype *) 1) \ 78 for (link_t *_link = (list).head.prev; \ 79 iterator = list_get_instance(_link, itype, member), \ 80 _link != &(list).head; _link = _link->prev) 72 81 73 82 /** Unlike list_foreach(), allows removing items while traversing a list. 74 * 83 * 75 84 * @code 76 85 * list_t mylist; … … 103 112 104 113 #define assert_link_not_used(link) \ 105 assert( ((link)->prev == NULL) && ((link)->next == NULL))114 assert(!link_used(link)) 106 115 107 116 /** Returns true if the link is definitely part of a list. False if not sure. */ … … 238 247 static inline link_t *list_last(list_t *list) 239 248 { 240 return ((list->head.prev == &list->head) ? NULL : list->head.prev); 249 return (list->head.prev == &list->head) ? NULL : list->head.prev; 250 } 251 252 /** Get next item in list. 253 * 254 * @param link Current item link 255 * @param list List containing @a link 256 * 257 * @return Next item or NULL if @a link is the last item. 258 * 259 */ 260 static inline link_t *list_next(link_t *link, const list_t *list) 261 { 262 return (link->next == &list->head) ? NULL : link->next; 263 } 264 265 /** Get previous item in list. 266 * 267 * @param link Current item link 268 * @param list List containing @a link 269 * 270 * @return Previous item or NULL if @a link is the first item. 271 * 272 */ 273 static inline link_t *list_prev(link_t *link, const list_t *list) 274 { 275 return (link->prev == &list->head) ? NULL : link->prev; 241 276 } 242 277 … … 308 343 unsigned int cnt = 0; 309 344 310 list_foreach(*list, link) { 345 link_t *link = list_first(list); 346 while (link != NULL) { 311 347 if (cnt == n) 312 348 return link; 313 349 314 350 cnt++; 351 link = list_next(link, list); 315 352 } 316 353 … … 325 362 { 326 363 return link; 364 } 365 366 /** Determine if link is used. 367 * 368 * @param link Link 369 * @return @c true if link is used, @c false if not. 370 */ 371 static inline bool link_used(link_t *link) 372 { 373 if (link->prev == NULL && link->next == NULL) 374 return false; 375 376 assert(link->prev != NULL && link->next != NULL); 377 return true; 327 378 } 328 379
Note:
See TracChangeset
for help on using the changeset viewer.