'''simple docstring'''


from collections import deque






class 					__SCREAMING_SNAKE_CASE						:







  '''simple docstring'''






  def __init__(			self				,		snake_case_				,		snake_case_				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       int       =				process_name  # process name
       UpperCAmelCase_							:       str       =				arrival_time  # arrival time of the process
       # completion time of finished process or last interrupted time
       UpperCAmelCase_							:       Optional[int]       =				arrival_time
       UpperCAmelCase_							:       Any       =				burst_time  # remaining burst time
       UpperCAmelCase_							:       List[str]       =				0  # total time of the process wait in ready queue
       UpperCAmelCase_							:       Tuple       =				0  # time from arrival time to completion time







class 					__SCREAMING_SNAKE_CASE						:







  '''simple docstring'''






  def __init__(			self				,		snake_case_				,		snake_case_				,		snake_case_				,		snake_case_				,		):




       '''simple docstring'''

       UpperCAmelCase_							:       int       =				number_of_queues
       # time slice of queues that round robin algorithm applied
       UpperCAmelCase_							:       str       =				time_slices
       # unfinished process is in this ready_queue
       UpperCAmelCase_							:       Optional[int]       =				queue
       # current time
       UpperCAmelCase_							:       Dict       =				current_time
       # finished process is in this sequence queue
       UpperCAmelCase_							:       deque[Process]       =				deque()


  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       UpperCAmelCase_							:       Tuple       =				[]
       for i in range(len(self.finish_queue						)						):
            sequence.append(self.finish_queue[i].process_name						)
       return sequence


  def _UpperCamelCase					(			self				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       Tuple       =				[]
       for i in range(len(snake_case_						)						):
            waiting_times.append(queue[i].waiting_time						)
       return waiting_times


  def _UpperCamelCase					(			self				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       Optional[int]       =				[]
       for i in range(len(snake_case_						)						):
            turnaround_times.append(queue[i].turnaround_time						)
       return turnaround_times


  def _UpperCamelCase					(			self				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       Union[str, Any]       =				[]
       for i in range(len(snake_case_						)						):
            completion_times.append(queue[i].stop_time						)
       return completion_times


  def _UpperCamelCase					(			self				,		snake_case_						):




       '''simple docstring'''

       return [q.burst_time for q in queue]


  def _UpperCamelCase					(			self				,		snake_case_						):




       '''simple docstring'''

       process.waiting_time += self.current_time - process.stop_time
       return process.waiting_time


  def _UpperCamelCase					(			self				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       deque[Process]       =				deque()  # sequence deque of finished process
       while len(snake_case_						) != 0:
            UpperCAmelCase_							:       Tuple       =				ready_queue.popleft()  # current process

            # if process's arrival time is later than current time, update current time
            if self.current_time < cp.arrival_time:
                 self.current_time += cp.arrival_time

            # update waiting time of current process
            self.update_waiting_time(snake_case_						)
            # update current time
            self.current_time += cp.burst_time
            # finish the process and set the process's burst-time 0
            UpperCAmelCase_							:       int       =				0
            # set the process's turnaround time because it is finished
            UpperCAmelCase_							:       Optional[int]       =				self.current_time - cp.arrival_time
            # set the completion time
            UpperCAmelCase_							:       Optional[int]       =				self.current_time
            # add the process to queue that has finished queue
            finished.append(snake_case_						)

       self.finish_queue.extend(snake_case_						)  # add finished process to finish queue
       # FCFS will finish all remaining processes
       return finished


  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       deque[Process]       =				deque()  # sequence deque of terminated process
       # just for 1 cycle and unfinished processes will go back to queue
       for _ in range(len(snake_case_						)						):
            UpperCAmelCase_							:       Optional[Any]       =				ready_queue.popleft()  # current process

            # if process's arrival time is later than current time, update current time
            if self.current_time < cp.arrival_time:
                 self.current_time += cp.arrival_time

            # update waiting time of unfinished processes
            self.update_waiting_time(snake_case_						)
            # if the burst time of process is bigger than time-slice
            if cp.burst_time > time_slice:
                 # use CPU for only time-slice
                 self.current_time += time_slice
                 # update remaining burst time
                 cp.burst_time -= time_slice
                 # update end point time
                 UpperCAmelCase_							:       str       =				self.current_time
                 # locate the process behind the queue because it is not finished
                 ready_queue.append(snake_case_						)
            else:
                 # use CPU for remaining burst time
                 self.current_time += cp.burst_time
                 # set burst time 0 because the process is finished
                 UpperCAmelCase_							:       List[Any]       =				0
                 # set the finish time
                 UpperCAmelCase_							:       Union[str, Any]       =				self.current_time
                 # update the process' turnaround time because it is finished
                 UpperCAmelCase_							:       Union[str, Any]       =				self.current_time - cp.arrival_time
                 # add the process to queue that has finished queue
                 finished.append(snake_case_						)

       self.finish_queue.extend(snake_case_						)  # add finished process to finish queue
       # return finished processes queue and remaining processes queue
       return finished, ready_queue






  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       for i in range(self.number_of_queues - 1						):
            UpperCAmelCase_							:       Tuple       =				self.round_robin(
                self.ready_queue				,		self.time_slices[i]						)
       #  the last queue has first_come_first_served algorithm
       self.first_come_first_served(self.ready_queue						)

       return self.finish_queue


if __name__ == "__main__":
 import doctest

 snake_case__					:      List[str]				      =  Process('''P1''', 0, 53)
 snake_case__					:      int				      =  Process('''P2''', 0, 17)
 snake_case__					:      Optional[Any]				      =  Process('''P3''', 0, 68)
 snake_case__					:      Optional[Any]				      =  Process('''P4''', 0, 24)
 snake_case__					:      List[str]				      =  3
 snake_case__					:      List[str]				      =  [17, 25]
 snake_case__					:      str				      =  deque([Pa, Pa, Pa, Pa])

 if len(time_slices) != number_of_queues - 1:
  raise SystemExit(0)

 doctest.testmod(extraglobs={'''queue''': deque([Pa, Pa, Pa, Pa])})

 snake_case__					:      Tuple				      =  Process('''P1''', 0, 53)
 snake_case__					:      Union[str, Any]				      =  Process('''P2''', 0, 17)
 snake_case__					:      Union[str, Any]				      =  Process('''P3''', 0, 68)
 snake_case__					:      str				      =  Process('''P4''', 0, 24)
 snake_case__					:      Optional[Any]				      =  3
 snake_case__					:      List[Any]				      =  [17, 25]
 snake_case__					:      Optional[int]				      =  deque([Pa, Pa, Pa, Pa])
 snake_case__					:      Tuple				      =  MLFQ(number_of_queues, time_slices, queue, 0)
 snake_case__					:      Optional[int]				      =  mlfq.multi_level_feedback_queue()

 # print total waiting times of processes(P1, P2, P3, P4)
 print(
     f'''waiting time:\
        \t\t\t{MLFQ.calculate_waiting_time(mlfq, [Pa, Pa, Pa, Pa])}'''
 )
 # print completion times of processes(P1, P2, P3, P4)
 print(
     f'''completion time:\
        \t\t{MLFQ.calculate_completion_time(mlfq, [Pa, Pa, Pa, Pa])}'''
 )
 # print total turnaround times of processes(P1, P2, P3, P4)
 print(
     f'''turnaround time:\
        \t\t{MLFQ.calculate_turnaround_time(mlfq, [Pa, Pa, Pa, Pa])}'''
 )
 # print sequence of finished processes
 print(
     f'''sequence of finished processes:\
        {mlfq.calculate_sequence_of_finish_queue()}'''
 )







'''simple docstring'''


from __future__ import annotations

from collections import deque
from collections.abc import Iterator
from dataclasses import dataclass






@dataclass
class 					__SCREAMING_SNAKE_CASE						:







  '''simple docstring'''






  lowerCamelCase_		:int
  lowerCamelCase_		:int







class 					__SCREAMING_SNAKE_CASE						:







  '''simple docstring'''






  def __init__(			self				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       list[list[Edge]]       =				[[] for _ in range(snake_case_						)]
       UpperCAmelCase_							:       Any       =				size


  def __getitem__(			self				,		snake_case_						):




       '''simple docstring'''

       return iter(self._graph[vertex]						)


  @property
  def _UpperCamelCase					(			self						):




       '''simple docstring'''

       return self._size


  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_				,		snake_case_						):




       '''simple docstring'''

       if weight not in (0, 1):
            raise ValueError('Edge weight must be either 0 or 1.'						)

       if to_vertex < 0 or to_vertex >= self.size:
            raise ValueError('Vertex indexes must be in [0; size).'						)

       self._graph[from_vertex].append(Edge(snake_case_				,		snake_case_						)						)






  def _UpperCamelCase					(			self				,		snake_case_				,		snake_case_						):




       '''simple docstring'''

       UpperCAmelCase_							:       Optional[int]       =				deque([start_vertex]						)
       UpperCAmelCase_							:       list[int | None]       =				[None] * self.size
       UpperCAmelCase_							:       str       =				0

       while queue:
            UpperCAmelCase_							:       Optional[int]       =				queue.popleft()
            UpperCAmelCase_							:       List[str]       =				distances[current_vertex]
            if current_distance is None:
                 continue

            for edge in self[current_vertex]:
                 UpperCAmelCase_							:       Union[str, Any]       =				current_distance + edge.weight
                 UpperCAmelCase_							:       Union[str, Any]       =				distances[edge.destination_vertex]
                 if (
                     isinstance(snake_case_				,		snake_case_						)
                     and new_distance >= dest_vertex_distance
                 ):
                      continue
                 UpperCAmelCase_							:       Optional[Any]       =				new_distance
                 if edge.weight == 0:
                      queue.appendleft(edge.destination_vertex						)
                 else:
                      queue.append(edge.destination_vertex						)

       if distances[finish_vertex] is None:
            raise ValueError('No path from start_vertex to finish_vertex.'						)

       return distances[finish_vertex]


if __name__ == "__main__":
 import doctest

 doctest.testmod()






