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


Ignore:
Timestamp:
2013-12-25T22:54:29Z (10 years ago)
Author:
Martin Decky <martin@…>
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.
Message:

merge mainline changes

File:
1 edited

Legend:

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

    rf7a33de r0c2d9bb  
    3838
    3939#include <assert.h>
     40#include <stdbool.h>
    4041#include <unistd.h>
    4142
     
    6768        ((type *) (((void *)(link)) - list_link_to_void(&(((type *) NULL)->member))))
    6869
    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)
    7281
    7382/** Unlike list_foreach(), allows removing items while traversing a list.
    74  * 
     83 *
    7584 * @code
    7685 * list_t mylist;
     
    103112
    104113#define assert_link_not_used(link) \
    105         assert(((link)->prev == NULL) && ((link)->next == NULL))
     114        assert(!link_used(link))
    106115
    107116/** Returns true if the link is definitely part of a list. False if not sure. */
     
    238247static inline link_t *list_last(list_t *list)
    239248{
    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 */
     260static 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 */
     273static inline link_t *list_prev(link_t *link, const list_t *list)
     274{
     275        return (link->prev == &list->head) ? NULL : link->prev;
    241276}
    242277
     
    308343        unsigned int cnt = 0;
    309344       
    310         list_foreach(*list, link) {
     345        link_t *link = list_first(list);
     346        while (link != NULL) {
    311347                if (cnt == n)
    312348                        return link;
    313349               
    314350                cnt++;
     351                link = list_next(link, list);
    315352        }
    316353       
     
    325362{
    326363        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 */
     371static 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;
    327378}
    328379
Note: See TracChangeset for help on using the changeset viewer.