



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





class  lowerCamelCase__	(    UpperCAmelCase_):
     """simple docstring"""



     def __init__(self     ):






           '''simple docstring'''



           self.test()







     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		0
           lowerCamelCase             =		False
           while not completed:
                 if counter == 1:
                       self.reset()
                 lowerCamelCase             =		self.advance()
                 if not self.does_advance(__a     ):
                       raise Exception(
                           "Custom Constraint is not defined correctly. self.does_advance(self.advance()) must be true."     )

                 lowerCamelCase      ,				lowerCamelCase      ,				lowerCamelCase             =		self.update(__a     )
                 counter += 1

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

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







     @abstractmethod
     def     _a   (self     ):






           '''simple docstring'''



           raise NotImplementedError(
               F"""{self.__class__} is an abstract class. Only classes inheriting this class can be called."""     )







     @abstractmethod
     def     _a   (self					,      __a     ):






           '''simple docstring'''



           raise NotImplementedError(
               F"""{self.__class__} is an abstract class. Only classes inheriting this class can be called."""     )







     @abstractmethod
     def     _a   (self					,      __a     ):






           '''simple docstring'''



           raise NotImplementedError(
               F"""{self.__class__} is an abstract class. Only classes inheriting this class can be called."""     )







     @abstractmethod
     def     _a   (self     ):






           '''simple docstring'''



           raise NotImplementedError(
               F"""{self.__class__} is an abstract class. Only classes inheriting this class can be called."""     )







     @abstractmethod
     def     _a   (self     ):






           '''simple docstring'''



           raise NotImplementedError(
               F"""{self.__class__} is an abstract class. Only classes inheriting this class can be called."""     )







     @abstractmethod
     def     _a   (self					,      __a=False     ):






           '''simple docstring'''



           raise NotImplementedError(
               F"""{self.__class__} is an abstract class. Only classes inheriting this class can be called."""     )





class  lowerCamelCase__	(    UpperCAmelCase_):
     """simple docstring"""



     def __init__(self					,      __a     ):






           '''simple docstring'''



           super(__a					,      self     ).__init__()

           if not isinstance(__a					,      __a     ) or len(__a     ) == 0:
                 raise ValueError(F"""`token_ids` has to be a non-empty list, but is {token_ids}."""     )
           if any((not isinstance(__a					,      __a     ) 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}."""     )

           lowerCamelCase             =		token_ids

           lowerCamelCase             =		len(self.token_ids     )
           lowerCamelCase             =		-1  # the index of the currently fulfilled step
           lowerCamelCase             =		False







     def     _a   (self     ):






           '''simple docstring'''



           if self.completed:
                 return None
           return self.token_ids[self.fulfilled_idx + 1]







     def     _a   (self					,      __a     ):






           '''simple docstring'''



           if not isinstance(__a					,      __a     ):
                 raise ValueError(F"""`token_id` has to be an `int`, but is {token_id} of type {type(__a     )}"""     )

           if self.completed:
                 return False

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







     def     _a   (self					,      __a     ):






           '''simple docstring'''



           if not isinstance(__a					,      __a     ):
                 raise ValueError(F"""`token_id` has to be an `int`, but is {token_id} of type {type(__a     )}"""     )

           lowerCamelCase             =		False
           lowerCamelCase             =		False
           lowerCamelCase             =		False

           if self.does_advance(__a     ):
                 self.fulfilled_idx += 1
                 lowerCamelCase             =		True
                 if self.fulfilled_idx == (self.seqlen - 1):
                       lowerCamelCase             =		True
                 lowerCamelCase             =		completed
           else:
                 # failed to make progress.
                 lowerCamelCase             =		True
                 self.reset()
           return stepped, completed, reset







     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		False
           lowerCamelCase             =		0







     def     _a   (self     ):






           '''simple docstring'''



           return self.seqlen - (self.fulfilled_idx + 1)







     def     _a   (self					,      __a=False     ):






           '''simple docstring'''



           lowerCamelCase             =		PhrasalConstraint(self.token_ids     )

           if stateful:
                 lowerCamelCase             =		self.seqlen
                 lowerCamelCase             =		self.fulfilled_idx
                 lowerCamelCase             =		self.completed

           return new_constraint





class  lowerCamelCase__	:
     """simple docstring"""



     def __init__(self					,      __a					,      __a=True     ):






           '''simple docstring'''



           lowerCamelCase             =		max([len(__a     ) for one in nested_token_ids]     )

           lowerCamelCase             =		{}
           for token_ids in nested_token_ids:
                 lowerCamelCase             =		root
                 for tidx, token_id in enumerate(__a     ):
                       if token_id not in level:
                             lowerCamelCase             =		{}

                       lowerCamelCase             =		level[token_id]

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

           lowerCamelCase             =		root







     def     _a   (self					,      __a     ):






           '''simple docstring'''



           lowerCamelCase             =		self.trie

           for current_token in current_seq:
                 lowerCamelCase             =		start[current_token]

           lowerCamelCase             =		list(start.keys()     )

           return next_tokens







     def     _a   (self					,      __a     ):






           '''simple docstring'''



           lowerCamelCase             =		self.next_tokens(__a     )

           return len(__a     ) == 0







     def     _a   (self					,      __a     ):






           '''simple docstring'''



           lowerCamelCase             =		list(root.values()     )
           if len(__a     ) == 0:
                 return 1
           else:
                 return sum([self.count_leaves(__a     ) for nn in next_nodes]     )







     def     _a   (self					,      __a					,      __a     ):






           '''simple docstring'''



           lowerCamelCase             =		self.count_leaves(__a     )
           return len(__a     ) != leaf_count





class  lowerCamelCase__	(    UpperCAmelCase_):
     """simple docstring"""



     def __init__(self					,      __a     ):






           '''simple docstring'''



           super(__a					,      self     ).__init__()

           if not isinstance(__a					,      __a     ) or len(__a     ) == 0:
                 raise ValueError(F"""`nested_token_ids` has to be a non-empty list, but is {nested_token_ids}."""     )
           if any(not isinstance(__a					,      __a     ) 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(__a					,      __a     ) 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}."""     )

           lowerCamelCase             =		DisjunctiveTrie(__a     )
           lowerCamelCase             =		nested_token_ids

           lowerCamelCase             =		self.trie.max_height
           lowerCamelCase             =		[]
           lowerCamelCase             =		False







     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		self.trie.next_tokens(self.current_seq     )

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







     def     _a   (self					,      __a     ):






           '''simple docstring'''



           if not isinstance(__a					,      __a     ):
                 raise ValueError(F"""`token_id` is supposed to be type `int`, but is {token_id} of type {type(__a     )}"""     )

           lowerCamelCase             =		self.trie.next_tokens(self.current_seq     )

           return token_id in next_tokens







     def     _a   (self					,      __a     ):






           '''simple docstring'''



           if not isinstance(__a					,      __a     ):
                 raise ValueError(F"""`token_id` is supposed to be type `int`, but is {token_id} of type {type(__a     )}"""     )

           lowerCamelCase             =		False
           lowerCamelCase             =		False
           lowerCamelCase             =		False

           if self.does_advance(__a     ):
                 self.current_seq.append(__a     )
                 lowerCamelCase             =		True
           else:
                 lowerCamelCase             =		True
                 self.reset()

           lowerCamelCase             =		self.trie.reached_leaf(self.current_seq     )
           lowerCamelCase             =		completed

           return stepped, completed, reset







     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		False
           lowerCamelCase             =		[]







     def     _a   (self     ):






           '''simple docstring'''



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







     def     _a   (self					,      __a=False     ):






           '''simple docstring'''



           lowerCamelCase             =		DisjunctiveConstraint(self.token_ids     )

           if stateful:
                 lowerCamelCase             =		self.seqlen
                 lowerCamelCase             =		self.current_seq
                 lowerCamelCase             =		self.completed

           return new_constraint





class  lowerCamelCase__	:
     """simple docstring"""



     def __init__(self					,      __a     ):






           '''simple docstring'''



           lowerCamelCase             =		constraints

           # max # of steps required to fulfill a given constraint
           lowerCamelCase             =		max([c.seqlen for c in constraints]     )
           lowerCamelCase             =		len(__a     )
           lowerCamelCase             =		False

           self.init_state()







     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		[]
           lowerCamelCase             =		None
           lowerCamelCase             =		[constraint.copy(stateful=__a     ) for constraint in self.constraints]







     def     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		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     _a   (self     ):






           '''simple docstring'''



           lowerCamelCase             =		[]
           if self.inprogress_constraint is None:
                 for constraint in self.pending_constraints:  # "pending" == "unfulfilled yet"
                       lowerCamelCase             =		constraint.advance()
                       if isinstance(__a					,      __a     ):
                             token_list.append(__a     )
                       elif isinstance(__a					,      __a     ):
                             token_list.extend(__a     )
           else:
                 lowerCamelCase             =		self.inprogress_constraint.advance()
                 if isinstance(__a					,      __a     ):
                       token_list.append(__a     )
                 elif isinstance(__a					,      __a     ):
                       token_list.extend(__a     )

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







     def     _a   (self					,      __a     ):






           '''simple docstring'''



           self.init_state()

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

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







     def     _a   (self					,      __a     ):






           '''simple docstring'''



           if not isinstance(__a					,      __a     ):
                 raise ValueError(F"""`token_id` should be an `int`, but is `{token_id}`."""     )

           lowerCamelCase      ,				lowerCamelCase             =		False, False

           if self.completed:
                 lowerCamelCase             =		True
                 lowerCamelCase             =		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

                 lowerCamelCase      ,				lowerCamelCase      ,				lowerCamelCase             =		self.inprogress_constraint.update(__a     )
                 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=__a     )     )
                       lowerCamelCase             =		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     )
                       lowerCamelCase             =		None

                       if len(self.pending_constraints     ) == 0:
                             # we're done!
                             lowerCamelCase             =		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(__a     ):
                             lowerCamelCase      ,				lowerCamelCase      ,				lowerCamelCase             =		pending_constraint.update(__a     )

                             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(__a     )
                                   lowerCamelCase             =		None

                             if not complete and stepped:
                                   lowerCamelCase             =		pending_constraint

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

                                   lowerCamelCase             =		(
                                       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.

                                         lowerCamelCase             =		True

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

           return complete, stepped







     def     _a   (self					,      __a=True     ):






           '''simple docstring'''



           lowerCamelCase             =		ConstraintListState(self.constraints     )  # we actually never though self.constraints objects
           # throughout this process. So it's at initialization state.

           if stateful:
                 lowerCamelCase             =		[
                     constraint.copy(stateful=__a     ) for constraint in self.complete_constraints
                 ]
                 if self.inprogress_constraint is not None:
                       lowerCamelCase             =		self.inprogress_constraint.copy(stateful=__a     )
                 lowerCamelCase             =		[constraint.copy() for constraint in self.pending_constraints]

           return new_state




a_    :  Dict						  =		8.314462  # Unit - J mol-1 K-1

def      __lowercase(						UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__			):


      """simple docstring"""



      if moles < 0 or kelvin < 0 or volume < 0:
            raise ValueError("Invalid inputs. Enter positive value."			)
      return moles * kelvin * UNIVERSAL_GAS_CONSTANT / volume

def      __lowercase(						UpperCAmelCase__ , UpperCAmelCase__ , UpperCAmelCase__			):


      """simple docstring"""



      if moles < 0 or kelvin < 0 or pressure < 0:
            raise ValueError("Invalid inputs. Enter positive value."			)
      return moles * kelvin * UNIVERSAL_GAS_CONSTANT / pressure


if __name__ == "__main__":
     from doctest import testmod

     testmod()