



"""simple docstring"""






from collections.abc import Iterator, MutableMapping
from dataclasses import dataclass
from typing import Generic, TypeVar

__snake_case							:   Any      =    TypeVar('KEY')
__snake_case							:   int      =    TypeVar('VAL')



@dataclass(frozen=__SCREAMING_SNAKE_CASE  ,		slots=__SCREAMING_SNAKE_CASE							)
class 			A__   (  Generic[KEY, VAL]							):




	'''simple docstring'''





	SCREAMING_SNAKE_CASE          =	42
	SCREAMING_SNAKE_CASE          =	42






class 			A__   (  _Item							):




	'''simple docstring'''





	def __init__(     self:		Any)				->       None:

				"""simple docstring"""

				super().__init__(_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE)







	def __bool__(     self:		Any)				->       bool:

				"""simple docstring"""

				return False






__snake_case							:   List[str]      =    _DeletedItem()



class 			A__   (  MutableMapping[KEY, VAL]							):




	'''simple docstring'''





	def __init__(     self:		Any       ,			_SCREAMING_SNAKE_CASE:		int = 8       ,			_SCREAMING_SNAKE_CASE:		float = 0.75)				->       None:

				"""simple docstring"""

				__lowerCAmelCase      :  List[str]												=       initial_block_size
				__lowerCAmelCase      :  list[_Item | None]												=       [None] * initial_block_size
				assert 0.0 < capacity_factor < 1.0
				__lowerCAmelCase      :  int												=       capacity_factor
				__lowerCAmelCase      :  Any												=       0

	def 							_SCREAMING_SNAKE_CASE   (     self:		Union[str, Any]       ,			_SCREAMING_SNAKE_CASE:		KEY)				->       int:

				"""simple docstring"""

				return hash(_SCREAMING_SNAKE_CASE) % len(self._buckets)

	def 							_SCREAMING_SNAKE_CASE   (     self:		Union[str, Any]       ,			_SCREAMING_SNAKE_CASE:		int)				->       int:

				"""simple docstring"""

				return (ind + 1) % len(self._buckets)

	def 							_SCREAMING_SNAKE_CASE   (     self:		Tuple       ,			_SCREAMING_SNAKE_CASE:		int       ,			_SCREAMING_SNAKE_CASE:		KEY       ,			_SCREAMING_SNAKE_CASE:		VAL)				->       bool:

				"""simple docstring"""

				__lowerCAmelCase      :  Tuple												=       self._buckets[ind]
				if not stored:
							__lowerCAmelCase      :  List[Any]												=       _Item(_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE)
							self._len += 1
							return True
				elif stored.key == key:
							__lowerCAmelCase      :  List[str]												=       _Item(_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE)
							return True
				else:
							return False

	def 							_SCREAMING_SNAKE_CASE   (     self:		Optional[int])				->       bool:

				"""simple docstring"""

				__lowerCAmelCase      :  str												=       len(self._buckets) * self._capacity_factor
				return len(self) >= int(_SCREAMING_SNAKE_CASE)

	def 							_SCREAMING_SNAKE_CASE   (     self:		Tuple)				->       bool:

				"""simple docstring"""

				if len(self._buckets) <= self._initial_block_size:
							return False
				__lowerCAmelCase      :  List[Any]												=       len(self._buckets) * self._capacity_factor / 2
				return len(self) < limit

	def 							_SCREAMING_SNAKE_CASE   (     self:		Optional[Any]       ,			_SCREAMING_SNAKE_CASE:		int)				->       None:

				"""simple docstring"""

				__lowerCAmelCase      :  Any												=       self._buckets
				__lowerCAmelCase      :  List[str]												=       [None] * new_size
				__lowerCAmelCase      :  int												=       0
				for item in old_buckets:
							if item:
										self._add_item(item.key       ,			item.val)

	def 							_SCREAMING_SNAKE_CASE   (     self:		List[Any])				->       None:

				"""simple docstring"""

				self._resize(len(self._buckets) * 2)

	def 							_SCREAMING_SNAKE_CASE   (     self:		List[str])				->       None:

				"""simple docstring"""

				self._resize(len(self._buckets) // 2)

	def 							_SCREAMING_SNAKE_CASE   (     self:		List[Any]       ,			_SCREAMING_SNAKE_CASE:		KEY)				->       Iterator[int]:

				"""simple docstring"""

				__lowerCAmelCase      :  Optional[int]												=       self._get_bucket_index(_SCREAMING_SNAKE_CASE)
				for _ in range(len(self._buckets)):
							yield ind
							__lowerCAmelCase      :  Tuple												=       self._get_next_ind(_SCREAMING_SNAKE_CASE)

	def 							_SCREAMING_SNAKE_CASE   (     self:		str       ,			_SCREAMING_SNAKE_CASE:		KEY       ,			_SCREAMING_SNAKE_CASE:		VAL)				->       None:

				"""simple docstring"""

				for ind in self._iterate_buckets(_SCREAMING_SNAKE_CASE):
							if self._try_set(_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE):
										break

	def __setitem__(     self:		str       ,			_SCREAMING_SNAKE_CASE:		KEY       ,			_SCREAMING_SNAKE_CASE:		VAL)				->       None:

				"""simple docstring"""

				if self._is_full():
							self._size_up()

				self._add_item(_SCREAMING_SNAKE_CASE       ,			_SCREAMING_SNAKE_CASE)

	def __delitem__(     self:		Optional[int]       ,			_SCREAMING_SNAKE_CASE:		KEY)				->       None:

				"""simple docstring"""

				for ind in self._iterate_buckets(_SCREAMING_SNAKE_CASE):
							__lowerCAmelCase      :  List[Any]												=       self._buckets[ind]
							if item is None:
										raise KeyError(_SCREAMING_SNAKE_CASE)
							if item is _deleted:
										continue
							if item.key == key:
										__lowerCAmelCase      :  Dict												=       _deleted
										self._len -= 1
										break
				if self._is_sparse():
							self._size_down()

	def __getitem__(     self:		List[str]       ,			_SCREAMING_SNAKE_CASE:		KEY)				->       VAL:

				"""simple docstring"""

				for ind in self._iterate_buckets(_SCREAMING_SNAKE_CASE):
							__lowerCAmelCase      :  str												=       self._buckets[ind]
							if item is None:
										break
							if item is _deleted:
										continue
							if item.key == key:
										return item.val
				raise KeyError(_SCREAMING_SNAKE_CASE)

	def __len__(     self:		int)				->       int:

				"""simple docstring"""

				return self._len

	def __iter__(     self:		Optional[Any])				->       Iterator[KEY]:

				"""simple docstring"""

				yield from (item.key for item in self._buckets if item)







	def __repr__(     self:		Optional[Any])				->       str:

				"""simple docstring"""

				__lowerCAmelCase      :  Tuple												=       " ,".join(
				    F"""{item.key}: {item.val}""" for item in self._buckets if item)
				return F"""HashMap({val_string})"""




"""simple docstring"""






from __future__ import annotations

import time

import numpy as np

__snake_case							:   Optional[Any]      =    [8, 5, 9, 7]
__snake_case							:   List[Any]      =    [
    [2, 0, 1, 1],
    [0, 1, 2, 1],
    [4, 0, 0, 3],
    [0, 2, 1, 0],
    [1, 0, 3, 0],
]
__snake_case							:   Optional[int]      =    [
    [3, 2, 1, 4],
    [0, 2, 5, 2],
    [5, 1, 0, 5],
    [1, 5, 3, 0],
    [3, 0, 3, 3],
]



class 			A__   :




	'''simple docstring'''





	def __init__(     self:		Any       ,			_SCREAMING_SNAKE_CASE:		list[int]       ,			_SCREAMING_SNAKE_CASE:		list[list[int]]       ,			_SCREAMING_SNAKE_CASE:		list[list[int]]       ,			)				->       None:

				"""simple docstring"""

				__lowerCAmelCase      :  Any												=       claim_vector
				__lowerCAmelCase      :  Tuple												=       allocated_resources_table
				__lowerCAmelCase      :  Tuple												=       maximum_claim_table

	def 							_SCREAMING_SNAKE_CASE   (     self:		Union[str, Any])				->       list[int]:

				"""simple docstring"""

				return [
				    sum(p_item[i] for p_item in self.__allocated_resources_table)
				    for i in range(len(self.__allocated_resources_table[0]))
				]

	def 							_SCREAMING_SNAKE_CASE   (     self:		int)				->       list[int]:

				"""simple docstring"""

				return np.array(self.__claim_vector) - np.array(
				    self.__processes_resource_summation())

	def 							_SCREAMING_SNAKE_CASE   (     self:		int)				->       list[list[int]]:

				"""simple docstring"""

				return [
				    list(np.array(self.__maximum_claim_table[i]) - np.array(_SCREAMING_SNAKE_CASE))
				    for i, allocated_resource in enumerate(self.__allocated_resources_table)
				]

	def 							_SCREAMING_SNAKE_CASE   (     self:		List[Any])				->       dict[int, list[int]]:

				"""simple docstring"""

				return {self.__need().index(_SCREAMING_SNAKE_CASE): i for i in self.__need()}

	def 							_SCREAMING_SNAKE_CASE   (     self:		Optional[Any]       ,			**_SCREAMING_SNAKE_CASE:		List[Any])				->       None:

				"""simple docstring"""

				__lowerCAmelCase      :  Optional[int]												=       self.__need()
				__lowerCAmelCase      :  int												=       self.__allocated_resources_table
				__lowerCAmelCase      :  Dict												=       self.__available_resources()
				__lowerCAmelCase      :  str												=       self.__need_index_manager()
				for kw, val in kwargs.items():
							if kw and val is True:
										self.__pretty_data()
				print("_" * 50 + "\n")
				while need_list:
							__lowerCAmelCase      :  int												=       False
							for each_need in need_list:
										__lowerCAmelCase      :  Dict												=       True
										for index, need in enumerate(_SCREAMING_SNAKE_CASE):
													if need > available_resources[index]:
																__lowerCAmelCase      :  Dict												=       False
																break
										if execution:
													__lowerCAmelCase      :  Any												=       True
													# get the original index of the process from ind_ctrl db
													for original_need_index, need_clone in need_index_manager.items():
																if each_need == need_clone:
																			__lowerCAmelCase      :  Union[str, Any]												=       original_need_index
													print(F"""Process {process_number + 1} is executing.""")
													# remove the process run from stack
													need_list.remove(_SCREAMING_SNAKE_CASE)
													# update available/freed resources stack
													__lowerCAmelCase      :  Dict												=       np.array(_SCREAMING_SNAKE_CASE) + np.array(
													    alloc_resources_table[process_number])
													print(
													    "Updated available resource stack for processes: "
													    + " ".join([str(_SCREAMING_SNAKE_CASE) for x in available_resources]))
													break
							if safe:
										print("The process is in a safe state.\n")
							else:
										print("System in unsafe state. Aborting...\n")
										break







	def 							_SCREAMING_SNAKE_CASE   (     self:		Union[str, Any])				->       List[Any]:

				"""simple docstring"""

				print(" " * 9 + "Allocated Resource Table")
				for item in self.__allocated_resources_table:
							print(
							    F"""P{self.__allocated_resources_table.index(_SCREAMING_SNAKE_CASE) + 1}"""
							    + " ".join(F"""{it:>8}""" for it in item)
							    + "\n")
				print(" " * 9 + "System Resource Table")
				for item in self.__maximum_claim_table:
							print(
							    F"""P{self.__maximum_claim_table.index(_SCREAMING_SNAKE_CASE) + 1}"""
							    + " ".join(F"""{it:>8}""" for it in item)
							    + "\n")
				print(
				    "Current Usage by Active Processes: "
				    + " ".join(str(_SCREAMING_SNAKE_CASE) for x in self.__claim_vector))
				print(
				    "Initial Available Resources:       "
				    + " ".join(str(_SCREAMING_SNAKE_CASE) for x in self.__available_resources()))
				time.sleep(1)


if __name__ == "__main__":
	import doctest

	doctest.testmod()