Extensible struct type via variadic template arguments

Joseph Rushton Wakeling via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Apr 10 11:45:02 PDT 2016


Hello all,

I've been playing around recently with some new ideas for a 
reworking of my Dgraph library:
https://github.com/WebDrake/Dgraph

... and particularly, considering how to use D's metaprogramming 
techniques to best allow the fundamental graph data structures to 
be extended arbitrarily.

One very basic idea is to allow graph edges to have arbitrary 
properties, and to do that, I've tried to come up with a basic 
data structure that can be extended via variadic template 
arguments.  However, I'm not entirely satisfied with the design, 
as I suspect it can be simplified/made nicer.  Here's what I have 
at present:


//////////////////////////////////////////

/**
  * Basic data structure containing arrays all of the same
  * length, such that fieldname[i] is the corresponding
  * property of edge i
  */
struct ExtensibleEdgeList (EdgePropertyList...)
{
     public size_t[] tail;
     public size_t[] head;

     mixin(multipleEdgeProperties!(EdgePropertyList));
}

/**
  * Custom edge properties can be specified in terms of
  * the type to be used and the name to be used
  */
struct EdgeProperty
{
     string type;
     string name;
}

/**
  * Template that evaluates to the string of code
  * describing the custom edge property fields
  */
template multipleEdgeProperties (EdgePropertyList...)
{
     static if (EdgePropertyList.length == 0)
     {
         const multipleEdgeProperties = ``;
     }
     else static if (EdgePropertyList.length == 1)
     {
         const multipleEdgeProperties = 
singleEdgeProperty!(EdgePropertyList[0]);
     }
     else
     {
         const multipleEdgeProperties = 
singleEdgeProperty!(EdgePropertyList[0]) ~ `\n` ~ 
multipleEdgeProperties!(EdgePropertyList[1 .. $]);
     }
}

/**
  * Template that evaluates to the string of code
  * for a single edge property field
  */
template singleEdgeProperty (EdgeProperty property)
{
     const singleEdgeProperty = `public ` ~ property.type ~ `[] ` 
~ property.name ~ `;`;
}

//////////////////////////////////////////


There are two things I'm wondering about the above design:

   * is it possible to redesign EdgeProperty as a template that 
would take
     a type parameter directly rather than as a string, so one 
could write
     e.g. EdgeProperty!("weight", double) ... ?

   * is it possible to rework things to avoid the string mixins 
...?

My metaprogramming-fu is a bit rusty these days (one reason for 
pursuing this project is to try and start exercising it 
again...), so I'd be very grateful for any thoughts or advice 
anyone could offer (on the above questions or the design in 
general).

Thanks & best wishes,

      -- Joe


More information about the Digitalmars-d-learn mailing list