comp.lang.ada
 help / color / mirror / Atom feed
From: mheaney@on2.com (Matthew Heaney)
Subject: Re: Charles: Generic_Find
Date: 13 Nov 2002 11:31:00 -0800
Date: 2002-11-13T19:31:00+00:00	[thread overview]
Message-ID: <1ec946d1.0211131130.3dfb22b9@posting.google.com> (raw)
In-Reply-To: Hx8A9.7947$6Z.7289@nwrddc01.gnilink.net

"Frank J. Lhota" <NOSPAM.lhota.adarose@verizon.net> wrote in message news:<Hx8A9.7947$6Z.7289@nwrddc01.gnilink.net>...
> "Hyman Rosen" <hyrosen@mail.com> wrote in message
> news:1037108893.573381@master.nyc.kbcfp.com...
> > It also introduces potentially significant optimization
> > opportunities, since for predicates, function call overhead can
> > easily dwarf the amount of computation done by the predicate
> > itself.
> 
> In other words, the possibility of inlining the function is possible with
> the template / generic approach,whereas the pointer-to-function approach
> would force a function call.

Yes, but not just that.  In Ada downward chains must be static. 
Consider my previous employee example, and assume we want to count how
many employees are male:

Employees : Container_Subtype;

function Count (Sex : Sex_Type) return Natural is

    function Predicate (E : Employee_Type) return Boolean is
    begin
       return E.Sex = Sex;
    end;

    function Find is
       new Generic_Find (Predicate);

    Result : Natural := 0;

    Back : constant Iterator_Type := 
       Employees_Types.Back (Employees);

    Iter : Iterator_Type = First (Employees);

begin

   loop

      Iter := Find (Iter, Back);

      if Iter = Back then
         return Result;
      end if;

      Result := Result + 1;

      Iter := Succ (Iter);

   end loop;

end Count;

This implementation (a locally declared subprogram that uses the
parameters of the enclosing subprogram) is very natural in Ada, but
doesn't have a direct translation in C++.



  reply	other threads:[~2002-11-13 19:31 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2002-11-11 17:16 Charles: Generic_Find Victor Porton
2002-11-12  8:07 ` tmoran
2002-11-12  8:27 ` Preben Randhol
2002-11-12 13:48   ` Hyman Rosen
2002-11-12 14:49     ` Frank J. Lhota
2002-11-13 19:31       ` Matthew Heaney [this message]
2002-11-13 19:19   ` Matthew Heaney
2002-11-14 10:03     ` Preben Randhol
2002-11-13 19:18 ` Matthew Heaney
replies disabled

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