I have a function here that is recursive, but I would instead like to make it non recursive. I'm just not sure how.
void AguiWidgetManager::recursiveRender(const AguiWidget *root)
{
//recursive开发者_如何转开发ly calls itself to render widgets from back to front
AguiWidget* nonConstRoot = (AguiWidget*)root;
if(!nonConstRoot->isVisable())
{
return;
}
clip(nonConstRoot);
nonConstRoot->paint(AguiPaintEventArgs(true,graphicsContext));
for(std::vector<AguiWidget*>::const_iterator it =
root->getPrivateChildBeginIterator();
it != root->getPrivateChildEndIterator(); ++it)
{
recursiveRender(*it);
}
for(std::vector<AguiWidget*>::const_iterator it =
root->getChildBeginIterator();
it != root->getChildEndIterator(); ++it)
{
recursiveRender(*it);
}
}
Its okay if the solution wont work with iterators.
Thanks
The easy way is just to maintain your own stack. Psuedoish example code:
stack s;
while(!s.empty())
{
root = s.pop();
//your code here
//replace each recursive call with s.push(it)
}
Also, casting away constness is a bad, bad idea. It shouldn't be a const argument in the first place if you want to modify it.
In general, you would need to implement a stack yourself:
clear stack
push first item onto stack
while stack is not empty:
pop top item off stack
do action for item (clip, paint)
push children onto stack (if any)
精彩评论