From: Lionel Draghi <lionel.draghi@gmail.com>
Subject: Re: Iterable container as generic parameter
Date: Thu, 25 Jan 2018 06:58:38 -0800 (PST)
Date: 2018-01-25T06:58:38-08:00 [thread overview]
Message-ID: <6427a793-91a4-4feb-b067-ed89b4c04421@googlegroups.com> (raw)
In-Reply-To: <p4bjbm$s9o$1@franka.jacob-sparre.dk>
> As to the question you didn't ask, you clearly need the container type
> somewhere in your interface, so you can pass container objects. (Or, I
> suppose, you could reference the type used in the instance of the
> Iterator_Interfaces.) Otherwise, you just have methods but no data. And
> there isn't anything interesting that you can do with just methods!
>
> Randy.
Thanks Randy for the answer, this is actually my question.
And what you put into parenthesis was what I was trying to do.
Here is my point :
If I limit the procedure scope to a specific container, the generic is simple :
with Ada.Containers.Doubly_Linked_Lists;
generic
type Element_Type is (<>);
with function Image (Element : Element_Type) return String is <>;
with package Lists is new Ada.Containers.Doubly_Linked_Lists (Element_Type);
function List_Image_1 (List : Lists.List) return string;
But if want something that could be used with both Lists and Maps, I have to provide much more parameters :
with Ada.Containers;
generic
type Element_Type (<>) is Private;
with function Image (Element : Element_Type) return String is <>;
type Cursor is private;
with function First return Cursor is <>;
with function Next (Position : Cursor) return Cursor is <>;
with function Length return Ada.Containers.Count_Type is <>;
with function Element (Position : Cursor) return Element_Type is <>;
function List_Image_2 return String;
And that sounds a bit to complex to me, because what I am doing is just creating my own iterator.
This is why I was trying to simplify the generic formal part by using the Iterator_Interfaces.
I have the feeling that I’m missing something.
Lionel
PS : here is my test code.
with Ada.Containers.Doubly_Linked_Lists;
generic
type Element_Type is (<>);
with function Image (Element : Element_Type) return String is <>;
with package Lists is new Ada.Containers.Doubly_Linked_Lists (Element_Type);
function List_Image_1 (List : Lists.List) return string;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
function List_Image_1 (List : Lists.List) return String is
Tmp : Unbounded_String;
begin
for E of List loop
Tmp := Tmp & " " & Image (E);
end loop;
return To_String (Tmp);
end List_Image_1;
with Ada.Containers;
generic
type Element_Type (<>) is Private;
with function Image (Element : Element_Type) return String is <>;
type Cursor is private;
with function First return Cursor is <>;
with function Next (Position : Cursor) return Cursor is <>;
with function Length return Ada.Containers.Count_Type is <>;
with function Element (Position : Cursor) return Element_Type is <>;
function List_Image_2 return String;
with Ada.Strings.Unbounded; use Ada.Strings.Unbounded;
function List_Image_2 return String is
Tmp : Unbounded_String;
C : Cursor;
begin
-- a bit more sophisticated format
case Length is
when 0 => return "";
when 1 => return "[" & Image (Element (First)) & "]";
when others =>
C := First;
Tmp := To_Unbounded_String ("[") & Image (Element (C));
for I in 2 .. Length loop
C := Next (C);
Tmp := Tmp & ", " & Image (Element (C));
end loop;
Tmp := Tmp & "]";
end case;
return To_String (Tmp);
end List_Image_2;
with List_Image_1;
with List_Image_2;
with Ada.Containers.Doubly_Linked_Lists;
with Ada.Containers.Indefinite_Hashed_Sets;
with Ada.Strings.Hash_Case_Insensitive;
with Ada.Text_Io;
procedure Test_List_Image is
-- container for test purposes
package Integer_Lists is new Ada.Containers.Doubly_Linked_Lists (Integer);
List : Integer_Lists.List;
Function Integer_List_Image_1 is new List_Image_1
(Integer,
Integer'Image,
Integer_Lists);
function Integer_List_Image_2 is new List_Image_2
(Integer,
Integer'Image,
Integer_Lists.Cursor,
List.First,
Integer_Lists.Next,
List.Length,
Integer_Lists.Element);
-- another container
function Image (S : String) return String is (String (S));
package Id_Sets is new Ada.Containers.Indefinite_Hashed_Sets
(String, Ada.Strings.Hash_Case_Insensitive, "=");
Set : Id_Sets.Set;
use Id_Sets;
function Id_Set_Image_2 is new List_Image_2
(String,
Image,
Cursor => Id_Sets.Cursor,
First => Set.First,
Next => Id_Sets.Next,
Length => Set.Length,
Element => Id_Sets.Element);
begin
-- ----------------------------------------------------------------------
Ada.Text_Io.Put_Line ("Test generic 1 on a list :");
Ada.Text_Io.Put_Line (Integer_List_Image_1 (List));
List.Append (1);
Ada.Text_Io.Put_Line (Integer_List_Image_1 (List));
List.Append (2);
Ada.Text_Io.Put_Line (Integer_List_Image_1 (List));
List.Append (3);
Ada.Text_Io.Put_Line (Integer_List_Image_1 (List));
Ada.Text_Io.New_Line;
-- ----------------------------------------------------------------------
Ada.Text_Io.Put_Line ("Test generic 2 on a list :");
List.Clear;
Ada.Text_Io.Put_Line (Integer_List_Image_2);
List.Append (1);
Ada.Text_Io.Put_Line (Integer_List_Image_2);
List.Append (2);
Ada.Text_Io.Put_Line (Integer_List_Image_2);
List.Append (3);
Ada.Text_Io.Put_Line (Integer_List_Image_2);
Ada.Text_Io.New_Line;
-- ----------------------------------------------------------------------
Ada.Text_Io.Put_Line ("Test generic 2 on a set :");
Ada.Text_Io.Put_Line (Id_Set_Image_2);
Set.Insert ("Hyperion");
Ada.Text_Io.Put_Line (Id_Set_Image_2);
Set.Insert ("Endymion");
Ada.Text_Io.Put_Line (Id_Set_Image_2);
Set.Insert ("TechnoCore");
Ada.Text_Io.Put_Line (Id_Set_Image_2);
end Test_List_Image;
next prev parent reply other threads:[~2018-01-25 14:58 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-01-25 0:22 Iterable container as generic parameter Lionel Draghi
2018-01-25 3:36 ` Randy Brukardt
2018-01-25 14:58 ` Lionel Draghi [this message]
2018-01-25 18:26 ` briot.emmanuel
2018-01-26 4:50 ` Randy Brukardt
2018-01-26 9:09 ` briot.emmanuel
2018-01-26 22:32 ` Lionel Draghi
2018-01-27 7:03 ` Randy Brukardt
2018-01-27 11:38 ` Lionel Draghi
2018-01-27 14:31 ` Jere
2018-01-28 18:08 ` Lionel Draghi
2018-01-29 13:34 ` briot.emmanuel
2018-01-29 22:26 ` Lionel Draghi
2018-01-29 23:00 ` Randy Brukardt
2018-01-30 22:35 ` Lionel Draghi
2018-01-30 23:32 ` Lionel Draghi
2018-01-31 0:15 ` Lionel Draghi
2018-01-30 15:13 ` Shark8
2018-01-26 23:07 ` Lionel Draghi
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox