


from copy import deepcopy
from typing import Optional, Union

import numpy as np

from ...processing_utils import ProcessorMixin
from ...tokenization_utils_base import BatchEncoding
from ...utils import TensorType, is_tf_available, is_torch_available


if is_torch_available():
				import torch

if is_tf_available():
				import tensorflow as tf







class 	__lowercase    (__SCREAMING_SNAKE_CASE  ):





							"""simple docstring"""







							_UpperCAmelCase      =						['image_processor']
							_UpperCAmelCase      =						'SamImageProcessor'






							def __init__(      self			,						lowerCAmelCase__       ):





										"""simple docstring"""




										super().__init__(lowerCAmelCase__       )
										SCREAMING_SNAKE_CASE_		:     List[Any]					=					self.image_processor
										SCREAMING_SNAKE_CASE_		:     List[str]					=					-1_0
										SCREAMING_SNAKE_CASE_		:     Optional[int]					=					self.image_processor.size['longest_edge']






							def __call__(      self			,						lowerCAmelCase__=None			,						lowerCAmelCase__=None			,						lowerCAmelCase__=None			,						lowerCAmelCase__=None			,						lowerCAmelCase__ = None			,						**lowerCAmelCase__			,						):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     List[Any]					=					self.image_processor(
										    lowerCAmelCase__			,						return_tensors=lowerCAmelCase__			,						**lowerCAmelCase__			,						)

										# pop arguments that are not used in the foward but used nevertheless
										SCREAMING_SNAKE_CASE_		:     Any					=					encoding_image_processor['original_sizes']

										if hasattr(lowerCAmelCase__			,						'numpy'       ):  # Checks if Torch or TF tensor
													SCREAMING_SNAKE_CASE_		:     str					=					original_sizes.numpy()

										SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		:     str					=					self._check_and_preprocess_points(
										    input_points=lowerCAmelCase__			,						input_labels=lowerCAmelCase__			,						input_boxes=lowerCAmelCase__			,						)

										SCREAMING_SNAKE_CASE_		:     Any					=					self._normalize_and_convert(
										    lowerCAmelCase__			,						lowerCAmelCase__			,						input_points=lowerCAmelCase__			,						input_labels=lowerCAmelCase__			,						input_boxes=lowerCAmelCase__			,						return_tensors=lowerCAmelCase__			,						)

										return encoding_image_processor






							def 					UpperCamelCase__     (      self			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__=None			,						lowerCAmelCase__=None			,						lowerCAmelCase__=None			,						lowerCAmelCase__="pt"			,						):





										"""simple docstring"""




										if input_points is not None:
													if len(lowerCAmelCase__       ) != len(lowerCAmelCase__       ):
																SCREAMING_SNAKE_CASE_		:     int					=					[
																    self._normalize_coordinates(self.target_size			,						lowerCAmelCase__			,						original_sizes[0]       ) for point in input_points
																]
													else:
																SCREAMING_SNAKE_CASE_		:     Optional[int]					=					[
																    self._normalize_coordinates(self.target_size			,						lowerCAmelCase__			,						lowerCAmelCase__       )
																    for point, original_size in zip(lowerCAmelCase__			,						lowerCAmelCase__       )
																]
													# check that all arrays have the same shape
													if not all(point.shape == input_points[0].shape for point in input_points       ):
																if input_labels is not None:
																			SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		:     List[str]					=					self._pad_points_and_labels(lowerCAmelCase__			,						lowerCAmelCase__       )

													SCREAMING_SNAKE_CASE_		:     Dict					=					np.array(lowerCAmelCase__       )

										if input_labels is not None:
													SCREAMING_SNAKE_CASE_		:     Optional[int]					=					np.array(lowerCAmelCase__       )

										if input_boxes is not None:
													if len(lowerCAmelCase__       ) != len(lowerCAmelCase__       ):
																SCREAMING_SNAKE_CASE_		:     List[str]					=					[
																    self._normalize_coordinates(self.target_size			,						lowerCAmelCase__			,						original_sizes[0]			,						is_bounding_box=lowerCAmelCase__       )
																    for box in input_boxes
																]
													else:
																SCREAMING_SNAKE_CASE_		:     Dict					=					[
																    self._normalize_coordinates(self.target_size			,						lowerCAmelCase__			,						lowerCAmelCase__			,						is_bounding_box=lowerCAmelCase__       )
																    for box, original_size in zip(lowerCAmelCase__			,						lowerCAmelCase__       )
																]
													SCREAMING_SNAKE_CASE_		:     Optional[int]					=					np.array(lowerCAmelCase__       )

										if input_boxes is not None:
													if return_tensors == "pt":
																SCREAMING_SNAKE_CASE_		:     Dict					=					torch.from_numpy(lowerCAmelCase__       )
																# boxes batch size of 1 by default
																SCREAMING_SNAKE_CASE_		:     int					=					input_boxes.unsqueeze(1       ) if len(input_boxes.shape       ) != 3 else input_boxes
													elif return_tensors == "tf":
																SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					tf.convert_to_tensor(lowerCAmelCase__       )
																# boxes batch size of 1 by default
																SCREAMING_SNAKE_CASE_		:     int					=					tf.expand_dims(lowerCAmelCase__			,						1       ) if len(input_boxes.shape       ) != 3 else input_boxes
													encoding_image_processor.update({'input_boxes': input_boxes}       )
										if input_points is not None:
													if return_tensors == "pt":
																SCREAMING_SNAKE_CASE_		:     List[str]					=					torch.from_numpy(lowerCAmelCase__       )
																# point batch size of 1 by default
																SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					input_points.unsqueeze(1       ) if len(input_points.shape       ) != 4 else input_points
													elif return_tensors == "tf":
																SCREAMING_SNAKE_CASE_		:     Tuple					=					tf.convert_to_tensor(lowerCAmelCase__       )
																# point batch size of 1 by default
																SCREAMING_SNAKE_CASE_		:     Tuple					=					tf.expand_dims(lowerCAmelCase__			,						1       ) if len(input_points.shape       ) != 4 else input_points
													encoding_image_processor.update({'input_points': input_points}       )
										if input_labels is not None:
													if return_tensors == "pt":
																SCREAMING_SNAKE_CASE_		:     Optional[int]					=					torch.from_numpy(lowerCAmelCase__       )
																# point batch size of 1 by default
																SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					input_labels.unsqueeze(1       ) if len(input_labels.shape       ) != 3 else input_labels
													elif return_tensors == "tf":
																SCREAMING_SNAKE_CASE_		:     Dict					=					tf.convert_to_tensor(lowerCAmelCase__       )
																# point batch size of 1 by default
																SCREAMING_SNAKE_CASE_		:     int					=					tf.expand_dims(lowerCAmelCase__			,						1       ) if len(input_labels.shape       ) != 3 else input_labels
													encoding_image_processor.update({'input_labels': input_labels}       )

										return encoding_image_processor






							def 					UpperCamelCase__     (      self			,						lowerCAmelCase__			,						lowerCAmelCase__       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     Dict					=					max([point.shape[0] for point in input_points]       )
										SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					[]
										for i, point in enumerate(lowerCAmelCase__       ):
													if point.shape[0] != expected_nb_points:
																SCREAMING_SNAKE_CASE_		:     Optional[int]					=					np.concatenate(
																    [point, np.zeros((expected_nb_points - point.shape[0], 2)       ) + self.point_pad_value]			,						axis=0       )
																SCREAMING_SNAKE_CASE_		:     Dict					=					np.append(input_labels[i]			,						[self.point_pad_value]       )
													processed_input_points.append(lowerCAmelCase__       )
										SCREAMING_SNAKE_CASE_		:     Any					=					processed_input_points
										return input_points, input_labels






							def 					UpperCamelCase__     (      self			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__			,						lowerCAmelCase__=False       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		:     Tuple					=					original_size
										SCREAMING_SNAKE_CASE_		,      SCREAMING_SNAKE_CASE_		:     List[Any]					=					self.image_processor._get_preprocess_shape(lowerCAmelCase__			,						longest_edge=lowerCAmelCase__       )
										SCREAMING_SNAKE_CASE_		:     Tuple					=					deepcopy(lowerCAmelCase__       ).astype(lowerCAmelCase__       )

										if is_bounding_box:
													SCREAMING_SNAKE_CASE_		:     Optional[Any]					=					coords.reshape(-1			,						2			,						2       )

										SCREAMING_SNAKE_CASE_		:     Tuple					=					coords[..., 0] * (new_w / old_w)
										SCREAMING_SNAKE_CASE_		:     str					=					coords[..., 1] * (new_h / old_h)

										if is_bounding_box:
													SCREAMING_SNAKE_CASE_		:     List[Any]					=					coords.reshape(-1			,						4       )

										return coords






							def 					UpperCamelCase__     (      self			,						lowerCAmelCase__=None			,						lowerCAmelCase__=None			,						lowerCAmelCase__=None			,						):





										"""simple docstring"""




										if input_points is not None:
													if hasattr(lowerCAmelCase__			,						'numpy'       ):  # Checks for TF or Torch tensor
																SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					input_points.numpy().tolist()

													if not isinstance(lowerCAmelCase__			,						lowerCAmelCase__       ) or not isinstance(input_points[0]			,						lowerCAmelCase__       ):
																raise ValueError('Input points must be a list of list of floating points.'       )
													SCREAMING_SNAKE_CASE_		:     List[Any]					=					[np.array(lowerCAmelCase__       ) for input_point in input_points]
										else:
													SCREAMING_SNAKE_CASE_		:     Any					=					None

										if input_labels is not None:
													if hasattr(lowerCAmelCase__			,						'numpy'       ):
																SCREAMING_SNAKE_CASE_		:     Tuple					=					input_labels.numpy().tolist()

													if not isinstance(lowerCAmelCase__			,						lowerCAmelCase__       ) or not isinstance(input_labels[0]			,						lowerCAmelCase__       ):
																raise ValueError('Input labels must be a list of list integers.'       )
													SCREAMING_SNAKE_CASE_		:     List[Any]					=					[np.array(lowerCAmelCase__       ) for label in input_labels]
										else:
													SCREAMING_SNAKE_CASE_		:     List[Any]					=					None

										if input_boxes is not None:
													if hasattr(lowerCAmelCase__			,						'numpy'       ):
																SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					input_boxes.numpy().tolist()

													if (
													    not isinstance(lowerCAmelCase__			,						lowerCAmelCase__       )
													    or not isinstance(input_boxes[0]			,						lowerCAmelCase__       )
													    or not isinstance(input_boxes[0][0]			,						lowerCAmelCase__       )
													):
																raise ValueError('Input boxes must be a list of list of list of floating points.'       )
													SCREAMING_SNAKE_CASE_		:     List[Any]					=					[np.array(lowerCAmelCase__       ).astype(np.floataa       ) for box in input_boxes]
										else:
													SCREAMING_SNAKE_CASE_		:     Union[str, Any]					=					None

										return input_points, input_labels, input_boxes






							@property
							def 					UpperCamelCase__     (      self       ):





										"""simple docstring"""




										SCREAMING_SNAKE_CASE_		:     List[Any]					=					self.image_processor.model_input_names
										return list(dict.fromkeys(lowerCAmelCase__       )       )






							def 					UpperCamelCase__     (      self			,						*lowerCAmelCase__			,						**lowerCAmelCase__       ):





										"""simple docstring"""




										return self.image_processor.post_process_masks(*lowerCAmelCase__			,						**lowerCAmelCase__       )









__UpperCamelCase     		=							2_5_6
# Modulus to hash a string
__UpperCamelCase     		=							1_0_0_0_0_0_3
def 							UpperCamelCase_(					_A :str					,  _A :str	)->		bool:
			UpperCamelCase__       =							len(_A	)
			UpperCamelCase__       =							len(_A	)
			if p_len > t_len:
						return False

			UpperCamelCase__       =							0
			UpperCamelCase__       =							0
			UpperCamelCase__       =							1

			# Calculating the hash of pattern and substring of text
			for i in range(_A	):
						UpperCamelCase__       =							(ord(pattern[i]	) + p_hash * alphabet_size) % modulus
						UpperCamelCase__       =							(ord(text[i]	) + text_hash * alphabet_size) % modulus
						if i == p_len - 1:
									continue
						UpperCamelCase__       =							(modulus_power * alphabet_size) % modulus

			for i in range(0					,  t_len - p_len + 1	):
						if text_hash == p_hash and text[i : i + p_len] == pattern:
									return True
						if i == t_len - p_len:
									continue
						# Calculate the https://en.wikipedia.org/wiki/Rolling_hash
						UpperCamelCase__       =							(
						    (text_hash - ord(text[i]	) * modulus_power) * alphabet_size
						    + ord(text[i + p_len]	)
						) % modulus
			return False
def 							UpperCamelCase_(					)->		None:
			UpperCamelCase__       =							"abc1abc12"
			UpperCamelCase__       =							"alskfjaldsabc1abc1abc12k23adsfabcabc"
			UpperCamelCase__       =							"alskfjaldsk23adsfabcabc"
			assert rabin_karp(_A					,  _A	) and not rabin_karp(_A					,  _A	)

			# Test 2)
			UpperCamelCase__       =							"ABABX"
			UpperCamelCase__       =							"ABABZABABYABABX"
			assert rabin_karp(_A					,  _A	)

			# Test 3)
			UpperCamelCase__       =							"AAAB"
			UpperCamelCase__       =							"ABAAAAAB"
			assert rabin_karp(_A					,  _A	)

			# Test 4)
			UpperCamelCase__       =							"abcdabcy"
			UpperCamelCase__       =							"abcxabcdabxabcdabcdabcy"
			assert rabin_karp(_A					,  _A	)

			# Test 5)
			UpperCamelCase__       =							"Lü"
			UpperCamelCase__       =							"Lüsai"
			assert rabin_karp(_A					,  _A	)
			UpperCamelCase__       =							"Lue"
			assert not rabin_karp(_A					,  _A	)
			print("Success."	)


if __name__ == "__main__":
						test_rabin_karp()


