comp.lang.ada
 help / color / mirror / Atom feed
From: Ludovic Brenta <ludovic@ludovic-brenta.org>
Subject: Re: What's wrong with my code?
Date: Tue, 29 Apr 2008 02:31:23 -0700 (PDT)
Date: 2008-04-29T02:31:23-07:00	[thread overview]
Message-ID: <09e534e4-9883-4da6-af46-ec4b53c74634@x41g2000hsb.googlegroups.com> (raw)
In-Reply-To: 5a3b83ab-c9eb-448d-8e01-093df11bd3d2@b1g2000hsg.googlegroups.com

> generic
>    type Element is private;
>    with procedure Element_Put(E : in Element);
> package SelectionP is

Others have explained why neither Ada.Text_IO.Put nor
Ada.Integer_Text_IO.Put are acceptable actuals for Element_Put.

Element_Put is only necessary for one subprogram (procedure Print).
Yet, all users of your package must provide an actual for Element_Put
even if they don't call Print. Similarly, Find_Min and Sort are likely
to require a function "<" to compare Elements. So I suggest:

generic
   type Element is private;
package Selection_P is
   type Index_Type is range 0 .. 10;
   type My_Array is array (Index_Type) of Element;

   generic
      with function "<" (Left, Right : in Element) return Boolean is
<>;
   procedure Find_Min (A : My_Array; Offset : in Index_Type; Pos : out
Index_Type);

   procedure Swap (A : in out My_Array; First, Second : Index_Type);

   generic
      with function "<" (Left, Right : in Element) return Boolean is
<>;
   procedure Sort (A : in out My_Array);

   generic
      with procedure Put (E : in Element) is <>;
   procedure Put (A : in My_Array);
end Selection_P;

The reason I didn't place "<" as a generic formal parameter of the
package, but rather of Find_Min and Sort, is because you might want to
find the maximum element but sort in ascending order, like this:

package S is new Selection_P (Element => Integer);
procedure Find_Max is new S.Find_Min ("<" => ">"); -- use ">" as the
actual
procedure Sort is new S.Sort; -- use the default "<", sort in
ascending order
procedure Put (J : in Integer) is
begin
   Ada.Integer_Text_IO.Put (J);
end Put;
procedure Put is new S.Put; -- use the default

HTH

--
Ludovic Brenta.



  parent reply	other threads:[~2008-04-29  9:31 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2008-04-28 14:42 What's wrong with my code? amal.alphonse
2008-04-28 15:18 ` stefan-lucks
2008-04-28 15:22   ` stefan-lucks
2008-04-28 15:24     ` stefan-lucks
2008-04-28 15:23 ` george.priv
2008-04-28 16:52 ` Ivan Levashew
2008-04-29  9:18   ` Ludovic Brenta
2008-04-29  5:30 ` christoph.grein
2008-04-29  9:31 ` Ludovic Brenta [this message]
2008-04-29 10:33 ` amal.alphonse
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox