如何比较一组枚举类型 [英] How to compare sets of enumerated types

查看:102
本文介绍了如何比较一组枚举类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从某种意义上讲,我已经厌倦了编写设置条件(),因为需要更多条件或更长的变量名开始变得笨拙且烦人,需要重新编写。所以我开始写助手,这样就可以写 ASet.ContainsOne([ceValue1,ceValue2])而不是(ASet中的ceValue1)或(ASet中的ceValue2 )

  type 
TCustomEnum =(ceValue1,ceValue2,ceValue3);
TCustomSet = TCustomEnum的集合;
TCustomSetHelper =记录TCustomSet的帮助器
函数ContainsOne(ASet:TCustomSet):布尔值;
函数ContainsAll(ASet:TCustomSet):布尔值;
结尾;

实现

函数TCustomSetHelper.ContainsOne(ASet:TCustomSet):布尔值;
var
lValue:TCustomEnum;
开始
以获得ASet中的lValue如果$ self $中的lValue则
开始
然后
Exit(True);
结尾;
结果:= False;
结尾;

函数TCustomSetHelper.ContainsAll(ASet:TCustomSet):布尔值;
var
lValue:TCustomEnum;
开始
结果:= True; ASet中的lValue的
会以
开头(如果不是,则为
)(Self中的lValue),然后
退出(错误);
结尾;
结尾;

不幸的是,这不是最有效的解决方案,它违反了DRY原则。令我惊讶的是,我找不到任何人处理过相同的问题,所以我想知道是否有更好的(通用)解决方案?

解决方案

集合运算符可帮助您实现这些功能



对于 ContainsOne ,我们使用 * 运算符

 函数TCustomSetHelper.ContainsOne(ASet:TCustomSet):布尔值;设置交集运算符。 
开始
结果:= ASet *自我<> [];
结尾;

对于 ContainsAll ,我们将使用< = 是子集运算符。

  function TCustomSetHelper.ContainsAll(ASet: TCustomSet):布尔值; 
开始
结果:= ASet< =自我;
结尾;

鉴于这些表达式有多简单,我怀疑您是否根本需要帮助程序类型。 / p>

文档给出可用集合运算符的完整列表。


From a certain point I got tired of writing set conditions (and, or), because for more conditions or longer variable names it begins to be clumsy and annoying to write all over again. So I started writing helpers so I could write ASet.ContainsOne([ceValue1, ceValue2]) instead of (ceValue1 in ASet) or (ceValue2 in ASet).

type
  TCustomEnum = (ceValue1, ceValue2, ceValue3);
  TCustomSet = set of TCustomEnum;
  TCustomSetHelper = record helper for TCustomSet 
    function ContainsOne(ASet: TCustomSet): Boolean;
    function ContainsAll(ASet: TCustomSet): Boolean;
  end;

implementation

function TCustomSetHelper.ContainsOne(ASet: TCustomSet): Boolean;
var
  lValue : TCustomEnum;
begin
  for lValue in ASet do
  begin
    if lValue in Self then
      Exit(True);
  end;
  Result := False;
end;

function TCustomSetHelper.ContainsAll(ASet: TCustomSet): Boolean;
var
  lValue : TCustomEnum;
begin
  Result := True;
  for lValue in ASet do
  begin
    if not (lValue in Self) then
      Exit(False);
  end;
end;

Unfortunately, this is not the most effective solution and it's against the DRY principle. To my surprise, I didn't find anyone ever dealing with the same problem, so I wonder if there is any better (generic) solution?

解决方案

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.

这篇关于如何比较一组枚举类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆