|
Archives
D Programming
DD.gnu digitalmars.D digitalmars.D.bugs digitalmars.D.dtl digitalmars.D.dwt digitalmars.D.announce digitalmars.D.learn digitalmars.D.debugger C/C++ Programming
c++c++.announce c++.atl c++.beta c++.chat c++.command-line c++.dos c++.dos.16-bits c++.dos.32-bits c++.idde c++.mfc c++.rtl c++.stl c++.stl.hp c++.stl.port c++.stl.sgi c++.stlsoft c++.windows c++.windows.16-bits c++.windows.32-bits c++.wxwindows digitalmars.empire digitalmars.DMDScript |
digitalmars.D.dtl - enumerations/sequences
I was thinking some more about polymorphic views onto containers (eg
Enumerations) with an eye towards experimenting with something along those
lines in MinTL. The functions in a basic Enumeration are
interface Enumeration(Value) {
Value next();
bool hasNext();
}
but this is very similar to what goes into a foreach statement. The
difference between foreach and Enumeration is that the enumeration state
can be "paused" at any point and picked up again. This will cause problems
for a library implementation of an Enumeration for a builtin associative
array since any such implementation will have to make assumptions about the
private (compiler-dependent) structure of the associative array. This isn't
a disaster but it is unfortunate. So I thought it wouldn't be so bad to
lose this ability to pause the enumeration as long as the polymorphic
aspect of the traversal is preserved. So I went and defined in MinTL two
interfaces
interface Seq(Value) {
int opApply( int delegate(inout Value value) dg);
}
and
interface SeqWithKeys(Keys,Value) : Seq!(Value) {
int opApply( int delegate(Key key, inout Value value) dg);
}
and made implementations for each of the containers in MinTL *and* the
builtin dynamic and associative arrays. For containers like lists and
dynamic arrays which don't have a user-specified Key type the Key type is
int (since the container is indexed by integers). This means functions that
don't want to be tied to a particular container type but just want to
"foreach" over a bunch of values or key-value pairs can take a sequence as
input and polymorphic dispatching will make sure the right opApply is
called at run-time. For example:
// define a function that takes an arbitrary sequence of doubles
void printItems(Seq!(double) seq) {
foreach( double item; seq) {
printf("%g\n",item);
}
}
...
double[] x;
... // fill x
Seq!(double) seq = toSeq!(double[])(x); // array as sequence
printItems(seq);
List!(double) y;
... // fill y
Seq!(double) seq2 = y.toSeq; // List as sequence
printItems(seq2);
The examples using associative arrays is similar. I think the trade-off of
losing the ability to suspend the traversals is offset by gaining "foreach"
support and builtin associative array support. What do others think?
-Ben
ps I've updated the download at http://home.comcast.net/~benhinkle/mintl/ so
that people can experiment with it.
Aug 05 2004
Not getting into your design philosophy, here is an alternative iterator
design
interface Map (T, S = int)
{
int opApply (int delegate (inout T));
int opApply (int delegate (inout S, inout T));
}
interface Seq (T) : Map!(T)
{
}
Aug 06 2004
Bent Rasmussen wrote: Aug 06 2004
A small question, too: shouldn't the default template parameters come Aug 06 2004
In article <ceuoev$2g9f$1 digitaldaemon.com>, Ben Hinkle says...The examples using associative arrays is similar. I think the trade-off of losing the ability to suspend the traversals is offset by gaining "foreach" support and builtin associative array support. What do others think? Aug 06 2004
"Sean Kelly" <sean f4.ca> wrote in message news:cf07o5$hkm$1 digitaldaemon.com...In article <ceuoev$2g9f$1 digitaldaemon.com>, Ben Hinkle says...The examples using associative arrays is similar. I think the trade-off Aug 06 2004
|