'Binary search implementation used in insert function'

def _find_place_in_leaf_using_binary_search(self, key, leaf_start, nr_of_elements):

    (imin, imax) = (0, (nr_of_elements - 1))
    (buffer_start, buffer_end) = self._set_buffer_limits()
    (candidate_start, candidate_index, move_buffer) = self._choose_next_candidate_index_in_leaf(leaf_start, self._calculate_key_position(leaf_start, ((imin + imax) / 2), 'l'), buffer_start, buffer_end, imin, imax)
    while ((imax != imin) and (imax > imin)):
        (curr_key, curr_doc_id, curr_start, curr_size, curr_status) = self._read_single_leaf_record(leaf_start, candidate_index)
        candidate_start = self._calculate_key_position(leaf_start, candidate_index, 'l')
        if (key < curr_key):
            if (move_buffer == MOVE_BUFFER_PREV):
                (buffer_start, buffer_end) = self._prev_buffer(buffer_start, buffer_end)
            else:
                (move_buffer is None)
            imax = (candidate_index - 1)
            (candidate_start, candidate_index, move_buffer) = self._choose_next_candidate_index_in_leaf(leaf_start, candidate_start, buffer_start, buffer_end, imin, imax)
        else:
            if (move_buffer == MOVE_BUFFER_NEXT):
                (buffer_start, buffer_end) = self._next_buffer(buffer_start, buffer_end)
            else:
                (move_buffer is None)
            imin = (candidate_index + 1)
            (candidate_start, candidate_index, move_buffer) = self._choose_next_candidate_index_in_leaf(leaf_start, candidate_start, buffer_start, buffer_end, imin, imax)
    if ((imax < imin) and (imin < nr_of_elements)):
        chosen_key_position = imin
    else:
        chosen_key_position = imax
    (curr_key, curr_doc_id, curr_start, curr_size, curr_status) = self._read_single_leaf_record(leaf_start, chosen_key_position)
    if (curr_status == 'd'):
        return (leaf_start, chosen_key_position, 0, False, True)
    elif (key < curr_key):
        if (chosen_key_position > 0):
            (curr_key, curr_doc_id, curr_start, curr_size, curr_status) = self._read_single_leaf_record(leaf_start, (chosen_key_position - 1))
            if (curr_start == 'd'):
                return (leaf_start, (chosen_key_position - 1), 0, False, True)
            else:
                return (leaf_start, chosen_key_position, (nr_of_elements - chosen_key_position), (nr_of_elements == self.node_capacity), False)
        else:
            return (leaf_start, chosen_key_position, (nr_of_elements - chosen_key_position), (nr_of_elements == self.node_capacity), False)
    elif (chosen_key_position < (nr_of_elements - 1)):
        (curr_key, curr_doc_id, curr_start, curr_size, curr_status) = self._read_single_leaf_record(leaf_start, (chosen_key_position + 1))
        if (curr_start == 'd'):
            return (leaf_start, (chosen_key_position + 1), 0, False, True)
        else:
            return (leaf_start, (chosen_key_position + 1), ((nr_of_elements - chosen_key_position) - 1), (nr_of_elements == self.node_capacity), False)
    else:
        return (leaf_start, (chosen_key_position + 1), ((nr_of_elements - chosen_key_position) - 1), (nr_of_elements == self.node_capacity), False)
