简体   繁体   English

在Delphi中:如何在不使用参数的情况下找到递归深度?

[英]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.

相关问题 如何找到是否安装了打印机(使用Delphi) - How can I find if there are printers installed (using Delphi) 如何在Delphi中使用“文件查找”执行布尔“ AND”搜索? - How can I perform a boolean 'AND' search in Delphi using 'Find in Files? 如何在不使用IDE的情况下编译Delphi应用程序? - How can I compile a Delphi application without using the IDE? "如何在不传递句柄参数的情况下使用 Delphi 在 dll 项目中获取主机应用程序句柄" - How can I get host application handle in dll project with Delphi without passing handle parameter 如何在Delphi中更改orderBy firebase数据库参数? - How I can Change the orderBy firebase database parameter in Delphi? 如何在delphi中传递一个字符串数组参数 - How can I pass array of string a parameter to function in delphi 如何在Delphi中使TDate参数成为可选参数? - How can I make a TDate parameter optional in Delphi? 如何通过使用delphi替换给定字符串中带空格或不带空格的特殊字符 - How can I replace a special characters in a given string with space or without space by using delphi 如何在不使用OLE的情况下在delphi中将word文档转换为pdf? - How can I convert a word document to pdf in delphi without using OLE? 在哪里可以找到将DUnit与Delphi 2007或更新版本一起使用的介绍? - Where can I find an introduction to using DUnit with Delphi 2007 or newer?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM