Program: 4470
Project: 4470
Buggy source:
/** 
 * Creates nDesiredNewSynapes synapses on the segment passed in if possible, choosing random cells from the previous winner cells that are not already on the segment. <p> <b>Notes:</b> The process of writing the last value into the index in the array that was most recently changed is to ensure the same results that we get in the c++ implementation using iter_swap with vectors. </p>
 * @param conn                      Connections instance for the tm
 * @param prevWinnerCells           Winner cells in `t-1`
 * @param segment                   Segment to grow synapses on.     
 * @param initialPermanence         Initial permanence of a new synapse.
 * @param nDesiredNewSynapses       Desired number of synapses to grow
 * @param random                    Tm object used to generate randomnumbers
 */
public void growSynapses(Connections conn,Set<Cell> prevWinnerCells,DistalDendrite segment,double initialPermanence,int nDesiredNewSynapses,Random random){
  List<Cell> candidates=new ArrayList<>(prevWinnerCells);
  Collections.sort(candidates);
  int eligibleEnd=candidates.size();
  for (  Synapse synapse : conn.unDestroyedSynapsesForSegment(segment)) {
    Cell presynapticCell=synapse.getPresynapticCell();
    int ineligible=candidates.subList(0,eligibleEnd).indexOf(presynapticCell);
    if (ineligible != -1) {
      eligibleEnd--;
      candidates.set(ineligible,candidates.get(eligibleEnd));
    }
  }
  int nActual=nDesiredNewSynapses < eligibleEnd ? nDesiredNewSynapses : eligibleEnd;
  for (int i=0; i < nActual; i++) {
    int rand=random.nextInt(eligibleEnd);
    conn.createSynapse(segment,candidates.get(rand),initialPermanence);
    candidates.set(rand,candidates.get(eligibleEnd));
    eligibleEnd--;
  }
}

Fixed version (ground truth):
/** 
 * Creates nDesiredNewSynapes synapses on the segment passed in if possible, choosing random cells from the previous winner cells that are not already on the segment. <p> <b>Notes:</b> The process of writing the last value into the index in the array that was most recently changed is to ensure the same results that we get in the c++ implementation using iter_swap with vectors. </p>
 * @param conn                      Connections instance for the tm
 * @param prevWinnerCells           Winner cells in `t-1`
 * @param segment                   Segment to grow synapses on.     
 * @param initialPermanence         Initial permanence of a new synapse.
 * @param nDesiredNewSynapses       Desired number of synapses to grow
 * @param random                    Tm object used to generate randomnumbers
 */
public void growSynapses(Connections conn,Set<Cell> prevWinnerCells,DistalDendrite segment,double initialPermanence,int nDesiredNewSynapses,Random random){
  List<Cell> candidates=new ArrayList<>(prevWinnerCells);
  Collections.sort(candidates);
  int eligibleEnd=candidates.size() - 1;
  for (  Synapse synapse : conn.unDestroyedSynapsesForSegment(segment)) {
    Cell presynapticCell=synapse.getPresynapticCell();
    int index=candidates.subList(0,eligibleEnd + 1).indexOf(presynapticCell);
    if (index != -1) {
      candidates.set(index,candidates.get(eligibleEnd));
      eligibleEnd--;
    }
  }
  int candidatesLength=eligibleEnd + 1;
  int nActual=nDesiredNewSynapses < candidatesLength ? nDesiredNewSynapses : candidatesLength;
  for (int i=0; i < nActual; i++) {
    int rand=random.nextInt(candidatesLength);
    conn.createSynapse(segment,candidates.get(rand),initialPermanence);
    candidates.set(rand,candidates.get(candidatesLength - 1));
    candidatesLength--;
  }
}