Standard library: Containers
Simple todo list
Goal: implement a simple to-do list system using vectors.
Steps:
Implement the
Todo_Lists
package.
Declare the
Todo_Item
type.Declare the
Todo_List
type.Implement the
Add
procedure.Implement the
Display
procedure.
Todo_Item
type is used to store to-do items.
It should be implemented as an access type to strings.
Todo_List
type is the container for all to-do items.
It should be implemented as a vector.
Procedure
Add
adds items (ofTodo_Item
type) to the list (ofTodo_List
type).
This requires allocating a string for the access type.
Procedure
Display
is 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_Array
type.Declare the
Integer_Sets
type.Implement the
Get_Unique
function that returns a set.Implement the
Get_Unique
function that returns an array of integer values.
Requirements:
The
Int_Array
type is an unconstrained array of positive range.The
Integer_Sets
package is an instantiation of theOrdered_Sets
package for theInteger
type.The
Get_Unique
function 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_Sets
package.
Get_Unique
must be implemented in two versions:
one version that returns a set —
Set
type from theOrdered_Sets
package.one version that returns an array of integer values —
Int_Array
type.
Remarks:
Sets — as the one found in the generic
Ordered_Sets
package — 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;