在 C#.NET 中执行布尔逻辑运算的最有效方法是什么?
我正在为我们在办公室使用的另一款软件编写一个插件,该插件将允许用户审核他们正在处理的文件。我正在努力让我的工具尽可能灵活。我的想法是,用户将生成一个节点树,其中可以包含其他节点作为子节点。在树的底部,节点将是条件节点,根据用户正在处理的文件,这些节点将失败或通过。此外,用户可以将每个节点设置为特定的逻辑类型,包括 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Linq 是您的朋友:
这实际上仍然只是执行
foreach
,但它们更加紧凑,并且All
和Any
操作适合您的操作需要。p =>; p
是一个返回布尔值的 lambda。例如,如果您正在检查具有方法DoesThisPass
的节点,则可以像这样重写检查:Linq is your friend:
This really still just does a
foreach
, but they are a lot more compact and theAll
andAny
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 methodDoesThisPass
, you rewrite the checks like this: