Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Ada > Re: What's wron...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 9 of 10 Topic 5671 of 5931
Post > Topic >>

Re: What's wrong with my code?

by Ludovic Brenta <ludovic@[EMAIL PROTECTED] > Apr 29, 2008 at 02:31 AM

> 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.
 




 10 Posts in Topic:
What's wrong with my code?
amal.alphonse@[EMAIL PROT  2008-04-28 07:42:44 
Re: What's wrong with my code?
george.priv@[EMAIL PROTEC  2008-04-28 08:23:10 
Re: What's wrong with my code?
stefan-lucks@[EMAIL PROTE  2008-04-28 17:18:31 
Re: What's wrong with my code?
stefan-lucks@[EMAIL PROTE  2008-04-28 17:22:25 
Re: What's wrong with my code?
stefan-lucks@[EMAIL PROTE  2008-04-28 17:24:52 
Re: What's wrong with my code?
Ivan Levashew <octagra  2008-04-28 23:52:00 
Re: What's wrong with my code?
christoph.grein@[EMAIL PR  2008-04-28 22:30:29 
Re: What's wrong with my code?
Ludovic Brenta <ludovi  2008-04-29 02:18:01 
Re: What's wrong with my code?
Ludovic Brenta <ludovi  2008-04-29 02:31:23 
Re: What's wrong with my code?
amal.alphonse@[EMAIL PROT  2008-04-29 03:33:41 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Wed Oct 15 22:26:10 CDT 2008.