Standard library: Containers
Simple todo list
Goal: implement a simple to-do list system using vectors.
Steps:
Implement the
Todo_Listspackage.
Declare the
Todo_Itemtype.Declare the
Todo_Listtype.Implement the
Addprocedure.Implement the
Displayprocedure.
Todo_Itemtype is used to store to-do items.
It should be implemented as an access type to strings.
Todo_Listtype is the container for all to-do items.
It should be implemented as a vector.
Procedure
Addadds items (ofTodo_Itemtype) to the list (ofTodo_Listtype).
This requires allocating a string for the access type.
Procedure
Displayis used to display all to-do items.
It must display one item per line.
Remarks:
This exercise is based on the Simple todo list exercise from the More About Types.
Your goal is to rewrite that exercise using vectors instead of arrays.
You may reuse the code you've already implemented as a starting point.
package Todo_Lists is
type Todo_Item is access String;
type Todo_List is null record;
procedure Add (Todos : in out Todo_List;
Item : String);
procedure Display (Todos : Todo_List);
end Todo_Lists;
with Ada.Text_IO; use Ada.Text_IO;
package body Todo_Lists is
procedure Add (Todos : in out Todo_List;
Item : String) is
begin
null;
end Add;
procedure Display (Todos : Todo_List) is
begin
Put_Line ("TO-DO LIST");
end Display;
end Todo_Lists;
with Ada.Command_Line; use Ada.Command_Line;
with Ada.Text_IO; use Ada.Text_IO;
with Todo_Lists; use Todo_Lists;
procedure Main is
type Test_Case_Index is
(Todo_List_Chk);
procedure Check (TC : Test_Case_Index) is
T : Todo_List;
begin
case TC is
when Todo_List_Chk =>
Add (T, "Buy milk");
Add (T, "Buy tea");
Add (T, "Buy present");
Add (T, "Buy tickets");
Add (T, "Pay electricity bill");
Add (T, "Schedule dentist appointment");
Add (T, "Call sister");
Add (T, "Revise spreasheet");
Add (T, "Edit entry page");
Add (T, "Select new design");
Add (T, "Create upgrade plan");
Display (T);
end case;
end Check;
begin
if Argument_Count < 1 then
Put_Line ("ERROR: missing arguments! Exiting...");
return;
elsif Argument_Count > 1 then
Put_Line ("Ignoring additional arguments...");
end if;
Check (Test_Case_Index'Value (Argument (1)));
end Main;
List of unique integers
Goal: create function that removes duplicates from and orders a collection of elements.
Steps:
Implement package
Ops.
Declare the
Int_Arraytype.Declare the
Integer_Setstype.Implement the
Get_Uniquefunction that returns a set.Implement the
Get_Uniquefunction that returns an array of integer values.
Requirements:
The
Int_Arraytype is an unconstrained array of positive range.The
Integer_Setspackage is an instantiation of theOrdered_Setspackage for theIntegertype.The
Get_Uniquefunction must remove duplicates from an input array of integer values and order the elements.
For example:
if the input array contains
(7, 7, 1)the function must return
(1, 7).You must implement this function by using sets from the
Ordered_Setspackage.
Get_Uniquemust be implemented in two versions:
one version that returns a set —
Settype from theOrdered_Setspackage.one version that returns an array of integer values —
Int_Arraytype.
Remarks:
Sets — as the one found in the generic
Ordered_Setspackage — are useful for quickly and easily creating an algorithm that removes duplicates from a list of elements.
with Ada.Containers.Ordered_Sets;
package Ops is
-- type Int_Array is ...
-- package Integer_Sets is ...
subtype Int_Set is Integer_Sets.Set;
function Get_Unique (A : Int_Array) return Int_Set;
function Get_Unique (A : Int_Array) return Int_Array;
end Ops;
package body Ops is
function Get_Unique (A : Int_Array) return Int_Set is
begin
null;
end Get_Unique;
function Get_Unique (A : Int_Array) return Int_Array is
begin
null;
end Get_Unique;
end Ops;
with Ada.Command_Line; use Ada.Command_Line;
with Ada.Text_IO; use Ada.Text_IO;
with Ops; use Ops;
procedure Main is
type Test_Case_Index is
(Get_Unique_Set_Chk,
Get_Unique_Array_Chk);
procedure Check (TC : Test_Case_Index;
A : Int_Array) is
procedure Display_Unique_Set (A : Int_Array) is
S : constant Int_Set := Get_Unique (A);
begin
for E of S loop
Put_Line (Integer'Image (E));
end loop;
end Display_Unique_Set;
procedure Display_Unique_Array (A : Int_Array) is
AU : constant Int_Array := Get_Unique (A);
begin
for E of AU loop
Put_Line (Integer'Image (E));
end loop;
end Display_Unique_Array;
begin
case TC is
when Get_Unique_Set_Chk => Display_Unique_Set (A);
when Get_Unique_Array_Chk => Display_Unique_Array (A);
end case;
end Check;
begin
if Argument_Count < 3 then
Put_Line ("ERROR: missing arguments! Exiting...");
return;
else
declare
A : Int_Array (1 .. Argument_Count - 1);
begin
for I in A'Range loop
A (I) := Integer'Value (Argument (1 + I));
end loop;
Check (Test_Case_Index'Value (Argument (1)), A);
end;
end if;
end Main;