[英]In Delphi: How can I find the recursion depth without using a parameter?
When I create recursive methods, I often include a Depth-parameter, especially when I need some sort of bailout mechanism. 当我创建递归方法时,我经常包含一个Depth参数,特别是当我需要某种救助机制时。 The code will usually be something like this
代码通常是这样的
procedure Recurse(<Params>; aDepth : integer = 0);
begin
if aDepth > SomeLimit then
begin
//Tidy up, return best result found>
exit;
end;
<stuff>
if <Condition> then
Recurse(<Params>; aDepth+1)
else
begin
//Tidy up, return result of endnode>
end;
end;
And I call it without the Depth-parameter 我称之为没有Depth参数
Recurse(<Params>);
Is there another way to easily find the depth? 还有另一种方法可以轻松找到深度吗?
If you had a way to walk the stack and see how many times your function's entry point was in there, I suppose you could do it that way. 如果你有办法走栈,看看你的函数的入口点在那里多少次,我想你可以这样做。 But then you'd notice that your aDepth parameter was right there too, and you'd realize that aDepth is just easier and much less trouble than snooping the stack.
但是你会注意到你的aDepth参数也是正确的,你会发现aDepth比窥探堆栈更简单,更麻烦。 IMO, the simple solution is best here, it's portable and future-proof, unlike whatever stack snooping solution you could invent.
IMO,简单的解决方案在这里是最好的,它是可移植的,面向未来的,不像你可以发明的任何堆栈侦听解决方案。
So yes, there are other ways, but your original solution is best, IMO. 所以是的,还有其他方法,但你最初的解决方案是最好的,IMO。
Declare a typed constant inside your procedure. 在过程中声明一个类型化的常量。 Make sure you set the compile option to allow changes in constants.
确保将compile选项设置为允许更改常量。
procedure Recurse;
const
aDepth : integer = 0;
begin
aDepth := aDepth + 1;
try
if aDepth > SomeLimit then
begin
//Tidy up, return best result found>
exit;
end;
<stuff>
if <Condition> then
Recurse
else
begin
//Tidy up, return result of endnode>
end;
finally
aDepth := aDepth - 1;
end;
end;
Have a global variable that would count recursion? 有一个可以计算递归的全局变量吗? Otherwise no - recursion is just calling some method with some parameters.
否则没有 - 递归只是用一些参数调用一些方法。
In C++ I am able to do this 在C ++中,我能够做到这一点
class recursion_guard
{
public:
recursion_guard() { depth_count++; }
~recursion_guard() { depth_count--; }
static int depth_count;
};
int recursion_guard::depth_count = 0;
void recurse(recursion_guard a = recursion_guard())
{
if(recursion_guard::depth_count > 100)
return;
recurse();
}
but since objects in Object Pascal are always allocated on the heap, I wonder if it is possible to use instead String with default argument and access somehow its reference count 但由于Object Pascal中的对象总是在堆上分配,我想知道是否可以使用带有默认参数的String,并以某种方式访问它的引用计数
const
MyRecursionGuardConstString: string = "whatever";
procedure Recurse(RecursionGuard: string = MyRecursionGuardConstString)
begin
if GetRefCount(MyRecursionGuardConstString) > 100 then //o_o
exit;
end;
Recurse;
end;
type
TMyRecursion = class
private
nDepth: integer;
public
constructor Create;
procedure Recursion(...)
end;
In constructor you must initialize nDepth, of course. 在构造函数中,您必须初始化nDepth,当然。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.