我有一堆 boost::property_map s 定义图中边遍历的成本。我正在执行这些 map 的不同加权组合的算法,目前通过手动执行 totalcost = weight1*weight1_factor + weight2*weight2_factor + ... .虽然属性图的数量在增加,但要像这样总结它们变得很麻烦。
因此,我设想创建一个聚合类,其中包含某种所有 map 的集合。但是,它们的模板不同,如 boost::property_map<Graph, PropertyTag> , 其中PropertyTag map 之间有所不同。因为他们都支持operator[](edge_escriptor) , 我可以使用一些技巧,还是我注定要使用 boost::any ?
最佳答案
我建议您创建一个 boost::tuple具有属性映射和因子。
鉴于在您的上下文中,您有每个 i 的 (propertymap_i, weight_i),创建一个聚合类,它使用此元组(作为聚合类的模板参数)在被询问时计算所需的值。
您可以使用 operator[] 或属性映射提供的 get() 函数。
如果需要,我可能会更清楚,但它必须稍等一下。
编辑:这是否接近您的需要?
#include <boost/graph/adjacency_list.hpp>
#include <iostream>
#include <boost/shared_ptr.hpp>
namespace property_aggregator
{
template<typename Tuple, typename T>
struct helper
{
double operator()(Tuple const& tuple, T t)
{
return boost::get<0>(tuple)*get(boost::get<1>(tuple), t) +
helper<typename Tuple::tail_type::tail_type, T>()(tuple.get_tail().get_tail(), t);
}
};
template<typename T>
struct helper<boost::tuples::null_type, T>
{
double operator()(boost::tuples::null_type const& tuple, T t)
{
return 0.;
}
};
template<typename T>
class BasePropertyAggregator
{
public:
virtual double compute( T t ) = 0;
};
template <typename PropertyTuple, typename T>
class PropertyAggregator : public BasePropertyAggregator<T>
{
public:
PropertyAggregator(PropertyTuple const& tuple) : m_tuple(tuple){}
virtual ~PropertyAggregator(){}
double compute( T t )
{
return property_aggregator::helper<PropertyTuple, T>()(m_tuple, t);
}
private:
PropertyTuple m_tuple;
};
}
template<typename T>
class PropertyAggregator
{
public:
template<typename Tuple>
PropertyAggregator(Tuple const& tuple) : m_computer(new property_aggregator::PropertyAggregator<Tuple, T>(tuple))
{}
double operator()(T t)
{
return m_computer->compute(t);
}
private:
boost::shared_ptr<property_aggregator::BasePropertyAggregator<T> > m_computer;
};
// Defaut type of a graph
typedef boost::adjacency_list<boost::listS, boost::vecS, boost::directedS,
boost::property<boost::vertex_index_t, unsigned int>,
boost::property<boost::edge_weight_t, double,
boost::property<boost::edge_color_t, double> > > Graph;
int main()
{
typedef boost::property_map<Graph, boost::edge_weight_t>::type PM1;
typedef boost::property_map<Graph, boost::edge_color_t>::type PM2;
typedef boost::graph_traits<Graph>::edge_descriptor EdgeType;
Graph g;
PM1 pm1 = get(boost::edge_weight, g);
PM2 pm2 = get(boost::edge_color, g);
add_vertex(g);
add_vertex(g);
EdgeType edge1 = boost::add_edge(0, 1, g).first;
put(pm1, edge1, 1.);
put(pm2, edge1, 2.);
typedef PropertyAggregator<EdgeType> ComboType;
ComboType combo1(boost::make_tuple(1., pm1));
ComboType combo2(boost::make_tuple(1., pm2));
ComboType combo3(boost::make_tuple(1., pm1, 2., pm2));
std::cout << "-- " << combo1(edge1) << std::endl;
std::cout << "-- " << combo2(edge1) << std::endl;
std::cout << "-- " << combo3(edge1) << std::endl;
return 0;
}
关于c++ - 模板异构类型的集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7192596/
我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server
我正在使用puppet为ruby程序提供一组常量。我需要提供一组主机名,我的程序将对其进行迭代。在我之前使用的bash脚本中,我只是将它作为一个puppet变量hosts=>"host1,host2"我将其提供给bash脚本作为HOSTS=显然这对ruby不太适用——我需要它的格式hosts=["host1","host2"]自从phosts和putsmy_array.inspect提供输出["host1","host2"]我希望使用其中之一。不幸的是,我终其一生都无法弄清楚如何让它发挥作用。我尝试了以下各项:我发现某处他们指出我需要在函数调用前放置“function_”……这
我可以得到Infinity和NaNn=9.0/0#=>Infinityn.class#=>Floatm=0/0.0#=>NaNm.class#=>Float但是当我想直接访问Infinity或NaN时:Infinity#=>uninitializedconstantInfinity(NameError)NaN#=>uninitializedconstantNaN(NameError)什么是Infinity和NaN?它们是对象、关键字还是其他东西? 最佳答案 您看到打印为Infinity和NaN的只是Float类的两个特殊实例的字符串
我不确定传递给方法的对象的类型是否正确。我可能会将一个字符串传递给一个只能处理整数的函数。某种运行时保证怎么样?我看不到比以下更好的选择:defsomeFixNumMangler(input)raise"wrongtype:integerrequired"unlessinput.class==FixNumother_stuffend有更好的选择吗? 最佳答案 使用Kernel#Integer在使用之前转换输入的方法。当无法以任何合理的方式将输入转换为整数时,它将引发ArgumentError。defmy_method(number)
有时我需要处理键/值数据。我不喜欢使用数组,因为它们在大小上没有限制(很容易不小心添加超过2个项目,而且您最终需要稍后验证大小)。此外,0和1的索引变成了魔数(MagicNumber),并且在传达含义方面做得很差(“当我说0时,我的意思是head...”)。散列也不合适,因为可能会不小心添加额外的条目。我写了下面的类来解决这个问题:classPairattr_accessor:head,:taildefinitialize(h,t)@head,@tail=h,tendend它工作得很好并且解决了问题,但我很想知道:Ruby标准库是否已经带有这样一个类? 最佳
我正在尝试解析一个CSV文件并使用SQL命令自动为其创建一个表。CSV中的第一行给出了列标题。但我需要推断每个列的类型。Ruby中是否有任何函数可以找到每个字段中内容的类型。例如,CSV行:"12012","Test","1233.22","12:21:22","10/10/2009"应该产生像这样的类型['integer','string','float','time','date']谢谢! 最佳答案 require'time'defto_something(str)if(num=Integer(str)rescueFloat(s
我正在玩HTML5视频并且在ERB中有以下片段:mp4视频从在我的开发环境中运行的服务器很好地流式传输到chrome。然而firefox显示带有海报图像的视频播放器,但带有一个大X。问题似乎是mongrel不确定ogv扩展的mime类型,并且只返回text/plain,如curl所示:$curl-Ihttp://0.0.0.0:3000/pr6.ogvHTTP/1.1200OKConnection:closeDate:Mon,19Apr201012:33:50GMTLast-Modified:Sun,18Apr201012:46:07GMTContent-Type:text/plain
如何将send与+=一起使用?a=20;a.send"+=",10undefinedmethod`+='for20:Fixnuma=20;a+=10=>30 最佳答案 恐怕你不能。+=不是方法,而是语法糖。参见http://www.ruby-doc.org/docs/ProgrammingRuby/html/tut_expressions.html它说Incommonwithmanyotherlanguages,Rubyhasasyntacticshortcut:a=a+2maybewrittenasa+=2.你能做的最好的事情是:
我正在使用Mandrill的RubyAPIGem并使用以下简单的测试模板:testastic按照Heroku指南中的示例,我有以下Ruby代码:require'mandrill'm=Mandrill::API.newrendered=m.templates.render'test-template',[{:header=>'someheadertext',:main_section=>'Themaincontentblock',:footer=>'asdf'}]mail(:to=>"JaysonLane",:subject=>"TestEmail")do|format|format.h
//1.验证返回状态码是否是200pm.test("Statuscodeis200",function(){pm.response.to.have.status(200);});//2.验证返回body内是否含有某个值pm.test("Bodymatchesstring",function(){pm.expect(pm.response.text()).to.include("string_you_want_to_search");});//3.验证某个返回值是否是100pm.test("Yourtestname",function(){varjsonData=pm.response.json