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


Ignore:
Timestamp:
2011-07-20T15:26:21Z (13 years ago)
Author:
Jakub Jermar <jakub@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
efcebe1
Parents:
25bef0ff (diff), a701812 (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

    r25bef0ff r6a44ee4  
    11/*
    22 * Copyright (c) 2001-2004 Jakub Jermar
     3 * Copyright (c) 2011 Jiri Svoboda
    34 * All rights reserved.
    45 *
     
    3637#define LIBC_LIST_H_
    3738
     39#include <assert.h>
    3840#include <unistd.h>
    3941
    40 /** Doubly linked list head and link type. */
     42/** Doubly linked list link. */
    4143typedef struct link {
    4244        struct link *prev;  /**< Pointer to the previous item in the list. */
     
    4446} link_t;
    4547
     48/** Doubly linked list. */
     49typedef struct list {
     50        link_t head;  /**< List head. Does not have any data. */
     51} list_t;
     52
    4653/** Declare and initialize statically allocated list.
    4754 *
     
    5057 */
    5158#define LIST_INITIALIZE(name) \
    52         link_t name = { \
    53                 .prev = &name, \
    54                 .next = &name \
     59        list_t name = { \
     60                .head = { \
     61                        .prev = &(name).head, \
     62                        .next = &(name).head \
     63                } \
    5564        }
    5665
     
    5968
    6069#define list_foreach(list, iterator) \
    61         for (link_t *iterator = (list).next; \
    62             iterator != &(list); iterator = iterator->next)
     70        for (link_t *iterator = (list).head.next; \
     71            iterator != &(list).head; iterator = iterator->next)
     72
     73#define assert_link_not_used(link) \
     74        assert((link)->prev == NULL && (link)->next == NULL)
    6375
    6476/** Initialize doubly-linked circular list link
     
    7991 * Initialize doubly-linked circular list.
    8092 *
    81  * @param list Pointer to link_t structure representing the list.
    82  *
    83  */
    84 static inline void list_initialize(link_t *list)
    85 {
    86         list->prev = list;
    87         list->next = list;
     93 * @param list Pointer to list_t structure.
     94 *
     95 */
     96static inline void list_initialize(list_t *list)
     97{
     98        list->head.prev = &list->head;
     99        list->head.next = &list->head;
     100}
     101
     102/** Insert item before another item in doubly-linked circular list.
     103 *
     104 */
     105static inline void list_insert_before(link_t *lnew, link_t *lold)
     106{
     107        lnew->next = lold;
     108        lnew->prev = lold->prev;
     109        lold->prev->next = lnew;
     110        lold->prev = lnew;
     111}
     112
     113/** Insert item after another item in doubly-linked circular list.
     114 *
     115 */
     116static inline void list_insert_after(link_t *lnew, link_t *lold)
     117{
     118        lnew->prev = lold;
     119        lnew->next = lold->next;
     120        lold->next->prev = lnew;
     121        lold->next = lnew;
    88122}
    89123
     
    93127 *
    94128 * @param link Pointer to link_t structure to be added.
    95  * @param list Pointer to link_t structure representing the list.
    96  *
    97  */
    98 static inline void list_prepend(link_t *link, link_t *list)
    99 {
    100         link->next = list->next;
    101         link->prev = list;
    102         list->next->prev = link;
    103         list->next = link;
     129 * @param list Pointer to list_t structure.
     130 *
     131 */
     132static inline void list_prepend(link_t *link, list_t *list)
     133{
     134        list_insert_after(link, &list->head);
    104135}
    105136
     
    109140 *
    110141 * @param link Pointer to link_t structure to be added.
    111  * @param list Pointer to link_t structure representing the list.
    112  *
    113  */
    114 static inline void list_append(link_t *link, link_t *list)
    115 {
    116         link->prev = list->prev;
    117         link->next = list;
    118         list->prev->next = link;
    119         list->prev = link;
    120 }
    121 
    122 /** Insert item before another item in doubly-linked circular list.
    123  *
    124  */
    125 static inline void list_insert_before(link_t *link, link_t *list)
    126 {
    127         list_append(link, list);
    128 }
    129 
    130 /** Insert item after another item in doubly-linked circular list.
    131  *
    132  */
    133 static inline void list_insert_after(link_t *link, link_t *list)
    134 {
    135         list_prepend(list, link);
     142 * @param list Pointer to list_t structure.
     143 *
     144 */
     145static inline void list_append(link_t *link, list_t *list)
     146{
     147        list_insert_before(link, &list->head);
    136148}
    137149
     
    155167 * Query emptiness of doubly-linked circular list.
    156168 *
    157  * @param list Pointer to link_t structure representing the list.
    158  *
    159  */
    160 static inline int list_empty(link_t *list)
    161 {
    162         return (list->next == list);
    163 }
    164 
    165 /** Get head item of a list.
    166  *
    167  * @param list Pointer to link_t structure representing the list.
     169 * @param list Pointer to lins_t structure.
     170 *
     171 */
     172static inline int list_empty(list_t *list)
     173{
     174        return (list->head.next == &list->head);
     175}
     176
     177/** Get first item in list.
     178 *
     179 * @param list Pointer to list_t structure.
    168180 *
    169181 * @return Head item of the list.
     
    171183 *
    172184 */
    173 static inline link_t *list_head(link_t *list)
    174 {
    175         return ((list->next == list) ? NULL : list->next);
     185static inline link_t *list_first(list_t *list)
     186{
     187        return ((list->head.next == &list->head) ? NULL : list->head.next);
     188}
     189
     190/** Get last item in list.
     191 *
     192 * @param list Pointer to list_t structure.
     193 *
     194 * @return Head item of the list.
     195 * @return NULL if the list is empty.
     196 *
     197 */
     198static inline link_t *list_last(list_t *list)
     199{
     200        return ((list->head.prev == &list->head) ? NULL : list->head.prev);
    176201}
    177202
     
    230255}
    231256
    232 /** Get n-th item of a list.
     257/** Get n-th item in a list.
    233258 *
    234259 * @param list Pointer to link_t structure representing the list.
     
    239264 *
    240265 */
    241 static inline link_t *list_nth(link_t *list, unsigned int n)
     266static inline link_t *list_nth(list_t *list, unsigned int n)
    242267{
    243268        unsigned int cnt = 0;
     
    253278}
    254279
    255 extern int list_member(const link_t *, const link_t *);
    256 extern void list_concat(link_t *, link_t *);
    257 extern unsigned int list_count(const link_t *);
     280extern int list_member(const link_t *, const list_t *);
     281extern void list_concat(list_t *, list_t *);
     282extern unsigned int list_count(const list_t *);
    258283
    259284#endif
Note: See TracChangeset for help on using the changeset viewer.