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

Rob T rob at ucora.com
Thu Nov 8 13:02:36 PST 2012


On Thursday, 8 November 2012 at 20:39:29 UTC, Manfred Nowak wrote:
> 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

I still don't follow what you are trying to point out.

I use this form of structure in real world applications, and they 
work just fine as-is.

If you mean by 'anchor' a way to end the recursion, that's a 
trivial problem already solved - the recursion ends when the list 
is empty. Of course I've left out the associated logic that does 
the necessary work, since that is not relevant to the problem at 
hand and clutters the issue.

No matter if there's merit to what you are describing, or not, 
the structure as-is should be legally definable as a template. In 
fact, I can define the structure just fine provided that I do not 
use a template.

--rt



More information about the Digitalmars-d-learn mailing list