How to compare sets of enumerated types

血红的双手。 提交于 2019-12-04 03:36:55

The set operators help you implement these functions

For ContainsOne we use the * operator which is the set intersection operator.

function TCustomSetHelper.ContainsOne(ASet: TCustomSet): Boolean;
begin
  Result := ASet * Self <> [];
end;

For ContainsAll we would use <= which is the subset operator.

function TCustomSetHelper.ContainsAll(ASet: TCustomSet): Boolean;
begin
  Result := ASet <= Self;
end;

Given how simple these expressions are, I question whether or not you need the helper type at all.

The documentation gives the full list of available set operators.

You can use the set intersection operator

For ContainsOne analog check if intersection is not empty set, for ContainsAll check that intersection coincides with argument set

type
  TCustomEnum = (ceValue1, ceValue2, ceValue3);
  TCustomSet = set of TCustomEnum;
var
  ASet: TCustomSet;
begin
  ASet := [ceValue1, ceValue3];

  if ([ceValue1, ceValue2] *  ASet) <> [] then
     Memo1.Lines.Add('Somebody here');

  if ([ceValue1, ceValue3] *  ASet) = [ceValue1, ceValue3] then
     Memo1.Lines.Add('All are in home');
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!