protected void sortDependents() {

        int bigIndex = 0;
        int smallIndex = 0;
        int prioritySum = 0;
        boolean noChange = true;
        boolean firstLoop = true;

        Node nI = null;
        Node nIMinus1 = null;

        if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
            Tr.debug(tc, "sortDependents entry: sort dependents of this node " + this);
        }

        if ((dependents == null) || (dependents.size() == 0)) {
            // nothing to sort
            if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
                Tr.debug(tc, "sortDependents exit");
            }
            return;
        }

        // If only 1 dependent, then by default it is ready to go
        if (dependents.size() == 1) {
            dependents.get(0).setPriorityRatioPositive(true);
            if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
                Tr.debug(tc, "sortDependents exit");
            }
            return;
        }

        bigIndex = dependents.size() - 1;

        // Determine the sum of the current priorities
        for (int i = bigIndex; i >= 0; i--) {
            prioritySum += dependents.get(i).getPriority();
        }

        // first time through we need to determine ratios, after that only do the comparisons.
        while (true) {

            noChange = true; // signals if we made at least one ordering change in this pass/loop

            // determine priorityRatio for biggest index before entering loop, then just determine ratios for each new node examined
            if (firstLoop) {
                nI = dependents.get(bigIndex);
                if ((dependentWriteCount == 0)
                    || ((nI.getPriority() * dependentWriteCount) > (nI.getWriteCount() * prioritySum))) {
                    nI.setPriorityRatioPositive(true);
                } else {
                    nI.setPriorityRatioPositive(false);
                }
            }

            // Each iteration of this for will move the (next) highest priority to the (next) front of the array.
            if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
                Tr.debug(tc, "looping through with indexes of: " + bigIndex + " and: " + smallIndex);
            }
            for (int i = bigIndex; i >= smallIndex + 1; i--) {

                nI = dependents.get(i);
                nIMinus1 = dependents.get(i - 1);

                // determine priorityRatio for biggest index before entering loop, then just determine ratios for each new node examined
                if (firstLoop) {
                    if ((dependentWriteCount == 0)
                        || ((nIMinus1.getPriority() * dependentWriteCount) > (nIMinus1.getWriteCount() * prioritySum))) {
                        nIMinus1.setPriorityRatioPositive(true);
                    } else {
                        nIMinus1.setPriorityRatioPositive(false);
                    }
                }

                // move up the node that has a positive ratio.  if both positive or negative, then move up the node with the higher priority.
                if (nIMinus1.getPriorityRatioPositive() == false) {
                    if ((nI.getPriorityRatioPositive() == true) || (nI.getPriority() > nIMinus1.getPriority())) {
                        noChange = false;
                        // move up nI
                        if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
                            Tr.debug(tc, "swapping node (n-1): " + nIMinus1 + " with node (n): " + nI);
                        }
                        dependents.set(i, nIMinus1);
                        dependents.set(i - 1, nI);
                    }
                } else if ((nI.getPriorityRatioPositive() == true) && (nI.getPriority() > nIMinus1.getPriority())) {
                    noChange = false;
                    // move up nI
                    if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
                        Tr.debug(tc, "swapping node (n-1): " + nIMinus1 + " with node (n): " + nI);
                    }
                    dependents.set(i, nIMinus1);
                    dependents.set(i - 1, nI);
                }
            }

            firstLoop = false;

            // highest priority has bubbled up to the smallest index for the above loop
            // move up the small index and loop again if there was any ordering change last loop
            smallIndex = smallIndex + 1;

            if ((noChange) || (bigIndex == smallIndex)) {
                // sort it complete
                break;
            }
        }

        if (TraceComponent.isAnyTracingEnabled() && tc.isDebugEnabled()) {
            Tr.debug(tc, "sortDependents exit");
        }
    }
protected void sortDependents ( ) { int bigIndex = 0 ; int smallIndex = 0 ; int prioritySum = 0 ; boolean noChange = true ; boolean firstLoop = true ; Node nI = null ; Node nIMinus1 = null ; if ( TraceComponent . isAnyTracingEnabled ( ) && tc . isDebugEnabled ( ) ) { Tr . debug ( tc , "sortDependents entry: sort dependents of this node " + this ) ; } if ( ( dependents == null ) || ( dependents . size ( ) == 0 ) ) { if ( TraceComponent . isAnyTracingEnabled ( ) && tc . isDebugEnabled ( ) ) { Tr . debug ( tc , "sortDependents exit" ) ; } return ; } if ( dependents . size ( ) == 1 ) { dependents . get ( 0 ) . setPriorityRatioPositive ( true ) ; if ( TraceComponent . isAnyTracingEnabled ( ) && tc . isDebugEnabled ( ) ) { Tr . debug ( tc , "sortDependents exit" ) ; } return ; } bigIndex = dependents . size ( ) - 1 ; for ( int i = bigIndex ; i >= 0 ; i -- ) { prioritySum += dependents . get ( i ) . getPriority ( ) ; } while ( true ) { noChange = true ; if ( firstLoop ) { nI = dependents . get ( bigIndex ) ; if ( ( dependentWriteCount == 0 ) || ( ( nI . getPriority ( ) * dependentWriteCount ) > ( nI . getWriteCount ( ) * prioritySum ) ) ) { nI . setPriorityRatioPositive ( true ) ; } else { nI . setPriorityRatioPositive ( false ) ; } } if ( TraceComponent . isAnyTracingEnabled ( ) && tc . isDebugEnabled ( ) ) { Tr . debug ( tc , "looping through with indexes of: " + bigIndex + " and: " + smallIndex ) ; } for ( int i = bigIndex ; i >= smallIndex + 1 ; i -- ) { nI = dependents . get ( i ) ; nIMinus1 = dependents . get ( i - 1 ) ; if ( firstLoop ) { if ( ( dependentWriteCount == 0 ) || ( ( nIMinus1 . getPriority ( ) * dependentWriteCount ) > ( nIMinus1 . getWriteCount ( ) * prioritySum ) ) ) { nIMinus1 . setPriorityRatioPositive ( true ) ; } else { nIMinus1 . setPriorityRatioPositive ( false ) ; } } if ( nIMinus1 . getPriorityRatioPositive ( ) == false ) { if ( ( nI . getPriorityRatioPositive ( ) == true ) || ( nI . getPriority ( ) > nIMinus1 . getPriority ( ) ) ) { noChange = false ; if ( TraceComponent . isAnyTracingEnabled ( ) && tc . isDebugEnabled ( ) ) { Tr . debug ( tc , "swapping node (n-1): " + nIMinus1 + " with node (n): " + nI ) ; } dependents . set ( i , nIMinus1 ) ; dependents . set ( i - 1 , nI ) ; } } else if ( ( nI . getPriorityRatioPositive ( ) == true ) && ( nI . getPriority ( ) > nIMinus1 . getPriority ( ) ) ) { noChange = false ; if ( TraceComponent . isAnyTracingEnabled ( ) && tc . isDebugEnabled ( ) ) { Tr . debug ( tc , "swapping node (n-1): " + nIMinus1 + " with node (n): " + nI ) ; } dependents . set ( i , nIMinus1 ) ; dependents . set ( i - 1 , nI ) ; } } firstLoop = false ; smallIndex = smallIndex + 1 ; if ( ( noChange ) || ( bigIndex == smallIndex ) ) { break ; } } if ( TraceComponent . isAnyTracingEnabled ( ) && tc . isDebugEnabled ( ) ) { Tr . debug ( tc , "sortDependents exit" ) ; } }
Sort array list of dependencies so highest weight is at the lowest array list index, up to the lowest weight.
Us a bubble sort because the list should be short, and/or it should not have much movement from iteration to iteration
Is: A/B > C/D ?
Answer: if A*D > C*B then A/B > C/D (due to creating common denominators)
if ( your-priority/children-priority-sum > your-hits/children-write-count) then do your operation.
if your-priority * children-write-count > your-hits * children-priority-sum,
then signal you are allowed to do your operation -> setPriorityRatioPositive(true)