📄 generic.h.svn-base
字号:
#ifndef GENERIC_H#define GENERIC_H#include <cassert>#include <boost/graph/graph_traits.hpp>#include <boost/bind.hpp>#include <boost/random.hpp>#include <boost/timer.hpp>class Experiment;extern boost::lagged_fibonacci607 fibonnacci_number_generator;extern boost::timer global_timer;//extern boost::uniform_01<boost::lagged_fibonacci607 > uniform_distribution_0_1;extern boost::variate_generator <boost::lagged_fibonacci607 &, boost::uniform_real < > > uniform_distribution_0_1;extern bool monitoring;extern Experiment * current_experiment;extern unsigned int mrf_rows;extern unsigned int mrf_columns;template <class A>inline A ReturnSelf (A a){ return a; }template <class A>inline void NaturalSetDouble (A & a, double b){ a = b;}template <class Graph, class F, class ReturnType, class Descriptor>class AdaptGraphDescriptor : public std::unary_function < Descriptor, void>{private: Graph & g; const F f;public: AdaptGraphDescriptor (Graph & a, const F b) : g(a), f(b) // here copy-constructor is called from b ! pass-by-value { } ReturnType operator()(Descriptor x) const{ return f( g[x]) ;}};// Two next functions are helper functions (global functions)template < class RT, class A, class B>AdaptGraphDescriptor <A, B, RT, typename boost::graph_traits<A>::edge_descriptor >adapt_graph_edges (A & graph, const B & function_object){ return AdaptGraphDescriptor <A, B, RT, typename boost::graph_traits<A>::edge_descriptor > (graph, function_object);}template <class RT, class A, class B>AdaptGraphDescriptor <A, B, RT, typename boost::graph_traits<A>::vertex_descriptor >adapt_graph_vertexes (A & graph, const B & function_object){ return AdaptGraphDescriptor <A, B, RT, typename boost::graph_traits<A>::vertex_descriptor > (graph, function_object);} // End helper functions// The make_product_over algorithm will return a double by making a product over a range.// Template parameters: the InputIterator (what range, ie list, etc...) and the UnaryFunction (how to convert from the object type to a double)template <class InputIterator, class GetDouble >double make_product_over ( InputIterator begin, InputIterator end, const GetDouble & f){ double a = 1.0; for (InputIterator it = begin; it != end; it++) { a = a * f(*it); } //ConvToDouble < typename ConvToDouble::argument_type, ConvToDouble> t(f); //return std::accumulate(begin,end,1.0,t); return a; }// The normalize_over algorithm will normalize over a range.// It needs two function objects, one for reading from the object (ConvDouble), and the other for writing (Div) to the object// We could make that nicer if functions allowed default tempalte arguments, now the default arguments are useless which is a little bit stupidtemplate <class InputIterator, class GetDouble, class SetDouble>void normalize_over ( InputIterator begin, InputIterator end, const GetDouble & f = &ReturnSelf<double>, const SetDouble & g = &NaturalSetDouble <double>){ double a = 0.0; for (InputIterator it = begin; it != end; it++) { a += f(*it); } assert (0.0 != a); for (InputIterator it = begin; it != end; it++) { g(*it,f(*it)/a); }}template <class InputIterator>bool fast_double_normalize_over ( InputIterator begin, InputIterator end ){ double a (0.0); for (InputIterator it = begin; it != end; it++) { a += *it; } if ( 0.0 == a) { return false; } for (InputIterator it = begin; it != end; it++) { *it = *it/a; } return true;}// Reduce_over doesn't exactly normalize (although by normalizing the same requested effect would be obtained). It just finds the maximum of a group and divide by that maximum every element. This, for example, allows messages not to overflow.template <class InputIterator, class GetDouble, class SetDouble>void reduce_over ( InputIterator begin, InputIterator end, const GetDouble & f = &ReturnSelf<double>, const SetDouble & g = &NaturalSetDouble <double> ){ double a (0.0); double b; for (InputIterator it = begin; it != end; it++) { b = f(*it); if (f(*it) > a) { a = b; } } assert (a != 0); for (InputIterator it = begin; it != end; it++) { g(*it,f(*it)/a); }}template <class InputIterator>void fast_double_reduce_over ( InputIterator begin, InputIterator end ){ double a (0.0); for (InputIterator it = begin; it != end; it++) { if (*it > a) { a = *it; } } assert (a != 0); for (InputIterator it = begin; it != end; it++) { *it = *it/a; }}#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -