




import pytest

from datasets.parallel import ParallelBackendConfig, parallel_backend
from datasets.utils.py_utils import map_nested

from .utils import require_dill_gt_0_3_2, require_joblibspark, require_not_windows





def 						__A						(      __lowerCamelCase       ) ->			List[str]:  # picklable for multiprocessing
	return i + 1





@require_dill_gt_0_3_2
@require_joblibspark
@require_not_windows
def 						__A						(      ) ->			int:
	with parallel_backend("""spark"""       ):
		assert ParallelBackendConfig.backend_name == "spark"

	a									=				[1, 2, 3]
	with pytest.raises(__lowerCamelCase       ):
		with parallel_backend("""unsupported backend"""       ):
			map_nested(__lowerCamelCase				,     __lowerCamelCase				,     num_proc=2       )

	with pytest.raises(__lowerCamelCase       ):
		with parallel_backend("""unsupported backend"""       ):
			map_nested(__lowerCamelCase				,     __lowerCamelCase				,     num_proc=-1       )





@require_dill_gt_0_3_2
@require_joblibspark
@require_not_windows
@pytest.mark.parametrize("""num_proc"""				,     [2, -1]       )
def 						__A						(      __lowerCamelCase       ) ->			List[Any]:
	a									=				[1, 2]
	a									=				{"""a""": 1, """b""": 2}
	a									=				{"""a""": [1, 2], """b""": [3, 4]}
	a									=				{"""a""": {"""1""": 1}, """b""": 2}
	a									=				{"""a""": 1, """b""": 2, """c""": 3, """d""": 4}
	a									=				[2, 3]
	a									=				{"""a""": 2, """b""": 3}
	a									=				{"""a""": [2, 3], """b""": [4, 5]}
	a									=				{"""a""": {"""1""": 2}, """b""": 3}
	a									=				{"""a""": 2, """b""": 3, """c""": 4, """d""": 5}

	with parallel_backend("""spark"""       ):
		assert map_nested(__lowerCamelCase				,     __lowerCamelCase				,     num_proc=__lowerCamelCase       ) == expected_map_nested_sa
		assert map_nested(__lowerCamelCase				,     __lowerCamelCase				,     num_proc=__lowerCamelCase       ) == expected_map_nested_sa
		assert map_nested(__lowerCamelCase				,     __lowerCamelCase				,     num_proc=__lowerCamelCase       ) == expected_map_nested_sa
		assert map_nested(__lowerCamelCase				,     __lowerCamelCase				,     num_proc=__lowerCamelCase       ) == expected_map_nested_sa
		assert map_nested(__lowerCamelCase				,     __lowerCamelCase				,     num_proc=__lowerCamelCase       ) == expected_map_nested_sa







def 						__A						(      ) ->			list[list[int]]:
	return [list(range(1000 - i				,     -1000 - i				,     -1       )       ) for i in range(1000       )]


__UpperCamelCase   :			str          = generate_large_matrix()
__UpperCamelCase   :			Tuple          = (
    [[4, 3, 2, -1], [3, 2, 1, -1], [1, 1, -1, -2], [-1, -1, -2, -3]],
    [[3, 2], [1, 0]],
    [[7, 7, 6]],
    [[7, 7, 6], [-1, -2, -3]],
    grid,
)





def 						__A						(      __lowerCamelCase       ) ->			None:
	assert all(row == sorted(__lowerCamelCase				,     reverse=__lowerCamelCase       ) for row in grid       )
	assert all(list(__lowerCamelCase       ) == sorted(__lowerCamelCase				,     reverse=__lowerCamelCase       ) for col in zip(*__lowerCamelCase       )       )





def 						__A						(      __lowerCamelCase       ) ->			int:
	a									=				0
	a									=				len(__lowerCamelCase       ) - 1

	# Edge cases such as no values or all numbers are negative.
	if not array or array[0] < 0:
		return 0

	while right + 1 > left:
		a									=				(left + right) // 2
		a									=				array[mid]

		# Num must be negative and the index must be greater than or equal to 0.
		if num < 0 and array[mid - 1] >= 0:
			return mid

		if num >= 0:
			a									=				mid + 1
		else:
			a									=				mid - 1
    # No negative numbers so return the last index of the array + 1 which is the length.
	return len(__lowerCamelCase       )





def 						__A						(      __lowerCamelCase       ) ->			int:
	a									=				0
	a									=				len(grid[0]       )

	for i in range(len(__lowerCamelCase       )       ):
		a									=				find_negative_index(grid[i][:bound]       )
		total += bound
	return (len(__lowerCamelCase       ) * len(grid[0]       )) - total





def 						__A						(      __lowerCamelCase       ) ->			int:
	return len([number for row in grid for number in row if number < 0]       )





def 						__A						(      __lowerCamelCase       ) ->			int:
	a									=				0
	for row in grid:
		for i, number in enumerate(__lowerCamelCase       ):
			if number < 0:
				total += len(__lowerCamelCase       ) - i
				break
	return total





def 						__A						(      ) ->			None:
	from timeit import timeit

	print("""Running benchmarks"""       )
	a									=				(
	    """from __main__ import count_negatives_binary_search, """
	    """count_negatives_brute_force, count_negatives_brute_force_with_break, grid"""
	)
	for func in (
	    "count_negatives_binary_search",  # took 0.7727 seconds
	    "count_negatives_brute_force_with_break",  # took 4.6505 seconds
	    "count_negatives_brute_force",  # took 12.8160 seconds
	):
		a									=				timeit(f'{func}(grid=grid)'				,     setup=__lowerCamelCase				,     number=500       )
		print(f'{func}() took {time:0.4f} seconds'       )


if __name__ == "__main__":
		import doctest

		doctest.testmod()
		benchmark()

