comp.lang.ada
 help / color / mirror / Atom feed
* Charles: Generic_Find
@ 2002-11-11 17:16 Victor Porton
  2002-11-12  8:07 ` tmoran
                   ` (2 more replies)
  0 siblings, 3 replies; 9+ messages in thread
From: Victor Porton @ 2002-11-11 17:16 UTC (permalink / raw)


Why Generic_Find is generic? Why not just pass the predicate
as a function access? Doing so would avoid the need of
generic instantiation. I suggest to change this.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Charles: Generic_Find
  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-13 19:18 ` Matthew Heaney
  2 siblings, 0 replies; 9+ messages in thread
From: tmoran @ 2002-11-12  8:07 UTC (permalink / raw)


> Why Generic_Find is generic? Why not just pass the predicate
> as a function access? Doing so would avoid the need of
> generic instantiation. I suggest to change this.
  Remember that you can't do an Unchecked_Access on a function,
so a function access parameter is more restricted in where it
can be used.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Charles: Generic_Find
  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-13 19:19   ` Matthew Heaney
  2002-11-13 19:18 ` Matthew Heaney
  2 siblings, 2 replies; 9+ messages in thread
From: Preben Randhol @ 2002-11-12  8:27 UTC (permalink / raw)


Victor Porton wrote:
> Why Generic_Find is generic? Why not just pass the predicate
> as a function access? Doing so would avoid the need of
> generic instantiation. I suggest to change this.

Doesn't sound to me like a good Ada solution to do that. Sounds more
like C++

-- 
Preben Randhol ------------------------ http://www.pvv.org/~randhol/ --
�There are three things you can do to a woman. You can love her, suffer
 for her, or turn her into literature.�  - Justine, by Lawrence Durrell



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Charles: Generic_Find
  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:19   ` Matthew Heaney
  1 sibling, 1 reply; 9+ messages in thread
From: Hyman Rosen @ 2002-11-12 13:48 UTC (permalink / raw)


Preben Randhol wrote:
> Victor Porton wrote:
>>Why Generic_Find is generic? Why not just pass the predicate
>>as a function access? Doing so would avoid the need of
>>generic instantiation.
> 
> Doesn't sound to me like a good Ada solution to do that. Sounds more
> like C++

Hey! VP is commenting on Charles, which is based on the C++ STL,
whose version of find is a template. The C++ standard library
uses generic instantiation almost everywhere.

Anyway, in C++ the reason to have the predicate be generic is
the many varieties of objects that can stand in for a predicate.
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.




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Charles: Generic_Find
  2002-11-12 13:48   ` Hyman Rosen
@ 2002-11-12 14:49     ` Frank J. Lhota
  2002-11-13 19:31       ` Matthew Heaney
  0 siblings, 1 reply; 9+ messages in thread
From: Frank J. Lhota @ 2002-11-12 14:49 UTC (permalink / raw)


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





^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Charles: Generic_Find
  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-13 19:18 ` Matthew Heaney
  2 siblings, 0 replies; 9+ messages in thread
From: Matthew Heaney @ 2002-11-13 19:18 UTC (permalink / raw)


porton@ex-code.com (Victor Porton) wrote in message news:<3dd082fe$0$307$bed64819@news.gradwell.net>...
> Why Generic_Find is generic? Why not just pass the predicate
> as a function access? Doing so would avoid the need of
> generic instantiation. I suggest to change this.

The generic version of find is generic because Ada doesn't have
downward closures.  You have to be able to do something like this:

type Employee_Type is
   record
      SSN : SSN_Type;
      ...
   end record;

Employees : Container_Subtype;

function Find (SSN : SSN_Type) return Iterator_Type is
 
   function Predicate (E : Employee_Type) return Boolean is
   begin
      return E.SSN = SSN;
   end;

   function Find is 
     new Generic_Find (Predicate);
begin
   return Find (First (Employees), Back (Employees));
end;

Function pointers exist in Ada to implement callbacks, from say, a
GUI.  Unless you need a callback, you should be passing subprograms as
generic formal subprogram parameters.

Your suggestion is noted, and rejected.

Matt



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Charles: Generic_Find
  2002-11-12  8:27 ` Preben Randhol
  2002-11-12 13:48   ` Hyman Rosen
@ 2002-11-13 19:19   ` Matthew Heaney
  2002-11-14 10:03     ` Preben Randhol
  1 sibling, 1 reply; 9+ messages in thread
From: Matthew Heaney @ 2002-11-13 19:19 UTC (permalink / raw)


Preben Randhol <randhol+news@pvv.org> wrote in message news:<slrnat1er9.rg.randhol+news@kiuk0152.chembio.ntnu.no>...
> Victor Porton wrote:
> > Why Generic_Find is generic? Why not just pass the predicate
> > as a function access? Doing so would avoid the need of
> > generic instantiation. I suggest to change this.
> 
> Doesn't sound to me like a good Ada solution to do that. Sounds more
> like C++

The proper Ada solution is to use generic formal subprogram parameters.



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Charles: Generic_Find
  2002-11-12 14:49     ` Frank J. Lhota
@ 2002-11-13 19:31       ` Matthew Heaney
  0 siblings, 0 replies; 9+ messages in thread
From: Matthew Heaney @ 2002-11-13 19:31 UTC (permalink / raw)


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



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: Charles: Generic_Find
  2002-11-13 19:19   ` Matthew Heaney
@ 2002-11-14 10:03     ` Preben Randhol
  0 siblings, 0 replies; 9+ messages in thread
From: Preben Randhol @ 2002-11-14 10:03 UTC (permalink / raw)


Matthew Heaney wrote:
> 
> The proper Ada solution is to use generic formal subprogram parameters.

Yes, as I thought :-)

-- 
Preben Randhol ------------------------ http://www.pvv.org/~randhol/ --
�There are three things you can do to a woman. You can love her, suffer
 for her, or turn her into literature.�  - Justine, by Lawrence Durrell



^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2002-11-14 10:03 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
2002-11-13 19:19   ` Matthew Heaney
2002-11-14 10:03     ` Preben Randhol
2002-11-13 19:18 ` Matthew Heaney

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