1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386
|
/*
* Copyright (C) 2003-2014 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.AbstractPriorityQueue;
#endif
import java.util.NoSuchElementException;
/** A type-specific heap-based priority queue.
*
* <P>Instances of this class represent a priority queue 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.
*/
public class HEAP_PRIORITY_QUEUE KEY_GENERIC extends ABSTRACT_PRIORITY_QUEUE KEY_GENERIC {
/** The heap array. */
@SuppressWarnings("unchecked")
protected KEY_GENERIC_TYPE[] heap = KEY_GENERIC_ARRAY_CAST ARRAYS.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 with a given capacity and comparator.
*
* @param capacity the initial capacity of this queue.
* @param c the comparator used in this queue, or <code>null</code> for the natural order.
*/
@SuppressWarnings("unchecked")
public HEAP_PRIORITY_QUEUE( int capacity, KEY_COMPARATOR KEY_SUPER_GENERIC c ) {
if ( capacity > 0 ) this.heap = KEY_GENERIC_ARRAY_CAST new KEY_TYPE[ capacity ];
this.c = c;
}
/** Creates a new empty queue with a given capacity and using the natural order.
*
* @param capacity the initial capacity of this queue.
*/
public HEAP_PRIORITY_QUEUE( int capacity ) {
this( capacity, null );
}
/** Creates a new empty queue with a given comparator.
*
* @param c the comparator used in this queue, or <code>null</code> for the natural order.
*/
public HEAP_PRIORITY_QUEUE( KEY_COMPARATOR KEY_SUPER_GENERIC c ) {
this( 0, c );
}
/** Creates a new empty queue using the natural order.
*/
public HEAP_PRIORITY_QUEUE() {
this( 0, 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 a an array.
* @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_PRIORITY_QUEUE( final KEY_GENERIC_TYPE[] a, int size, final KEY_COMPARATOR KEY_SUPER_GENERIC c ) {
this( c );
this.heap = a;
this.size = size;
HEAPS.makeHeap( 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 a an array.
* @param c the comparator used in this queue, or <code>null</code> for the natural order.
*/
public HEAP_PRIORITY_QUEUE( final KEY_GENERIC_TYPE[] a, final KEY_COMPARATOR KEY_SUPER_GENERIC c ) {
this( 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 a an array.
* @param size the number of elements to be included in the queue.
*/
public HEAP_PRIORITY_QUEUE( final KEY_GENERIC_TYPE[] a, int size ) {
this( 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 a an array.
*/
public HEAP_PRIORITY_QUEUE( final KEY_GENERIC_TYPE[] a ) {
this( a, a.length );
}
@SuppressWarnings("unchecked")
public void enqueue( KEY_GENERIC_TYPE x ) {
if ( size == heap.length ) heap = ARRAYS.grow( heap, size + 1 );
heap[ size++ ] = x;
HEAPS.upHeap( heap, size, size - 1, c );
}
public KEY_GENERIC_TYPE DEQUEUE() {
if ( size == 0 ) throw new NoSuchElementException();
final KEY_GENERIC_TYPE result = heap[ 0 ];
heap[ 0 ] = heap[ --size ];
#if #keyclass(Object)
heap[ size ] = null;
#endif
if ( size != 0 ) HEAPS.downHeap( heap, size, 0, c );
return result;
}
public KEY_GENERIC_TYPE FIRST() {
if ( size == 0 ) throw new NoSuchElementException();
return heap[ 0 ];
}
public void changed() {
HEAPS.downHeap( heap, size, 0, c );
}
public int size() { return size; }
public void clear() {
#if #keyclass(Object)
ObjectArrays.fill( heap, 0, size, null );
#endif
size = 0;
}
/** Trims the underlying heap array so that it has exactly {@link #size()} elements.
*/
public void trim() {
heap = ARRAYS.trim( heap, size );
}
public KEY_COMPARATOR KEY_SUPER_GENERIC comparator() { return c; }
#ifdef TEST
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( KEY_TYPE[] a, KEY_TYPE[] b, int sizea, int sizeb ) {
if ( sizea != sizeb ) return false;
KEY_TYPE[] aa = (KEY_TYPE[])a.clone();
KEY_TYPE[] bb = (KEY_TYPE[])b.clone();
java.util.Arrays.sort( aa, 0, sizea );
java.util.Arrays.sort( bb, 0, sizeb );
while( sizea-- != 0 ) if ( ! KEY_EQUALS( aa[sizea], bb[sizea] ) ) return false;
return true;
}
private static KEY_TYPE k[];
protected static void test( int n ) {
long ms;
Exception mThrowsIllegal, tThrowsIllegal, mThrowsOutOfBounds, tThrowsOutOfBounds, mThrowsNoElement, tThrowsNoElement;
KEY_TYPE rm = KEY_NULL, rt = KEY_NULL;
k = new KEY_TYPE[ n ];
for( int i = 0; i < n; i++ ) k[i] = genKey();
HEAP_PRIORITY_QUEUE m = new HEAP_PRIORITY_QUEUE( COMPARATORS.NATURAL_COMPARATOR );
ARRAY_PRIORITY_QUEUE t = new ARRAY_PRIORITY_QUEUE( COMPARATORS.NATURAL_COMPARATOR );
/* We add pairs to t. */
for( int i = 0; i < n / 2; i++ ) {
t.enqueue( k[ i ] );
m.enqueue( k[ i ] );
}
ensure( heapEqual( m.heap, t.array, m.size(), t.size() ), "Error (" + seed + "): m and t differ after creation (" + m + ", " + t + ")" );
if ( m.size() != 0 ) {
ensure( KEY_EQUALS(m.FIRST(), t.FIRST()), "Error (" + seed + "): m and t differ in first element after creation (" + m.FIRST() + ", " + t.FIRST() + ")");
}
/* 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.enqueue( k[ j ] );
m.enqueue( k[ j ] );
}
}
KEY_TYPE T = genKey();
mThrowsNoElement = tThrowsNoElement = mThrowsOutOfBounds = tThrowsOutOfBounds = mThrowsIllegal = tThrowsIllegal = null;
try {
m.enqueue( T );
}
catch ( IndexOutOfBoundsException e ) { mThrowsOutOfBounds = e; }
catch ( IllegalArgumentException e ) { mThrowsIllegal = e; }
try {
t.enqueue( 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.array, m.size(), t.size() ), "Error (" + seed + "): m and t differ after enqueue (" + m + ", " + t + ")" );
if ( m.size() != 0 ) {
ensure( KEY_EQUALS(m.FIRST(), t.FIRST()), "Error (" + seed + "): m and t differ in first element after enqueue (" + m.FIRST() + ", " + t.FIRST() + ")");
}
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.DEQUEUE();
}
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.array, m.size(), t.size() ), "Error (" + seed + "): m and t differ after dequeue (" + m + ", " + t + ")");
if ( m.size() != 0 ) {
ensure( KEY_EQUALS(m.FIRST(), t.FIRST()), "Error (" + seed + "): m and t differ in first element after dequeue (" + m.FIRST() + ", " + t.FIRST() + ")");
}
HEAP_PRIORITY_QUEUE m2 = new HEAP_PRIORITY_QUEUE( t.array, t.size() );
ARRAY_PRIORITY_QUEUE t2 = new ARRAY_PRIORITY_QUEUE( m.heap, m.size() );
m = m2;
t = t2;
ensure( heapEqual( m.heap, t.array, m.size(), t.size() ), "Error (" + seed + "): m and t differ after wrap (" + m + ", " + t + ")");
if ( m.size() != 0 ) {
ensure( KEY_EQUALS(m.FIRST(), t.FIRST()), "Error (" + seed + "): m and t differ in first element after wrap (" + m.FIRST() + ", " + t.FIRST() + ")");
}
if ( m.size() != 0 && ( ( new OPEN_HASH_SET( m.heap, 0, m.size ) ).size() == m.size() ) ) {
int j = t.size(), M = --j;
#if #keys(primitive)
while( j-- != 0 ) if ( KEY_LESS( t.array[ j ], t.array[ M ] ) ) M = j;
#else
while( j-- != 0 ) if ( ((Comparable)t.array[ j ]).compareTo( t.array[ M ] )< 0 ) M = j;
#endif
m.heap[ 0 ] = t.array[ M ] = genKey();
m.changed();
t.changed();
ensure( heapEqual( m.heap, t.array, m.size(), t.size() ), "Error (" + seed + "): m and t differ after change (" + m + ", " + t + ")");
if ( m.size() != 0 ) {
ensure( KEY_EQUALS(m.FIRST(), t.FIRST()), "Error (" + seed + "): m and t differ in first element after change (" + m.FIRST() + ", " + t.FIRST() + ")");
}
}
}
/* 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
}
|