public TValue modifyTop()

in java/src/main/java/com/epam/deltix/containers/Heap.java [204:232]


    public TValue modifyTop(TValue value) throws IndexOutOfBoundsException {
        if (count == 0) throw new IndexOutOfBoundsException("Heap is empty");
        if (isHeap) {
            TValue returnValue = (TValue) elements[0];
            int current = 0;
            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) elements[left], value) < 0) swapIndex = left;
                } else {
                    if (comparator.compare((TValue) elements[left], (TValue) elements[right]) < 0) {
                        if (comparator.compare((TValue) elements[left], value) < 0) swapIndex = left;
                    } else if (comparator.compare((TValue) elements[right], value) < 0) swapIndex = right;
                }
                if (swapIndex == current) break;
                elements[current] = elements[swapIndex];
                current = swapIndex;
            }
            elements[current] = value;
            return returnValue;
        } else {
            TValue returnValue = pop();
            addList(value);
            return returnValue;
        }
    }