/// <summary> /// 初始化四叉树递归模拟 /// </summary> /// <param name="f">递归作用函数</param> /// <param name="f1">递归条件函数1</param> /// <param name="f2">递归条件函数2</param> /// <param name="f3">递归条件函数3</param> /// <param name="f4">递归条件函数4</param> /// <param name="stackElem">递归作用对象</param> public QuadtreeRecurrenceHelper(Func <StackElem <StackElemData>, StackElem <StackElemData> > f, Func <StackElem <StackElemData>, bool> f1, Func <StackElem <StackElemData>, bool> f2, Func <StackElem <StackElemData>, bool> f3, Func <StackElem <StackElemData>, bool> f4, StackElem <StackElemData> stackElem) { this.f1 = f1; this.f2 = f2; this.f3 = f3; this.f4 = f4; this.f = f; this.stackElem = stackElem; }
public StackElem <StackElemData> Recurrence() { int CaseNum = 0; do { if (stackElem.fragmentIndex == 5)//如果将要执行第5个片段,则表示一次递归完了 { CaseNum = 5; } if (CaseNum == 0) //代表进入了函数 { f(stackElem); //实施操作 if (f1(stackElem)) //当满足f1234函数时判断为true后,代表需要进行递归,其参数stackElem里面的OtherParameter就会更新为下次需要的参数 { CaseNum = 1; //记录此次位置,便于后面更改为下次递归的完成后的开始位置 } else if (f2(stackElem)) { CaseNum = 2; } else if (f3(stackElem)) { CaseNum = 3; } else if (f4(stackElem)) { CaseNum = 4; } } else if (CaseNum != 5) {//pop后,接着记录的caseNum执行 while (true) { if (CaseNum == 1) { if (f1(stackElem)) { CaseNum = 1; break; } if (f2(stackElem)) { CaseNum = 2; break; } if (f3(stackElem)) { CaseNum = 3; break; } if (f4(stackElem)) { CaseNum = 4; break; } } else if (CaseNum == 2) { if (f2(stackElem)) { CaseNum = 2; break; } if (f3(stackElem)) { CaseNum = 3; break; } if (f4(stackElem)) { CaseNum = 4; break; } } else if (CaseNum == 3) { if (f3(stackElem)) { CaseNum = 3; break; } if (f4(stackElem)) { CaseNum = 4; break; } } else if (CaseNum == 4) { if (f4(stackElem)) { CaseNum = 4; break; } } CaseNum = 0; break; } } if (CaseNum == 0 || CaseNum == 5) { //条件未匹配到或者已经递归完成,pop出函数 if (RecurrenceStack.Count == 0) { break; } StackElem <StackElemData> elem = RecurrenceStack.Pop(); CaseNum = elem.fragmentIndex;//此处不对 stackElem = elem; continue; } // else RecurrenceStack.Push(new StackElem<DataType> { fragmentIndex = CaseNum, OtherParameter = stackElem.OtherParameter }); else { switch (CaseNum) { case 1: //记录出函数时的片段位置,然后进入函数(push) CaseNum++; RecurrenceStack.Push(new StackElem <StackElemData> { fragmentIndex = CaseNum, OtherParameter = new StackElemData { fx = stackElem.OtherParameter.fx, index = stackElem.OtherParameter.index } }); break; case 2: CaseNum++; RecurrenceStack.Push(new StackElem <StackElemData> { fragmentIndex = CaseNum, OtherParameter = new StackElemData { fx = stackElem.OtherParameter.fx, index = stackElem.OtherParameter.index } }); break; case 3: CaseNum++; RecurrenceStack.Push(new StackElem <StackElemData> { fragmentIndex = CaseNum, OtherParameter = new StackElemData { fx = stackElem.OtherParameter.fx, index = stackElem.OtherParameter.index } }); break; case 4: CaseNum++; RecurrenceStack.Push(new StackElem <StackElemData> { fragmentIndex = CaseNum, OtherParameter = new StackElemData { fx = stackElem.OtherParameter.fx, index = stackElem.OtherParameter.index } }); break; } CaseNum = 0;//进入函数信号 } } while (RecurrenceStack.Count != 0); return(stackElem); }