在 C#.NET 中执行布尔逻辑运算的最有效方法是什么?

发布于 2024-09-30 22:44:12 字数 494 浏览 5 评论 0原文

我正在为我们在办公室使用的另一款软件编写一个插件,该插件将允许用户审核他们正在处理的文件。我正在努力让我的工具尽可能灵活。我的想法是,用户将生成一个节点树,其中可以包含其他节点作为子节点。在树的底部,节点将是条件节点,根据用户正在处理的文件,这些节点将失败或通过。此外,用户可以将每个节点设置为特定的逻辑类型,包括 AND、OR、NOR、NAND 。

AND:  All sub nodes must pass 
OR:   At least one sub node must pass 
NAND: At least one sub node must fail
NOR:  All sub nodes must fail 

我现在想弄清楚的是,如果我有一些由节点或子节点返回的 bool 集合,那么将上面的逻辑类型应用到此列表的最有效方法是什么?我开始临时编写 foreach 语句,但似乎由于二进制逻辑对于计算机工作方式至关重要,因此应该有更好、更快、迭代次数更少的方法。

I'm writing a plugin for another piece of sofware we use in my office that will allow users to audit the files they are working on. I'm trying to keep my tool as flexible as possible. The idea I have is that the user will generate a tree of Nodes that can contain other Nodes as sub-nodes. At the bottom of the tree the Nodes will be condition nodes that will either fail or pass depending on the file the user is working in. In addition, the user can set each Node to a specific logic type including AND, OR, NOR, NAND.

AND:  All sub nodes must pass 
OR:   At least one sub node must pass 
NAND: At least one sub node must fail
NOR:  All sub nodes must fail 

What I'm trying to figure out now is if I have some collection of bool's that were returned by a node or sub-node, what is the most efficient way to apply the logic types above to this list? Off hand I started writing foreach statments, but it seems since binary logic is so fundamental to the way computers work there would be a better, faster, and less iterative method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柠檬色的秋千 2024-10-07 22:44:13

Linq 是您的朋友:

var booleans = new List<bool> { true, true, false, true };

bool allPass = booleans.All(p => p);
bool anyPass = booleans.Any(p => p);
bool allFail = booleans.All(p => !p);
bool anyFail = booleans.Any(p => !p);

这实际上仍然只是执行 foreach,但它们更加紧凑,并且 AllAny 操作适合您的操作需要。

p =>; p 是一个返回布尔值的 lambda。例如,如果您正在检查具有方法 DoesThisPass 的节点,则可以像这样重写检查:

bool allPass = nodes.All(p => p.DoesThisPass());
bool anyPass = nodes.Any(p => p.DoesThisPass());
bool allFail = nodes.All(p => !p.DoesThisPass());
bool anyFail = nodes.Any(p => !p.DoesThisPass());

Linq is your friend:

var booleans = new List<bool> { true, true, false, true };

bool allPass = booleans.All(p => p);
bool anyPass = booleans.Any(p => p);
bool allFail = booleans.All(p => !p);
bool anyFail = booleans.Any(p => !p);

This really still just does a foreach, but they are a lot more compact and the All and Any operations fit what you need.

The p => p is a lambda that returns a boolean. If you for example are checking nodes that have a method DoesThisPass, you rewrite the checks like this:

bool allPass = nodes.All(p => p.DoesThisPass());
bool anyPass = nodes.Any(p => p.DoesThisPass());
bool allFail = nodes.All(p => !p.DoesThisPass());
bool anyFail = nodes.Any(p => !p.DoesThisPass());
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文