From: "Frank J. Lhota" <NOSPAM.lhota.adarose@verizon.net>
Subject: Smarter Generics
Date: Sat, 14 Aug 2004 19:55:39 GMT
Date: 2004-08-14T19:55:39+00:00 [thread overview]
Message-ID: <%6uTc.592$de4.1@trndny07> (raw)
The recent thread on "A Simple Ada Problem" got me thinking about a
limitation of Ada generics. Generic are a great way of generalizing an
implementation strategy for a particular problem, but generics cannot (or
cannot easily) select one of several implementation strategies based on the
generic parameters.
Take the following simple example: Assume that I want to implement stacks of
limited length, e.g. a stack that I can push or pop elements of a given
type, but with a limit on how many elements will be on the stack at one
time:
generic
type Stack_Element is private;
Max_Length : in Positive;
package Limited_Stacks is
type Stack_Type is limited private;
Stack_Overflow, Stack_Underflow : exception;
procedure Push ( Item : in Stack_Element; Onto : in out
Stack_Type );
procedure Pop ( Item : out Stack_Element; Onto : in out
Stack_Type );
private
...
end Limited_Stacks;
Two possible implementation strategies:
1. We could implement the stack as an array of Max_Length elements, along
with a component indicating the current length.
2. We could maintain an adjustable list of pointers to elements allocated
from the heap.
Obviously, implementation 1 would be faster and more convenient if
Max_Length * Stack_Element'Size is relatively small. Implementation 2
involves additional overhead, but can handle cases where stack elements can
vary quite a bit in size.
What would be great is if there were some way to write Limited_Stacks so
that the implementation strategy could be chosen based on the generic
parameters, so that we could do something like this:
if Max_Length * Stack_Element'Size <= Some_Threshold then
-- Static memory allocation should be fine
Implement Limited_Stacks using strategy 1;
else
-- Size could be a problem, better go with pointers.
Implement Limited_Stacks using strategy 2;
end if;
AFAIK neither Ada generics nor C++ templates have this level of
intelligence. Would smarter generics be a worthwhile addition to Ada, and if
so, what form should it take?
next reply other threads:[~2004-08-14 19:55 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2004-08-14 19:55 Frank J. Lhota [this message]
2004-08-15 1:14 ` Smarter Generics Georg Bauhaus
2004-08-15 1:32 ` Francois G. Dorais
2004-08-16 5:36 ` Wes Groleau
2004-08-16 13:45 ` Jean-Marc Bourguet
2004-08-16 13:52 ` Jean-Marc Bourguet
2004-08-18 5:31 ` Florian Weimer
2004-08-24 17:37 ` Jean-Marc Bourguet
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox