


'''simple docstring'''



import numpy as np




def      _a     (						_lowercase :     np.array  ):





     '''simple docstring'''






     return (2 / (1 + np.exp(-2 * vector  ))) - 1


if __name__ == "__main__":
    import doctest

    doctest.testmod()



'''simple docstring'''



from abc import ABC, abstractmethod
from typing import List, Optional



class      a    (		_a	):


     """simple docstring"""






     def __init__(    self					:		Optional[Any]      )						->							int:
          # test for the above condition
          self.test()

     def 			lowerCamelCase__			(    self					:		Dict      )						->							List[str]:
          __UpperCAmelCase     :			Optional[int]  							=      0
          __UpperCAmelCase     :			Any  							=      False
          while not completed:
               if counter == 1:
                    self.reset()
               __UpperCAmelCase     :			Optional[int]  							=      self.advance()
               if not self.does_advance(snake_case      ):
                    raise Exception(
                        '''Custom Constraint is not defined correctly. self.does_advance(self.advance()) must be true.'''      )

               __UpperCAmelCase							,       __UpperCAmelCase							,       __UpperCAmelCase     :			int  							=      self.update(snake_case      )
               counter += 1

               if counter > 1_0000:
                    raise Exception('''update() does not fulfill the constraint.'''      )

          if self.remaining() != 0:
               raise Exception('''Custom Constraint is not defined correctly.'''      )

     @abstractmethod
     def 			lowerCamelCase__			(    self					:		Union[str, Any]      )						->							List[str]:
          raise NotImplementedError(
              f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.'      )

     @abstractmethod
     def 			lowerCamelCase__			(    self					:		Optional[int]							,			snake_case					:		int      )						->							Optional[int]:
          raise NotImplementedError(
              f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.'      )

     @abstractmethod
     def 			lowerCamelCase__			(    self					:		List[Any]							,			snake_case					:		int      )						->							int:
          raise NotImplementedError(
              f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.'      )

     @abstractmethod
     def 			lowerCamelCase__			(    self					:		int      )						->							Optional[int]:
          raise NotImplementedError(
              f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.'      )

     @abstractmethod
     def 			lowerCamelCase__			(    self					:		int      )						->							Tuple:
          raise NotImplementedError(
              f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.'      )






     @abstractmethod
     def 			lowerCamelCase__			(    self					:		Union[str, Any]							,			snake_case					:		List[Any]=False      )						->							Any:
          raise NotImplementedError(
              f'{self.__class__} is an abstract class. Only classes inheriting this class can be called.'      )



class      a    (		_a	):


     """simple docstring"""






     def __init__(    self					:		int							,			snake_case					:		List[int]      )						->							Tuple:
          super(snake_case							,			self      ).__init__()

          if not isinstance(snake_case							,			snake_case      ) or len(snake_case      ) == 0:
               raise ValueError(f'`token_ids` has to be a non-empty list, but is {token_ids}.'      )
          if any((not isinstance(snake_case							,			snake_case      ) or token_id < 0) for token_id in token_ids      ):
               raise ValueError(f'Each list in `token_ids` has to be a list of positive integers, but is {token_ids}.'      )

          __UpperCAmelCase     :			Dict  							=      token_ids

          __UpperCAmelCase     :			Tuple  							=      len(self.token_ids      )
          __UpperCAmelCase     :			List[str]  							=      -1  # the index of the currently fulfilled step
          __UpperCAmelCase     :			int  							=      False

     def 			lowerCamelCase__			(    self					:		List[str]      )						->							str:
          if self.completed:
               return None
          return self.token_ids[self.fulfilled_idx + 1]

     def 			lowerCamelCase__			(    self					:		Any							,			snake_case					:		int      )						->							Optional[int]:
          if not isinstance(snake_case							,			snake_case      ):
               raise ValueError(f'`token_id` has to be an `int`, but is {token_id} of type {type(snake_case      )}'      )

          if self.completed:
               return False

          return token_id == self.token_ids[self.fulfilled_idx + 1]

     def 			lowerCamelCase__			(    self					:		Union[str, Any]							,			snake_case					:		int      )						->							Optional[int]:
          if not isinstance(snake_case							,			snake_case      ):
               raise ValueError(f'`token_id` has to be an `int`, but is {token_id} of type {type(snake_case      )}'      )

          __UpperCAmelCase     :			Any  							=      False
          __UpperCAmelCase     :			Tuple  							=      False
          __UpperCAmelCase     :			Tuple  							=      False

          if self.does_advance(snake_case      ):
               self.fulfilled_idx += 1
               __UpperCAmelCase     :			Union[str, Any]  							=      True
               if self.fulfilled_idx == (self.seqlen - 1):
                    __UpperCAmelCase     :			List[Any]  							=      True
               __UpperCAmelCase     :			Union[str, Any]  							=      completed
          else:
               # failed to make progress.
               __UpperCAmelCase     :			List[str]  							=      True
               self.reset()
          return stepped, completed, reset

     def 			lowerCamelCase__			(    self					:		int      )						->							List[Any]:
          __UpperCAmelCase     :			Union[str, Any]  							=      False
          __UpperCAmelCase     :			Union[str, Any]  							=      0

     def 			lowerCamelCase__			(    self					:		str      )						->							Optional[int]:
          return self.seqlen - (self.fulfilled_idx + 1)






     def 			lowerCamelCase__			(    self					:		int							,			snake_case					:		Dict=False      )						->							List[str]:
          __UpperCAmelCase     :			List[str]  							=      PhrasalConstraint(self.token_ids      )

          if stateful:
               __UpperCAmelCase     :			int  							=      self.seqlen
               __UpperCAmelCase     :			Optional[Any]  							=      self.fulfilled_idx
               __UpperCAmelCase     :			List[Any]  							=      self.completed

          return new_constraint



class      a    :


     """simple docstring"""






     def __init__(    self					:		List[str]							,			snake_case					:		List[List[int]]							,			snake_case					:		Dict=True      )						->							Any:
          __UpperCAmelCase     :			List[Any]  							=      max([len(snake_case      ) for one in nested_token_ids]      )

          __UpperCAmelCase     :			Union[str, Any]  							=      {}
          for token_ids in nested_token_ids:
               __UpperCAmelCase     :			List[str]  							=      root
               for tidx, token_id in enumerate(snake_case      ):
                    if token_id not in level:
                         __UpperCAmelCase     :			List[Any]  							=      {}

                    __UpperCAmelCase     :			Tuple  							=      level[token_id]

          if no_subsets and self.has_subsets(snake_case							,			snake_case      ):
               raise ValueError(
                   '''Each list in `nested_token_ids` can\'t be a complete subset of another list, but is'''
                   f' {nested_token_ids}.'      )

          __UpperCAmelCase     :			Tuple  							=      root

     def 			lowerCamelCase__			(    self					:		Dict							,			snake_case					:		List[str]      )						->							List[Any]:
          __UpperCAmelCase     :			Dict  							=      self.trie

          for current_token in current_seq:
               __UpperCAmelCase     :			List[str]  							=      start[current_token]

          __UpperCAmelCase     :			str  							=      list(start.keys()      )

          return next_tokens

     def 			lowerCamelCase__			(    self					:		Optional[Any]							,			snake_case					:		List[str]      )						->							Any:
          __UpperCAmelCase     :			Optional[Any]  							=      self.next_tokens(snake_case      )

          return len(snake_case      ) == 0

     def 			lowerCamelCase__			(    self					:		Union[str, Any]							,			snake_case					:		int      )						->							Optional[int]:
          __UpperCAmelCase     :			str  							=      list(root.values()      )
          if len(snake_case      ) == 0:
               return 1
          else:
               return sum([self.count_leaves(snake_case      ) for nn in next_nodes]      )






     def 			lowerCamelCase__			(    self					:		Optional[int]							,			snake_case					:		int							,			snake_case					:		Dict      )						->							str:
          __UpperCAmelCase     :			Dict  							=      self.count_leaves(snake_case      )
          return len(snake_case      ) != leaf_count



class      a    (		_a	):


     """simple docstring"""






     def __init__(    self					:		Union[str, Any]							,			snake_case					:		List[List[int]]      )						->							str:
          super(snake_case							,			self      ).__init__()

          if not isinstance(snake_case							,			snake_case      ) or len(snake_case      ) == 0:
               raise ValueError(f'`nested_token_ids` has to be a non-empty list, but is {nested_token_ids}.'      )
          if any(not isinstance(snake_case							,			snake_case      ) for token_ids in nested_token_ids      ):
               raise ValueError(f'`nested_token_ids` has to be a list of lists, but is {nested_token_ids}.'      )
          if any(
              any((not isinstance(snake_case							,			snake_case      ) or token_id < 0) for token_id in token_ids      )
              for token_ids in nested_token_ids      ):
               raise ValueError(
                   f'Each list in `nested_token_ids` has to be a list of positive integers, but is {nested_token_ids}.'      )

          __UpperCAmelCase     :			Optional[int]  							=      DisjunctiveTrie(snake_case      )
          __UpperCAmelCase     :			Tuple  							=      nested_token_ids

          __UpperCAmelCase     :			List[Any]  							=      self.trie.max_height
          __UpperCAmelCase     :			List[Any]  							=      []
          __UpperCAmelCase     :			Union[str, Any]  							=      False

     def 			lowerCamelCase__			(    self					:		int      )						->							List[Any]:
          __UpperCAmelCase     :			Optional[int]  							=      self.trie.next_tokens(self.current_seq      )

          if len(snake_case      ) == 0:
               return None
          else:
               return token_list

     def 			lowerCamelCase__			(    self					:		Tuple							,			snake_case					:		int      )						->							Dict:
          if not isinstance(snake_case							,			snake_case      ):
               raise ValueError(f'`token_id` is supposed to be type `int`, but is {token_id} of type {type(snake_case      )}'      )

          __UpperCAmelCase     :			List[str]  							=      self.trie.next_tokens(self.current_seq      )

          return token_id in next_tokens

     def 			lowerCamelCase__			(    self					:		Any							,			snake_case					:		int      )						->							Tuple:
          if not isinstance(snake_case							,			snake_case      ):
               raise ValueError(f'`token_id` is supposed to be type `int`, but is {token_id} of type {type(snake_case      )}'      )

          __UpperCAmelCase     :			Dict  							=      False
          __UpperCAmelCase     :			Optional[Any]  							=      False
          __UpperCAmelCase     :			str  							=      False

          if self.does_advance(snake_case      ):
               self.current_seq.append(snake_case      )
               __UpperCAmelCase     :			int  							=      True
          else:
               __UpperCAmelCase     :			Optional[Any]  							=      True
               self.reset()

          __UpperCAmelCase     :			Optional[Any]  							=      self.trie.reached_leaf(self.current_seq      )
          __UpperCAmelCase     :			Tuple  							=      completed

          return stepped, completed, reset

     def 			lowerCamelCase__			(    self					:		Dict      )						->							Optional[Any]:
          __UpperCAmelCase     :			str  							=      False
          __UpperCAmelCase     :			Tuple  							=      []

     def 			lowerCamelCase__			(    self					:		Tuple      )						->							Any:
          if self.completed:
               # since this can be completed without reaching max height
               return 0
          else:
               return self.seqlen - len(self.current_seq      )






     def 			lowerCamelCase__			(    self					:		Any							,			snake_case					:		Optional[int]=False      )						->							Tuple:
          __UpperCAmelCase     :			str  							=      DisjunctiveConstraint(self.token_ids      )

          if stateful:
               __UpperCAmelCase     :			Tuple  							=      self.seqlen
               __UpperCAmelCase     :			Dict  							=      self.current_seq
               __UpperCAmelCase     :			str  							=      self.completed

          return new_constraint



class      a    :


     """simple docstring"""






     def __init__(    self					:		Union[str, Any]							,			snake_case					:		List[Constraint]      )						->							Union[str, Any]:
          __UpperCAmelCase     :			Optional[Any]  							=      constraints

          # max # of steps required to fulfill a given constraint
          __UpperCAmelCase     :			int  							=      max([c.seqlen for c in constraints]      )
          __UpperCAmelCase     :			int  							=      len(snake_case      )
          __UpperCAmelCase     :			Optional[int]  							=      False

          self.init_state()

     def 			lowerCamelCase__			(    self					:		List[str]      )						->							List[str]:
          __UpperCAmelCase     :			List[Any]  							=      []
          __UpperCAmelCase     :			List[str]  							=      None
          __UpperCAmelCase     :			str  							=      [constraint.copy(stateful=snake_case      ) for constraint in self.constraints]

     def 			lowerCamelCase__			(    self					:		Optional[int]      )						->							Optional[Any]:
          __UpperCAmelCase     :			Dict  							=      0
          if self.inprogress_constraint:
               # extra points for having a constraint mid-fulfilled
               add += self.max_seqlen - self.inprogress_constraint.remaining()

          return (len(self.complete_constraints      ) * self.max_seqlen) + add

     def 			lowerCamelCase__			(    self					:		Tuple      )						->							int:
          __UpperCAmelCase     :			int  							=      []
          if self.inprogress_constraint is None:
               for constraint in self.pending_constraints:  # "pending" == "unfulfilled yet"
                    __UpperCAmelCase     :			Optional[int]  							=      constraint.advance()
                    if isinstance(snake_case							,			snake_case      ):
                         token_list.append(snake_case      )
                    elif isinstance(snake_case							,			snake_case      ):
                         token_list.extend(snake_case      )
          else:
               __UpperCAmelCase     :			Optional[Any]  							=      self.inprogress_constraint.advance()
               if isinstance(snake_case							,			snake_case      ):
                    token_list.append(snake_case      )
               elif isinstance(snake_case							,			snake_case      ):
                    token_list.extend(snake_case      )

          if len(snake_case      ) == 0:
               return None
          else:
               return token_list

     def 			lowerCamelCase__			(    self					:		List[str]							,			snake_case					:		Optional[List[int]]      )						->							Optional[int]:
          self.init_state()

          if token_ids is not None:
               for token in token_ids:
                    # completes or steps **one** constraint
                    __UpperCAmelCase							,       __UpperCAmelCase     :			Dict  							=      self.add(snake_case      )

                    # the entire list of constraints are fulfilled
                    if self.completed:
                         break

     def 			lowerCamelCase__			(    self					:		List[str]							,			snake_case					:		int      )						->							List[str]:
          if not isinstance(snake_case							,			snake_case      ):
               raise ValueError(f'`token_id` should be an `int`, but is `{token_id}`.'      )

          __UpperCAmelCase							,       __UpperCAmelCase     :			str  							=      False, False

          if self.completed:
               __UpperCAmelCase     :			Union[str, Any]  							=      True
               __UpperCAmelCase     :			str  							=      False
               return complete, stepped

          if self.inprogress_constraint is not None:
               # In the middle of fulfilling a constraint. If the `token_id` *does* makes an incremental progress to current
               # job, simply update the state

               __UpperCAmelCase							,       __UpperCAmelCase							,       __UpperCAmelCase     :			Any  							=      self.inprogress_constraint.update(snake_case      )
               if reset:
                    # 1. If the next token breaks the progress, then we must restart.
                    #     e.g. constraint = "I love pies" and sequence so far is "I love" but `token_id` == "books".

                    #     But that doesn't mean we self.init_state(), since we only reset the state for this particular
                    #     constraint, not the full list of constraints.

                    self.pending_constraints.append(self.inprogress_constraint.copy(stateful=snake_case      )      )
                    __UpperCAmelCase     :			Optional[Any]  							=      None

               if complete:
                    # 2. If the next token completes the constraint, move it to completed list, set
                    #     inprogress to None. If there are no pending constraints either, then this full list of constraints
                    #     is complete.

                    self.complete_constraints.append(self.inprogress_constraint      )
                    __UpperCAmelCase     :			str  							=      None

                    if len(self.pending_constraints      ) == 0:
                         # we're done!
                         __UpperCAmelCase     :			Optional[int]  							=      True

          else:
               # Not in the middle of fulfilling a constraint. So does this `token_id` helps us step towards any of our list
               # of constraints?

               for cidx, pending_constraint in enumerate(self.pending_constraints      ):
                    if pending_constraint.does_advance(snake_case      ):
                         __UpperCAmelCase							,       __UpperCAmelCase							,       __UpperCAmelCase     :			Union[str, Any]  							=      pending_constraint.update(snake_case      )

                         if not stepped:
                              raise Exception(
                                  '''`constraint.update(token_id)` is not yielding incremental progress, '''
                                  '''even though `constraint.does_advance(token_id)` is true.'''      )

                         if complete:
                              self.complete_constraints.append(snake_case      )
                              __UpperCAmelCase     :			Tuple  							=      None

                         if not complete and stepped:
                              __UpperCAmelCase     :			List[Any]  							=      pending_constraint

                         if complete or stepped:
                              # If we made any progress at all, then it's at least not a "pending constraint".

                              __UpperCAmelCase     :			Optional[int]  							=      (
                                  self.pending_constraints[:cidx] + self.pending_constraints[cidx + 1 :]
                              )

                              if len(self.pending_constraints      ) == 0 and self.inprogress_constraint is None:
                                   # If there's no longer any pending after this and no inprogress either, then we must be
                                   # complete.

                                   __UpperCAmelCase     :			Any  							=      True

                              break  # prevent accidentally stepping through multiple constraints with just one token.

          return complete, stepped






     def 			lowerCamelCase__			(    self					:		int							,			snake_case					:		Optional[int]=True      )						->							Optional[int]:
          __UpperCAmelCase     :			Union[str, Any]  							=      ConstraintListState(self.constraints      )  # we actually never though self.constraints objects
          # throughout this process. So it's at initialization state.

          if stateful:
               __UpperCAmelCase     :			str  							=      [
                   constraint.copy(stateful=snake_case      ) for constraint in self.complete_constraints
               ]
               if self.inprogress_constraint is not None:
                    __UpperCAmelCase     :			Union[str, Any]  							=      self.inprogress_constraint.copy(stateful=snake_case      )
               __UpperCAmelCase     :			Tuple  							=      [constraint.copy() for constraint in self.pending_constraints]

          return new_state