Last active
February 17, 2016 22:38
-
-
Save mk270/5402833ce87a716f2d3b to your computer and use it in GitHub Desktop.
Ada container example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
with Ada.Text_IO; use Ada.Text_IO; | |
with Ada.Command_Line; | |
with Ada.Containers.Vectors; use Ada.Containers; | |
package body Dynamic_Allocation_Experiment | |
is | |
type Side is (Left, Right); | |
package Side_Container is new Vectors (Natural, Side); | |
use Side_Container; | |
function Make_Sides (Size : Natural) return Vector | |
is | |
begin | |
declare | |
Toggle : Boolean := True; | |
Sides : Vector; | |
begin | |
for I in 1 .. Size loop | |
Sides.Append (if Toggle then Right else Left); | |
Toggle := not Toggle; | |
end loop; | |
return Sides; | |
end; | |
end Make_Sides; | |
procedure Put_Side (A_Cursor : Cursor) is | |
begin | |
Ada.Text_IO.Put_Line (Side'Image (Element (Position => A_Cursor))); | |
end Put_Side; | |
procedure Dump_Sides (S : Vector) is | |
begin | |
S.Iterate (Process => Put_Side'Access); | |
end Dump_Sides; | |
procedure Dynamic_Allocation_Experiment | |
is | |
S : Vector; | |
Arg1 : String := Ada.Command_Line.Argument (1); | |
Size : Integer := Integer'Value (Arg1); | |
begin | |
S := Make_Sides (Size); | |
Dump_Sides (S); | |
Ada.Text_IO.Put_Line ("len: " & Count_Type'Image (S.Length)); | |
end Dynamic_Allocation_Experiment; | |
end Dynamic_Allocation_Experiment; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment