private void popHeap()

in java/src/main/java/com/epam/deltix/containers/HeapWithDictionary.java [234:271]


    private void popHeap() {
        int last = head;
        head = elementsKey[0];
        keys.remove((TKey)elementsUserKey[0]);
        keysPosition[head] = -last - 1;
        elementsKey[0] = elementsKey[count - 1];
        elementsValue[0] = elementsValue[count - 1];
        TValue tempValue = (TValue) elementsValue[count - 1];
        TKey tempUserKey = (TKey) elementsUserKey[count - 1];
        int tempKey = elementsKey[count - 1];
        count--;
        if (count == 0) return;
        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) elementsValue[left], tempValue) < 0) swapIndex = left;
            } else {
                if (comparator.compare((TValue) elementsValue[left], (TValue) elementsValue[right]) < 0) {
                    if (comparator.compare((TValue) elementsValue[left], tempValue) < 0) swapIndex = left;
                } else if (comparator.compare((TValue) elementsValue[right], tempValue) < 0) swapIndex = right;
            }
            if (swapIndex == current) break;
            elementsValue[current] = elementsValue[swapIndex];
            elementsKey[current] = elementsKey[swapIndex];
            elementsUserKey[current] = elementsUserKey[swapIndex];
            keysPosition[elementsKey[current]] = current;
            current = swapIndex;
        }
        elementsValue[current] = tempValue;
        elementsKey[current] = tempKey;
        elementsUserKey[current] = tempUserKey;
        keysPosition[elementsKey[current]] = current;
    }