VirtualBox

Changeset 93693 in vbox for trunk/include/iprt/cpp


Ignore:
Timestamp:
Feb 11, 2022 11:06:35 AM (3 years ago)
Author:
vboxsync
Message:

IPRT/hardavl: Added a getHeight() method for tstRTAvl and extended the testcase to check the tree height. bugref:10093

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/iprt/cpp/hardavlrange.h

    r93689 r93693  
    212212        return i_rebalance(a_pAllocator, &AVLStack);
    213213    }
    214 
    215 #ifdef RT_STRICT
    216 
    217     static void dumpStack(RTCHardAvlTreeSlabAllocator<NodeType> *a_pAllocator, HardAvlStack const *pStack)
    218     {
    219         uint32_t const * const *paidx = pStack->apidxEntries;
    220         RTAssertMsg2("stack: %u:\n", pStack->cEntries);
    221         for (unsigned i = 0; i < pStack->cEntries; i++)
    222         {
    223             uint32_t idx     = *paidx[i];
    224             uint32_t idxNext = i + 1 < pStack->cEntries ? *paidx[i + 1] : UINT32_MAX;
    225             NodeType const *pNode = a_pAllocator->ptrFromInt(idx);
    226             RTAssertMsg2(" #%02u: %p[%#06x] pNode=%p h=%02d l=%#06x%c r=%#06x%c\n", i, paidx[i], idx, pNode, pNode->cHeight,
    227                          pNode->idxLeft,  pNode->idxLeft  == idxNext ? '*' : ' ',
    228                          pNode->idxRight, pNode->idxRight == idxNext ? '*' : ' ');
    229         }
    230     }
    231 
    232     static void printTree(RTCHardAvlTreeSlabAllocator<NodeType> *a_pAllocator, uint32_t a_idxRoot, unsigned iLevel = 0)
    233     {
    234         if (a_idxRoot == a_pAllocator->kNilIndex)
    235             RTAssertMsg2("%*snil\n", iLevel * 6, "");
    236         else if (iLevel < 7)
    237         {
    238             NodeType *pNode = a_pAllocator->ptrFromInt(a_idxRoot);
    239             printTree(a_pAllocator, pNode->idxRight, iLevel + 1);
    240             RTAssertMsg2("%*s%#x/%u\n", iLevel * 6, "", a_idxRoot, pNode->cHeight);
    241             printTree(a_pAllocator, pNode->idxLeft, iLevel + 1);
    242         }
    243         else
    244             RTAssertMsg2("%*stoo deep\n", iLevel * 6, "");
    245     }
    246 
    247 #endif
    248214
    249215    /**
     
    723689    }
    724690
     691
     692    /**
     693     * Gets the tree height value (reads cHeigh from the root node).
     694     *
     695     * @retval UINT8_MAX if bogus tree.
     696     */
     697    uint8_t getHeight(RTCHardAvlTreeSlabAllocator<NodeType> *a_pAllocator)
     698    {
     699        NodeType *pNode = a_pAllocator->ptrFromInt(m_idxRoot);
     700        AssertMsgReturnStmt(a_pAllocator->isPtrRetOkay(pNode), ("m_idxRoot=%#x pNode=%p\n", m_idxRoot, pNode),
     701                            m_cErrors++, UINT8_MAX);
     702        if (pNode)
     703            return pNode->cHeight;
     704        return 0;
     705    }
     706
     707#ifdef RT_STRICT
     708
     709    static void dumpStack(RTCHardAvlTreeSlabAllocator<NodeType> *a_pAllocator, HardAvlStack const *pStack)
     710    {
     711        uint32_t const * const *paidx = pStack->apidxEntries;
     712        RTAssertMsg2("stack: %u:\n", pStack->cEntries);
     713        for (unsigned i = 0; i < pStack->cEntries; i++)
     714        {
     715            uint32_t idx     = *paidx[i];
     716            uint32_t idxNext = i + 1 < pStack->cEntries ? *paidx[i + 1] : UINT32_MAX;
     717            NodeType const *pNode = a_pAllocator->ptrFromInt(idx);
     718            RTAssertMsg2(" #%02u: %p[%#06x] pNode=%p h=%02d l=%#06x%c r=%#06x%c\n", i, paidx[i], idx, pNode, pNode->cHeight,
     719                         pNode->idxLeft,  pNode->idxLeft  == idxNext ? '*' : ' ',
     720                         pNode->idxRight, pNode->idxRight == idxNext ? '*' : ' ');
     721        }
     722    }
     723
     724    static void printTree(RTCHardAvlTreeSlabAllocator<NodeType> *a_pAllocator, uint32_t a_idxRoot,
     725                          unsigned a_uLevel = 0, unsigned a_uMaxLevel = 8, const char *a_pszDir = "")
     726    {
     727        if (a_idxRoot == a_pAllocator->kNilIndex)
     728            RTAssertMsg2("%*snil\n", a_uLevel * 6, a_pszDir);
     729        else if (a_uLevel < a_uMaxLevel)
     730        {
     731            NodeType *pNode = a_pAllocator->ptrFromInt(a_idxRoot);
     732            printTree(a_pAllocator, pNode->idxRight, a_uLevel + 1, a_uMaxLevel, "/ ");
     733            RTAssertMsg2("%*s%#x/%u\n", a_uLevel * 6, a_pszDir, a_idxRoot, pNode->cHeight);
     734            printTree(a_pAllocator, pNode->idxLeft, a_uLevel + 1, a_uMaxLevel, "\\ ");
     735        }
     736        else
     737            RTAssertMsg2("%*stoo deep\n", a_uLevel * 6, a_pszDir);
     738    }
     739
     740#endif
     741
    725742private:
    726743    /**
Note: See TracChangeset for help on using the changeset viewer.

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