Skip to content

Commit

Permalink
fix minor case of a bad default
Browse files Browse the repository at this point in the history
RELNOTES=n/a
PiperOrigin-RevId: 504929321
  • Loading branch information
kevinb authored and Google Java Core Libraries committed Jan 26, 2023
1 parent ef779cb commit 76cd7fa
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -644,17 +644,18 @@ int crossOverUp(int index, E x) {
int parentIndex = getParentIndex(index);
E parentElement = elementData(parentIndex);
if (parentIndex != 0) {
// This is a guard for the case of the childless uncle.
// Since the end of the array is actually the middle of the heap,
// a smaller childless uncle can become a child of x when we
// bubble up alternate levels, violating the invariant.
/*
* This is a guard for the case of the childless aunt node. Since the end of the array is
* actually the middle of the heap, a smaller childless aunt node can become a child of x
* when we bubble up alternate levels, violating the invariant.
*/
int grandparentIndex = getParentIndex(parentIndex);
int uncleIndex = getRightChildIndex(grandparentIndex);
if (uncleIndex != parentIndex && getLeftChildIndex(uncleIndex) >= size) {
E uncleElement = elementData(uncleIndex);
if (ordering.compare(uncleElement, parentElement) < 0) {
parentIndex = uncleIndex;
parentElement = uncleElement;
int auntIndex = getRightChildIndex(grandparentIndex);
if (auntIndex != parentIndex && getLeftChildIndex(auntIndex) >= size) {
E auntElement = elementData(auntIndex);
if (ordering.compare(auntElement, parentElement) < 0) {
parentIndex = auntIndex;
parentElement = auntElement;
}
}
}
Expand All @@ -667,26 +668,30 @@ int crossOverUp(int index, E x) {
return index;
}

// About the term "aunt node": it's better to leave gender out of it, but for this the English
// language has nothing for us. Except for the whimsical neologism "pibling" (!) which we
// obviously could not expect to increase anyone's understanding of the code.

/**
* Swap {@code actualLastElement} with the conceptually correct last element of the heap.
* Returns the index that {@code actualLastElement} now resides in.
*
* <p>Since the last element of the array is actually in the middle of the sorted structure, a
* childless uncle node could be smaller, which would corrupt the invariant if this element
* becomes the new parent of the uncle. In that case, we first switch the last element with its
* uncle, before returning.
* childless aunt node could be smaller, which would corrupt the invariant if this element
* becomes the new parent of the aunt node. In that case, we first switch the last element with
* its aunt node, before returning.
*/
int swapWithConceptuallyLastElement(E actualLastElement) {
int parentIndex = getParentIndex(size);
if (parentIndex != 0) {
int grandparentIndex = getParentIndex(parentIndex);
int uncleIndex = getRightChildIndex(grandparentIndex);
if (uncleIndex != parentIndex && getLeftChildIndex(uncleIndex) >= size) {
E uncleElement = elementData(uncleIndex);
if (ordering.compare(uncleElement, actualLastElement) < 0) {
queue[uncleIndex] = actualLastElement;
queue[size] = uncleElement;
return uncleIndex;
int auntIndex = getRightChildIndex(grandparentIndex);
if (auntIndex != parentIndex && getLeftChildIndex(auntIndex) >= size) {
E auntElement = elementData(auntIndex);
if (ordering.compare(auntElement, actualLastElement) < 0) {
queue[auntIndex] = actualLastElement;
queue[size] = auntElement;
return auntIndex;
}
}
}
Expand Down
45 changes: 25 additions & 20 deletions guava/src/com/google/common/collect/MinMaxPriorityQueue.java
Original file line number Diff line number Diff line change
Expand Up @@ -644,17 +644,18 @@ int crossOverUp(int index, E x) {
int parentIndex = getParentIndex(index);
E parentElement = elementData(parentIndex);
if (parentIndex != 0) {
// This is a guard for the case of the childless uncle.
// Since the end of the array is actually the middle of the heap,
// a smaller childless uncle can become a child of x when we
// bubble up alternate levels, violating the invariant.
/*
* This is a guard for the case of the childless aunt node. Since the end of the array is
* actually the middle of the heap, a smaller childless aunt node can become a child of x
* when we bubble up alternate levels, violating the invariant.
*/
int grandparentIndex = getParentIndex(parentIndex);
int uncleIndex = getRightChildIndex(grandparentIndex);
if (uncleIndex != parentIndex && getLeftChildIndex(uncleIndex) >= size) {
E uncleElement = elementData(uncleIndex);
if (ordering.compare(uncleElement, parentElement) < 0) {
parentIndex = uncleIndex;
parentElement = uncleElement;
int auntIndex = getRightChildIndex(grandparentIndex);
if (auntIndex != parentIndex && getLeftChildIndex(auntIndex) >= size) {
E auntElement = elementData(auntIndex);
if (ordering.compare(auntElement, parentElement) < 0) {
parentIndex = auntIndex;
parentElement = auntElement;
}
}
}
Expand All @@ -667,26 +668,30 @@ int crossOverUp(int index, E x) {
return index;
}

// About the term "aunt node": it's better to leave gender out of it, but for this the English
// language has nothing for us. Except for the whimsical neologism "pibling" (!) which we
// obviously could not expect to increase anyone's understanding of the code.

/**
* Swap {@code actualLastElement} with the conceptually correct last element of the heap.
* Returns the index that {@code actualLastElement} now resides in.
*
* <p>Since the last element of the array is actually in the middle of the sorted structure, a
* childless uncle node could be smaller, which would corrupt the invariant if this element
* becomes the new parent of the uncle. In that case, we first switch the last element with its
* uncle, before returning.
* childless aunt node could be smaller, which would corrupt the invariant if this element
* becomes the new parent of the aunt node. In that case, we first switch the last element with
* its aunt node, before returning.
*/
int swapWithConceptuallyLastElement(E actualLastElement) {
int parentIndex = getParentIndex(size);
if (parentIndex != 0) {
int grandparentIndex = getParentIndex(parentIndex);
int uncleIndex = getRightChildIndex(grandparentIndex);
if (uncleIndex != parentIndex && getLeftChildIndex(uncleIndex) >= size) {
E uncleElement = elementData(uncleIndex);
if (ordering.compare(uncleElement, actualLastElement) < 0) {
queue[uncleIndex] = actualLastElement;
queue[size] = uncleElement;
return uncleIndex;
int auntIndex = getRightChildIndex(grandparentIndex);
if (auntIndex != parentIndex && getLeftChildIndex(auntIndex) >= size) {
E auntElement = elementData(auntIndex);
if (ordering.compare(auntElement, actualLastElement) < 0) {
queue[auntIndex] = actualLastElement;
queue[size] = auntElement;
return auntIndex;
}
}
}
Expand Down

0 comments on commit 76cd7fa

Please sign in to comment.