






"""simple docstring"""




import logging
import os

from .state import PartialState






class        UpperCamelCase			(	logging.LoggerAdapter      ):


 @staticmethod
 def _lowercase	(     UpperCAmelCase__		:						Dict							)					->       Optional[Any]:
   _a  :       Tuple             =	PartialState()
   return not main_process_only or (main_process_only and state.is_main_process)







 def _lowercase	(     self		:						Union[str, Any]							,						UpperCAmelCase__		:						int							,						UpperCAmelCase__		:						Optional[int]							,						*UpperCAmelCase__		:						str							,						**UpperCAmelCase__		:						Union[str, Any]							)					->       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."""							)
   _a  :       Dict             =	kwargs.pop("""main_process_only"""							,						__UpperCAmelCase							)
   _a  :       str             =	kwargs.pop("""in_order"""							,						__UpperCAmelCase							)

   if self.isEnabledFor(__UpperCAmelCase							):
     if self._should_log(__UpperCAmelCase							):
       _a      ,  _a  :       Optional[Any]             =	self.process(__UpperCAmelCase							,						__UpperCAmelCase							)
       self.logger.log(__UpperCAmelCase							,						__UpperCAmelCase							,						*__UpperCAmelCase							,						**__UpperCAmelCase							)

     elif in_order:
       _a  :       str             =	PartialState()
       for i in range(state.num_processes							):
         if i == state.process_index:
           _a      ,  _a  :       List[Any]             =	self.process(__UpperCAmelCase							,						__UpperCAmelCase							)
           self.logger.log(__UpperCAmelCase							,						__UpperCAmelCase							,						*__UpperCAmelCase							,						**__UpperCAmelCase							)
         state.wait_for_everyone()







def 			lowerCAmelCase__     (     UpperCamelCase__     ,   UpperCamelCase__ = None				):



  '''simple docstring'''


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


from __future__ import annotations

from collections import deque





class      A							:


   def __init__(self     :						Dict       ,  __UpperCAmelCase     :						list[str]		)						->				Optional[int]:
         """simple docstring"""



         UpperCAmelCase__								=  []
         self.adlist.append(
             {"value": "", "next_states": [], "fail_state": 0, "output": []}		)

         for keyword in keywords:
               self.add_keyword(__UpperCAmelCase		)
         self.set_fail_transitions()


   def 			lowercase_      (self     :						Tuple       ,  __UpperCAmelCase     :						int       ,  __UpperCAmelCase     :						str		)						->				int | None:
         """simple docstring"""



         for state in self.adlist[current_state]["next_states"]:
               if char == self.adlist[state]["value"]:
                     return state
         return None


   def 			lowercase_      (self     :						Dict       ,  __UpperCAmelCase     :						str		)						->				None:
         """simple docstring"""



         UpperCAmelCase__								=  0
         for character in keyword:
               UpperCAmelCase__								=  self.find_next_state(__UpperCAmelCase       ,  __UpperCAmelCase		)
               if next_state is None:
                     self.adlist.append(
                         {
                             "value": character,
                             "next_states": [],
                             "fail_state": 0,
                             "output": [],
                         }		)
                     self.adlist[current_state]["next_states"].append(len(self.adlist		) - 1		)
                     UpperCAmelCase__								=  len(self.adlist		) - 1
               else:
                     UpperCAmelCase__								=  next_state
         self.adlist[current_state]["output"].append(__UpperCAmelCase		)


   def 			lowercase_      (self     :						Optional[int]		)						->				None:
         """simple docstring"""



         UpperCAmelCase__								=  deque()
         for node in self.adlist[0]["next_states"]:
               q.append(__UpperCAmelCase		)
               UpperCAmelCase__								=  0
         while q:
               UpperCAmelCase__								=  q.popleft()
               for child in self.adlist[r]["next_states"]:
                     q.append(__UpperCAmelCase		)
                     UpperCAmelCase__								=  self.adlist[r]["fail_state"]
                     while (
                         self.find_next_state(__UpperCAmelCase       ,  self.adlist[child]["value"]		) is None
                         and state != 0
                     ):
                           UpperCAmelCase__								=  self.adlist[state]["fail_state"]
                     UpperCAmelCase__								=  self.find_next_state(
                         __UpperCAmelCase       ,  self.adlist[child]["value"]		)
                     if self.adlist[child]["fail_state"] is None:
                           UpperCAmelCase__								=  0
                     UpperCAmelCase__								=  (
                         self.adlist[child]["output"]
                         + self.adlist[self.adlist[child]["fail_state"]]["output"]
                     )





   def 			lowercase_      (self     :						Union[str, Any]       ,  __UpperCAmelCase     :						str		)						->				dict[str, list[int]]:
         """simple docstring"""



         UpperCAmelCase__								=  {}  # returns a dict with keywords and list of its occurrences
         UpperCAmelCase__								=  0
         for i in range(len(__UpperCAmelCase		)		):
               while (
                   self.find_next_state(__UpperCAmelCase       ,  string[i]		) is None
                   and current_state != 0
               ):
                     UpperCAmelCase__								=  self.adlist[current_state]["fail_state"]
               UpperCAmelCase__								=  self.find_next_state(__UpperCAmelCase       ,  string[i]		)
               if next_state is None:
                     UpperCAmelCase__								=  0
               else:
                     UpperCAmelCase__								=  next_state
                     for key in self.adlist[current_state]["output"]:
                           if key not in result:
                                 UpperCAmelCase__								=  []
                           result[key].append(i - len(__UpperCAmelCase		) + 1		)
         return result


if __name__ == "__main__":
      import doctest

      doctest.testmod()



