lundi 9 octobre 2023

How to copy vertex properties of boost graph which contains a graph itself?

I have a boost graph with custom properties. I want to make a copy of it. I tried it by following way.

using BGType = boost::adjacency_list<boost::vecS, boost::vecS, boost::bidirectionalS,
                                     // Vertex Properties...
                                     vertexProps,
                                     // Edge Propereties...
                                     edgeProps,
                                     // Graph Properties
                                     graphProps>;

vertexProps.h

class vertexProps {
   public:
    explicit vertexProps(const std::string *moduleName = nullptr, const std::string *name = nullptr,
                            long refPtr = 0 )
     : _refPtr(refPtr),
    {
        _moduleName = moduleName ? *moduleName : "";
        _name = name ? *name : "";
    };
   std::string _moduleName;
   std::string _name;
   BGType *_subGraph = nullptr;
   BGType *_graph = nullptr;
struct CustomVertexCopy {
    BGType const &g1;
    BGType &g2;
    void operator()(BGType::vertex_descriptor v1, BGType::vertex_descriptor v2) const
    {
        schVertexProps const &p1 = g1[v1];
        schVertexProps &p2 = g2[v2];
        p2._subGraph = p1._subGraph;
        p2._graph = p1._graph;
        p2._moduleName = p1._moduleName;
        p2._name = p1._name;
    }
};

edgeProps.h

class edgeProps {
   public:
    explicit edgeProps(std::string name = "")
        : _name(name){};
    std::string _name;
};

struct CustomEdgeCopy {
    BGType const &g1;
    BGType &g2;

    void operator()(BGType::edge_descriptor e1, BGType::edge_descriptor e2) const { g2[e2]._name = g1[e1]._name; }
};

someFunction.cpp

OnClick(BGType* bgNew)
{
   // some code
  BGType* oldBg = new BGType;
  boost::copy_graph(
                  *bgNew, *oldBg,
                  boost::vertex_copy(CustomVertexCopy{*bgNew, *oldBg}).edge_copy(CustomEdgeCopy{*bgNew, *oldBg}));
  boost::get_property(*oldBg) = boost::get_property(*bgNew);
  // Copying graph properties
  DeepCopyOfBG(bgNew, oldBg);
}

void someFunction::DeepCopyOfBG(BGType* bGraph, BGType* oldBg)
{
    // Iterate through the source map and copy its contents
    for (const auto& entry : (*bGraph)[boost::graph_bundle]._modInfo) {
        const std::string& key = entry.first;
        const auto& value = entry.second;

        // Create a deep copy of value (a tuple containing vectors of schPinInfo)
        std::tuple<std::vector<schPinInfo*>, std::vector<schPinInfo*>, std::vector<schPinInfo*>> deepCopyValue = value;

        // Add the deep copy to the target map
        (*oldBg)[boost::graph_bundle]._modInfo[key] = deepCopyValue;
    }
    // some more properties.
}

Above way working fine. But it has 1 issue. Vertex property has 1 field which itself is a boost graph.

p2._subGraph = p1._subGraph;

And above line merely copying pointers. So in old boost graph, I am getting new boost graph's sub graph which I dont want. So how to deep copy this _subGraph field ?

Aucun commentaire:

Enregistrer un commentaire