litesoft
@ 947
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 |
// This Source Code is in the Public Domain per: http://unlicense.org package org.litesoft.render; public class DistributeToChildren { private ChildAdjustStruct m1stChild = null; private int mCount = 0; public DistributeToChildren add( ChildAdjustStruct pStruct ) { mCount++; if ( m1stChild == null ) { m1stChild = pStruct; } else { pStruct.insertBefore( m1stChild ); } return this; } public boolean hasChildren() { return (mCount != 0); } public ChildAdjustStruct[] getChildren() { return mChildren; } private ChildAdjustStruct[] mChildren = null; private ChildAdjustStruct buildChildren( int pIndexOfLastAdjusted ) { ChildAdjustStruct rv = m1stChild; mChildren = new ChildAdjustStruct[mCount]; ChildAdjustStruct zChild = m1stChild; for ( int i = 0; i < mChildren.length; i++ ) { if ( zChild.getIndex() <= pIndexOfLastAdjusted ) { rv = zChild; } mChildren[i] = zChild; zChild = zChild.mNext; } return rv; } private void freeChildren() { for ( ChildAdjustStruct zChild : mChildren ) { zChild.clearLinks(); } } /** * @param pAdjustSizeBy !0 */ public int distribute( int pAdjustSizeBy, int pIndexOfLastAdjusted ) { ChildAdjustStruct zLastAdjusted = buildChildren( pIndexOfLastAdjusted ); zLastAdjusted = (pAdjustSizeBy > 0) ? // increaseBy( pAdjustSizeBy, zLastAdjusted ) : decreaseBy( -pAdjustSizeBy, zLastAdjusted ); freeChildren(); return zLastAdjusted.getIndex(); } /** * @param pIncreaseBy > 0 */ private ChildAdjustStruct increaseBy( int pIncreaseBy, ChildAdjustStruct pLastAdjusted ) { int zPerInc = pIncreaseBy / mCount; if ( zPerInc > 0 ) { // Since going all the way around, pLastAdjusted will be back at the starting point for ( int i = 0; i < mCount; i++ ) { pIncreaseBy -= (pLastAdjusted = pLastAdjusted.mNext).incAdjustBy( zPerInc ); } } while ( pIncreaseBy > 0 ) { pIncreaseBy -= (pLastAdjusted = pLastAdjusted.mNext).incAdjustBy(); } return pLastAdjusted; } /** * @param pDecreaseBy > 0 */ private ChildAdjustStruct decreaseBy( int pDecreaseBy, ChildAdjustStruct pLastAdjusted ) { int zPerInc = pDecreaseBy / mCount; if ( zPerInc > 0 ) { // Since going all the way around, pLastAdjusted will be back at the starting point for ( int i = 0; i < mCount; i++ ) { pDecreaseBy -= pLastAdjusted.decAdjustBy( zPerInc ); pLastAdjusted = pLastAdjusted.mPrev; } } while ( pDecreaseBy > 0 ) { ChildAdjustStruct zPrev = pLastAdjusted.mPrev; int zDecreasedBy = pLastAdjusted.decAdjustBy(); pDecreaseBy -= zDecreasedBy; if ( zDecreasedBy == 0 ) { if ( zPrev == null ) // Unlinked last node - we can make no more progress { return pLastAdjusted; } pLastAdjusted.unlink(); } pLastAdjusted = zPrev; } return pLastAdjusted; } public String toString() { StringBuilder sb = new StringBuilder().append( "DTC(" ).append( mCount ).append( "): " ); if ( mChildren == null ) { sb.append( mChildren ); } else { sb.append( mChildren[0] ); for ( int i = 1; i < mChildren.length; i++ ) { sb.append( ", " ).append( mChildren[i] ); } } return sb.toString(); } } |