简体   繁体   English

枚举类型:限制项目数量?

[英]Enumerated Type: Limit to number of items?

Is there a limit in Delphi to the number of items you can have in an enumerated type? Delphi中对枚举类型中的项目数有限制吗? I need to create an enumerated type that might have several hundred items, and want to make sure there is not a limit at 255 items for example. 我需要创建一个可能有几百个项目的枚举类型,并且要确保例如255个项目没有限制。

type 
  TMyType = (mtOne, mtTwo, mtThree, ..., mtThreeHundred);

I believe the theoretical limit is 2^32 items; 我相信理论上的限制是2 ^ 32项; but in practice, RTTI generation is normally the limit, as RTTI can't exceed 65535 bytes to store everything, including the names of the enumeration elements; 但实际上,RTTI生成通常是限制,因为RTTI不能超过65535字节来存储所有内容,包括枚举元素的名称; the names are stored in UTF-8, so it's not too bad. 名称存储在UTF-8中,所以它不是太糟糕。

On the other hand, enumerations with explicit values for the elements don't have full RTTI, so you can evade the limit that way. 另一方面,具有元素显式值的枚举没有完整的RTTI,因此您可以通过这种方式规避限制。 Here's a program which creates a source file with 500,001 enumeration elements, which itself compiles: 这是一个程序,它创建一个包含500,001个枚举元素的源文件,它本身编译:

var
  i: Integer;
begin
  Writeln('type');
  Writeln('  E = (');
  for i := 1 to 500000 do
    Writeln('  x_', i, ' = ', i, ',');
  Writeln('x_last);');
  Writeln('begin');
  Writeln('end.');
end.

The output of this program takes some time to compile with dcc32 because the Delphi compiler uses a hash table with only 32 buckets for checking for enumeration identifier duplicates, and a hash table with only 256 buckets for file-level scope, which (in the absence of {$SCOPEDENUMS ON} ) is where enumeration identifiers are added. 该程序的输出需要一些时间来使用dcc32进行编译,因为Delphi编译器使用只有32个桶的哈希表来检查枚举标识符重复项,以及一个只有256个桶用于文件级范围的哈希表(在没有的情况下) {$SCOPEDENUMS ON} )是添加枚举标识符的位置。

I found a maximum of 65535 items in a german Delphi book. 我在德语Delphi书中发现了最多65535个项目。

After some digging in the documenation I found the respective section: 在对文件进行一些挖掘之后,我找到了相应的部分:

Enumerated Types 枚举类型

An enumerated type is stored as an unsigned byte if the enumeration has no more than 256 values and the type was declared in the {$Z1} state (the default). 如果枚举的值不超过256且类型以{$Z1}状态(默认值)声明,则枚举类型将存储为无符号字节。 If an enumerated type has more than 256 values, or if the type was declared in the {$Z2} state, it is stored as an unsigned word. 如果枚举类型具有超过256个值,或者如果类型是在{$Z2}状态中声明的,则它将存储为无符号字。 If an enumerated type is declared in the {$Z4} state, it is stored as an unsigned double-word. 如果在{$Z4}状态中声明了枚举类型,则将其存储为无符号双字。

So in fact there should be a possible maximum of 4294967295 ( $FFFFFFFF ) items. 所以实际上最多应该有4294967295( $FFFFFFFF )个项目。

Try it and see? 试试吧,看看? It should just take a few minutes to write a loop that will build your type statement as long as you want. 只需几分钟就可以编写一个循环来构建你的类型语句,只要你愿意。 Output with a messagebox (which can be copied to the clipboard with ctrl+c), paste back into Delphi, and you're all set. 使用消息框输出(可以使用ctrl + c复制到剪贴板),粘贴回Delphi,然后就可以了。

Yes enums in Delphi can have more than 256 items. 是的Delphi中的枚举可以有超过256个项目。 You won't have problem with them, but if you are going to use set types, you should take note that sets can have 256 elements at most. 你不会遇到问题,但是如果要使用集合类型,你应该注意集合最多可以包含256个元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM