Compilable Recursive Data Structure ( was: Recursive data structure using template won't compile)

Manfred Nowak svv1999 at hotmail.com
Thu Nov 8 12:39:29 PST 2012


Manfred Nowak wrote:

> or `T' itself must take the lead.

`T' was given as:
| struct R
| {
|     int value;
|     d_list!R Rlist;
| }

... and using the given changes as well as D-parlor `T' becomes:
| struct R {
|     int value;
|     Ancor!R list;
| }

Because `R' can recurse infinitely over `Ancor' a mooring and a way 
to that mooring is needed. Let the mooring be, when there are no more 
`Ancor' to expect, i.e. the way to that mooring leads from some 
height down to zero.

That means `R' has to know its height. `R' roughly becomes: 
| struct R( int height) {
|     int value;
|     Ancor!R( height-1) list;
| }

That describes the way. Of course the mooring is still missing:
| struct R( int height)
|   if( height > 0) {
|     int value;
|     Ancor!R( height-1) list;
| }
| struct R( int height)
|   if( height <= 0) {
|     int value;
| }

That's it. Only missing some testing:

import std.stdio: writeln;
void main(){

  alias R!(1) First;
  First elem;
  elem.value= 1;
  writeln( elem);

  alias Ancor!First.Node Node;
  Node n;
  n.payload= elem;
  n.pred= null;
  n.succ= null;
  writeln( n);

  alias Ancor!First Ancor1;
  Ancor1 ancor;
  ancor.head= &n;
  ancor.tail= &n;
  writeln( ancor);
}

-manfred


More information about the Digitalmars-d-learn mailing list