Changeset feeac0d in mainline for uspace/lib/c/include/adt/list.h


Ignore:
Timestamp:
2013-09-10T16:32:35Z (11 years ago)
Author:
Jiri Svoboda <jiri@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
4982d87
Parents:
e8d6ce2
Message:

Simplify use of list_foreach.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • uspace/lib/c/include/adt/list.h

    re8d6ce2 rfeeac0d  
    6767        ((type *) (((void *)(link)) - list_link_to_void(&(((type *) NULL)->member))))
    6868
    69 #define list_foreach(list, iterator) \
    70         for (link_t *iterator = (list).head.next; \
    71             iterator != &(list).head; iterator = iterator->next)
     69#define list_foreach(list, member, itype, iterator) \
     70        for (itype *iterator = NULL; iterator == NULL; iterator =(itype *)1) \
     71            for (link_t *_link = (list).head.next; \
     72            iterator = list_get_instance(_link, itype, member), \
     73            _link != &(list).head; _link = _link->next)
    7274
    7375/** Unlike list_foreach(), allows removing items while traversing a list.
     
    238240static inline link_t *list_last(list_t *list)
    239241{
    240         return ((list->head.prev == &list->head) ? NULL : list->head.prev);
     242        return (list->head.prev == &list->head) ? NULL : list->head.prev;
     243}
     244
     245/** Get next item in list.
     246 *
     247 * @param link Current item link
     248 * @param list List containing @a link
     249 *
     250 * @return Next item or NULL if @a link is the last item.
     251 */
     252static inline link_t *list_next(link_t *link, const list_t *list)
     253{
     254        return (link->next == &list->head) ? NULL : link->next;
     255}
     256
     257/** Get previous item in list.
     258 *
     259 * @param link Current item link
     260 * @param list List containing @a link
     261 *
     262 * @return Previous item or NULL if @a link is the first item.
     263 */
     264static inline link_t *list_prev(link_t *link, const list_t *list)
     265{
     266        return (link->prev == &list->head) ? NULL : link->prev;
    241267}
    242268
     
    308334        unsigned int cnt = 0;
    309335       
    310         list_foreach(*list, link) {
     336        link_t *link = list_first(list);
     337        while (link != NULL) {
    311338                if (cnt == n)
    312339                        return link;
    313340               
    314341                cnt++;
     342                link = list_next(link, list);
    315343        }
    316344       
Note: See TracChangeset for help on using the changeset viewer.