question: static void calculateTiledLayout ( LayoutIterator iter , int targetSpan ) { long preferred = 0 ; long currentPreferred ; int lastMargin = 0 ; int totalSpacing = 0 ; int n = iter . getCount ( ) ; int adjustmentWeightsCount = LayoutIterator . WorstAdjustmentWeight + 1 ; long gain [ ] = new long [ adjustmentWeightsCount ] ; long loss [ ] = new long [ adjustmentWeightsCount ] ; for ( int i = 0 ; i < adjustmentWeightsCount ; i ++ ) { gain [ i ] = loss [ i ] = 0 ; } for ( int i = 0 ; i < n ; i ++ ) { iter . setIndex ( i ) ; int margin0 = lastMargin ; int margin1 = ( int ) iter . getLeadingCollapseSpan ( ) ; iter . setOffset ( Math . max ( margin0 , margin1 ) ) ; totalSpacing += iter . getOffset ( ) ; currentPreferred = ( long ) iter . getPreferredSpan ( targetSpan ) ; iter . setSpan ( ( int ) currentPreferred ) ; preferred += currentPreferred ; gain [ iter . getAdjustmentWeight ( ) ] += ( long ) iter . getMaximumSpan ( targetSpan ) - currentPreferred ; loss [ iter . getAdjustmentWeight ( ) ] += currentPreferred - ( long ) iter . getMinimumSpan ( targetSpan ) ; lastMargin = ( int ) iter . getTrailingCollapseSpan ( ) ; } totalSpacing += lastMargin ; totalSpacing += 2 * iter . getBorderWidth ( ) ; for ( int i = 1 ; i < adjustmentWeightsCount ; i ++ ) { gain [ i ] += gain [ i - 1 ] ; loss [ i ] += loss [ i - 1 ] ; } int allocated = targetSpan - totalSpacing ; long desiredAdjustment = allocated - preferred ; long adjustmentsArray [ ] = ( desiredAdjustment > 0 ) ? gain : loss ; desiredAdjustment = Math . abs ( desiredAdjustment ) ; int adjustmentLevel = 0 ; for ( ; adjustmentLevel <= LayoutIterator . WorstAdjustmentWeight ; adjustmentLevel ++ ) { if ( adjustmentsArray [ adjustmentLevel ] >= desiredAdjustment ) { break ; } } float adjustmentFactor = 0.0f ; if ( adjustmentLevel <= LayoutIterator . WorstAdjustmentWeight ) { desiredAdjustment -= ( adjustmentLevel > 0 ) ? adjustmentsArray [ adjustmentLevel - 1 ] : 0 ; if ( desiredAdjustment != 0 ) { float maximumAdjustment = adjustmentsArray [ adjustmentLevel ] - ( ( adjustmentLevel > 0 ) ? adjustmentsArray [ adjustmentLevel - 1 ] : 0 ) ; adjustmentFactor = desiredAdjustment / maximumAdjustment ; } } int totalOffset = ( int ) iter . getBorderWidth ( ) ; for ( int i = 0 ; i < n ; i ++ ) { iter . setIndex ( i ) ; iter . setOffset ( iter . getOffset ( ) + totalOffset ) ; if ( iter . getAdjustmentWeight ( ) < adjustmentLevel ) { iter . setSpan ( ( int ) ( ( allocated > preferred ) ? Math . floor ( iter . getMaximumSpan ( targetSpan ) ) : Math . ceil ( iter . getMinimumSpan ( targetSpan ) ) ) ) ; } else if ( iter . getAdjustmentWeight ( ) == adjustmentLevel ) { int availableSpan = ( allocated > preferred ) ? ( int ) iter . getMaximumSpan ( targetSpan ) - iter . getSpan ( ) : iter . getSpan ( ) - ( int ) iter . getMinimumSpan ( targetSpan ) ; int adj = ( int ) Math . floor ( adjustmentFactor * availableSpan ) ; iter . setSpan ( iter . getSpan ( ) + ( ( allocated > preferred ) ? adj : - adj ) ) ; } totalOffset = ( int ) Math . min ( ( long ) iter . getOffset ( ) + ( long ) iter . getSpan ( ) , Integer . MAX_VALUE ) ; } int roundError = targetSpan - totalOffset - ( int ) iter . getTrailingCollapseSpan ( ) - ( int ) iter . getBorderWidth ( ) ; int adj = ( roundError > 0 ) ? 1 : - 1 ; roundError *= adj ; boolean canAdjust = true ; while ( roundError > 0 && canAdjust ) { canAdjust = false ; int offsetAdjust = 0 ; for ( int i = 0 ; i < n ; i ++ ) { iter . setIndex ( i ) ; iter . setOffset ( iter . getOffset ( ) + offsetAdjust ) ; int curSpan = iter . getSpan ( ) ; if ( roundError > 0 ) { int boundGap = ( adj > 0 ) ? ( int ) Math . floor ( iter . getMaximumSpan ( targetSpan ) ) - curSpan : curSpan - ( int ) Math . ceil ( iter . getMinimumSpan ( targetSpan ) ) ; if ( boundGap >= 1 ) { canAdjust = true ; iter . setSpan ( curSpan + adj ) ; offsetAdjust += adj ; roundError -- ; } } } } }
target:  Calculate a tiled layout for the given iterator. This should be done collapsing the neighboring margins to be a total of the maximum of the two neighboring margin areas as described in the CSS spec.