/*		 
 * Copyright (C) 2003-2013 Paolo Boldi and Sebastiano Vigna 
 *
 * 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 PACKAGE;

#if #keyclass(Object)
import java.util.Comparator;
import it.unimi.dsi.fastutil.IndirectPriorityQueue;
#endif

import java.util.NoSuchElementException;

import it.unimi.dsi.fastutil.ints.IntArrays;
import it.unimi.dsi.fastutil.AbstractIndirectPriorityQueue;

/** A type-specific heap-based semi-indirect priority queue. 
 *
 * <P>Instances of this class use as reference list a <em>reference array</em>,
 * which must be provided to each constructor. The priority queue is
 * represented using a heap. The heap is enlarged as needed, but it is never
 * shrunk. Use the {@link #trim()} method to reduce its size, if necessary.
 *
 * <P>This implementation allows one to enqueue several time the same index, but
 * you must be careful when calling {@link #changed()}.
 */

public class HEAP_SEMI_INDIRECT_PRIORITY_QUEUE KEY_GENERIC extends AbstractIndirectPriorityQueue<KEY_GENERIC_CLASS> implements INDIRECT_PRIORITY_QUEUE KEY_GENERIC {

	/** The reference array. */
	protected KEY_GENERIC_TYPE refArray[];

	/** The semi-indirect heap. */
	protected int heap[] = IntArrays.EMPTY_ARRAY;

	/** The number of elements in this queue. */
	protected int size;
	
	/** The type-specific comparator used in this queue. */
	protected KEY_COMPARATOR KEY_SUPER_GENERIC c;

	/** Creates a new empty queue without elements with a given capacity and comparator.
	 *
	 * @param refArray the reference array.
	 * @param capacity the initial capacity of this queue.
	 * @param c the comparator used in this queue, or <code>null</code> for the natural order.
	 */
	public HEAP_SEMI_INDIRECT_PRIORITY_QUEUE( KEY_GENERIC_TYPE[] refArray, int capacity, KEY_COMPARATOR KEY_SUPER_GENERIC c ) {
		if ( capacity > 0 ) this.heap = new int[ capacity ];
		this.refArray = refArray;
		this.c = c;
	}

	/** Creates a new empty queue with given capacity and using the natural order.
	 *
	 * @param refArray the reference array.
	 * @param capacity the initial capacity of this queue.
	 */
	public HEAP_SEMI_INDIRECT_PRIORITY_QUEUE( KEY_GENERIC_TYPE[] refArray, int capacity ) {
		this( refArray, capacity, null );
	}

	/** Creates a new empty queue with capacity equal to the length of the reference array and a given comparator.
	 *
	 * @param refArray the reference array.
	 * @param c the comparator used in this queue, or <code>null</code> for the natural order.
	 */
	public HEAP_SEMI_INDIRECT_PRIORITY_QUEUE( KEY_GENERIC_TYPE[] refArray, KEY_COMPARATOR KEY_SUPER_GENERIC c ) {
		this( refArray, refArray.length, c );
	}

	/** Creates a new empty queue with capacity equal to the length of the reference array and using the natural order. 
	 * @param refArray the reference array.
	 */
	public HEAP_SEMI_INDIRECT_PRIORITY_QUEUE( final KEY_GENERIC_TYPE[] refArray ) {
		this( refArray, refArray.length, null );
	}


	/** Wraps a given array in a queue using a given comparator.
	 *
	 * <P>The queue returned by this method will be backed by the given array.
	 * The first <code>size</code> element of the array will be rearranged so to form a heap (this is
	 * more efficient than enqueing the elements of <code>a</code> one by one).
	 *
	 * @param refArray the reference array.
	 * @param a an array of indices into <code>refArray</code>.
	 * @param size the number of elements to be included in the queue.
	 * @param c the comparator used in this queue, or <code>null</code> for the natural order.
	 */
	public HEAP_SEMI_INDIRECT_PRIORITY_QUEUE( final KEY_GENERIC_TYPE[] refArray, final int[] a, int size, final KEY_COMPARATOR KEY_SUPER_GENERIC c ) {
		this( refArray, 0, c );
		this.heap = a;
		this.size = size;
		SEMI_INDIRECT_HEAPS.makeHeap( refArray, a, size, c );
	}


	/** Wraps a given array in a queue using a given comparator.
	 *
	 * <P>The queue returned by this method will be backed by the given array.
	 * The elements of the array will be rearranged so to form a heap (this is
	 * more efficient than enqueing the elements of <code>a</code> one by one).
	 *
	 * @param refArray the reference array.
	 * @param a an array of indices into <code>refArray</code>.
	 * @param c the comparator used in this queue, or <code>null</code> for the natural order.
	 */
	public HEAP_SEMI_INDIRECT_PRIORITY_QUEUE( final KEY_GENERIC_TYPE[] refArray, final int[] a, final KEY_COMPARATOR KEY_SUPER_GENERIC c ) {
		this( refArray, a, a.length, c );
	}

	/** Wraps a given array in a queue using the natural order.
	 *
	 * <P>The queue returned by this method will be backed by the given array.
	 * The first <code>size</code> element of the array will be rearranged so to form a heap (this is
	 * more efficient than enqueing the elements of <code>a</code> one by one).
	 *
	 * @param refArray the reference array.
	 * @param a an array of indices into <code>refArray</code>.
	 * @param size the number of elements to be included in the queue.
	 */
	public HEAP_SEMI_INDIRECT_PRIORITY_QUEUE( final KEY_GENERIC_TYPE[] refArray, final int[] a, int size ) {
		this( refArray, a, size, null );
	}


	/** Wraps a given array in a queue using the natural order.
	 *
	 * <P>The queue returned by this method will be backed by the given array.
	 * The elements of the array will be rearranged so to form a heap (this is
	 * more efficient than enqueing the elements of <code>a</code> one by one).
	 *
	 * @param refArray the reference array.
	 * @param a an array of indices into <code>refArray</code>.
	 */
	public HEAP_SEMI_INDIRECT_PRIORITY_QUEUE( final KEY_GENERIC_TYPE[] refArray, final int[] a ) {
		this( refArray, a, a.length );
	}

	/** Ensures that the given index is a valid reference.
	 *
	 * @param index an index in the reference array.
	 * @throws IndexOutOfBoundsException if the given index is negative or larger than the reference array length.
	 */
	protected void ensureElement( final int index ) {
		if ( index < 0 )  throw new IndexOutOfBoundsException( "Index (" + index + ") is negative" );
		if ( index >= refArray.length ) throw new IndexOutOfBoundsException( "Index (" + index + ") is larger than or equal to reference array size (" + refArray.length + ")" );
	}
	
	public void enqueue( int x ) {
		ensureElement( x );

		if ( size == heap.length ) heap = IntArrays.grow( heap, size + 1 );

		heap[ size++ ] = x;
		SEMI_INDIRECT_HEAPS.upHeap( refArray, heap, size, size - 1, c );
	}

	public int dequeue() {
		if ( size == 0 ) throw new NoSuchElementException();
		final int result = heap[ 0 ];
		heap[ 0 ] = heap[ --size ];
		if ( size != 0 ) SEMI_INDIRECT_HEAPS.downHeap( refArray, heap, size, 0, c );
		return result;
	}

	public int first() {
		if ( size == 0 ) throw new NoSuchElementException();
		return heap[ 0 ];
	}

	/** {@inheritDoc}
	 *
	 * <P>The caller <strong>must</strong> guarantee that when this method is called the
	 * index of the first element appears just once in the queue. Failure to do so
	 * will bring the queue in an inconsistent state, and will cause
	 * unpredictable behaviour.
	 */

	public void changed() {
		SEMI_INDIRECT_HEAPS.downHeap( refArray, heap, size, 0, c );
	}

	/** Rebuilds this heap in a bottom-up fashion.
	 */

	public void allChanged() {
		SEMI_INDIRECT_HEAPS.makeHeap( refArray, heap, size, c );
	}

	public int size() { return size; }

	public void clear() { size = 0; }

	/** Trims the backing array so that it has exactly {@link #size()} elements.
	 */

	public void trim() {
		heap = IntArrays.trim( heap, size );
	}

	public KEY_COMPARATOR KEY_SUPER_GENERIC comparator() { return c; }

	public int front( final int[] a ) {
		return c == null ? SEMI_INDIRECT_HEAPS.front( refArray, heap, size, a ) : SEMI_INDIRECT_HEAPS.front( refArray, heap, size, a, c );
	}

	public String toString() {
		StringBuffer s = new StringBuffer();
		s.append( "[" );
		for ( int i = 0; i < size; i++ ) {
			if ( i != 0 ) s.append( ", " );
			s.append( refArray[ heap [ i ] ] );
		}
		s.append( "]" );
		return s.toString();
	}

#ifdef TEST

	/** The original class, now just used for testing. */

	private static class TestQueue {

		/** The reference array */
		private KEY_TYPE refArray[];
		/** Its length */
		private int N;
		/** The number of elements in the heaps */
		private int n;
		/** The two comparators */
		private KEY_COMPARATOR primaryComp, secondaryComp;
		/** Two indirect heaps are used, called <code>primary</code> and <code>secondary</code>. Each of them contains
			a permutation of <code>n</code> among the indices 0, 1, ..., <code>N</code>-1 in such a way that the corresponding
			objects be sorted with respect to the two comparators.
			We also need an array <code>inSec[]</code> so that <code>inSec[k]</code> is the index of <code>secondary</code> 
			containing <code>k</code>.
		*/
		private int primary[], secondary[], inSec[];

		/** Builds a double indirect priority queue.
		 *  @param refArray The reference array.
		 *  @param primaryComp The primary comparator.
		 *  @param secondaryComp The secondary comparator.
		 */
		public TestQueue( KEY_TYPE refArray[], KEY_COMPARATOR primaryComp, KEY_COMPARATOR secondaryComp ) {
			this.refArray = refArray;
			this.N = refArray.length;
			assert this.N != 0;
			this.n = 0;
			this.primaryComp = primaryComp;
			this.secondaryComp = secondaryComp;
			this.primary = new int[N];
			this.secondary = new int[N];
			this.inSec = new int[N];
			java.util.Arrays.fill( inSec, -1 );
		}

		/** Adds an index to the queue. Notice that the index should not be already present in the queue.
		 *  @param i The index to be added
		 */
		public void add( int i ) {
			if ( i < 0 || i >= refArray.length ) throw new IndexOutOfBoundsException();
			//if ( inSec[ i ] >= 0 ) throw new IllegalArgumentException();
			primary[n] = i;
			n++;
			swimPrimary( n-1 );
		}

		/** Heapify the primary heap.
		 *  @param i The index of the heap to be heapified.
		 */
		private void heapifyPrimary( int i ) {
			int dep = primary[i];
			int child;

			while ( ( child = 2*i+1 ) < n ) {
				if ( child+1 < n && primaryComp.compare( refArray[primary[child+1]], refArray[primary[child]] ) < 0 ) child++;
				if ( primaryComp.compare( refArray[dep], refArray[primary[child]] ) <= 0 ) break;
				primary[i] = primary[child];
				i = child;
			}
			primary[i] = dep;
		}

		/** Heapify the secondary heap.
		 *  @param i The index of the heap to be heapified.
		 */
		private void heapifySecondary( int i ) {
			int dep = secondary[i];
			int child;

			while ( ( child = 2*i+1 ) < n ) {
				if ( child+1 < n && secondaryComp.compare( refArray[secondary[child+1]], refArray[secondary[child]] ) < 0 ) child++;
				if ( secondaryComp.compare( refArray[dep], refArray[secondary[child]] ) <= 0 ) break;
				secondary[i] = secondary[child]; inSec[secondary[i]] = i;
				i = child;
			}
			secondary[i] = dep; inSec[secondary[i]] = i;
		}

		/** Swim and heapify the primary heap.
		 *  @param i The index to be moved.
		 */
		private void swimPrimary( int i ) {
			int dep = primary[i];
			int parent;

			while ( i != 0 && ( parent = ( i - 1 ) / 2 ) >= 0 ) {
				if ( primaryComp.compare( refArray[primary[parent]], refArray[dep] ) <= 0 ) break;
				primary[i] = primary[parent];
				i = parent;
			}
			primary[i] = dep;
			heapifyPrimary( i );
		}

		/** Swim and heapify the secondary heap.
		 *  @param i The index to be moved.
		 */
		private void swimSecondary( int i ) {
			int dep = secondary[i];
			int parent;

			while ( i != 0 && ( parent = ( i - 1 ) / 2 ) >= 0 ) {
				if ( secondaryComp.compare( refArray[secondary[parent]], refArray[dep] ) <= 0 ) break;
				secondary[i] = secondary[parent]; inSec[secondary[i]] = i;
				i = parent;
			}
			secondary[i] = dep; inSec[secondary[i]] = i;
			heapifySecondary( i );
		}

		/** Returns the minimum element with respect to the primary comparator.
			@return the minimum element.
		*/
		public int top() {
			if ( n == 0 ) throw new NoSuchElementException();
			return primary[0];
		}

		/** Returns the minimum element with respect to the secondary comparator.
			@return the minimum element.
		*/
		public int secTop() {
			if ( n == 0 ) throw new NoSuchElementException();
			return secondary[0];
		}

		/** Removes the minimum element with respect to the primary comparator.
		 *  @return the removed element.
		 */
		public void remove() {
			if ( n == 0 ) throw new NoSuchElementException();
			int result = primary[0];
			// Copy a leaf 
			primary[0] = primary[n-1];
			n--;
			heapifyPrimary( 0 );	
			return;
		}

		public void clear() {
			while( size() != 0 ) remove();
		}

		/** Signals that the minimum element with respect to the comparator has changed.
		 */
		public void change() {
			heapifyPrimary( 0 );
		}

		/** Returns the number of elements in the queue.
		 *  @return the size of the queue
		 */
		public int size() {
			return n;
		}

		public String toString() {
			String s = "[";
			for ( int i = 0; i < n; i++ )
				s += refArray[primary[i]]+", ";
			return s+ "]";
		}
	}

	private static long seed = System.currentTimeMillis(); 
	private static java.util.Random r = new java.util.Random( seed );

	private static KEY_TYPE genKey() {
#if #keyclass(Byte) || #keyclass(Short) || #keyclass(Character)
		return (KEY_TYPE)(r.nextInt());
#elif #keys(primitive)
		return r.NEXT_KEY(); 
#elif #keyclass(Object)
		return Integer.toBinaryString( r.nextInt() );
#else 
		return new java.io.Serializable() {};
#endif
	}

	private static java.text.NumberFormat format = new java.text.DecimalFormat( "#,###.00" );
	private static java.text.FieldPosition p = new java.text.FieldPosition( 0 );

	private static String format( double d ) {
		StringBuffer s = new StringBuffer();
		return format.format( d, s, p ).toString();
	}

	private static void speedTest( int n, boolean comp ) {
		System.out.println( "There are presently no speed tests for this class." );
	}


	private static void fatal( String msg ) {
		System.out.println( msg );
		System.exit( 1 );
	}

	private static void ensure( boolean cond, String msg ) {
		if ( cond ) return;
		fatal( msg );
	}

	private static boolean heapEqual( int[] a, int[] b, int sizea, int sizeb ) {
		if ( sizea != sizeb ) return false;
		while( sizea-- != 0 ) if ( a[sizea] != b[sizea] ) return false;
		return true;
	}

	protected static void test( int n ) {
		long ms;
		Exception mThrowsIllegal, tThrowsIllegal, mThrowsOutOfBounds, tThrowsOutOfBounds, mThrowsNoElement, tThrowsNoElement;
		int rm = 0, rt = 0;
		KEY_TYPE[] refArray = new KEY_TYPE[ n ];

		for( int i = 0; i < n; i++ ) refArray[ i ] = genKey();
		  
		HEAP_SEMI_INDIRECT_PRIORITY_QUEUE m = new HEAP_SEMI_INDIRECT_PRIORITY_QUEUE( refArray,  COMPARATORS.NATURAL_COMPARATOR );
		TestQueue t = new TestQueue( refArray, COMPARATORS.NATURAL_COMPARATOR, COMPARATORS.OPPOSITE_COMPARATOR );

		/* We add pairs to t. */
		for( int i = 0; i < n / 2;  i++ ) {
			t.add( i );
			m.enqueue( i );
		}

		ensure( heapEqual( m.heap, t.primary, m.size(), t.size() ), "Error (" + seed + "): m and t differ after creation (" + m + ", " + t + ")" );

		/* Now we add and remove random data in m and t, checking that the result is the same. */

		for(int i=0; i<2*n;  i++ ) {
			if ( r.nextDouble() < 0.01 ) {
				t.clear();
				m.clear();
				for( int j = 0; j < n / 2;  j++ ) {
					t.add( j );
					m.enqueue( j );
				}
			}

			int T = r.nextInt( 2 * n );

			mThrowsNoElement = tThrowsNoElement = mThrowsOutOfBounds = tThrowsOutOfBounds = mThrowsIllegal = tThrowsIllegal = null;

			try {
				m.enqueue( T );
			}
			catch ( IndexOutOfBoundsException e ) { mThrowsOutOfBounds = e; }
			catch ( IllegalArgumentException e ) { mThrowsIllegal = e; }

			try {
				t.add( T );
			}
			catch ( IndexOutOfBoundsException e ) { tThrowsOutOfBounds = e; }
			catch ( IllegalArgumentException e ) { tThrowsIllegal = e; }

			ensure( ( mThrowsOutOfBounds == null ) == ( tThrowsOutOfBounds == null ), "Error (" + seed + "): enqueue() divergence in IndexOutOfBoundsException for " + T + " (" + mThrowsOutOfBounds + ", " + tThrowsOutOfBounds + ")" );
			ensure( ( mThrowsIllegal == null ) == ( tThrowsIllegal == null ), "Error (" + seed + "): enqueue() divergence in IllegalArgumentException for " + T + " (" + mThrowsIllegal + ", " + tThrowsIllegal + ")" );

			ensure( heapEqual( m.heap, t.primary, m.size(), t.size() ), "Error (" + seed + "): m and t differ after enqueue (" + m + ", " + t + ")" );
			
			if ( m.size() != 0 ) {
				ensure( m.first() == t.top(), "Error (" + seed + "): m and t differ in first element after enqueue (" + m.first() + ", " + t.top() + ")");
			}

			mThrowsNoElement = tThrowsNoElement = mThrowsOutOfBounds = tThrowsOutOfBounds = mThrowsIllegal = tThrowsIllegal = null;

			try {
				rm = m.dequeue();
			}
			catch ( IndexOutOfBoundsException e ) { mThrowsOutOfBounds = e; }
			catch ( IllegalArgumentException e ) { mThrowsIllegal = e; }
			catch ( NoSuchElementException e ) { mThrowsNoElement = e; }

			try {
				rt = t.top();
				t.remove();
			}
			catch ( IndexOutOfBoundsException e ) { tThrowsOutOfBounds = e; }
			catch ( IllegalArgumentException e ) { tThrowsIllegal = e; }
			catch ( NoSuchElementException e ) { tThrowsNoElement = e; }

			ensure( ( mThrowsOutOfBounds == null ) == ( tThrowsOutOfBounds == null ), "Error (" + seed + "): dequeue() divergence in IndexOutOfBoundsException (" + mThrowsOutOfBounds + ", " + tThrowsOutOfBounds + ")" );
			ensure( ( mThrowsIllegal == null ) == ( tThrowsIllegal == null ), "Error (" + seed + "): dequeue() divergence in IllegalArgumentException  (" + mThrowsIllegal + ", " + tThrowsIllegal + ")" );
			ensure( ( mThrowsNoElement == null ) == ( tThrowsNoElement == null ), "Error (" + seed + "): dequeue() divergence in NoSuchElementException  (" + mThrowsNoElement + ", " + tThrowsNoElement + ")" );
			if ( mThrowsOutOfBounds == null ) ensure( rt == rm , "Error (" + seed + "): divergence in dequeue() between t and m (" + rt + ", " + rm + ")" );


			ensure( heapEqual( m.heap, t.primary, m.size(), t.size() ), "Error (" + seed + "): m and t differ after dequeue (" + m + ", " + t + ")");
			
			if ( m.size() != 0 ) {
				ensure( m.first() == t.top(), "Error (" + seed + "): m and t differ in first element after dequeue (" + m.first() + ", " + t.top() + ")");
			}

			if ( m.size() != 0 && ( ( new it.unimi.dsi.fastutil.ints.IntOpenHashSet( m.heap, 0, m.size ) ).size() == m.size() ) ) {

				refArray[ m.first() ] = genKey();

				m.changed();
				t.change();
				
				ensure( heapEqual( m.heap, t.primary, m.size(), t.size() ), "Error (" + seed + "): m and t differ after change (" + m + ", " + t + ")");
				
				if ( m.size() != 0 ) {
					ensure( m.first() == t.top(), "Error (" + seed + "): m and t differ in first element after change (" + m.first() + ", " + t.top() + ")");
				}
			}
		}


		/* Now we check that m actually holds the same data. */
		  
		m.clear();
		ensure( m.isEmpty(), "Error (" + seed + "): m is not empty after clear()" );

		System.out.println("Test OK");
	}



	public static void main( String args[] ) {
		int n  = Integer.parseInt(args[1]);
		if ( args.length > 2 ) r = new java.util.Random( seed = Long.parseLong( args[ 2 ] ) );
		  

		try {
			if ("speedTest".equals(args[0]) || "speedComp".equals(args[0])) speedTest( n, "speedComp".equals(args[0]) );
			else if ( "test".equals( args[0] ) ) test(n);
		} catch( Throwable e ) {
			e.printStackTrace( System.err );
			System.err.println( "seed: " + seed );
		}
	}

#endif
}
