public void update ( double p , long m ) { Long mi = bin . get ( p ) ; if ( mi != null ) { // we found the same p so increment that counter bin . put ( p , mi + m ) ; } else { bin . put ( p , m ) ; // if bin size exceeds maximum bin size then trim down to max size while ( bin . size ( ) > maxBinSize ) { // find points p1, p2 which have smallest difference Iterator < Double > keys = bin . keySet ( ) . iterator ( ) ; double p1 = keys . next ( ) ; double p2 = keys . next ( ) ; double smallestDiff = p2 - p1 ; double q1 = p1 , q2 = p2 ; while ( keys . hasNext ( ) ) { p1 = p2 ; p2 = keys . next ( ) ; double diff = p2 - p1 ; if ( diff < smallestDiff ) { smallestDiff = diff ; q1 = p1 ; q2 = p2 ; } } // merge those two long k1 = bin . remove ( q1 ) ; long k2 = bin . remove ( q2 ) ; bin . put ( ( q1 * k1 + q2 * k2 ) / ( k1 + k2 ) , k1 + k2 ) ; } } }
Adds new point p with value m to this histogram .