How to create a directed graph at compile time?

I want to build a directed graph in C ++ 11 at compile time.

Example: I have some threads and queues and you want to create:

+-------+            +---------+            +-------+
| f_gen | -> QGen -> | f_check | -> QOut -> | f_out |
+-------+            +---------+    ^       +-------+
                          |         |
                         \|/        |
                          |         |
                        QProc       |
                          |         |
                         \|/        |
                          |         |
                     +-----------+  |
                     | f_process | /
                     +-----------+

Note that this is just an example: a solution should handle oriented graphs of each node / edge type.

I want to write this, maybe as:

make_directed_graph<Queue, Thread>(
// Queues
{
   // ID, Type of Queue, queue size
   { 0, std::string, 100 }, // QGen
   { 1, int, 250 },         // QProc
   { 2, std::string, 500 }  // QOut
},
// Threads
{ 
   // Fn, thread cnt, in queues, out queues
   { f_gen, 5, {}, { qref(0) } }, // ID 1: QGen 
   { f_check, 30, { qref(0) }, { qref(1), qref(2) }}, // IDs of queues
   { f_process, 75, { qref(1) }, { qref(2) }},
   { f_out, 12, { qref(2) }, {} }
});

Please note that this is just an idea - any other opportunity to record it is good for me.

I managed to implement a function make_tree. It can be used as

make_tree< arexp, int >(
     { '+', { 1, 2, { '*', { 3, 4, 5 } } } } )

There is one big difference: nodes and edges can be created "on fly '- there is no need to refer to any existing one.

- / /, . : ( ).

:

  • ?
  • , , ?
+4
2

, , , .

Queue<std::string> QGen(100); // No need for an ID, we have &QGen.
// We  *do* need to pass the Queue type to figure out the type of &QGen.
Thread<void, nullptr, std::string, &QGen> f_gen(5);

, .

+1

, ( ):

, , - . , , :

directed_graph const dg(
  make_directed_graph<Node, Edge>(
     {
        { 1, "qgen" },
        { 2, "qproc" },
        { 3, "qout" },
        { "fgen", {}, { 1 } },
        { "fcheck", { 1 }, { 2, 3 } },
        { "fproc", { 2 }, { 3 } },
        { "fout", { 3 }, {} }
     }));

make_directed_graph initializer_list:

class NodeRef {
public:
   NodeRef(int nid);
};

class digraph_initializer {
public:
   digraph_initializer(int id, std::string const & s);
   digraph_initializer(
      std::string const & s,
      std::initializer_list< NodeRef > const nr_in,
      std::initializer_list< NodeRef > const nr_out);
};

class directed_graph {
};

template< typename TNode, typename TEdge >
directed_graph make_directed_graph(
   std::initializer_list<digraph_initializer> const & dgi);

:

  • , , ?
  • "" initializer_list .
  • () / /.
0

Source: https://habr.com/ru/post/1536435/


All Articles