/*
 * (C) Copyright 2014-2016, by Dimitrios Michail
 *
 * JHeaps Library
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.jheaps.array;

import java.util.Comparator;
import java.util.NoSuchElementException;

import org.jheaps.annotations.ConstantTime;
import org.jheaps.annotations.LogarithmicTime;

/**
 * Abstract implementation of a heap using an array representation.
 * 
 * @author Dimitrios Michail
 *
 * @param <K>
 *            the type of keys maintained by this heap
 */
abstract class AbstractArrayHeap<K> extends AbstractArrayWeakHeap<K> {

    private static final long serialVersionUID = 1L;

    public AbstractArrayHeap(Comparator<? super K> comparator, int capacity) {
        super(comparator, capacity);
    }

    /**
     * Initialize the array representation
     */
    @Override
    @SuppressWarnings("unchecked")
    protected void initCapacity(int capacity) {
        this.array = (K[]) new Object[capacity + 1];
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @ConstantTime
    public K findMin() {
        if (size == 0) {
            throw new NoSuchElementException();
        }
        return array[1];
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @LogarithmicTime(amortized = true)
    public void insert(K key) {
        if (key == null) {
            throw new NullPointerException("Null keys not permitted");
        }

        // make sure there is space
        if (size == array.length - 1) {
            if (array.length == 1) {
                ensureCapacity(1);
            } else {
                ensureCapacity(2 * (array.length - 1));
            }
        }

        array[++size] = key;

        if (comparator == null) {
            fixup(size);
        } else {
            fixupWithComparator(size);
        }
    }

    /**
     * {@inheritDoc}
     */
    @Override
    @LogarithmicTime(amortized = true)
    public K deleteMin() {
        if (size == 0) {
            throw new NoSuchElementException();
        }

        K result = array[1];
        if (size == 1) {
            array[1] = null;
            size = 0;
        } else {
            array[1] = array[size--];
            if (comparator == null) {
                fixdown(1);
            } else {
                fixdownWithComparator(1);
            }
        }

        if (2 < array.length - 1 && 4 * size < array.length - 1) {
            ensureCapacity((array.length - 1) / 2 + 1);
        }
        return result;
    }

}

@@ -2673,15 +2673,65 @@
 ray%5B
+size%5D;%0A            array%5Bsize%5D = null;%0A            
 size--
-%5D
 ;%0A  

Set last element to null when deleting to let it be garbage collected