• boost graph之bgl_named_params


    简介

    bgl_named_params作为graph中的有名参数

    定义

    宏BOOST_BGL_ONE_PARAM_REF

    有两个定义,一个定义在类bgl_named_params中用于定义成员方法,另外一个作为全局方法
    bgl_named_params类中的成员方法

    #define BOOST_BGL_ONE_PARAM_REF(name, key) \
        template <typename PType> \
        bgl_named_params<boost::reference_wrapper<PType>, BOOST_PP_CAT(key, _t), self> \
        name(PType& p) const { \
          typedef bgl_named_params<boost::reference_wrapper<PType>, BOOST_PP_CAT(key, _t), self> Params; \
          return Params(boost::ref(p), *this); \
        } \
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    全局方法

    #define BOOST_BGL_ONE_PARAM_REF(name, key) \
        template <typename PType> \
        bgl_named_params<boost::reference_wrapper<PType>, BOOST_PP_CAT(key, _t)> \
        name(PType& p) { \
          typedef bgl_named_params<boost::reference_wrapper<PType>, BOOST_PP_CAT(key, _t)> Params; \
          return Params(boost::ref(p)); \
        } \
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    宏BOOST_BGL_ONE_PARAM_CREF

    有两个定义,一个定义在类bgl_named_params中用于定义成员方法,另外一个作为全局方法
    bgl_named_params类中的成员方法

    #define BOOST_BGL_ONE_PARAM_CREF(name, key) \
        template <typename PType> \
        bgl_named_params<PType, BOOST_PP_CAT(key, _t), self> \
        name(const PType& p) const { \
          typedef bgl_named_params<PType, BOOST_PP_CAT(key, _t), self> Params; \
          return Params(p, *this); \
        } \
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    全局方法

    #define BOOST_BGL_ONE_PARAM_CREF(name, key) \
        template <typename PType> \
        bgl_named_params<PType, BOOST_PP_CAT(key, _t)> \
        name(const PType& p) { \
          typedef bgl_named_params<PType, BOOST_PP_CAT(key, _t)> Params; \
          return Params(p); \
        } \
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    宏BOOST_BGL_DECLARE_NAMED_PARAMS

    是一系列BOOST_BGL_ONE_PARAM_CREF集,定义的方法名包含

    • weight_map
      对应类型为edge_weight_t
    • weight_map2
      对应类型为edge_weight2_t
    • distance_map
      对应类型为vertex_distance_t
    • distance_map2
      对应类型为vertex_distance2_t
    • predecessor_map
      对应类型为vertex_predecessor_t
    • rank_map
      对应类型为vertex_rank_t
    • root_map
      对应类型为vertex_root_t
    • root_vertex
      对应类型为root_vertex_t
    • edge_centrality_map
      对应类型为edge_centrality_t
    • centrality_map
      对应类型为vertex_centrality_t
    • parity_map
      对应类型为parity_map_t
    • color_map
      对应类型为vertex_color_t
    • edge_color_map
      对应类型为edge_color_t
    • capacity_map
      对应类型为edge_capacity_t
    • residual_capacity_map
      对应类型为edge_residual_capacity_t
    • reverse_edge_map
      对应类型为edge_reverse_t
    • discover_time_map
      对应类型为vertex_discover_time_t
    • lowpoint_map
      对应类型为vertex_lowpoint_t
    • vertex_index_map
      对应类型为vertex_index_t
    • vertex_index1_map
      对应类型为vertex_index1_t
    • vertex_index2_map
      对应类型为vertex_index2_t
    • vertex_assignment_map
      对应类型为vertex_assignment_map_t
    • visitor
      对应类型为graph_visitor_t
    • distance_compare
      对应类型为distance_compare_t
    • distance_combine
      对应类型为distance_combine_t
    • distance_inf
      对应类型为distance_inf_t
    • distance_zero
      对应类型为distance_zero_t
    • edge_copy
      对应类型为edge_copy_t
    • vertex_copy
      对应类型为vertex_copy_t
    • buffer
      对应类型为buffer_param_t
    • orig_to_copy
      对应类型为orig_to_copy_t
    • isomorphism_map
      对应类型为vertex_isomorphism_t
    • vertex_invariant
      对应类型为vertex_invariant_t
    • vertex_invariant1
      对应类型为vertex_invariant1_t
    • vertex_invariant2
      对应类型为vertex_invariant2_t
    • vertex_max_invariant
      对应类型为vertex_max_invariant_t
    • polling
      对应类型为polling_t
    • lookahead
      对应类型为lookahead_t
    • in_parallel
      对应类型为in_parallel_t
    • displacement_map
      对应类型为vertex_displacement_t
    • attractive_force
      对应类型为attractive_force_t
    • repulsive_force
      对应类型为repulsive_force_t
    • force_pairs
      对应类型为force_pairs_t
    • cooling
      对应类型为cooling_t
    • iterations
      对应类型为iterations_t
    • diameter_range
      对应类型为diameter_range_t
    • learning_constant_range
      对应类型为learning_constant_range_t
    • vertices_equivalent
      对应类型为vertices_equivalent_t
    • edges_equivalent
      对应类型为edges_equivalent_t
    • index_in_heap_map
      对应类型为index_in_heap_map_t
    • max_priority_queue
      对应类型为max_priority_queue_t
    #define BOOST_BGL_DECLARE_NAMED_PARAMS \
        BOOST_BGL_ONE_PARAM_CREF(weight_map, edge_weight) \
        BOOST_BGL_ONE_PARAM_CREF(weight_map2, edge_weight2) \
        BOOST_BGL_ONE_PARAM_CREF(distance_map, vertex_distance) \
        BOOST_BGL_ONE_PARAM_CREF(distance_map2, vertex_distance2) \
        BOOST_BGL_ONE_PARAM_CREF(predecessor_map, vertex_predecessor) \
        BOOST_BGL_ONE_PARAM_CREF(rank_map, vertex_rank) \
        BOOST_BGL_ONE_PARAM_CREF(root_map, vertex_root) \
        BOOST_BGL_ONE_PARAM_CREF(root_vertex, root_vertex) \
        BOOST_BGL_ONE_PARAM_CREF(edge_centrality_map, edge_centrality) \
        BOOST_BGL_ONE_PARAM_CREF(centrality_map, vertex_centrality) \
        BOOST_BGL_ONE_PARAM_CREF(parity_map, parity_map) \
        BOOST_BGL_ONE_PARAM_CREF(color_map, vertex_color) \
        BOOST_BGL_ONE_PARAM_CREF(edge_color_map, edge_color) \
        BOOST_BGL_ONE_PARAM_CREF(capacity_map, edge_capacity) \
        BOOST_BGL_ONE_PARAM_CREF(residual_capacity_map, edge_residual_capacity) \
        BOOST_BGL_ONE_PARAM_CREF(reverse_edge_map, edge_reverse) \
        BOOST_BGL_ONE_PARAM_CREF(discover_time_map, vertex_discover_time) \
        BOOST_BGL_ONE_PARAM_CREF(lowpoint_map, vertex_lowpoint) \
        BOOST_BGL_ONE_PARAM_CREF(vertex_index_map, vertex_index) \
        BOOST_BGL_ONE_PARAM_CREF(vertex_index1_map, vertex_index1) \
        BOOST_BGL_ONE_PARAM_CREF(vertex_index2_map, vertex_index2) \
        BOOST_BGL_ONE_PARAM_CREF(vertex_assignment_map, vertex_assignment_map) \
        BOOST_BGL_ONE_PARAM_CREF(visitor, graph_visitor) \
        BOOST_BGL_ONE_PARAM_CREF(distance_compare, distance_compare) \
        BOOST_BGL_ONE_PARAM_CREF(distance_combine, distance_combine) \
        BOOST_BGL_ONE_PARAM_CREF(distance_inf, distance_inf) \
        BOOST_BGL_ONE_PARAM_CREF(distance_zero, distance_zero) \
        BOOST_BGL_ONE_PARAM_CREF(edge_copy, edge_copy) \
        BOOST_BGL_ONE_PARAM_CREF(vertex_copy, vertex_copy) \
        BOOST_BGL_ONE_PARAM_REF(buffer, buffer_param) \
        BOOST_BGL_ONE_PARAM_CREF(orig_to_copy, orig_to_copy) \
        BOOST_BGL_ONE_PARAM_CREF(isomorphism_map, vertex_isomorphism) \
        BOOST_BGL_ONE_PARAM_CREF(vertex_invariant, vertex_invariant) \
        BOOST_BGL_ONE_PARAM_CREF(vertex_invariant1, vertex_invariant1) \
        BOOST_BGL_ONE_PARAM_CREF(vertex_invariant2, vertex_invariant2) \
        BOOST_BGL_ONE_PARAM_CREF(vertex_max_invariant, vertex_max_invariant) \
        BOOST_BGL_ONE_PARAM_CREF(polling, polling) \
        BOOST_BGL_ONE_PARAM_CREF(lookahead, lookahead) \
        BOOST_BGL_ONE_PARAM_CREF(in_parallel, in_parallel) \
        BOOST_BGL_ONE_PARAM_CREF(displacement_map, vertex_displacement) \
        BOOST_BGL_ONE_PARAM_CREF(attractive_force, attractive_force) \
        BOOST_BGL_ONE_PARAM_CREF(repulsive_force, repulsive_force) \
        BOOST_BGL_ONE_PARAM_CREF(force_pairs, force_pairs) \
        BOOST_BGL_ONE_PARAM_CREF(cooling, cooling) \
        BOOST_BGL_ONE_PARAM_CREF(iterations, iterations) \
        BOOST_BGL_ONE_PARAM_CREF(diameter_range, diameter_range) \
        BOOST_BGL_ONE_PARAM_CREF(learning_constant_range, learning_constant_range) \
        BOOST_BGL_ONE_PARAM_CREF(vertices_equivalent, vertices_equivalent) \
        BOOST_BGL_ONE_PARAM_CREF(edges_equivalent, edges_equivalent) \
        BOOST_BGL_ONE_PARAM_CREF(index_in_heap_map, index_in_heap_map) \
        BOOST_BGL_ONE_PARAM_REF(max_priority_queue, max_priority_queue)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52

    bgl_named_params定义

    template <typename T, typename Tag, typename Base = no_property>
      struct bgl_named_params
      {
        typedef bgl_named_params self;
        typedef Base next_type;
        typedef Tag tag_type;
        typedef T value_type;
        bgl_named_params(T v = T()) : m_value(v) { }
        bgl_named_params(T v, const Base& b) : m_value(v), m_base(b) { }
        T m_value;
        Base m_base;
    
    	BOOST_BGL_DECLARE_NAMED_PARAMS
    	
        // Duplicate
        template <typename PType>
        bgl_named_params<PType, vertex_color_t, self>
        vertex_color_map(const PType& p) const {return this->color_map(p);}
      };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    bgl_named_params中方法创建新的bgl_named_params,其中m_base为旧bgl_named_params,也就是嵌套结构

    获取子项

    get_param

    通过函数模板get_param指定tag来获取子项

    template <class Tag, typename Args>
      inline
      const typename lookup_named_param_def<Tag, Args, param_not_found>::type&
      get_param(const Args& p, Tag) {
        return lookup_named_param_def<Tag, Args, param_not_found>::get(p, param_not_found());
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    发现其内部是调用lookup_named_param_def的get方法

    lookup_named_param_def

    有三个模板
    第一个模板是针对非bgl_named_params类型时,返回 的是def类型默认值

     template <typename Tag, typename Args, typename Def>
      struct lookup_named_param_def {
        typedef Def type;
        static const Def& get(const Args&, const Def& def) {return def;}
      };
    
    • 1
    • 2
    • 3
    • 4
    • 5

    第二个模板是表示与bgl_named_params类型匹配时,直接取bgl_named_params的成员m_value

    template <typename T, typename Tag, typename Base, typename Def>
      struct lookup_named_param_def<Tag, bgl_named_params<T, Tag, Base>, Def> {
        typedef T type;
        static const type& get(const bgl_named_params<T, Tag, Base>& p, const Def&) {
          return p.m_value;
        }
      };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    第三个模板是匹配最外层bgl_named_params时,嵌套调用内层bgl_named_params

      template <typename Tag1, typename T, typename Tag, typename Base, typename Def>
      struct lookup_named_param_def<Tag1, bgl_named_params<T, Tag, Base>, Def> {
        typedef typename lookup_named_param_def<Tag1, Base, Def>::type type;
        static const type& get(const bgl_named_params<T, Tag, Base>& p, const Def& def) {
          return lookup_named_param_def<Tag1, Base, Def>::get(p.m_base, def);
        }
      };
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
  • 相关阅读:
    Kotlin中使用ViewBinding绑定控件并添加点击事件
    最新版CleanMyMac X4.13.4中文版Mac清理软件
    Blender程序化建模简明教程【PCG】
    DDD领域驱动设计-子域
    详述MIMIC 的ICU患者检测时间信息表(十六)
    smallWhiteDot Tech Suppor
    设计模式与应用:原型模式
    0 upgraded, 0 newly installed, 0 to remove and 112 not upgraded解决方法
    国家网络安全周2023时间是什么时候?有什么特点?谁举办的?
    CSS之伪类和伪元素 | :before和::before
  • 原文地址:https://blog.csdn.net/wuli2496/article/details/133819393