#NEWLINE# def _find_longest_prefix_match( tree , bin_X , hash_size , left_masks , right_masks ) : #NEWLINE# #INDENT# 'Find the longest prefix match in tree for each query in bin_X\n\n    Most significant bits are considered as the prefix.\n    ' #NEWLINE# hi = np . empty_like ( bin_X , dtype = np . intp ) #NEWLINE# hi . fill ( hash_size ) #NEWLINE# lo = np . zeros_like ( bin_X , dtype = np . intp ) #NEWLINE# res = np . empty_like ( bin_X , dtype = np . intp ) #NEWLINE# ( left_idx , right_idx ) = _find_matching_indices ( tree , bin_X , left_masks [ hi ] , right_masks [ hi ] ) #NEWLINE# found = ( right_idx > left_idx ) #NEWLINE# res [ found ] = lo [ found ] = hash_size #NEWLINE# r = np . arange ( bin_X . shape [ 0 ] ) #NEWLINE# kept = r [ ( lo < hi ) ] #NEWLINE# while kept . shape [ 0 ] : #NEWLINE# #INDENT# mid = ( ( lo . take ( kept ) + hi . take ( kept ) ) // 2 ) #NEWLINE# ( left_idx , right_idx ) = _find_matching_indices ( tree , bin_X . take ( kept ) , left_masks [ mid ] , right_masks [ mid ] ) #NEWLINE# found = ( right_idx > left_idx ) #NEWLINE# mid_found = mid [ found ] #NEWLINE# lo [ kept [ found ] ] = ( mid_found + 1 ) #NEWLINE# res [ kept [ found ] ] = mid_found #NEWLINE# hi [ kept [ ( ~ found ) ] ] = mid [ ( ~ found ) ] #NEWLINE# kept = r [ ( lo < hi ) ] #NEWLINE# #UNINDENT# return res