comp.lang.ada
 help / color / mirror / Atom feed
From: Richard D Riehle <LaoXhai@ix.netcom.com>
Subject: Re: parameter passing
Date: 2000/02/18
Date: 2000-02-18T23:27:43+00:00	[thread overview]
Message-ID: <88kkhf$460$1@nntp4.atl.mindspring.net> (raw)
In-Reply-To: 88gqg5$4a8$1@bunyip.cc.uq.edu.au

In article <88gqg5$4a8$1@bunyip.cc.uq.edu.au>,
	"Riyaz Mansoor" <s800032@student.uq.edu.au> wrote:

>i want to pass a generic package as a parameter to another genric package.
> --how do i declare the gen2_pack with gen1 as a parameter?
>also how would the code look like in gen2_pack which actually accepts the
>gen1 package as a generic "paramter" ?

The following Sort Utilities package is an abbreviated version of the original
package with removal of a lot of comments.  It should be easy to read.

-- =========================================================
-- Sort_Utilities.Ads              Richard Riehle, AdaWorks
--
-- This package contains a set of generic sort routines that
-- can be instantiated for the sorting of an unconstrained
-- array.  The Big-O notation associated with each sort is a
-- kind of design metric that represents the relative performance
-- of each algorithm.  
-- =============================================================
package Sort_Utilities is

  generic
    type Data is private;
    type Index is (<>);
    type Data_Set is array(Index range <>) of Data;
    with function Is_Greater (Left, Right : Data) return Boolean;
    with function Is_Equal   (Left, Right : Data) return Boolean;
  package Sort_Signature is end Sort_Signature;

  generic
    with package Signature is new Sort_Signature(<>);
  procedure QuickSort(The_Data : in out Signature.Data_Set); -- Note the dot notation

  generic
    with package Signature is new Sort_Signature(<>);
  procedure BubbleSort(The_Data : in out Signature.Data_Set);

  generic
    with package Signature is new Sort_Signature(<>);
    use Signature;                                          -- Note the use clause
  procedure InsertionSort(The_Data : in out Data_Set);      -- Dot notation not required

  -- More kinds of sort algorithms.

end Sort_Utilities;

The generic sort procedures each have a generic formal package parameter using
a signature immediately enclosed in the same package specification.  The following
little procedure, some code deleted for pedagogic purposes, demonstrates how to
instantiate one of these sort routines.  

with Sort_Utilities;
procedure Test_Utilities is

  type Stock is record
     Data : String(1..20);
     Key  : Positive;
  end record;

  type Stock_Array is array(Positive range <>) of Stock;

  function "="(L, R : Stock) return Boolean is
  begin
     return L.Key = R.Key;
  end "=";

  function ">"(L, R : Stock) return Boolean is
  begin
     return L.Key > R.Key;
  end ">";

  package Stock_Signature is new Sort_Utilities.Sort_Signature
                   (Data => Stock,
                    Index => Positive,
                    Data_Set => Stock_Array,
                    Is_Equal => "=",
                    Is_Greater => ">");

  procedure Stock_Sort is new Sort_Utilities.QuickSort
                   (Signature => Stock_Signature);

begin
  null;  -- Of course you will want some actual code here. :)
end Test_Utilities;

In the body of the Sort_Utilities, you will refer to each signature parameter with
dot notation, unless you prefer a use clause, as we did for InsertionSort.

Hope you find this useful.

Richard Riehle




  reply	other threads:[~2000-02-18  0:00 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-02-17  0:00 parameter passing Riyaz Mansoor
2000-02-18  0:00 ` Richard D Riehle [this message]
2000-02-19  0:00 ` Ehud Lamm
  -- strict thread matches above, loose matches on Subject: below --
1990-06-29 15:32 David Guaspari
     [not found] <1569@oravax.UUCP>
1990-06-27 15:32 ` stt
1990-06-27 17:15 ` Michael Feldman
1989-06-21 13:16 Parameter Passing Jon Humphreys
1989-06-21 17:23 ` William Thomas Wolfe,2847,
1986-10-08 18:56 parameter passing Eric Marshall
1986-10-10 16:46 ` Geoff Mendal
1986-10-30 23:08   ` Mats_Ohlin_FOA2
replies disabled

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