Advertisement
evanescente-ondine

Untitled

Nov 22nd, 2024
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ada 3.42 KB | Source Code | 0 0
  1. pragma Ada_2022;
  2. with ada.text_io;
  3. use ada.text_io;
  4. procedure main is
  5.   generic
  6.     type Element is private;
  7.     NullElement : Element;
  8.     with FUNCTION "<" (E1, E2: Element) RETURN Boolean IS <>;
  9.   package dst_matrices  is
  10.     type Matrix (<>) is limited private;
  11.     type Indexes is array (Positive range <>) of Positive;
  12.     function MakeMatrix (Dimensions: Indexes) return Matrix;
  13.     procedure Insert (M: in out Matrix; N: Indexes; E: Element);
  14.     procedure Retrieve (M: Matrix; N:Indexes; Value: out Element; Success: out Boolean);
  15.   private
  16.     type Node;
  17.     type Position is access Node;
  18.     type NodeKind is (ElementKind, Dummy, Index);
  19.     type Node (Kind: NodeKind) is record
  20.       Link: Position;
  21.       case Kind is
  22.         when ElementKind|Index =>
  23.           Rank: Positive;
  24.           case Kind is
  25.             when ElementKind => Info: Element;
  26.             when Index => Nextdimension: Position;
  27.             when others => null;
  28.           end case;
  29.         when others => null;
  30.       end case;
  31.     end record;
  32.     type Matrix (First: Position; DimNumber: Positive) is record
  33.       Dimensions: Indexes (1..DimNumber);    
  34.     end record;
  35.   end dst_matrices;
  36.  
  37.   package body dst_matrices is
  38.     function MakeMatrix (Dimensions: Indexes) return Matrix is
  39.         (new Node(Dummy), Dimensions'Length, Dimensions);
  40.  
  41.     procedure Insert (M: in out Matrix; N: Indexes; E: Element)
  42.     -- with Refined_Pre => N'Length = M.DimNumber and (for all J in N'Range => N(J) <= M.Dimensions(J))
  43.     is
  44.         Current: Position := M.First;
  45.     begin
  46.         for A of N(1..N'Last-1) loop
  47.             while Current.Link /= null and then Current.Link.Rank < A loop
  48.                 Current := @.Link;
  49.             end loop;
  50.             if Current.Link = null or else Current.Link.Rank /= A then
  51.                 Current.Link := new Node'(Kind => Index, Link => @, Rank => A, NextDimension => new Node(Dummy));
  52.             end if;
  53.             Current := @.Link.NextDimension;
  54.         end loop;
  55.         while Current.Link /= null and then Current.Link.Rank < N(N'Last) loop
  56.             Current := @.Link;
  57.         end loop;
  58.         if Current.Link = null or else Current.Link.Rank /= N(N'Last) then
  59.             Current.Link := new Node'(Kind => ElementKind, Link => @, Rank => N(N'Last), Info => E);
  60.             put_line (Current.Link.all'Image);
  61.         end if;
  62.     end Insert;
  63.  
  64.     procedure Retrieve (M: Matrix; N:Indexes; Value: out Element; Success: out Boolean)
  65.      -- with Refined_Pre => N'Length = M.DimNumber and (for all J in N'Range => N(J) <= M.Dimensions(J))
  66.      is
  67.         Current: Position := M.First.Link;
  68.     begin
  69.         for A of N(1..N'Last-1) loop
  70.             while Current /= null and then Current.Rank < A loop
  71.                 Current := @.Link;
  72.             end loop;
  73.             if Current /= null or else Current.Rank /= A then
  74.                 Success := False;
  75.                 return;
  76.             else
  77.                 Current := @.NextDimension.Link;
  78.             end if;
  79.         end loop;
  80.         while Current /= null and then Current.Rank < N(N'Last) loop
  81.             Current := @.Link;
  82.         end loop;
  83.         if Current.Link = null or else Current.Rank /= N(N'Last) then
  84.             Success := False;
  85.         else
  86.             Value := Current.Info;
  87.         end if;
  88.     end Retrieve;
  89.  
  90.   end dst_matrices;
  91.  
  92.     package dmp is new dst_matrices (Natural, 0);
  93.     use dmp;
  94.     M: Matrix := MakeMatrix ([9,2]);
  95.     Value: Positive;
  96.     Success: Boolean;
  97. begin
  98.     Insert (M, [1,2], 82);
  99.     Insert (M, [1,1], 87);
  100.     Insert (M, [3,2], 87);
  101.     Insert (M, [3,1], 87);
  102.     Retrieve (M, [1,1], Value, Success);
  103.     put_line (Value'Image);
  104. end main;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement