



'''simple docstring'''





import logging
import os

from .state import PartialState






class 			lowerCAmelCase_(							logging.LoggerAdapter	):
  '''simple docstring'''




  @staticmethod
  def 				UpperCAmelCase_      (       __UpperCAmelCase )       ->						Optional[Any]:
      lowerCAmelCase__					:    Union[str, Any]				 =   PartialState()
      return not main_process_only or (main_process_only and state.is_main_process)






  def 				UpperCAmelCase_      (       self    ,__UpperCAmelCase    ,__UpperCAmelCase    ,*__UpperCAmelCase    ,**__UpperCAmelCase )       ->						List[str]:
      if PartialState._shared_state == {}:
          raise RuntimeError(
              """You must initialize the accelerate state by calling either `PartialState()` or `Accelerator()` before using the logging utility.""" )
      lowerCAmelCase__					:    int				 =   kwargs.pop("""main_process_only"""    ,__SCREAMING_SNAKE_CASE )
      lowerCAmelCase__					:    Tuple				 =   kwargs.pop("""in_order"""    ,__SCREAMING_SNAKE_CASE )

      if self.isEnabledFor(__SCREAMING_SNAKE_CASE ):
          if self._should_log(__SCREAMING_SNAKE_CASE ):
              lowerCAmelCase__					:    Any				 =   self.process(__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE )
              self.logger.log(__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,*__SCREAMING_SNAKE_CASE    ,**__SCREAMING_SNAKE_CASE )

          elif in_order:
              lowerCAmelCase__					:    str				 =   PartialState()
              for i in range(state.num_processes ):
                  if i == state.process_index:
                      lowerCAmelCase__					:    Tuple				 =   self.process(__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE )
                      self.logger.log(__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,*__SCREAMING_SNAKE_CASE    ,**__SCREAMING_SNAKE_CASE )
                  state.wait_for_everyone()







def   _SCREAMING_SNAKE_CASE    (			UpperCamelCase				,       UpperCamelCase = None     ):



    """simple docstring"""





    if log_level is None:
        lowerCAmelCase__					:    Union[str, Any]				 =   os.environ.get("""ACCELERATE_LOG_LEVEL"""				,       __A     )
    lowerCAmelCase__					:    Optional[int]				 =   logging.getLogger(__A     )
    if log_level is not None:
        logger.setLevel(log_level.upper()     )
        logger.root.setLevel(log_level.upper()     )
    return MultiProcessAdapter(__A				,       {}     )










'''simple docstring'''







from __future__ import annotations





def 		_UpperCAmelCase (				__A				: str    ,		__A				: str   ):
				a_		:	int									=   get_failure_array(__A   )

				# 2) Step through text searching for pattern
				a_					,      a_		:	Any									=   0, 0  # index into text, pattern
				while i < len(__A   ):
								if pattern[j] == text[i]:
												if j == (len(__A   ) - 1):
																return True
												j += 1

								# if this is a prefix in our pattern
								# just go back far enough to continue
								elif j > 0:
												a_		:	Any									=   failure[j - 1]
												continue
								i += 1
				return False





def 		_UpperCAmelCase (				__A				: str   ):
				a_		:	Optional[Any]									=   [0]
				a_		:	Any									=   0
				a_		:	int									=   1
				while j < len(__A   ):
								if pattern[i] == pattern[j]:
												i += 1
								elif i > 0:
												a_		:	List[Any]									=   failure[i - 1]
												continue
								j += 1
								failure.append(__A   )
				return failure


if __name__ == "__main__":
		# Test 1)
		__lowerCAmelCase									=   'abc1abc12'
		__lowerCAmelCase									=   'alskfjaldsabc1abc1abc12k23adsfabcabc'
		__lowerCAmelCase									=   'alskfjaldsk23adsfabcabc'
		assert kmp(pattern, texta) and not kmp(pattern, texta)

		# Test 2)
		__lowerCAmelCase									=   'ABABX'
		__lowerCAmelCase									=   'ABABZABABYABABX'
		assert kmp(pattern, text)

		# Test 3)
		__lowerCAmelCase									=   'AAAB'
		__lowerCAmelCase									=   'ABAAAAAB'
		assert kmp(pattern, text)

		# Test 4)
		__lowerCAmelCase									=   'abcdabcy'
		__lowerCAmelCase									=   'abcxabcdabxabcdabcdabcy'
		assert kmp(pattern, text)

		# Test 5)
		__lowerCAmelCase									=   'aabaabaaa'
		assert get_failure_array(pattern) == [0, 1, 0, 1, 2, 3, 4, 5, 2]




