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
|
/*
* 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;
#endif
import it.unimi.dsi.fastutil.ints.IntArrays;
/** A class providing static methods and objects that do useful things with semi-indirect heaps.
*
* <P>A semi-indirect heap is based on a <em>reference array</em>. Elements of
* a semi-indirect heap are integers that index the reference array (note that
* in an <em>indirect</em> heap you can also map elements of the reference
* array to heap positions).
*/
public class SEMI_INDIRECT_HEAPS {
private SEMI_INDIRECT_HEAPS() {}
/** Moves the given element down into the semi-indirect heap until it reaches the lowest possible position.
*
* @param refArray the reference array.
* @param heap the semi-indirect heap (starting at 0).
* @param size the number of elements in the heap.
* @param i the index in the heap of the element to be moved down.
* @param c a type-specific comparator, or <code>null</code> for the natural order.
* @return the new position in the heap of the element of heap index <code>i</code>.
*/
@SuppressWarnings("unchecked")
public static KEY_GENERIC int downHeap( final KEY_GENERIC_TYPE[] refArray, final int[] heap, final int size, int i, final KEY_COMPARATOR KEY_GENERIC c ) {
if ( i >= size ) throw new IllegalArgumentException( "Heap position (" + i + ") is larger than or equal to heap size (" + size + ")" );
final int e = heap[ i ];
final KEY_GENERIC_TYPE E = refArray[ e ];
int child;
if ( c == null )
while ( ( child = 2 * i + 1 ) < size ) {
if ( child + 1 < size && KEY_LESS( refArray[ heap[ child + 1 ] ], refArray[ heap[ child ] ] ) ) child++;
if ( KEY_LESSEQ( E, refArray[ heap[ child ] ] ) ) break;
heap[ i ] = heap[ child ];
i = child;
}
else
while ( ( child = 2 * i + 1 ) < size ) {
if ( child + 1 < size && c.compare( refArray[ heap[ child + 1 ] ], refArray[ heap[ child ] ] ) < 0 ) child++;
if ( c.compare( E, refArray[ heap[ child ] ] ) <= 0 ) break;
heap[ i ] = heap[ child ];
i = child;
}
heap[ i ] = e;
return i;
}
/** Moves the given element up in the semi-indirect heap until it reaches the highest possible position.
*
* @param refArray the reference array.
* @param heap the semi-indirect heap (starting at 0).
* @param size the number of elements in the heap.
* @param i the index in the heap of the element to be moved up.
* @param c a type-specific comparator, or <code>null</code> for the natural order.
* @return the new position in the heap of the element of heap index <code>i</code>.
*/
@SuppressWarnings("unchecked")
public static KEY_GENERIC int upHeap( final KEY_GENERIC_TYPE[] refArray, final int[] heap, final int size, int i, final KEY_COMPARATOR KEY_GENERIC c ) {
if ( i >= size ) throw new IllegalArgumentException( "Heap position (" + i + ") is larger than or equal to heap size (" + size + ")" );
final int e = heap[ i ];
int parent;
final KEY_GENERIC_TYPE E = refArray[ e ];
if ( c == null )
while ( i != 0 && ( parent = ( i - 1 ) / 2 ) >= 0 ) {
if ( KEY_LESSEQ( refArray[ heap[ parent ] ], E ) ) break;
heap[ i ] = heap[ parent ];
i = parent;
}
else
while ( i != 0 && ( parent = ( i - 1 ) / 2 ) >= 0 ) {
if ( c.compare( refArray[ heap[ parent ] ], E ) <= 0 ) break;
heap[ i ] = heap[ parent ];
i = parent;
}
heap[ i ] = e;
return i;
}
/** Creates a semi-indirect heap in the given array.
*
* @param refArray the reference array.
* @param offset the first element of the reference array to be put in the heap.
* @param length the number of elements to be put in the heap.
* @param heap the array where the heap is to be created.
* @param c a type-specific comparator, or <code>null</code> for the natural order.
*/
public static KEY_GENERIC void makeHeap( final KEY_GENERIC_TYPE[] refArray, final int offset, final int length, final int[] heap, final KEY_COMPARATOR KEY_GENERIC c ) {
ARRAYS.ensureOffsetLength( refArray, offset, length );
if ( heap.length < length ) throw new IllegalArgumentException( "The heap length (" + heap.length + ") is smaller than the number of elements (" + length + ")" );
int i = length;
while( i-- != 0 ) heap[ i ] = offset + i;
i = length / 2;
while( i-- != 0 ) downHeap( refArray, heap, length, i, c );
}
/** Creates a semi-indirect heap, allocating its heap array.
*
* @param refArray the reference array.
* @param offset the first element of the reference array to be put in the heap.
* @param length the number of elements to be put in the heap.
* @param c a type-specific comparator, or <code>null</code> for the natural order.
* @return the heap array.
*/
public static KEY_GENERIC int[] makeHeap( final KEY_GENERIC_TYPE[] refArray, final int offset, final int length, final KEY_COMPARATOR KEY_GENERIC c ) {
int[] heap = length <= 0 ? IntArrays.EMPTY_ARRAY : new int[ length ];
makeHeap( refArray, offset, length, heap, c );
return heap;
}
/** Creates a semi-indirect heap from a given index array.
*
* @param refArray the reference array.
* @param heap an array containing indices into <code>refArray</code>.
* @param size the number of elements in the heap.
* @param c a type-specific comparator, or <code>null</code> for the natural order.
*/
public static KEY_GENERIC void makeHeap( final KEY_GENERIC_TYPE[] refArray, final int[] heap, final int size, final KEY_COMPARATOR KEY_GENERIC c ) {
int i = size / 2;
while( i-- != 0 ) downHeap( refArray, heap, size, i, c );
}
/** Retrieves the front of a heap in a given array.
*
* <p>The <em>front</em> of a semi-indirect heap is the set of indices whose associated elements in the reference array
* are equal to the element associated to the first index.
*
* <p>In several circumstances you need to know the front, and scanning linearly the entire heap is not
* the best strategy. This method simulates (using a partial linear scan) a breadth-first visit that
* terminates when all visited nodes are larger than the element associated
* to the top index, which implies that no elements of the front can be found later.
* In most cases this trick yields a significant improvement.
*
* @param refArray the reference array.
* @param heap an array containing indices into <code>refArray</code>.
* @param size the number of elements in the heap.
* @param a an array large enough to hold the front (e.g., at least long as <code>refArray</code>).
* @return the number of elements actually written (starting from the first position of <code>a</code>).
*/
@SuppressWarnings("unchecked")
public static KEY_GENERIC int front( final KEY_GENERIC_TYPE[] refArray, final int[] heap, final int size, final int[] a ) {
final KEY_GENERIC_TYPE top = refArray[ heap[ 0 ] ];
int j = 0, // The current position in a
l = 0, // The first position to visit in the next level (inclusive)
r = 1, // The last position to visit in the next level (exclusive)
f = 0; // The first position (in the heap array) of the next level
for( int i = 0; i < r; i++ ) {
if ( i == f ) { // New level
if ( l >= r ) break; // If we are crossing the two bounds, we're over
f = (f << 1) + 1; // Update the first position of the next level...
i = l; // ...and jump directly to position l
l = -1; // Invalidate l
}
if ( KEY_CMP_EQ( top, refArray[ heap[ i ] ] ) ) {
a[ j++ ] = heap[ i ];
if ( l == -1 ) l = i * 2 + 1; // If this is the first time in this level, set l
r = Math.min( size, i * 2 + 3 ); // Update r, but do not go beyond size
}
}
return j;
}
/** Retrieves the front of a heap in a given array using a given comparator.
*
* <p>The <em>front</em> of a semi-indirect heap is the set of indices whose associated elements in the reference array
* are equal to the element associated to the first index.
*
* <p>In several circumstances you need to know the front, and scanning linearly the entire heap is not
* the best strategy. This method simulates (using a partial linear scan) a breadth-first visit that
* terminates when all visited nodes are larger than the element associated
* to the top index, which implies that no elements of the front can be found later.
* In most cases this trick yields a significant improvement.
*
* @param refArray the reference array.
* @param heap an array containing indices into <code>refArray</code>.
* @param size the number of elements in the heap.
* @param a an array large enough to hold the front (e.g., at least long as <code>refArray</code>).
* @param c a type-specific comparator.
* @return the number of elements actually written (starting from the first position of <code>a</code>).
*/
public static KEY_GENERIC int front( final KEY_GENERIC_TYPE[] refArray, final int[] heap, final int size, final int[] a, final KEY_COMPARATOR KEY_GENERIC c ) {
final KEY_GENERIC_TYPE top = refArray[ heap[ 0 ] ];
int j = 0, // The current position in a
l = 0, // The first position to visit in the next level (inclusive)
r = 1, // The last position to visit in the next level (exclusive)
f = 0; // The first position (in the heap array) of the next level
for( int i = 0; i < r; i++ ) {
if ( i == f ) { // New level
if ( l >= r ) break; // If we are crossing the two bounds, we're over
f = (f << 1) + 1; // Update the first position of the next level...
i = l; // ...and jump directly to position l
l = -1; // Invalidate l
}
if ( c.compare( top, refArray[ heap[ i ] ] ) == 0 ) {
a[ j++ ] = heap[ i ];
if ( l == -1 ) l = i * 2 + 1; // If this is the first time in this level, set l
r = Math.min( size, i * 2 + 3 ); // Update r, but do not go beyond size
}
}
return j;
}
}
|