in java/src/main/java/com/epam/deltix/containers/HeapWithIndices.java [287:330]
private boolean modifyHeap(int key, TValue newValue) {
if (key < 0 || key >= keysPosition.length || keysPosition[key] < 0) return false;
int index = keysPosition[key];
int current = index;
while (current < count) {
int left = (current << 1) + 1;
int right = (current << 1) + 2;
int swapIndex = current;
if (left >= count) swapIndex = current;
else if (right >= count) {
if (comparator.compare((TValue) elementsValue[left], newValue) < 0) swapIndex = left;
} else {
if (comparator.compare((TValue) elementsValue[left], (TValue) elementsValue[right]) < 0) {
if (comparator.compare((TValue) elementsValue[left], newValue) < 0) swapIndex = left;
} else if (comparator.compare((TValue) elementsValue[right], newValue) < 0) swapIndex = right;
}
if (swapIndex == current) break;
elementsValue[current] = elementsValue[swapIndex];
elementsKey[current] = elementsKey[swapIndex];
keysPosition[elementsKey[current]] = current;
current = swapIndex;
}
elementsValue[current] = newValue;
elementsKey[current] = key;
keysPosition[elementsKey[current]] = current;
current = index;
TValue tempValue = (TValue) elementsValue[current];
int tempKey = elementsKey[current];
while (current != 0) {
int swapIndex = (current - 1) >> 1;
if (comparator.compare(tempValue, (TValue) elementsValue[swapIndex]) < 0) {
elementsValue[current] = elementsValue[swapIndex];
elementsKey[current] = elementsKey[swapIndex];
keysPosition[elementsKey[current]] = current;
current = swapIndex;
} else break;
}
if (current != index) {
elementsValue[current] = tempValue;
elementsKey[current] = tempKey;
keysPosition[elementsKey[current]] = current;
}
return true;
}