COIN-OR::LEMON - Graph Library

Changeset 1193:c8fa41fcc4a7 in lemon for lemon/smart_graph.h


Ignore:
Timestamp:
12/01/11 09:05:47 (12 years ago)
Author:
Balazs Dezso <deba@…>
Branch:
default
Phase:
public
Message:

Type safe red and blue node set (#69)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • lemon/smart_graph.h

    r1191 r1193  
    855855    };
    856856
     857    class RedNode : public Node {
     858      friend class SmartBpGraphBase;
     859    protected:
     860
     861      explicit RedNode(int pid) : Node(pid) {}
     862
     863    public:
     864      RedNode() {}
     865      RedNode(const RedNode& node) : Node(node) {}
     866      RedNode(Invalid) : Node(INVALID){}
     867    };
     868
     869    class BlueNode : public Node {
     870      friend class SmartBpGraphBase;
     871    protected:
     872
     873      explicit BlueNode(int pid) : Node(pid) {}
     874
     875    public:
     876      BlueNode() {}
     877      BlueNode(const BlueNode& node) : Node(node) {}
     878      BlueNode(Invalid) : Node(INVALID){}
     879    };
     880
    857881    class Edge {
    858882      friend class SmartBpGraphBase;
     
    914938    bool blue(Node n) const { return !nodes[n._id].red; }
    915939
     940    static RedNode asRedNodeUnsafe(Node n) { return RedNode(n._id); }
     941    static BlueNode asBlueNodeUnsafe(Node n) { return BlueNode(n._id); }
     942
    916943    Node source(Arc a) const { return Node(arcs[a._id ^ 1].target); }
    917944    Node target(Arc a) const { return Node(arcs[a._id].target); }
    918945
    919     Node redNode(Edge e) const { return Node(arcs[2 * e._id].target); }
    920     Node blueNode(Edge e) const { return Node(arcs[2 * e._id + 1].target); }
     946    RedNode redNode(Edge e) const {
     947      return RedNode(arcs[2 * e._id].target);
     948    }
     949    BlueNode blueNode(Edge e) const {
     950      return BlueNode(arcs[2 * e._id + 1].target);
     951    }
    921952
    922953    static bool direction(Arc a) {
     
    936967    }
    937968
    938     void firstRed(Node& node) const {
     969    void first(RedNode& node) const {
    939970      node._id = first_red;
    940971    }
    941972
    942     void nextRed(Node& node) const {
     973    void next(RedNode& node) const {
    943974      node._id = nodes[node._id].partition_next;
    944975    }
    945976
    946     void firstBlue(Node& node) const {
     977    void first(BlueNode& node) const {
    947978      node._id = first_blue;
    948979    }
    949980
    950     void nextBlue(Node& node) const {
     981    void next(BlueNode& node) const {
    951982      node._id = nodes[node._id].partition_next;
    952983    }
     
    10061037
    10071038    static int id(Node v) { return v._id; }
    1008     int redId(Node v) const {
    1009       LEMON_DEBUG(nodes[v._id].red, "Node has to be red");
    1010       return nodes[v._id].partition_index;
    1011     }
    1012     int blueId(Node v) const {
    1013       LEMON_DEBUG(!nodes[v._id].red, "Node has to be blue");
    1014       return nodes[v._id].partition_index;
    1015     }
     1039    int id(RedNode v) const { return nodes[v._id].partition_index; }
     1040    int id(BlueNode v) const { return nodes[v._id].partition_index; }
    10161041    static int id(Arc e) { return e._id; }
    10171042    static int id(Edge e) { return e._id; }
     
    10311056    }
    10321057
    1033     Node addRedNode() {
     1058    RedNode addRedNode() {
    10341059      int n = nodes.size();
    10351060      nodes.push_back(NodeT());
     
    10401065      first_red = n;
    10411066
    1042       return Node(n);
    1043     }
    1044 
    1045     Node addBlueNode() {
     1067      return RedNode(n);
     1068    }
     1069
     1070    BlueNode addBlueNode() {
    10461071      int n = nodes.size();
    10471072      nodes.push_back(NodeT());
     
    10521077      first_blue = n;
    10531078
    1054       return Node(n);
    1055     }
    1056 
    1057     Edge addEdge(Node u, Node v) {
     1079      return BlueNode(n);
     1080    }
     1081
     1082    Edge addEdge(RedNode u, BlueNode v) {
    10581083      int n = arcs.size();
    10591084      arcs.push_back(ArcT());
     
    11251150    /// This function adds a red new node to the graph.
    11261151    /// \return The new node.
    1127     Node addRedNode() { return Parent::addRedNode(); }
     1152    RedNode addRedNode() { return Parent::addRedNode(); }
    11281153
    11291154    /// \brief Add a new blue node to the graph.
     
    11311156    /// This function adds a blue new node to the graph.
    11321157    /// \return The new node.
    1133     Node addBlueNode() { return Parent::addBlueNode(); }
     1158    BlueNode addBlueNode() { return Parent::addBlueNode(); }
    11341159
    11351160    /// \brief Add a new edge to the graph.
     
    11391164    /// node \c v.
    11401165    /// \return The new edge.
    1141     Edge addEdge(Node red, Node blue) {
    1142       LEMON_DEBUG(Parent::red(red) && Parent::blue(blue),
    1143                   "Edge has to be formed by a red and a blue nodes");
    1144       return Parent::addEdge(red, blue);
     1166    Edge addEdge(RedNode u, BlueNode v) {
     1167      return Parent::addEdge(u, v);
     1168    }
     1169    Edge addEdge(BlueNode v, RedNode u) {
     1170      return Parent::addEdge(u, v);
    11451171    }
    11461172
     
    12381264            max_red = -1;
    12391265          }
    1240           Parent::notifier(RedNode()).erase(node);         
     1266          Parent::notifier(RedNode()).erase(asRedNodeUnsafe(node));         
    12411267        } else {
    12421268          first_blue = nodes[n].partition_next;
     
    12461272            max_blue = -1;
    12471273          }
    1248           Parent::notifier(BlueNode()).erase(node);
     1274          Parent::notifier(BlueNode()).erase(asBlueNodeUnsafe(node));
    12491275        }
    12501276        Parent::notifier(Node()).erase(node);
Note: See TracChangeset for help on using the changeset viewer.