VirtualBox

Changeset 32445 in vbox


Ignore:
Timestamp:
Sep 13, 2010 12:22:30 PM (15 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
65813
Message:

iprt/list.h: Added RTListForEachSafe and RTListForEachReverseSafe.

Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/list.h

    r28800 r32445  
    228228
    229229/**
     230 * Enumerate the list in head to tail order, safe against removal of the
     231 * current node.
     232 *
     233 * @param   pList               List to enumerate.
     234 * @param   pIterator           The iterator variable name.
     235 * @param   pIterNext           The name of the variable saving the pointer to
     236 *                              the next element.
     237 * @param   Type                Structure the list node is a member of.
     238 * @param   Member              The list node member name.
     239 */
     240#define RTListForEachSafe(pList, pIterator, pIterNext, Type, Member) \
     241    for (pIterator = RTListNodeGetNext(pList, Type, Member), \
     242         pIterNext = RT_FROM_MEMBER((pIterator)->Member.pNext, Type, Member); \
     243         !RTListNodeIsDummy(pList, pIterator, Type, Member); \
     244         pIterator = pIterNext, \
     245         pIterNext = RT_FROM_MEMBER((pIterator)->Member.pNext, Type, Member) )
     246
     247
     248/**
    230249 * Enumerate the list in reverse order (tail to head).
    231250 *
     
    242261
    243262/**
     263 * Enumerate the list in reverse order (tail to head).
     264 *
     265 * @param   pList               List to enumerate.
     266 * @param   pIterator           The iterator variable name.
     267 * @param   pIterPrev           The name of the variable saving the pointer to
     268 *                              the previous element.
     269 * @param   Type                Structure the list node is a member of.
     270 * @param   Member              The list node member name.
     271 */
     272#define RTListForEachReverseSafe(pList, pIterator, pIterPrev, Type, Member) \
     273    for (pIterator = RTListNodeGetPrev(pList, Type, Member), \
     274         pIterPrev = RT_FROM_MEMBER((pIterator)->Member.pPrev, Type, Member); \
     275         !RTListNodeIsDummy(pList, pIterator, Type, Member); \
     276         pIterator = pIterPrev, \
     277         pIterPrev = RT_FROM_MEMBER((pIterator)->Member.pPrev, Type, Member) )
     278
     279
     280/**
    244281 * Move the given list to a new list header.
    245282 *
  • trunk/src/VBox/Runtime/testcase/tstRTList.cpp

    r28800 r32445  
    106106static void tstRTListCreate(RTTEST hTest, unsigned cElements)
    107107{
    108     RTTestISubF("RTList - Test with %u elements", cElements);
     108    RTTestISubF("Creating and moving - %u elements", cElements);
    109109
    110110    RTLISTNODE ListHead;
     
    140140    tstRTListOrder(hTest, &ListHeadNew, cElements, 0, cElements-1, 1);
    141141
    142     /* Remove elements now  */
     142    /*
     143     * Safe iteration w/ removal.
     144     */
     145    RTTestISubF("Safe iteration w/ removal - %u elements", cElements);
     146
     147    /* Move it element by element. */
     148    PLISTELEM pNode, pSafe;
     149    RTListForEachSafe(&ListHeadNew, pNode, pSafe, LISTELEM, Node)
     150    {
     151        RTListNodeRemove(&pNode->Node);
     152        RTListAppend(&ListHead, &pNode->Node);
     153    }
     154    tstRTListOrder(hTest, &ListHead, cElements, 0, cElements-1, 1);
     155
     156    /* And the other way. */
     157    RTListForEachReverseSafe(&ListHead, pNode, pSafe, LISTELEM, Node)
     158    {
     159        RTListNodeRemove(&pNode->Node);
     160        RTListPrepend(&ListHeadNew, &pNode->Node);
     161    }
     162    tstRTListOrder(hTest, &ListHeadNew, cElements, 0, cElements-1, 1);
     163
     164    /*
     165     * Remove elements now.
     166     */
    143167    if (cElements > 1)
    144168    {
    145169        /* Remove every second */
    146         RTTestISub("Remove every second node");
    147 
    148         PLISTELEM pNode = RTListNodeGetFirst(&ListHeadNew, LISTELEM, Node);
     170        RTTestISubF("Remove every second node - %u elements", cElements);
     171
     172        pNode = RTListNodeGetFirst(&ListHeadNew, LISTELEM, Node);
    149173        for (unsigned i = 0; i < cElements; i++)
    150174        {
     
    168192
    169193    /* Remove the rest now. */
    170     RTTestISub("Remove all nodes");
    171     PLISTELEM pNode = RTListNodeGetFirst(&ListHeadNew, LISTELEM, Node);
     194    RTTestISubF("Remove all nodes - %u elements", cElements);
     195    pNode = RTListNodeGetFirst(&ListHeadNew, LISTELEM, Node);
    172196    for (unsigned i = 0; i < cElements; i++)
    173197    {
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette