英文:
Can someone help deconstruct this terse Java function into plain English?
问题
以下是你要求的翻译部分:
public E poll() {
final Object[] es;
final E result;
if ((result = (E) ((es = queue)[0])) != null) {
modCount++;
final int n;
final E x = (E) es[(n = --size)];
es[n] = null;
if (n > 0) {
final Comparator<? super E> cmp;
if ((cmp = comparator) == null)
siftDownComparable(0, x, es, n);
else
siftDownUsingComparator(0, x, es, n, cmp);
}
}
return result;
}
变量 queue
是一个定义在类中的 Object[]
数组。
有几行让我感到困惑。首先:
if ((result = (E) ((es = queue)[0])) != null)
这是否意味着“将数组 queue
赋值给变量 es
,访问元素 0
,如果它不是空,则执行以下操作?” result = (E)
表达式的含义是什么?我知道 E
是一种泛型类型。
final E x = (E) es[(n = --size)];
这行代码的操作顺序是什么?这是否意味着减小 size
,将该值赋值给 n
,然后在 es
数组中访问该索引?如果是这样的话,这个表达式之前的 x = (E)
是什么意思?我猜它是将元素转换为类型 E
?
最后,这几行代码:
final Comparator<? super E> cmp;
if ((cmp = comparator) == null)
comparator
是一个类变量(持有一个 Comparator
)。为什么要将它赋值给局部变量 cmp
,第一行的问号代表什么意思?
英文:
I'm trying to port the PriorityQueue
class from the OpenJDK implementation to another language (Xojo) that doesn't have a similar data structure. I'm really struggling to break down the following method into pseudocode so I can translate it to Xojo:
public E poll() {
final Object[] es;
final E result;
if ((result = (E) ((es = queue)[0])) != null) {
modCount++;
final int n;
final E x = (E) es[(n = --size)];
es[n] = null;
if (n > 0) {
final Comparator<? super E> cmp;
if ((cmp = comparator) == null)
siftDownComparable(0, x, es, n);
else
siftDownUsingComparator(0, x, es, n, cmp);
}
}
return result;
}
The variable queue
is an Object[]
array defined on the class.
There are a couple of lines that are confusing me. Firstly:
if ((result = (E) ((es = queue)[0])) != null)
Does this mean "assign the array queue
to the variable es
and access element 0
and do the following if it's not null?" What does the result = (E)
expression mean? I know E
is a generic type.
What is the order of operation of final E x = (E) es[(n = --size)];
? Does this mean decrement size
, assign that value to n
and then access this index within the es
array? If so, what does x = (E)
before this expression mean? I'm guessing it means to cast the element to type E
?
Lastly, these lines:
final Comparator<? super E> cmp;
if ((cmp = comparator) == null)
comparator
is a class variable (holding a Comparator
). Why assign it to a local variable cmp
and what does the question mark mean on the first line?
答案1
得分: 3
> 这是不是意味着“将数组队列分配给变量es
”?
是的。
> 并且访问元素0,如果不为空,执行以下操作?
是的。
> result = (E)
表达式是什么意思?
与上面的两个表达式同时进行,它还将 queue[0]
赋值给 result
。 (E)
是一个类型转换。所以基本上就是:
result = queue[0]
附加了一些额外的内容。
> final E x = (E) es[(n = --size)];
这是什么意思?是不是先将 size
减小,将该值赋值给 n
,然后在 es
数组中访问这个索引?
是的。
> 如果是这样,那么在这个表达式之前的 x = (E)
是什么意思?我猜想这意味着将元素转换为类型 E
?
是的,再次只是像之前一样的类型转换。
> comparator
是一个类变量
严格来说,comparator
可能是实例变量,而不是类变量。请检查其定义。
> 为什么将其赋值给局部变量 cmp
?
我猜是为了创建一个局部变量的副本。我在代码中没有看到这样做的充分理由,所以这可能是一个错误,或者在之前的某些代码更改后遗留下来的。
> 第一行的问号代表什么意思?
问号表示 Comparator
的类型是未知的,只要它是 E
的超类即可。例如,如果 Integer
没有 Comparator
,但 Number
有,那么这是可以的,因为 Number
是 Integer
的超类,这就足够了。
英文:
> Does this mean "assign the array queue to the variable es
"
Yes.
> and access element 0 and do the following if it's not null?
Yes.
> What does the result = (E)
expression mean?
At the same time as the two expressions above, it also assigns queue[0]
to result
. The (E)
is a cast to a type. So it's basically just:
result = queue[0]
With some extra stuff thrown in.
> final E x = (E) es[(n = --size)];
? Does this mean decrement size
, assign that value to n
and then access this index within the es
array?
Yes.
> If so, what does x = (E)
before this expression mean? I'm guessing it means to cast the element to type E
?
Yes, again just a cast like before.
> comparator
is a class variable
Just to be pedantic, comparator
is likely an instance variable, not a class variable. Check its definition.
> Why assign it to a local variable cmp
I suppose to make a local variable copy. I don't see a good reason to do that in the code, so it might be a mistake or something that was left in after some previous code got changed.
> and what does the question mark mean on the first line?
The question mark means the type of the Comparator
is unknown, and can be anything as long as it's a super class of E
. For example, if Integer
doesn't have a Comparator
but Number
does, then that's OK, Number
is a super class of Integer
and that's good enough.
答案2
得分: 2
1- if ((result = (E) ((es = queue)[0])) != null)
首先将数组queue
赋值给变量es
,然后从中获取元素0
,将其转换为泛型类型E
并赋值给result
,然后检查result
是否不为null。
2- final E x = (E) es[(n = --size)];
首先Java计算--size
,然后将其赋值给整型变量n
,然后从数组es
中获取第n
个元素,将其转换为泛型类型E
,然后将其赋值给变量x
。
英文:
1- if ((result = (E) ((es = queue)[0])) != null)
<br>
First it assigns the array queue
to the variable es
and gets element 0
from it the casts it t E
generic type and assigns it to result
then checks if result
is not null.
2- final E x = (E) es[(n = --size)];
<br>
First java evaluates --size
then assigns to int type n
then gets n
th from es
array, casts it to E
, and then assigns it to variable x
.
<br>
I think the next two lines you asked are clear now!
答案3
得分: 2
让我们看看我能否帮助:
if ((result = (E) ((es = queue)[0])) != null)
以上代码的意思是“将queue
赋值给es
,访问其索引0,将其强制转换为类型E
,将其赋值给result
,如果不为null,则执行以下操作。”
final E x = (E) es[(n = --size)];
这意味着“从size
中减去一,并将新值赋值给n
,将其用作es
的索引,并将该元素强制转换为类型E
,然后将其赋值给类型为E
的最终变量x
。”
final Comparator<? super E> cmp;
问号是一个通配符。<? super E>
表示“某种类型,它是E的祖先”。至于为什么将comparator
分配给局部变量cmp
,我不太确定,但我记得最近在另一个问题中有类似的问题。我会看看是否能找到并编辑那个答案。我希望这至少能在一定程度上帮助你。如果我说的任何内容不清楚,只需提问,我会尝试重新表述解释。
编辑:这是我在前面提到的问题。答案提到了性能的好处,但我再次不确定在这种情况下是否是这个原因,因为具体情况有些不同。
英文:
Let's see if I can help:
if ((result = (E) ((es = queue)[0])) != null)
The above means "assign queue
to es
, access index 0 of it, cast it to type E
, assign it to result
and do the following if it's not null".
final E x = (E) es[(n = --size)];
This means "substract one from size
and assign the new value to n
, use it as an index of es
and cast that element to type E
, then assign it to the final variable x
of type E
.
final Comparator<? super E> cmp;
The question mark is a wildcard. <? super E>
means "some type which is an ancestor of E". As for why comparator
is assigned to the local variable cmp
, I'm not quite sure but I remember something similar being asked recently in another question. I'll see if I can find it and edit this answer. I hope that helps you at least a bit. If any of what I said is not clear just ask and I'll try to reword the explanation.
Edit: This is the question I mentioned in the previous paragraph. The answers suggest performance benefits, but again I'm not sure if that's the reason in this case, as the specifics differ slightly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论