#NEWLINE# def update( self , x , w = 1 ) : #NEWLINE# #INDENT# '\n        Update the t-digest with value x and weight w.\n\n        ' #NEWLINE# self . n += w #NEWLINE# if ( len ( self ) == 0 ) : #NEWLINE# #INDENT# self . _add_centroid ( Centroid ( x , w ) ) #NEWLINE# return #NEWLINE# #UNINDENT# S = self . _find_closest_centroids ( x ) #NEWLINE# while ( ( len ( S ) != 0 ) and ( j > 0 ) ) : #NEWLINE# #INDENT# j = choice ( list ( range ( len ( S ) ) ) ) #NEWLINE# c_j = S [ j ] #NEWLINE# q = self . _compute_centroid_quantile ( c_j ) #NEWLINE# if ( ( c_j . count + w ) > self . _theshold ( q ) ) : #NEWLINE# #INDENT# S . pop ( j ) #NEWLINE# continue #NEWLINE# #UNINDENT# delta_w = min ( ( self . _theshold ( q ) - c_j . count ) , w ) #NEWLINE# self . _update_centroid ( c_j , x , delta_w ) #NEWLINE# w -= delta_w #NEWLINE# S . pop ( j ) #NEWLINE# #UNINDENT# if ( w > 0 ) : #NEWLINE# #INDENT# self . _add_centroid ( Centroid ( x , w ) ) #NEWLINE# #UNINDENT# if ( len ( self ) > ( self . K / self . delta ) ) : #NEWLINE# #INDENT# self . compress ( ) #NEWLINE# #UNINDENT# return