for ( d = k = 0; k <= ( ( cache - 1 ) >>> quantumDivisionShift ); k++ ) d += ( cachePointer[k].writtenBits() + cacheDataLength[ k ] );
quantumBitLength = (int)( ( ( d * quantum ) + ( cache - 1 ) ) / cache );
final TowerData td = new TowerData();
final Int2IntRBTreeMap candidates = new Int2IntRBTreeMap();
/* As a first try, we compute the tower costs using 0 as average entry bit length. */
tryTower( quantumBitLength, positionsQuantumBitLength, 0, toTheEnd, cacheSkipBitCount, td, false );
if ( td.numberOfSkipTowers > 0 ) { // There actually is at least a tower.
/* Now we repeat this operation, trying to obtain the best value for the
* average entry bit length.
*/
while( candidates.size() < MAX_TRY && ! candidates.containsValue( entryBitLength = (int)( td.bitsForTowers() / td.numberOfEntries() ) ) ) {
td.clear();
tryTower( quantumBitLength, positionsQuantumBitLength, entryBitLength, toTheEnd, cacheSkipBitCount, td, false );
candidates.put( (int)( td.bitsForTowers() / td.numberOfEntries() ), entryBitLength );
}
if ( ASSERTS ) assert candidates.size() < MAX_TRY;
entryBitLength = candidates.get( candidates.firstIntKey() );
if ( DEBUG ) System.err.println( "Going to write tower at position " + obs.writtenBits() );
tryTower( quantumBitLength, positionsQuantumBitLength, entryBitLength, toTheEnd, cacheSkip, towerData, true );
}