我尝试简化以下条件:
for ( int t=0, size=fo.getPrintViewsPerFile().size();
t<size && t<countPerFile;
t++)
{
// ...
}
,更准确地说:
t<s && t<c
您需要比较两次,然后计算它们的布尔值。有没有更简单的方法来做到这一点?如果不是,你如何证明呢?我可以在某种程度上简化它, 证明树。
[已添加]
我试图直接通过逻辑来解决问题。看看选择最小值的含义会很有趣。链接:
http://www.umsu.de/logik/trees/?f=< /a>(\exists%20s%20\exists%20c%20\forall%20t%20%20(Pts%20\land%20Ptc))\leftrightarrow\neg(\foralls\forallc\existst(\neg(Pts) )\lor\neg(Ptc)))
I try to simplify conditionals in:
for ( int t=0, size=fo.getPrintViewsPerFile().size();
t<size && t<countPerFile;
t++)
{
// ...
}
, more precisely:
t<s && t<c
You need to compare two times, then calc the boolean value from them. Is there any simpler way to do it? If no, how can you prove it? I can simplify it to some extent, proof tree.
[Added]
I tried to solve the problem directly through logic. It would interesting to see the implication in choosing the minima. Link:
http://www.umsu.de/logik/trees/?f=(\exists%20s%20\exists%20c%20\forall%20t%20%20(Pts%20\land%20Ptc))\leftrightarrow\neg(\foralls\forallc\existst(\neg(Pts)\lor\neg(Ptc)))
发布评论
评论(3)
如果
size
和countPerFile
在循环期间保持不变,则可以在循环之前预先计算它们的最小值,然后测试将是t.
If
size
andcountPerFile
are constant for the duration of the loop, you can precalculate their minimum before the loop, and then the test would bet<minimum
.你可以做
t < Math.min(s, c)
,但这实际上不会减少比较次数。我确实认为适当使用
Math.min
和Math.max
使代码更具可读性。不幸的是,它们只有 2 个参数的重载(对于int
、long
、float
和double
参数)。如果它们也有 3 个参数和可变参数重载,那就太好了。您始终可以为此类事情编写实用程序方法(间隔检查是常见的习惯用法
(minV <= v) && (v <= maxV)
等),但是 从语言上来说,Java 没有任何奇特的运算符可以完成这些事情。它们只有基本的数字比较运算符 (JLS 15.20 .1)和基本布尔运算符(JLS 15.22.2,15.23 、15.24)。旁注
高级语言,如 Icon 确实允许这些类型的构造:
You can do
t < Math.min(s, c)
, but that wouldn't actually reduce the number of comparison.I do think that appropriate use of
Math.min
andMath.max
makes for a much more readable code, though. Unfortunately they only have overloads for 2 args (forint
,long
,float
anddouble
arguments). It would've been really nice if they also have 3 args and varargs overloads.You can always write utilities method for these kinds of things (interval checking is a common idiom
(minV <= v) && (v <= maxV)
etc), but linguistically, no Java doesn't have any fancy operators that would do these things. They only have basic numerical comparison operators (JLS 15.20.1) and basic boolean operators (JLS 15.22.2, 15.23, 15.24).Sidenote
Higher-level languages like Icon does allow these kinds of constructs:
忘记它吧。如果您正在处理打印或文件,这种微观优化几乎不会为您节省任何东西。
Forget about it. If you're dealing with printing or files this kind of micro-optimizaton would save you practically nothing.