C++ 如何在编译时建立有向图?
我想在编译时在C++11中构建一个有向图 示例:我有一些线程和队列,希望建立:C++ 如何在编译时建立有向图?,c++,c++11,compile-time,C++,C++11,Compile Time,我想在编译时在C++11中构建一个有向图 示例:我有一些线程和队列,希望建立: +-------+ +---------+ +-------+ | f_gen | -> QGen -> | f_check | -> QOut -> | f_out | +-------+ +---------+ ^ +-------+ |
+-------+ +---------+ +-------+
| f_gen | -> QGen -> | f_check | -> QOut -> | f_out |
+-------+ +---------+ ^ +-------+
| |
\|/ |
| |
QProc |
| |
\|/ |
| |
+-----------+ |
| f_process | /
+-----------+
请注意,这只是一个示例:解决方案应该处理
每个节点/边类型的有向图
我想这样写:
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) }, {} }
});
这里有一个很大的区别:节点和边可以“在”上创建
苍蝇”-没有必要引用任何现有的苍蝇
有向图的最大问题是如何引用对象/
先前定义的结构/零件。比如:如何引用
定义线程时排队(反之亦然)
我的问题是:
这通常是可能的,因为您使用指针标识对象,而这些指针是有效的非类型模板参数
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);
队列QGen(100);//不需要身份证,我们有&QGen。
//我们*确实*需要传递队列类型以确定&QGen的类型。
螺纹f_gen(5);
当然,不能用这种方式定义循环图 我认为我离解决方案还有一步(但尚未完成): 我的错误是,没有必要直接写下图形本身,而是某种形式的表示。至少编译以下源代码:
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 }, {} }
}));
有向图常数dg(
生成有向图(
{
{1,“qgen”},
{2,“qproc”},
{3,“qout”},
{“fgen”,{},{1},
{“fcheck”{1},{2,3},
{“fproc”{2},{3},
{“fout”,{3},{}
}));
给出了make_定向图和相应初始值设定项列表的以下定义:
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);
class节点ref{
公众:
NodeRef(int-nid);
};
类有向图\初始值设定项{
公众:
有向图_初始值设定项(int-id,std::string-const&s);
有向图初始化器(
标准::字符串常量&s,
标准::初始值设定项列表中的常数,
标准::初始值设定项列表const nr\u out);
};
类有向图{
};
模板
有向图生成有向图(
std::初始值设定项(列表常量和dgi);
还有一些问题有待解决:
不知怎的,它让我想起:)你真的想要二元图和三元图吗?注意,它们是C和C++的实际特征。HMMM:看“有向图”这个词有不止一种用法:用“有向图”重新定义。“拥有”任意的图形表示与使用它不一样。对于元处理,我通常建议从函数重载集而不是指针形成结构。但是我们真的需要知道手头的问题。为什么不只是有一个队列列表,然后创建一些使用这些队列的线程呢?为什么要额外努力将其建模为编译时有向图?使用extern对循环图进行正向声明。@brianbeuning:行不通。即使使用
extern
也需要指定类型。在这种情况下,类型本身将具有循环依赖关系。这并不能回答以下问题:我需要一个有向图(object)(由“make_directed_graph”返回)而不是一系列队列和线程。然而,答案解决了类型处理/规格的问题。
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);