什么Delphi类型为'整数集'?

Jan*_*gen 9 delphi types

我有几个这样的硬编码验证:

const
  cLstAct      =  1;   
  cLstOrg      =  4;
  cLstClockAct = 11;
const
  FUNCT_1 = 224;
  FUNCT_2 = 127;
  FUNCT_3 =   3;

if lFuncID in [FUNCT_1,FUNCT_2,FUNCT_3] then ...
if not (lListType in [cLstAct..cLstOrg,cLstClockAct]) then ...
if not (lPurpose in [0..2]) then ...
Run Code Online (Sandbox Code Playgroud)

我想用一个常见的方法替换

function ValidateInSet(AIntValue: integer; AIntSet: @@@): Boolean;
begin
  Result := (AIntValue in AIntSet);
  if not Result then ...
end;  
Run Code Online (Sandbox Code Playgroud)

但是选择什么类型AIntSet

目前,整个代码中要测试的值达到const值232(因此我可以使用TByteSet = Set of Byte),但我可以预见到E1012 Constant expression violates subrange bounds当常量值超过255时我们将碰到.

我的Google-fu在这里让我失望......

(目前在Delphi Seattle Update 1上)

Dav*_*nan 8

使用字典TDictionary<Integer, Integer>.价值无关紧要,你只关心钥匙.如果字典包含特定键,则该键是该组的成员.使用AddOrSetValue添加成员,Remove删除成员,ContainsKey以测试会员.

使用字典的关键是它为您提供O(1)查找.

您不希望将此类型直接用作集合.你应该将它包装在一个只暴露set like功能的类中.可以在此处找到一个示例:https://stackoverflow.com/a/33530037/505088

  • @arioch对齐意味着没有区别 (2认同)

Rem*_*eau 4

您可以使用array of Integer

function ValidateInSet(AIntValue: integer; AIntSet: array of Integer): Boolean;
var
  I: Integer;
begin
  Result := False;
  for I := Low(AIntSet) to High(AIntSet) do
  begin
    if AIntSet[I] = AIntValue then
    begin
      Result := True;
      Break;
    end;
  end;
  if not Result then ...
end;  
Run Code Online (Sandbox Code Playgroud)

const
  cLstAct      =  1;   
  cLstOrg      =  4;
  cLstClockAct = 11;
const
  FUNCT_1 = 224;
  FUNCT_2 = 127;
  FUNCT_3 =   3;

if ValidateInSet(lFuncID, [FUNCT_1, FUNCT_2, FUNCT_3]) then ...
if not ValidateInSet(lListType, [cLstAct, 2, 3, cLstOrg, cLstClockAct]) then ...
if not ValidateInSet(lPurpose, [0, 1, 2]) then ...
Run Code Online (Sandbox Code Playgroud)