如何使用循环而不是递归 [英] how to use loop instead of recursion

查看:66
本文介绍了如何使用循环而不是递归的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如下:

As follow:

void divconqrecurse(struct mesh *m, struct behavior *b, vertex *sortarray,
                    int vertices, int axis,
                    struct otri *farleft, struct otri *farright)
{
  struct otri innerleft, innerright;
  int divider;
  if (vertices == 2) 
  {
    //end statement
    //that assign to farleft and farright
    return;
  }
  else if (vertices == 3) 
  {
    //end statement
    //that assign to farleft and farright
    return;
  } 
  else 
  {
    divider = vertices >> 1;
    divconqrecurse(m, b, sortarray, divider, 1 - axis, farleft, &innerleft);
    divconqrecurse(m, b, &sortarray[divider], vertices - divider, 1 - axis,
                   &innerright, farright);
    mergehulls(m, b, farleft, &innerleft, &innerright, farright, axis);
  }
}

推荐答案

潜在的开始:):

A potential starting :) :
#include <stack>
#include <memory>

class ParList
{
  struct mesh *m;
  struct behavior *b;
  vertex *sortarray;
  int vertices;
  int axis;
  struct otri *farleft;
  struct otri *farright;

public:
  ParList()
  {
    ..
  }
  ParList(../*all needed pars*/)
  {
    ..
  }
  ParList(const ParList& src)
  {
    ..
  }
  const ParList& operator=(const ParList& src) {..}

  int getVertices() const { return vertices; }

  //..
};

extern void mergehulls(const ParList& listPars);


void TheLoop(const ParList& listPars)
{
  std::stack<std::unique_ptr<ParList>> stackParLists;
  stackParLists.push(std::make_unique<ParList>(listPars));

  while (stackParLists.size())
  {
    bool bCurListValid(false);

    ParList curList;
    ParList* pcurList(stackParLists.top().get());
    if (pcurList)
    {
      curList = *pcurList;
      bCurListValid = true;
    }
    stackParLists.pop();

    enum { retVert2 = 2, retVert3 };

    if (bCurListValid &&
        retVert2 != curList.getVertices() &&
        retVert3 != curList.getVertices()) {
      
      ParList leftList(curList);
      // discover pars of leftList:
      //..here :)

      // push the leftList:
      stackParLists.push(std::make_unique<ParList>(leftList));

      ParList rightList(curList);
      // discover pars of rightList:
      //.. here :)

      // push the rightList:
      stackParLists.push(std::make_unique<ParList>(leftList));

      // make the merged list
      ParList mergedList(curList);
      //.. here :)

      // call for the merged list
      mergehulls(mergedList);

      // now the left- and right-lists will be precessed
    }
  }
}


每个OP请求...



此代码在ASP.NET控制树中搜索具有特定ID的一个。

这样的控制树是为经典的递归搜索而构建的,因为每个节点看起来完全一样,但在这里我显示一种非递归的方法。

我在图像中的像素级搜索中也使用了相同的想法,我也测量了性能 - 非递归执行的性能提高了一百倍...... br />
Per OP request...

This code search in an ASP.NET control tree for one with the specific ID.
Such control tree is built for the classic recursive search as every node looks exactly the same, but here I show a not-recursive approach.
I also used the very same idea in a pixel-level search inside an image where I also measured performance - the non-recursive performed a hundred times better...
public static Control FindControlById ( Control oParent, string szID )
{
	Control oControl = null;
	Stack oStack = new Stack( );

	oStack.Push( oParent );

	while ( oStack.Count != 0 )
	{
		Control oCheckControl = ( Control )oStack.Pop( );

		if ( oCheckControl.ID == szID )
		{
			oControl = oCheckControl;

			break;
		}
		else if ( oCheckControl.HasControls( ) )
		{
			foreach ( Control oChildControl in oCheckControl.Controls )
			{
				oStack.Push( oChildControl );
			}
		}
	}

	return ( oControl );
}


这篇关于如何使用循环而不是递归的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆