Advertisement
evanescente-ondine

Untitled

Dec 6th, 2024
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 3.17 KB | None | 0 0
  1. pragma Ada_2022;
  2. with queues_generic.priority;
  3. with Ada.Text_IO;
  4. with Ada.Integer_Text_IO;
  5. procedure Simulation with Spark_mode is
  6.  
  7.   NumQueues : CONSTANT Positive := 4;
  8.   subtype QueueNumber is Positive range 1..NumQueues;
  9.  
  10.   type CustomerRecord is RECORD
  11.     ArrivalTime: Positive;
  12.     NumItems:    Positive;
  13.   end RECORD;
  14.  
  15.   package MarketQueues is NEW Queues_Generic (Element => CustomerRecord);
  16.   USE MarketQueues;
  17.   type SingleQueue (Capacity: Positive) is record
  18.     Size: Natural;
  19.     List: Queue (Capacity);
  20.   end record with Dynamic_Predicate => Size <= Capacity;
  21.  
  22.   LimitSizeQueue: constant := 20;
  23.   type Market is ARRAY(1..NumQueues) OF SingleQueue (20);
  24.   Queues: Market;
  25.  
  26.   type Eventtype is (Arrival, Departure);
  27.   type Event (T: Eventtype := Arrival) is record
  28.     case T is
  29.       when Arrival =>
  30.         Arrivaltime: Positive;
  31.         NBitems: Positive;
  32.       when Departure =>
  33.         DepartureTime: Positive;
  34.         Nb: QueueNumber;
  35.     end case;
  36.   end record;
  37.   subtype Arrivaltype is Event (Arrival);
  38.   subtype Departuretype is Event (Departure);
  39.  
  40.   function KeyOf (E: Event) return Positive is
  41.     ((if E.T = Arrival then E.ArrivalTime else E.DepartureTime));
  42.   package EventQueues is NEW Queues_generic (Element => Event);
  43.   package EventQueuesPriority is NEW EventQueues.Priority (Positive);
  44.   use EventQueuesPriority;
  45.   subtype EventQueue is EventQueuesPriority.Queue;
  46.   EventList: EventQueue (Capacity => 4);
  47.  
  48.   use Ada.Text_IO;
  49.   InputFile : Ada.Text_IO.File_type;
  50.  
  51.   function MostAvailableQueue return QueueNumber
  52.     with Global => Queues is
  53.     SmallestQueue: Natural := Queues(1).Size;
  54.   begin
  55.     for A of Queues (2..Queues'Last) when A.Size < SmallestQueue loop
  56.       SmallestQueue := A.Size;
  57.     end loop;
  58.     return SmallestQueue;
  59.   end MostAvailableQueue;
  60.  
  61.   E: Event;
  62.   procedure AddArrival
  63.     with Exceptional_Cases => (Data_Error => True),
  64.       Global => (Input => InputFile, Output => E) is
  65.     Last: Positive;
  66.     Line: constant String := Get_line (InputFile);
  67.     use Ada.Integer_text_io;
  68.   begin
  69.     Get (Line, E.Arrivaltime, Last);
  70.     Get(Line(Last+1..Line'Last), E.NBitems, Last);
  71.     Enqueue (EventList, E);
  72.   exception when others =>
  73.     Put_line ("Line ill formated:  """ & Line & "");
  74.     raise;
  75.   end AddArrival;
  76.  
  77.   procedure ProcessArrival (ArrivalTime: IN Positive; NumItems: IN Positive)
  78.     with Global => (In_Out => Queues)  is
  79.   begin
  80.     pragma Unimplemented_Unit;
  81.     null;
  82.   end ProcessArrival;
  83.   procedure ProcessDeparture(DepartureTime: IN Positive; Q: IN OUT MarketQueues.Queue)
  84.      is
  85.   begin
  86.     pragma Unimplemented_Unit;
  87.     null;
  88.   end ProcessDeparture;
  89. begin
  90.   begin
  91.     Open (Inputfile, In_File, "data");
  92.   exception when others =>
  93.     Put_line ("No ""data"" file, closing.");
  94.     raise;
  95.   end;
  96.   declare
  97.     Last: Positive;
  98.     Line: constant String := Get_line (InputFile);
  99.   begin
  100.     Get(Line, E.Arrivaltime, Last);
  101.     Get(Line(Last+1..Line'Last), E.NBitems, Last);
  102.     Enqueue (EventList, E);
  103.   exception when others =>
  104.     Put_line ("Line ill formated:  """ & Line & "");
  105.     raise;
  106.   end;
  107.   return;
  108. exception when others =>
  109.   return;
  110. end Simulation;
  111.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement