4

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?

Garry Pettet
  • 8,096
  • 22
  • 65
  • 103
  • 2
    Just FYI, the code you are porting is licensed under GPLv2. This has implications for software that is derived from it, including ports. See https://softwareengineering.stackexchange.com/questions/151515/rewrote-gnu-gpl-v2-code-in-another-language-can-i-change-a-license – dnault Sep 15 '20 at 21:24

3 Answers3

3

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.

markspace
  • 10,621
  • 3
  • 25
  • 39
2

1- if ((result = (E) ((es = queue)[0])) != null)
First it assigns the array queue to the variable esand 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)];
First java evaluates --size then assigns to int type n then gets nth from es array, casts it to E, and then assigns it to variable x.


I think the next two lines you asked are clear now!
Omid.N
  • 824
  • 9
  • 19
2

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.

JustAnotherDeveloper
  • 2,061
  • 2
  • 10
  • 24