直接的答案是TBits
类
http://docwiki.embarcadero.com/Libraries/Seattle/en/System.Classes.TBits.Bits
注:此只能使用从德尔福XE4开始 - http://qc.embarcadero.com/wc/qcmain.aspx?d=108829
但是你在大多数情况下膨胀“整数集”,将采取2^31/8
字节的内存(因为整数负值将不考虑),这将是一个很大... 所以我希望你永远不会真的想拥有一整套整数。或者你应该投入稀疏阵列。
function ValidateInSet(const AIntValue: integer; const AIntSet: TBits): Boolean;
begin
Result := (AIntValue >= 0) and (AIntValue < AIntSet.Size);
if Result then
Result := AIntSet.Bits[AIntValue];
if not Result then ...
v-a-l-i-d-a-t-e
end;
或者说
function ValidateInSet(const AIntValue: integer; const AIntSet: TBits): Boolean;
begin
Result := false;
if AIntValue < 0 then exit; // Validation criterion #1
if AIntValue >= AIntSet.Size then exit; // Validation criterion #2
if not AIntSet.Bits[AIntValue] then exit; // Validation criterion #3
if .... then exit; // Validation criterion #4
if .... then exit; // Validation criterion #5
if .... then exit; // Validation criterion #6
Result := true;
end;
或许
TSetTestCriterion = TFunc<Integer, Boolean>;
TSetTestCriteria = TArray<TFunc<Integer, Boolean>>;
function ValidateInSet(const AIntValue: integer;
const AIntSet: TBits; const Tests: TSetTestCriteria = nil): Boolean;
var ExtraTest: TSetTestCriterion;
begin
Result := false;
if AIntValue < 0 then exit; // Validation criterion #1
if AIntValue >= AIntSet.Size then exit; // Validation criterion #2
if not AIntSet.Bits[AIntValue] then exit; // Validation criterion #3
if Tests <> nil then // Validation criteria #4, #5, #6, ...
for ExtraTest in Tests do
if not ExtraTest(AIntValue) then exit;
Result := true;
end;
http://docwiki.embarcadero.com/Libraries/Seattle/en/System.SysUtils.TFunc
现在 - 只是为了演示,在真正的应用程序,你会创建这些设置和阵列一次和缓存长(永远,或者至少除非配置改变需要重建它们)。
Type FuncIDs = (FUNCT_3 = 3, FUNCT_2 = 127, FUNCT_1 = 224);
var MysticGlobalFlag: Boolean;
function ValidateFuncID(const lFuncID: FuncIDs): Boolean;
var map: TBits;
begin
map := TBits.Create;
try
map.Size := High(lFuncID) + 1;
map.Bits[ Ord(Func_1) ] := True;
map.Bits[ Ord(Func_2) ] := True;
map.Bits[ Ord(Func_3) ] := True;
Result := ValidateInSet(Ord(lFuncID), map,
TSetTestCriteria.Create(
function(lFuncID: integer) : Boolean
begin
Result := MysticGlobalFlag or (lFuncID <> Ord(FuncIDs.FUNC_2))
end
,
function(lFuncID: integer) : Boolean
begin
Result := (lFuncID <> Ord(FuncIDs.FUNC_3)) or (DayOfTheWeek(Now()) = 4)
end
)
);
finally
map.Destroy;
end;
if not Result then // from the original question code
... // seems like a placeholder for error handling or object creation and registration
end;
Spring4D有THashSet:http://www.devjetsoftware.com/docs/spring4d/index.htm?Spring .Collections.Sets.THashSet%28T%29.IsSupersetOf.htm – Johan
有趣的是,FPC中的AFAIR可以创建与内存将包含的大集合。虽然我不确定这个特定的Delphi限制是不是真的有优势... –
您可以使用泛型集[here](http://stackoverflow.com/a/19524788/576719)。 –