Program: 108470
Project: 108470
Buggy source:
/** 
 * Returns a contingency table for the given columns. The order for string data items is derived from the provided hierarchies
 * @param column1     The first column
 * @param size1       The maximal size in this dimension
 * @param hierarchy1  The hierarchy for the first column, may be null
 * @param column2     The second column
 * @param size2       The maximal size in this dimension
 * @param hierarchy2  The hierarchy for the second column, may be null
 * @return
 */
public StatisticsContingencyTable getContingencyTable(int column1,int size1,Hierarchy hierarchy1,int column2,int size2,Hierarchy hierarchy2){
  if (size1 <= 0 || size2 <= 0)   throw new IllegalArgumentException("String_Node_Str");
  StatisticsContingencyTable table=getContingencyTable(column1,hierarchy1,column2,hierarchy2);
  if (table.values1.length <= size1 && table.values2.length <= size2) {
    return table;
  }
  String[] values1;
  String[] values2;
  double factor1;
  double factor2;
  if (table.values1.length > size1) {
    factor1=(double)size1 / (double)table.values1.length;
    values1=getScaledValues(table.values1,factor1);
  }
 else {
    factor1=1;
    values1=table.values1;
  }
  if (table.values2.length > size2) {
    factor2=(double)size2 / (double)table.values2.length;
    values2=getScaledValues(table.values2,factor2);
  }
 else {
    factor2=1;
    values2=table.values2;
  }
  final Map<Entry,Double> entries=new HashMap<Entry,Double>();
  Iterator<Entry> iter=table.iterator;
  double max=Double.MIN_VALUE;
  while (iter.hasNext()) {
    Entry old=iter.next();
    int index1=(int)Math.round((double)old.value1 * factor1);
    int index2=(int)Math.round((double)old.value2 * factor2);
    index1=index1 < size1 ? index1 : size1 - 1;
    index2=index2 < size2 ? index2 : size2 - 1;
    Entry entry=new Entry(index1,index2);
    Double previous=entries.get(entry);
    double value=previous != null ? previous + old.frequency : old.frequency;
    max=Math.max(value,max);
    entries.put(entry,value);
  }
  final Iterator<Entry> internal=entries.keySet().iterator();
  final Iterator<Entry> iterator=new Iterator<Entry>(){
    private Iterator<Entry> _internal=internal;
    private Map<Entry,Double> _entries=entries;
    @Override public boolean hasNext(){
      if (_internal == null)       return false;
      boolean result=_internal.hasNext();
      if (!result) {
        _internal=null;
        _entries=null;
      }
      return result;
    }
    @Override public Entry next(){
      if (_internal == null)       return null;
      Entry e=_internal.next();
      e.frequency=_entries.get(e);
      return e;
    }
    @Override public void remove(){
      throw new UnsupportedOperationException();
    }
  }
;
  return new StatisticsContingencyTable(values1,values2,table.count,max,iterator);
}

Fixed version (ground truth):
/** 
 * Returns a contingency table for the given columns. The order for string data items is derived from the provided hierarchies
 * @param column1     The first column
 * @param size1       The maximal size in this dimension
 * @param hierarchy1  The hierarchy for the first column, may be null
 * @param column2     The second column
 * @param size2       The maximal size in this dimension
 * @param hierarchy2  The hierarchy for the second column, may be null
 * @return
 */
public StatisticsContingencyTable getContingencyTable(int column1,int size1,Hierarchy hierarchy1,int column2,int size2,Hierarchy hierarchy2){
  if (size1 <= 0 || size2 <= 0)   throw new IllegalArgumentException("String_Node_Str");
  StatisticsContingencyTable table=getContingencyTable(column1,hierarchy1,column2,hierarchy2);
  if (table.values1.length <= size1 && table.values2.length <= size2) {
    return table;
  }
  String[] values1;
  String[] values2;
  double factor1;
  double factor2;
  if (table.values1.length > size1) {
    factor1=(double)size1 / (double)table.values1.length;
    values1=getScaledValues(table.values1,size1);
  }
 else {
    factor1=1;
    values1=table.values1;
  }
  if (table.values2.length > size2) {
    factor2=(double)size2 / (double)table.values2.length;
    values2=getScaledValues(table.values2,size2);
  }
 else {
    factor2=1;
    values2=table.values2;
  }
  final Map<Entry,Double> entries=new HashMap<Entry,Double>();
  Iterator<Entry> iter=table.iterator;
  double max=Double.MIN_VALUE;
  while (iter.hasNext()) {
    Entry old=iter.next();
    int index1=(int)Math.round((double)old.value1 * factor1);
    int index2=(int)Math.round((double)old.value2 * factor2);
    index1=index1 < size1 ? index1 : size1 - 1;
    index2=index2 < size2 ? index2 : size2 - 1;
    Entry entry=new Entry(index1,index2);
    Double previous=entries.get(entry);
    double value=previous != null ? previous + old.frequency : old.frequency;
    max=Math.max(value,max);
    entries.put(entry,value);
  }
  final Iterator<Entry> internal=entries.keySet().iterator();
  final Iterator<Entry> iterator=new Iterator<Entry>(){
    private Iterator<Entry> _internal=internal;
    private Map<Entry,Double> _entries=entries;
    @Override public boolean hasNext(){
      if (_internal == null)       return false;
      boolean result=_internal.hasNext();
      if (!result) {
        _internal=null;
        _entries=null;
      }
      return result;
    }
    @Override public Entry next(){
      if (_internal == null)       return null;
      Entry e=_internal.next();
      e.frequency=_entries.get(e);
      return e;
    }
    @Override public void remove(){
      throw new UnsupportedOperationException();
    }
  }
;
  return new StatisticsContingencyTable(values1,values2,table.count,max,iterator);
}