Ticket #69: 434a20e74585.patch
File 434a20e74585.patch, 58.0 KB (added by , 13 years ago) |
---|
-
lemon/bits/graph_extender.h
# HG changeset patch # User Balazs Dezso <deba@inf.elte.hu> # Date 1322726747 -3600 # Node ID 434a20e7458562409f15f552583d9ca64f7f8c6c # Parent 008af84a4d4af8ab0898ba29bd8c07cceda12e3b Type safe red and blue node set diff -r 008af84a4d4a -r 434a20e74585 lemon/bits/graph_extender.h
a b 760 760 typedef True UndirectedTag; 761 761 762 762 typedef typename Parent::Node Node; 763 typedef typename Parent::RedNode RedNode; 764 typedef typename Parent::BlueNode BlueNode; 763 765 typedef typename Parent::Arc Arc; 764 766 typedef typename Parent::Edge Edge; 765 767 766 768 // BpGraph extension 767 769 768 class RedNode : public Node {769 public:770 RedNode() {}771 RedNode(const RedNode& node) : Node(node) {}772 RedNode(Invalid) : Node(INVALID){}773 RedNode(const Node& node) : Node(node) {}774 };775 class BlueNode : public Node {776 public:777 BlueNode() {}778 BlueNode(const BlueNode& node) : Node(node) {}779 BlueNode(Invalid) : Node(INVALID){}780 BlueNode(const Node& node) : Node(node) {}781 };782 783 770 using Parent::first; 784 771 using Parent::next; 785 786 void first(RedNode& node) const {787 Parent::firstRed(node);788 }789 790 void next(RedNode& node) const {791 Parent::nextRed(node);792 }793 794 void first(BlueNode& node) const {795 Parent::firstBlue(node);796 }797 798 void next(BlueNode& node) const {799 Parent::nextBlue(node);800 }801 802 772 using Parent::id; 803 773 804 int id(const RedNode& node) const {805 return Parent::redId(node);806 }807 808 int id(const BlueNode& node) const {809 return Parent::blueId(node);810 }811 812 774 int maxId(Node) const { 813 775 return Parent::maxNodeId(); 814 776 } … … 862 824 return Parent::direct(edge, Parent::redNode(edge) == node); 863 825 } 864 826 827 RedNode asRedNode(const Node& node) const { 828 if (node == INVALID || Parent::blue(node)) { 829 return INVALID; 830 } else { 831 return Parent::asRedNodeUnsafe(node); 832 } 833 } 834 835 BlueNode asBlueNode(const Node& node) const { 836 if (node == INVALID || Parent::red(node)) { 837 return INVALID; 838 } else { 839 return Parent::asBlueNodeUnsafe(node); 840 } 841 } 842 843 std::pair<RedNode, BlueNode> asRedBlueNode(const Node& node) const { 844 if (node == INVALID) { 845 return std::make_pair(RedNode(INVALID), BlueNode(INVALID)); 846 } else if (Parent::red(node)) { 847 return std::make_pair(Parent::asRedNodeUnsafe(node), BlueNode(INVALID)); 848 } else { 849 return std::make_pair(RedNode(INVALID), Parent::asBlueNodeUnsafe(node)); 850 } 851 } 852 865 853 // Alterable extension 866 854 867 855 typedef AlterationNotifier<BpGraphExtender, Node> NodeNotifier; … … 925 913 926 914 }; 927 915 928 class RedIt : public Node {916 class RedIt : public RedNode { 929 917 const BpGraph* _graph; 930 918 public: 931 919 932 920 RedIt() {} 933 921 934 RedIt(Invalid i) : Node(i) { }922 RedIt(Invalid i) : RedNode(i) { } 935 923 936 924 explicit RedIt(const BpGraph& graph) : _graph(&graph) { 937 _graph->first Red(static_cast<Node&>(*this));925 _graph->first(static_cast<RedNode&>(*this)); 938 926 } 939 927 940 RedIt(const BpGraph& graph, const Node& node) 941 : Node(node), _graph(&graph) { 942 LEMON_DEBUG(_graph->red(node), "Node has to be red."); 943 } 928 RedIt(const BpGraph& graph, const RedNode& node) 929 : RedNode(node), _graph(&graph) {} 944 930 945 931 RedIt& operator++() { 946 _graph->next Red(*this);932 _graph->next(static_cast<RedNode&>(*this)); 947 933 return *this; 948 934 } 949 935 950 936 }; 951 937 952 class BlueIt : public Node {938 class BlueIt : public BlueNode { 953 939 const BpGraph* _graph; 954 940 public: 955 941 956 942 BlueIt() {} 957 943 958 BlueIt(Invalid i) : Node(i) { }944 BlueIt(Invalid i) : BlueNode(i) { } 959 945 960 946 explicit BlueIt(const BpGraph& graph) : _graph(&graph) { 961 _graph->first Blue(static_cast<Node&>(*this));947 _graph->first(static_cast<BlueNode&>(*this)); 962 948 } 963 949 964 BlueIt(const BpGraph& graph, const Node& node) 965 : Node(node), _graph(&graph) { 966 LEMON_DEBUG(_graph->blue(node), "Node has to be blue."); 967 } 950 BlueIt(const BpGraph& graph, const BlueNode& node) 951 : BlueNode(node), _graph(&graph) {} 968 952 969 953 BlueIt& operator++() { 970 _graph->next Blue(*this);954 _graph->next(static_cast<BlueNode&>(*this)); 971 955 return *this; 972 956 } 973 957 … … 1258 1242 1259 1243 // Alteration extension 1260 1244 1261 Node addRedNode() {1262 Node node = Parent::addRedNode();1245 RedNode addRedNode() { 1246 RedNode node = Parent::addRedNode(); 1263 1247 notifier(RedNode()).add(node); 1264 1248 notifier(Node()).add(node); 1265 1249 return node; 1266 1250 } 1267 1251 1268 Node addBlueNode() {1269 Node node = Parent::addBlueNode();1252 BlueNode addBlueNode() { 1253 BlueNode node = Parent::addBlueNode(); 1270 1254 notifier(BlueNode()).add(node); 1271 1255 notifier(Node()).add(node); 1272 1256 return node; 1273 1257 } 1274 1258 1275 Edge addEdge(const Node& from, constNode& to) {1259 Edge addEdge(const RedNode& from, const BlueNode& to) { 1276 1260 Edge edge = Parent::addEdge(from, to); 1277 1261 notifier(Edge()).add(edge); 1278 1262 std::vector<Arc> av; … … 1317 1301 } 1318 1302 1319 1303 if (Parent::red(node)) { 1320 notifier(RedNode()).erase( node);1304 notifier(RedNode()).erase(asRedNodeUnsafe(node)); 1321 1305 } else { 1322 notifier(BlueNode()).erase( node);1306 notifier(BlueNode()).erase(asBlueNodeUnsafe(node)); 1323 1307 } 1324 1308 1325 1309 notifier(Node()).erase(node); -
lemon/concepts/bpgraph.h
diff -r 008af84a4d4a -r 434a20e74585 lemon/concepts/bpgraph.h
a b 149 149 /// \sa Invalid for more details. 150 150 RedNode(Invalid) { } 151 151 152 /// Constructor for conversion from a node.153 154 /// Constructor for conversion from a node. The conversion can155 /// be invalid, since the Node can be member of the blue156 /// set.157 RedNode(const Node&) {}158 152 }; 159 153 160 154 /// Class to represent blue nodes. … … 182 176 /// \sa Invalid for more details. 183 177 BlueNode(Invalid) { } 184 178 185 /// Constructor for conversion from a node.186 187 /// Constructor for conversion from a node. The conversion can188 /// be invalid, since the Node can be member of the red189 /// set.190 BlueNode(const Node&) {}191 179 }; 192 180 193 181 /// Iterator class for the red nodes. … … 199 187 /// int count=0; 200 188 /// for (BpGraph::RedNodeIt n(g); n!=INVALID; ++n) ++count; 201 189 ///\endcode 202 class RedIt : public Node {190 class RedIt : public RedNode { 203 191 public: 204 192 /// Default constructor 205 193 … … 210 198 211 199 /// Copy constructor. 212 200 /// 213 RedIt(const RedIt& n) : Node(n) { }201 RedIt(const RedIt& n) : RedNode(n) { } 214 202 /// %Invalid constructor \& conversion. 215 203 216 204 /// Initializes the iterator to be invalid. … … 225 213 226 214 /// Sets the iterator to the given red node of the given 227 215 /// digraph. 228 RedIt(const BpGraph&, const Node&) { }216 RedIt(const BpGraph&, const RedNode&) { } 229 217 /// Next node. 230 218 231 219 /// Assign the iterator to the next red node. … … 242 230 /// int count=0; 243 231 /// for (BpGraph::BlueNodeIt n(g); n!=INVALID; ++n) ++count; 244 232 ///\endcode 245 class BlueIt : public Node {233 class BlueIt : public BlueNode { 246 234 public: 247 235 /// Default constructor 248 236 … … 253 241 254 242 /// Copy constructor. 255 243 /// 256 BlueIt(const BlueIt& n) : Node(n) { }244 BlueIt(const BlueIt& n) : BlueNode(n) { } 257 245 /// %Invalid constructor \& conversion. 258 246 259 247 /// Initializes the iterator to be invalid. … … 268 256 269 257 /// Sets the iterator to the given blue node of the given 270 258 /// digraph. 271 BlueIt(const BpGraph&, const Node&) { }259 BlueIt(const BpGraph&, const BlueNode&) { } 272 260 /// Next node. 273 261 274 262 /// Assign the iterator to the next blue node. … … 784 772 /// Gives back %true for blue nodes. 785 773 bool blue(const Node&) const { return true; } 786 774 775 /// \brief Converts the node to red node object. 776 /// 777 /// This class is converts unsafely the node to red node 778 /// object. It should be called only if the node is from the red 779 /// partition or INVALID. 780 RedNode asRedNodeUnsafe(const Node&) const { return RedNode(); } 781 782 /// \brief Converts the node to blue node object. 783 /// 784 /// This class is converts unsafely the node to blue node 785 /// object. It should be called only if the node is from the red 786 /// partition or INVALID. 787 BlueNode asBlueNodeUnsafe(const Node&) const { return BlueNode(); } 788 789 /// \brief Converts the node to red node object. 790 /// 791 /// This class is converts safely the node to red node 792 /// object. If the node is not from the red partition, then it 793 /// returns INVALID. 794 RedNode asRedNode(const Node&) const { return RedNode(); } 795 796 /// \brief Converts the node to blue node object. 797 /// 798 /// This class is converts unsafely the node to blue node 799 /// object. If the node is not from the blue partition, then it 800 /// returns INVALID. 801 BlueNode asBlueNode(const Node&) const { return BlueNode(); } 802 803 /// \brief Convert the node to either red or blue node. 804 /// 805 /// If the node is from the red partition then it is returned in 806 /// first and second is INVALID. If the node is from the blue 807 /// partition then it is returned in second and first is 808 /// INVALID. If the node INVALID then both first and second are 809 /// INVALID in the return value. 810 std::pair<RedNode, BlueNode> asRedBlueNode(const Node&) const { 811 return std::make_pair(RedNode(), BlueNode()); 812 } 813 787 814 /// \brief Gives back the red end node of the edge. 788 815 /// 789 816 /// Gives back the red end node of the edge. 790 Node redNode(const Edge&) const { returnNode(); }817 RedNode redNode(const Edge&) const { return RedNode(); } 791 818 792 819 /// \brief Gives back the blue end node of the edge. 793 820 /// 794 821 /// Gives back the blue end node of the edge. 795 Node blueNode(const Edge&) const { returnNode(); }822 BlueNode blueNode(const Edge&) const { return BlueNode(); } 796 823 797 824 /// \brief The first node of the edge. 798 825 /// … … 822 849 /// \brief The red ID of the node. 823 850 /// 824 851 /// Returns the red ID of the given node. 825 int redId(Node) const { return -1; }826 827 /// \brief The red ID of the node.828 ///829 /// Returns the red ID of the given node.830 852 int id(RedNode) const { return -1; } 831 853 832 854 /// \brief The blue ID of the node. 833 855 /// 834 856 /// Returns the blue ID of the given node. 835 int blueId(Node) const { return -1; }836 837 /// \brief The blue ID of the node.838 ///839 /// Returns the blue ID of the given node.840 857 int id(BlueNode) const { return -1; } 841 858 842 859 /// \brief The ID of the edge. … … 928 945 void first(Node&) const {} 929 946 void next(Node&) const {} 930 947 931 void firstRed( Node&) const {}932 void nextRed( Node&) const {}948 void firstRed(RedNode&) const {} 949 void nextRed(RedNode&) const {} 933 950 934 void firstBlue( Node&) const {}935 void nextBlue( Node&) const {}951 void firstBlue(BlueNode&) const {} 952 void nextBlue(BlueNode&) const {} 936 953 937 954 void first(Edge&) const {} 938 955 void next(Edge&) const {} -
lemon/concepts/graph_components.h
diff -r 008af84a4d4a -r 434a20e74585 lemon/concepts/graph_components.h
a b 312 312 313 313 /// \brief Class to represent red nodes. 314 314 /// 315 /// This class represents the red nodes of the graph. It does 316 /// not supposed to be used directly, because the nodes can be 317 /// represented as Node instances. This class can be used as 318 /// template parameter for special map classes. 315 /// This class represents the red nodes of the graph. The red 316 /// nodes can be used also as normal nodes. 319 317 class RedNode : public Node { 320 318 typedef Node Parent; 321 319 … … 339 337 /// It initializes the item to be invalid. 340 338 /// \sa Invalid for more details. 341 339 RedNode(Invalid) {} 342 343 /// \brief Constructor for conversion from a node.344 ///345 /// Constructor for conversion from a node. The conversion can346 /// be invalid, since the Node can be member of the blue347 /// set.348 RedNode(const Node&) {}349 340 }; 350 341 351 342 /// \brief Class to represent blue nodes. 352 343 /// 353 /// This class represents the blue nodes of the graph. It does 354 /// not supposed to be used directly, because the nodes can be 355 /// represented as Node instances. This class can be used as 356 /// template parameter for special map classes. 344 /// This class represents the blue nodes of the graph. The blue 345 /// nodes can be used also as normal nodes. 357 346 class BlueNode : public Node { 358 347 typedef Node Parent; 359 348 … … 399 388 /// \brief Gives back the red end node of the edge. 400 389 /// 401 390 /// Gives back the red end node of the edge. 402 Node redNode(const Edge&) const { returnNode(); }391 RedNode redNode(const Edge&) const { return RedNode(); } 403 392 404 393 /// \brief Gives back the blue end node of the edge. 405 394 /// 406 395 /// Gives back the blue end node of the edge. 407 Node blueNode(const Edge&) const { return Node(); } 396 BlueNode blueNode(const Edge&) const { return BlueNode(); } 397 398 /// \brief Converts the node to red node object. 399 /// 400 /// This class is converts unsafely the node to red node 401 /// object. It should be called only if the node is from the red 402 /// partition or INVALID. 403 RedNode asRedNodeUnsafe(const Node&) const { return RedNode(); } 404 405 /// \brief Converts the node to blue node object. 406 /// 407 /// This class is converts unsafely the node to blue node 408 /// object. It should be called only if the node is from the red 409 /// partition or INVALID. 410 BlueNode asBlueNodeUnsafe(const Node&) const { return BlueNode(); } 411 412 /// \brief Converts the node to red node object. 413 /// 414 /// This class is converts safely the node to red node 415 /// object. If the node is not from the red partition, then it 416 /// returns INVALID. 417 RedNode asRedNode(const Node&) const { return RedNode(); } 418 419 /// \brief Converts the node to blue node object. 420 /// 421 /// This class is converts unsafely the node to blue node 422 /// object. If the node is not from the blue partition, then it 423 /// returns INVALID. 424 BlueNode asBlueNode(const Node&) const { return BlueNode(); } 425 426 /// \brief Convert the node to either red or blue node. 427 /// 428 /// If the node is from the red partition then it is returned in 429 /// first and second is INVALID. If the node is from the blue 430 /// partition then it is returned in second and first is 431 /// INVALID. If the node INVALID then both first and second are 432 /// INVALID in the return value. 433 std::pair<RedNode, BlueNode> asRedBlueNode(const Node&) const { 434 return std::make_pair(RedNode(), BlueNode()); 435 } 408 436 409 437 template <typename _BpGraph> 410 438 struct Constraints { … … 419 447 checkConcept<GraphItem<'n'>, RedNode>(); 420 448 checkConcept<GraphItem<'n'>, BlueNode>(); 421 449 { 422 Node n; 423 RedNode rn = n; 424 BlueNode bn = bn; 450 RedNode rn; 451 BlueNode bn; 452 Node rnan = rn; 453 Node bnan = bn; 425 454 Edge e; 426 455 bool b; 427 b = bpgraph.red(n); 428 b = bpgraph.blue(n); 429 n = bpgraph.redNode(e); 430 n = bpgraph.blueNode(e); 431 rn = n; 432 bn = n; 456 rnan = rn; 457 bnan = bn; 458 b = bpgraph.red(rnan); 459 b = bpgraph.blue(bnan); 460 rn = bpgraph.redNode(e); 461 bn = bpgraph.blueNode(e); 462 rn = bpgraph.asRedNodeUnsafe(rnan); 463 bn = bpgraph.asBlueNodeUnsafe(bnan); 464 rn = bpgraph.asRedNode(rnan); 465 bn = bpgraph.asBlueNode(bnan); 466 std::pair<RedNode, BlueNode> p = bpgraph.asRedBlueNode(rnan); 433 467 } 434 468 } 435 469 … … 591 625 /// \brief Return a unique integer id for the given node in the red set. 592 626 /// 593 627 /// Return a unique integer id for the given node in the red set. 594 int redId(const Node&) const { return -1; }595 596 /// \brief Return the same value as redId().597 ///598 /// Return the same value as redId().599 628 int id(const RedNode&) const { return -1; } 600 629 601 630 /// \brief Return a unique integer id for the given node in the blue set. 602 631 /// 603 632 /// Return a unique integer id for the given node in the blue set. 604 int blueId(const Node&) const { return -1; }605 606 /// \brief Return the same value as blueId().607 ///608 /// Return the same value as blueId().609 633 int id(const BlueNode&) const { return -1; } 610 634 611 635 /// \brief Return an integer greater or equal to the maximum … … 630 654 typename _BpGraph::Node node; 631 655 typename _BpGraph::RedNode red; 632 656 typename _BpGraph::BlueNode blue; 633 int rid = bpgraph.redId(node); 634 int bid = bpgraph.blueId(node); 635 rid = bpgraph.id(red); 636 bid = bpgraph.id(blue); 657 int rid = bpgraph.id(red); 658 int bid = bpgraph.id(blue); 637 659 rid = bpgraph.maxRedId(); 638 660 bid = bpgraph.maxBlueId(); 639 661 ignore_unused_variable_warning(rid); … … 1121 1143 1122 1144 typedef BAS Base; 1123 1145 typedef typename Base::Node Node; 1146 typedef typename Base::RedNode RedNode; 1147 typedef typename Base::BlueNode BlueNode; 1124 1148 typedef typename Base::Arc Arc; 1125 1149 typedef typename Base::Edge Edge; 1150 1151 typedef IterableBpGraphComponent BpGraph; 1126 1152 1127 1128 typedef IterableBpGraphComponent BpGraph;1153 using IterableGraphComponent<BAS>::first; 1154 using IterableGraphComponent<BAS>::next; 1129 1155 1130 1156 /// \name Base Iteration 1131 1157 /// … … 1136 1162 /// \brief Return the first red node. 1137 1163 /// 1138 1164 /// This function gives back the first red node in the iteration order. 1139 void first Red(Node&) const {}1165 void first(RedNode&) const {} 1140 1166 1141 1167 /// \brief Return the next red node. 1142 1168 /// 1143 1169 /// This function gives back the next red node in the iteration order. 1144 void next Red(Node&) const {}1170 void next(RedNode&) const {} 1145 1171 1146 1172 /// \brief Return the first blue node. 1147 1173 /// 1148 1174 /// This function gives back the first blue node in the iteration order. 1149 void first Blue(Node&) const {}1175 void first(BlueNode&) const {} 1150 1176 1151 1177 /// \brief Return the next blue node. 1152 1178 /// 1153 1179 /// This function gives back the next blue node in the iteration order. 1154 void next Blue(Node&) const {}1180 void next(BlueNode&) const {} 1155 1181 1156 1182 1157 1183 /// @} … … 1165 1191 /// \brief This iterator goes through each red node. 1166 1192 /// 1167 1193 /// This iterator goes through each red node. 1168 typedef GraphItemIt<BpGraph, Node> RedIt;1194 typedef GraphItemIt<BpGraph, RedNode> RedIt; 1169 1195 1170 1196 /// \brief This iterator goes through each blue node. 1171 1197 /// 1172 1198 /// This iterator goes through each blue node. 1173 typedef GraphItemIt<BpGraph, Node> BlueIt;1199 typedef GraphItemIt<BpGraph, BlueNode> BlueIt; 1174 1200 1175 1201 /// @} 1176 1202 … … 1179 1205 void constraints() { 1180 1206 checkConcept<IterableGraphComponent<Base>, _BpGraph>(); 1181 1207 1182 typename _BpGraph::Node node(INVALID); 1183 bpgraph.firstRed(node); 1184 bpgraph.nextRed(node); 1185 bpgraph.firstBlue(node); 1186 bpgraph.nextBlue(node); 1208 typename _BpGraph::RedNode rn(INVALID); 1209 bpgraph.first(rn); 1210 bpgraph.next(rn); 1211 typename _BpGraph::BlueNode bn(INVALID); 1212 bpgraph.first(bn); 1213 bpgraph.next(bn); 1187 1214 1188 checkConcept<GraphItemIt<_BpGraph, typename _BpGraph:: Node>,1215 checkConcept<GraphItemIt<_BpGraph, typename _BpGraph::RedNode>, 1189 1216 typename _BpGraph::RedIt>(); 1190 checkConcept<GraphItemIt<_BpGraph, typename _BpGraph:: Node>,1217 checkConcept<GraphItemIt<_BpGraph, typename _BpGraph::BlueNode>, 1191 1218 typename _BpGraph::BlueIt>(); 1192 1219 } 1193 1220 … … 1769 1796 1770 1797 { // int map test 1771 1798 typedef typename _BpGraph::template RedMap<int> IntRedMap; 1772 checkConcept<GraphMap<_BpGraph, typename _BpGraph:: Node, int>,1799 checkConcept<GraphMap<_BpGraph, typename _BpGraph::RedNode, int>, 1773 1800 IntRedMap >(); 1774 1801 } { // bool map test 1775 1802 typedef typename _BpGraph::template RedMap<bool> BoolRedMap; 1776 checkConcept<GraphMap<_BpGraph, typename _BpGraph:: Node, bool>,1803 checkConcept<GraphMap<_BpGraph, typename _BpGraph::RedNode, bool>, 1777 1804 BoolRedMap >(); 1778 1805 } { // Dummy map test 1779 1806 typedef typename _BpGraph::template RedMap<Dummy> DummyRedMap; 1780 checkConcept<GraphMap<_BpGraph, typename _BpGraph:: Node, Dummy>,1807 checkConcept<GraphMap<_BpGraph, typename _BpGraph::RedNode, Dummy>, 1781 1808 DummyRedMap >(); 1782 1809 } 1783 1810 1784 1811 { // int map test 1785 1812 typedef typename _BpGraph::template BlueMap<int> IntBlueMap; 1786 checkConcept<GraphMap<_BpGraph, typename _BpGraph:: Node, int>,1813 checkConcept<GraphMap<_BpGraph, typename _BpGraph::BlueNode, int>, 1787 1814 IntBlueMap >(); 1788 1815 } { // bool map test 1789 1816 typedef typename _BpGraph::template BlueMap<bool> BoolBlueMap; 1790 checkConcept<GraphMap<_BpGraph, typename _BpGraph:: Node, bool>,1817 checkConcept<GraphMap<_BpGraph, typename _BpGraph::BlueNode, bool>, 1791 1818 BoolBlueMap >(); 1792 1819 } { // Dummy map test 1793 1820 typedef typename _BpGraph::template BlueMap<Dummy> DummyBlueMap; 1794 checkConcept<GraphMap<_BpGraph, typename _BpGraph:: Node, Dummy>,1821 checkConcept<GraphMap<_BpGraph, typename _BpGraph::BlueNode, Dummy>, 1795 1822 DummyBlueMap >(); 1796 1823 } 1797 1824 } … … 1900 1927 1901 1928 typedef BAS Base; 1902 1929 typedef typename Base::Node Node; 1930 typedef typename Base::RedNode RedNode; 1931 typedef typename Base::BlueNode BlueNode; 1903 1932 typedef typename Base::Edge Edge; 1904 1933 1905 1934 /// \brief Add a new red node to the digraph. 1906 1935 /// 1907 1936 /// This function adds a red new node to the digraph. 1908 Node addRedNode() {1937 RedNode addRedNode() { 1909 1938 return INVALID; 1910 1939 } 1911 1940 1912 1941 /// \brief Add a new blue node to the digraph. 1913 1942 /// 1914 1943 /// This function adds a blue new node to the digraph. 1915 Node addBlueNode() {1944 BlueNode addBlueNode() { 1916 1945 return INVALID; 1917 1946 } 1918 1947 … … 1921 1950 /// This function adds a new edge connecting the given two nodes 1922 1951 /// of the graph. The first node has to be a red node, and the 1923 1952 /// second one a blue node. 1924 Edge addEdge(const Node&, const Node&) { 1953 Edge addEdge(const RedNode&, const BlueNode&) { 1954 return INVALID; 1955 } 1956 Edge addEdge(const BlueNode&, const RedNode&) { 1925 1957 return INVALID; 1926 1958 } 1927 1959 … … 1929 1961 struct Constraints { 1930 1962 void constraints() { 1931 1963 checkConcept<Base, _BpGraph>(); 1932 typename _BpGraph::Node red_node, blue_node; 1964 typename _BpGraph::RedNode red_node; 1965 typename _BpGraph::BlueNode blue_node; 1933 1966 red_node = bpgraph.addRedNode(); 1934 1967 blue_node = bpgraph.addBlueNode(); 1935 1968 typename _BpGraph::Edge edge; 1936 1969 edge = bpgraph.addEdge(red_node, blue_node); 1970 edge = bpgraph.addEdge(blue_node, red_node); 1937 1971 } 1938 1972 1939 1973 _BpGraph& bpgraph; -
lemon/core.h
diff -r 008af84a4d4a -r 434a20e74585 lemon/core.h
a b 550 550 { 551 551 template <typename From, typename NodeRefMap, typename EdgeRefMap> 552 552 static void copy(const From& from, Graph &to, 553 NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) { 553 NodeRefMap& nodeRefMap, 554 EdgeRefMap& edgeRefMap) { 554 555 to.build(from, nodeRefMap, edgeRefMap); 555 556 } 556 557 }; 557 558 558 559 template <typename BpGraph, typename Enable = void> 559 560 struct BpGraphCopySelector { 560 template <typename From, typename NodeRefMap, typename EdgeRefMap> 561 template <typename From, typename RedNodeRefMap, 562 typename BlueNodeRefMap, typename EdgeRefMap> 561 563 static void copy(const From& from, BpGraph &to, 562 NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) { 564 RedNodeRefMap& redNodeRefMap, 565 BlueNodeRefMap& blueNodeRefMap, 566 EdgeRefMap& edgeRefMap) { 563 567 to.clear(); 564 568 for (typename From::RedIt it(from); it != INVALID; ++it) { 565 nodeRefMap[it] = to.addRedNode();569 redNodeRefMap[it] = to.addRedNode(); 566 570 } 567 571 for (typename From::BlueIt it(from); it != INVALID; ++it) { 568 nodeRefMap[it] = to.addBlueNode();572 blueNodeRefMap[it] = to.addBlueNode(); 569 573 } 570 574 for (typename From::EdgeIt it(from); it != INVALID; ++it) { 571 edgeRefMap[it] = to.addEdge( nodeRefMap[from.redNode(it)],572 nodeRefMap[from.blueNode(it)]);575 edgeRefMap[it] = to.addEdge(redNodeRefMap[from.redNode(it)], 576 blueNodeRefMap[from.blueNode(it)]); 573 577 } 574 578 } 575 579 }; … … 579 583 BpGraph, 580 584 typename enable_if<typename BpGraph::BuildTag, void>::type> 581 585 { 582 template <typename From, typename NodeRefMap, typename EdgeRefMap> 586 template <typename From, typename RedNodeRefMap, 587 typename BlueNodeRefMap, typename EdgeRefMap> 583 588 static void copy(const From& from, BpGraph &to, 584 NodeRefMap& nodeRefMap, EdgeRefMap& edgeRefMap) { 585 to.build(from, nodeRefMap, edgeRefMap); 589 RedNodeRefMap& redNodeRefMap, 590 BlueNodeRefMap& blueNodeRefMap, 591 EdgeRefMap& edgeRefMap) { 592 to.build(from, redNodeRefMap, blueNodeRefMap, edgeRefMap); 586 593 } 587 594 }; 588 595 … … 1182 1189 typedef typename From::EdgeIt EdgeIt; 1183 1190 1184 1191 typedef typename To::Node TNode; 1192 typedef typename To::RedNode TRedNode; 1193 typedef typename To::BlueNode TBlueNode; 1185 1194 typedef typename To::Arc TArc; 1186 1195 typedef typename To::Edge TEdge; 1187 1196 1188 typedef typename From::template NodeMap<TNode> NodeRefMap; 1197 typedef typename From::template RedMap<TRedNode> RedNodeRefMap; 1198 typedef typename From::template BlueMap<TBlueNode> BlueNodeRefMap; 1189 1199 typedef typename From::template EdgeMap<TEdge> EdgeRefMap; 1190 1200 1201 struct NodeRefMap { 1202 NodeRefMap(const From& from, const RedNodeRefMap& red_node_ref, 1203 const BlueNodeRefMap& blue_node_ref) 1204 : _from(from), _red_node_ref(red_node_ref), 1205 _blue_node_ref(blue_node_ref) {} 1206 1207 typedef typename From::Node Key; 1208 typedef typename To::Node Value; 1209 1210 Value operator[](const Key& key) const { 1211 std::pair<RedNode, BlueNode> red_blue_pair = _from.asRedBlueNode(key); 1212 if (red_blue_pair.first != INVALID) { 1213 return _red_node_ref[red_blue_pair.first]; 1214 } else { 1215 return _blue_node_ref[red_blue_pair.second]; 1216 } 1217 } 1218 1219 const From& _from; 1220 const RedNodeRefMap& _red_node_ref; 1221 const BlueNodeRefMap& _blue_node_ref; 1222 }; 1223 1191 1224 struct ArcRefMap { 1192 1225 ArcRefMap(const From& from, const To& to, const EdgeRefMap& edge_ref) 1193 1226 : _from(from), _to(to), _edge_ref(edge_ref) {} … … 1292 1325 template <typename RedRef> 1293 1326 BpGraphCopy& redRef(RedRef& map) { 1294 1327 _red_maps.push_back(new _core_bits::RefCopy<From, RedNode, 1295 NodeRefMap, RedRef>(map));1328 RedNodeRefMap, RedRef>(map)); 1296 1329 return *this; 1297 1330 } 1298 1331 … … 1306 1339 template <typename RedCrossRef> 1307 1340 BpGraphCopy& redCrossRef(RedCrossRef& map) { 1308 1341 _red_maps.push_back(new _core_bits::CrossRefCopy<From, RedNode, 1309 NodeRefMap, RedCrossRef>(map));1342 RedNodeRefMap, RedCrossRef>(map)); 1310 1343 return *this; 1311 1344 } 1312 1345 … … 1321 1354 template <typename FromMap, typename ToMap> 1322 1355 BpGraphCopy& redMap(const FromMap& map, ToMap& tmap) { 1323 1356 _red_maps.push_back(new _core_bits::MapCopy<From, RedNode, 1324 NodeRefMap, FromMap, ToMap>(map, tmap)); 1357 RedNodeRefMap, FromMap, ToMap>(map, tmap)); 1358 return *this; 1359 } 1360 1361 /// \brief Make a copy of the given red node. 1362 /// 1363 /// This function makes a copy of the given red node. 1364 BpGraphCopy& redNode(const RedNode& node, TRedNode& tnode) { 1365 _red_maps.push_back(new _core_bits::ItemCopy<From, RedNode, 1366 RedNodeRefMap, TRedNode>(node, tnode)); 1325 1367 return *this; 1326 1368 } 1327 1369 … … 1334 1376 template <typename BlueRef> 1335 1377 BpGraphCopy& blueRef(BlueRef& map) { 1336 1378 _blue_maps.push_back(new _core_bits::RefCopy<From, BlueNode, 1337 NodeRefMap, BlueRef>(map));1379 BlueNodeRefMap, BlueRef>(map)); 1338 1380 return *this; 1339 1381 } 1340 1382 … … 1348 1390 template <typename BlueCrossRef> 1349 1391 BpGraphCopy& blueCrossRef(BlueCrossRef& map) { 1350 1392 _blue_maps.push_back(new _core_bits::CrossRefCopy<From, BlueNode, 1351 NodeRefMap, BlueCrossRef>(map));1393 BlueNodeRefMap, BlueCrossRef>(map)); 1352 1394 return *this; 1353 1395 } 1354 1396 … … 1363 1405 template <typename FromMap, typename ToMap> 1364 1406 BpGraphCopy& blueMap(const FromMap& map, ToMap& tmap) { 1365 1407 _blue_maps.push_back(new _core_bits::MapCopy<From, BlueNode, 1366 NodeRefMap, FromMap, ToMap>(map, tmap)); 1408 BlueNodeRefMap, FromMap, ToMap>(map, tmap)); 1409 return *this; 1410 } 1411 1412 /// \brief Make a copy of the given blue node. 1413 /// 1414 /// This function makes a copy of the given blue node. 1415 BpGraphCopy& blueNode(const BlueNode& node, TBlueNode& tnode) { 1416 _blue_maps.push_back(new _core_bits::ItemCopy<From, BlueNode, 1417 BlueNodeRefMap, TBlueNode>(node, tnode)); 1367 1418 return *this; 1368 1419 } 1369 1420 … … 1470 1521 /// This function executes the copying of the graph along with the 1471 1522 /// copying of the assigned data. 1472 1523 void run() { 1473 NodeRefMap nodeRefMap(_from); 1524 RedNodeRefMap redNodeRefMap(_from); 1525 BlueNodeRefMap blueNodeRefMap(_from); 1526 NodeRefMap nodeRefMap(_from, redNodeRefMap, blueNodeRefMap); 1474 1527 EdgeRefMap edgeRefMap(_from); 1475 1528 ArcRefMap arcRefMap(_from, _to, edgeRefMap); 1476 1529 _core_bits::BpGraphCopySelector<To>:: 1477 copy(_from, _to, nodeRefMap, edgeRefMap);1530 copy(_from, _to, redNodeRefMap, blueNodeRefMap, edgeRefMap); 1478 1531 for (int i = 0; i < int(_node_maps.size()); ++i) { 1479 1532 _node_maps[i]->copy(_from, nodeRefMap); 1480 1533 } 1481 1534 for (int i = 0; i < int(_red_maps.size()); ++i) { 1482 _red_maps[i]->copy(_from, nodeRefMap);1535 _red_maps[i]->copy(_from, redNodeRefMap); 1483 1536 } 1484 1537 for (int i = 0; i < int(_blue_maps.size()); ++i) { 1485 _blue_maps[i]->copy(_from, nodeRefMap);1538 _blue_maps[i]->copy(_from, blueNodeRefMap); 1486 1539 } 1487 1540 for (int i = 0; i < int(_edge_maps.size()); ++i) { 1488 1541 _edge_maps[i]->copy(_from, edgeRefMap); … … 1500 1553 std::vector<_core_bits::MapCopyBase<From, Node, NodeRefMap>* > 1501 1554 _node_maps; 1502 1555 1503 std::vector<_core_bits::MapCopyBase<From, RedNode, NodeRefMap>* >1556 std::vector<_core_bits::MapCopyBase<From, RedNode, RedNodeRefMap>* > 1504 1557 _red_maps; 1505 1558 1506 std::vector<_core_bits::MapCopyBase<From, BlueNode, NodeRefMap>* >1559 std::vector<_core_bits::MapCopyBase<From, BlueNode, BlueNodeRefMap>* > 1507 1560 _blue_maps; 1508 1561 1509 1562 std::vector<_core_bits::MapCopyBase<From, Arc, ArcRefMap>* > -
lemon/full_graph.h
diff -r 008af84a4d4a -r 434a20e74585 lemon/full_graph.h
a b 651 651 bool operator<(const Node& node) const {return _id < node._id;} 652 652 }; 653 653 654 class RedNode : public Node { 655 friend class FullBpGraphBase; 656 protected: 657 658 explicit RedNode(int pid) : Node(pid) {} 659 660 public: 661 RedNode() {} 662 RedNode(const RedNode& node) : Node(node) {} 663 RedNode(Invalid) : Node(INVALID){} 664 }; 665 666 class BlueNode : public Node { 667 friend class FullBpGraphBase; 668 protected: 669 670 explicit BlueNode(int pid) : Node(pid) {} 671 672 public: 673 BlueNode() {} 674 BlueNode(const BlueNode& node) : Node(node) {} 675 BlueNode(Invalid) : Node(INVALID){} 676 }; 677 654 678 class Edge { 655 679 friend class FullBpGraphBase; 656 680 protected: … … 717 741 bool red(Node n) const { return n._id < _red_num; } 718 742 bool blue(Node n) const { return n._id >= _red_num; } 719 743 744 static RedNode asRedNodeUnsafe(Node n) { return RedNode(n._id); } 745 static BlueNode asBlueNodeUnsafe(Node n) { return BlueNode(n._id); } 746 720 747 Node source(Arc a) const { 721 748 if (a._id & 1) { 722 749 return Node((a._id >> 1) % _red_num); … … 732 759 } 733 760 } 734 761 735 Node redNode(Edge e) const {736 return Node(e._id % _red_num);762 RedNode redNode(Edge e) const { 763 return RedNode(e._id % _red_num); 737 764 } 738 Node blueNode(Edge e) const {739 return Node(e._id / _red_num + _red_num);765 BlueNode blueNode(Edge e) const { 766 return BlueNode(e._id / _red_num + _red_num); 740 767 } 741 768 742 769 static bool direction(Arc a) { … … 755 782 --node._id; 756 783 } 757 784 758 void first Red(Node& node) const {785 void first(RedNode& node) const { 759 786 node._id = _red_num - 1; 760 787 } 761 788 762 static void next Red(Node& node) {789 static void next(RedNode& node) { 763 790 --node._id; 764 791 } 765 792 766 void first Blue(Node& node) const {793 void first(BlueNode& node) const { 767 794 if (_red_num == _node_num) node._id = -1; 768 795 else node._id = _node_num - 1; 769 796 } 770 797 771 void next Blue(Node& node) const {798 void next(BlueNode& node) const { 772 799 if (node._id == _red_num) node._id = -1; 773 800 else --node._id; 774 801 } … … 842 869 } 843 870 } 844 871 845 static int id(Node v) { return v._id; } 846 int redId(Node v) const { 847 LEMON_DEBUG(v._id < _red_num, "Node has to be red"); 848 return v._id; 849 } 850 int blueId(Node v) const { 851 LEMON_DEBUG(v._id >= _red_num, "Node has to be blue"); 852 return v._id - _red_num; 853 } 872 static int id(const Node& v) { return v._id; } 873 int id(const RedNode& v) const { return v._id; } 874 int id(const BlueNode& v) const { return v._id - _red_num; } 854 875 static int id(Arc e) { return e._id; } 855 876 static int id(Edge e) { return e._id; } 856 877 … … 868 889 return e._id >= 0 && e._id < _edge_num; 869 890 } 870 891 871 Node redNode(int index) const {872 return Node(index);892 RedNode redNode(int index) const { 893 return RedNode(index); 873 894 } 874 895 875 int redIndex(Node n) const {896 int index(RedNode n) const { 876 897 return n._id; 877 898 } 878 899 879 Node blueNode(int index) const {880 return Node(index + _red_num);900 BlueNode blueNode(int index) const { 901 return BlueNode(index + _red_num); 881 902 } 882 903 883 int blueIndex(Node n) const {904 int index(BlueNode n) const { 884 905 return n._id - _red_num; 885 906 } 886 907 … … 1000 1021 /// structure is completely static, the red nodes can be indexed 1001 1022 /// with integers from the range <tt>[0..redNum()-1]</tt>. 1002 1023 /// \sa redIndex() 1003 Node redNode(int index) const { return Parent::redNode(index); }1024 RedNode redNode(int index) const { return Parent::redNode(index); } 1004 1025 1005 1026 /// \brief Returns the index of the given red node. 1006 1027 /// … … 1009 1030 /// integers from the range <tt>[0..redNum()-1]</tt>. 1010 1031 /// 1011 1032 /// \sa operator()() 1012 int redIndex(Node node) const { return Parent::redIndex(node); }1033 int index(RedNode node) const { return Parent::index(node); } 1013 1034 1014 1035 /// \brief Returns the blue node with the given index. 1015 1036 /// … … 1017 1038 /// structure is completely static, the blue nodes can be indexed 1018 1039 /// with integers from the range <tt>[0..blueNum()-1]</tt>. 1019 1040 /// \sa blueIndex() 1020 Node blueNode(int index) const { return Parent::blueNode(index); }1041 BlueNode blueNode(int index) const { return Parent::blueNode(index); } 1021 1042 1022 1043 /// \brief Returns the index of the given blue node. 1023 1044 /// … … 1026 1047 /// integers from the range <tt>[0..blueNum()-1]</tt>. 1027 1048 /// 1028 1049 /// \sa operator()() 1029 int blueIndex(Node node) const { return Parent::blueIndex(node); }1050 int index(BlueNode node) const { return Parent::index(node); } 1030 1051 1031 1052 /// \brief Returns the edge which connects the given nodes. 1032 1053 /// -
lemon/list_graph.h
diff -r 008af84a4d4a -r 434a20e74585 lemon/list_graph.h
a b 1647 1647 bool operator<(const Node& node) const {return id < node.id;} 1648 1648 }; 1649 1649 1650 class RedNode : public Node { 1651 friend class ListBpGraphBase; 1652 protected: 1653 1654 explicit RedNode(int pid) : Node(pid) {} 1655 1656 public: 1657 RedNode() {} 1658 RedNode(const RedNode& node) : Node(node) {} 1659 RedNode(Invalid) : Node(INVALID){} 1660 }; 1661 1662 class BlueNode : public Node { 1663 friend class ListBpGraphBase; 1664 protected: 1665 1666 explicit BlueNode(int pid) : Node(pid) {} 1667 1668 public: 1669 BlueNode() {} 1670 BlueNode(const BlueNode& node) : Node(node) {} 1671 BlueNode(Invalid) : Node(INVALID){} 1672 }; 1673 1650 1674 class Edge { 1651 1675 friend class ListBpGraphBase; 1652 1676 protected: … … 1692 1716 bool red(Node n) const { return nodes[n.id].red; } 1693 1717 bool blue(Node n) const { return !nodes[n.id].red; } 1694 1718 1719 static RedNode asRedNodeUnsafe(Node n) { return RedNode(n.id); } 1720 static BlueNode asBlueNodeUnsafe(Node n) { return BlueNode(n.id); } 1721 1695 1722 int maxNodeId() const { return nodes.size()-1; } 1696 1723 int maxRedId() const { return max_red; } 1697 1724 int maxBlueId() const { return max_blue; } … … 1701 1728 Node source(Arc e) const { return Node(arcs[e.id ^ 1].target); } 1702 1729 Node target(Arc e) const { return Node(arcs[e.id].target); } 1703 1730 1704 Node redNode(Edge e) const { return Node(arcs[2 * e.id].target); } 1705 Node blueNode(Edge e) const { return Node(arcs[2 * e.id + 1].target); } 1731 RedNode redNode(Edge e) const { 1732 return RedNode(arcs[2 * e.id].target); 1733 } 1734 BlueNode blueNode(Edge e) const { 1735 return BlueNode(arcs[2 * e.id + 1].target); 1736 } 1706 1737 1707 1738 static bool direction(Arc e) { 1708 1739 return (e.id & 1) == 1; … … 1720 1751 node.id = nodes[node.id].next; 1721 1752 } 1722 1753 1723 void first Red(Node& node) const {1754 void first(RedNode& node) const { 1724 1755 node.id = first_red; 1725 1756 } 1726 1757 1727 void next Red(Node& node) const {1758 void next(RedNode& node) const { 1728 1759 node.id = nodes[node.id].partition_next; 1729 1760 } 1730 1761 1731 void first Blue(Node& node) const {1762 void first(BlueNode& node) const { 1732 1763 node.id = first_blue; 1733 1764 } 1734 1765 1735 void next Blue(Node& node) const {1766 void next(BlueNode& node) const { 1736 1767 node.id = nodes[node.id].partition_next; 1737 1768 } 1738 1769 … … 1835 1866 } 1836 1867 1837 1868 static int id(Node v) { return v.id; } 1838 int redId(Node v) const { 1839 LEMON_DEBUG(nodes[v.id].red, "Node has to be red"); 1840 return nodes[v.id].partition_index; 1841 } 1842 int blueId(Node v) const { 1843 LEMON_DEBUG(!nodes[v.id].red, "Node has to be blue"); 1844 return nodes[v.id].partition_index; 1845 } 1869 int id(RedNode v) const { return nodes[v.id].partition_index; } 1870 int id(BlueNode v) const { return nodes[v.id].partition_index; } 1846 1871 static int id(Arc e) { return e.id; } 1847 1872 static int id(Edge e) { return e.id; } 1848 1873 … … 1865 1890 arcs[2 * e.id].prev_out != -2; 1866 1891 } 1867 1892 1868 Node addRedNode() {1893 RedNode addRedNode() { 1869 1894 int n; 1870 1895 1871 1896 if(first_free_red==-1) { … … 1890 1915 1891 1916 nodes[n].first_out = -1; 1892 1917 1893 return Node(n);1918 return RedNode(n); 1894 1919 } 1895 1920 1896 Node addBlueNode() {1921 BlueNode addBlueNode() { 1897 1922 int n; 1898 1923 1899 1924 if(first_free_blue==-1) { … … 1918 1943 1919 1944 nodes[n].first_out = -1; 1920 1945 1921 return Node(n);1946 return BlueNode(n); 1922 1947 } 1923 1948 1924 1949 Edge addEdge(Node u, Node v) { … … 2029 2054 2030 2055 protected: 2031 2056 2032 void changeRed(Edge e, Node n) { 2033 LEMON_DEBUG(nodes[n].red, "Node has to be red"); 2057 void changeRed(Edge e, RedNode n) { 2034 2058 if(arcs[(2 * e.id) | 1].next_out != -1) { 2035 2059 arcs[arcs[(2 * e.id) | 1].next_out].prev_out = 2036 2060 arcs[(2 * e.id) | 1].prev_out; … … 2052 2076 nodes[n.id].first_out = ((2 * e.id) | 1); 2053 2077 } 2054 2078 2055 void changeBlue(Edge e, Node n) { 2056 LEMON_DEBUG(nodes[n].red, "Node has to be blue"); 2057 if(arcs[2 * e.id].next_out != -1) { 2079 void changeBlue(Edge e, BlueNode n) { 2080 if(arcs[2 * e.id].next_out != -1) { 2058 2081 arcs[arcs[2 * e.id].next_out].prev_out = arcs[2 * e.id].prev_out; 2059 2082 } 2060 2083 if(arcs[2 * e.id].prev_out != -1) { … … 2119 2142 /// 2120 2143 /// This function adds a red new node to the graph. 2121 2144 /// \return The new node. 2122 Node addRedNode() { return Parent::addRedNode(); }2145 RedNode addRedNode() { return Parent::addRedNode(); } 2123 2146 2124 2147 /// \brief Add a new blue node to the graph. 2125 2148 /// 2126 2149 /// This function adds a blue new node to the graph. 2127 2150 /// \return The new node. 2128 Node addBlueNode() { return Parent::addBlueNode(); }2151 BlueNode addBlueNode() { return Parent::addBlueNode(); } 2129 2152 2130 2153 /// \brief Add a new edge to the graph. 2131 2154 /// … … 2133 2156 /// \c u and \c v with inherent orientation from node \c u to 2134 2157 /// node \c v. 2135 2158 /// \return The new edge. 2136 Edge addEdge(Node u, Node v) { 2159 Edge addEdge(RedNode u, BlueNode v) { 2160 return Parent::addEdge(u, v); 2161 } 2162 Edge addEdge(BlueNode v, RedNode u) { 2137 2163 return Parent::addEdge(u, v); 2138 2164 } 2139 2165 … … 2188 2214 /// 2189 2215 ///\warning This functionality cannot be used together with the 2190 2216 ///Snapshot feature. 2191 void changeRed(Edge e, Node n) {2192 Parent::changeRed(e, n);2217 void changeRed(Edge e, RedNode n) { 2218 Parent::changeRed(e, n); 2193 2219 } 2194 2220 /// \brief Change the blue node of an edge. 2195 2221 /// … … 2202 2228 /// 2203 2229 ///\warning This functionality cannot be used together with the 2204 2230 ///Snapshot feature. 2205 void changeBlue(Edge e, Node n) {2206 Parent::changeBlue(e, n);2231 void changeBlue(Edge e, BlueNode n) { 2232 Parent::changeBlue(e, n); 2207 2233 } 2208 2234 2209 2235 ///Clear the graph. -
lemon/smart_graph.h
diff -r 008af84a4d4a -r 434a20e74585 lemon/smart_graph.h
a b 854 854 bool operator<(const Node& node) const {return _id < node._id;} 855 855 }; 856 856 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 857 881 class Edge { 858 882 friend class SmartBpGraphBase; 859 883 protected: … … 913 937 bool red(Node n) const { return nodes[n._id].red; } 914 938 bool blue(Node n) const { return !nodes[n._id].red; } 915 939 940 static RedNode asRedNodeUnsafe(Node n) { return RedNode(n._id); } 941 static BlueNode asBlueNodeUnsafe(Node n) { return BlueNode(n._id); } 942 916 943 Node source(Arc a) const { return Node(arcs[a._id ^ 1].target); } 917 944 Node target(Arc a) const { return Node(arcs[a._id].target); } 918 945 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 } 921 952 922 953 static bool direction(Arc a) { 923 954 return (a._id & 1) == 1; … … 935 966 --node._id; 936 967 } 937 968 938 void first Red(Node& node) const {969 void first(RedNode& node) const { 939 970 node._id = first_red; 940 971 } 941 972 942 void next Red(Node& node) const {973 void next(RedNode& node) const { 943 974 node._id = nodes[node._id].partition_next; 944 975 } 945 976 946 void first Blue(Node& node) const {977 void first(BlueNode& node) const { 947 978 node._id = first_blue; 948 979 } 949 980 950 void next Blue(Node& node) const {981 void next(BlueNode& node) const { 951 982 node._id = nodes[node._id].partition_next; 952 983 } 953 984 … … 1005 1036 } 1006 1037 1007 1038 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; } 1016 1041 static int id(Arc e) { return e._id; } 1017 1042 static int id(Edge e) { return e._id; } 1018 1043 … … 1030 1055 return e._id >= 0 && 2 * e._id < static_cast<int>(arcs.size()); 1031 1056 } 1032 1057 1033 Node addRedNode() {1058 RedNode addRedNode() { 1034 1059 int n = nodes.size(); 1035 1060 nodes.push_back(NodeT()); 1036 1061 nodes[n].first_out = -1; … … 1039 1064 nodes[n].partition_next = first_red; 1040 1065 first_red = n; 1041 1066 1042 return Node(n);1067 return RedNode(n); 1043 1068 } 1044 1069 1045 Node addBlueNode() {1070 BlueNode addBlueNode() { 1046 1071 int n = nodes.size(); 1047 1072 nodes.push_back(NodeT()); 1048 1073 nodes[n].first_out = -1; … … 1051 1076 nodes[n].partition_next = first_blue; 1052 1077 first_blue = n; 1053 1078 1054 return Node(n);1079 return BlueNode(n); 1055 1080 } 1056 1081 1057 Edge addEdge( Node u,Node v) {1082 Edge addEdge(RedNode u, BlueNode v) { 1058 1083 int n = arcs.size(); 1059 1084 arcs.push_back(ArcT()); 1060 1085 arcs.push_back(ArcT()); … … 1124 1149 /// 1125 1150 /// This function adds a red new node to the graph. 1126 1151 /// \return The new node. 1127 Node addRedNode() { return Parent::addRedNode(); }1152 RedNode addRedNode() { return Parent::addRedNode(); } 1128 1153 1129 1154 /// \brief Add a new blue node to the graph. 1130 1155 /// 1131 1156 /// This function adds a blue new node to the graph. 1132 1157 /// \return The new node. 1133 Node addBlueNode() { return Parent::addBlueNode(); }1158 BlueNode addBlueNode() { return Parent::addBlueNode(); } 1134 1159 1135 1160 /// \brief Add a new edge to the graph. 1136 1161 /// … … 1138 1163 /// \c u and \c v with inherent orientation from node \c u to 1139 1164 /// node \c v. 1140 1165 /// \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); 1145 1171 } 1146 1172 1147 1173 /// \brief Node validity check … … 1237 1263 } else { 1238 1264 max_red = -1; 1239 1265 } 1240 Parent::notifier(RedNode()).erase( node);1266 Parent::notifier(RedNode()).erase(asRedNodeUnsafe(node)); 1241 1267 } else { 1242 1268 first_blue = nodes[n].partition_next; 1243 1269 if (first_blue != -1) { … … 1245 1271 } else { 1246 1272 max_blue = -1; 1247 1273 } 1248 Parent::notifier(BlueNode()).erase( node);1274 Parent::notifier(BlueNode()).erase(asBlueNodeUnsafe(node)); 1249 1275 } 1250 1276 Parent::notifier(Node()).erase(node); 1251 1277 nodes.pop_back(); -
test/bpgraph_test.cc
diff -r 008af84a4d4a -r 434a20e74585 test/bpgraph_test.cc
a b 41 41 G.reserveNode(3); 42 42 G.reserveEdge(3); 43 43 44 Node44 RedNode 45 45 rn1 = G.addRedNode(); 46 46 checkGraphNodeList(G, 1); 47 47 checkGraphRedNodeList(G, 1); … … 49 49 checkGraphEdgeList(G, 0); 50 50 checkGraphArcList(G, 0); 51 51 52 Node52 BlueNode 53 53 bn1 = G.addBlueNode(), 54 54 bn2 = G.addBlueNode(); 55 55 checkGraphNodeList(G, 3); … … 76 76 checkGraphConArcList(G, 2); 77 77 78 78 Edge 79 e2 = G.addEdge( rn1, bn1),79 e2 = G.addEdge(bn1, rn1), 80 80 e3 = G.addEdge(rn1, bn2); 81 81 82 82 checkGraphNodeList(G, 3); … … 112 112 TEMPLATE_BPGRAPH_TYPEDEFS(BpGraph); 113 113 114 114 BpGraph G; 115 Node 116 n1 = G.addRedNode(), n2 = G.addBlueNode(), 117 n3 = G.addBlueNode(), n4 = G.addRedNode(); 115 RedNode 116 n1 = G.addRedNode(), n4 = G.addRedNode(); 117 BlueNode 118 n2 = G.addBlueNode(), n3 = G.addBlueNode(); 118 119 Edge 119 120 e1 = G.addEdge(n1, n2), e2 = G.addEdge(n1, n3), 120 121 e3 = G.addEdge(n4, n2), e4 = G.addEdge(n4, n3); … … 159 160 TEMPLATE_BPGRAPH_TYPEDEFS(BpGraph); 160 161 161 162 BpGraph G; 162 Node 163 n1 = G.addRedNode(), n2 = G.addBlueNode(), 164 n3 = G.addBlueNode(), n4 = G.addRedNode(); 163 RedNode 164 n1 = G.addRedNode(), n4 = G.addRedNode(); 165 BlueNode 166 n2 = G.addBlueNode(), n3 = G.addBlueNode(); 165 167 Edge 166 168 e1 = G.addEdge(n1, n2), e2 = G.addEdge(n1, n3), 167 169 e3 = G.addEdge(n4, n2), e4 = G.addEdge(n4, n3); … … 209 211 TEMPLATE_BPGRAPH_TYPEDEFS(BpGraph); 210 212 211 213 BpGraph G; 212 Node 213 n1 = G.addRedNode(), 214 RedNode 215 n1 = G.addRedNode(); 216 BlueNode 214 217 n2 = G.addBlueNode(), 215 218 n3 = G.addBlueNode(); 216 219 Edge … … 225 228 226 229 typename BpGraph::Snapshot snapshot(G); 227 230 228 Node n4 = G.addRedNode();231 RedNode n4 = G.addRedNode(); 229 232 G.addEdge(n4, n2); 230 233 G.addEdge(n4, n3); 231 234 … … 292 295 TEMPLATE_BPGRAPH_TYPEDEFS(BpGraph); 293 296 BpGraph g; 294 297 295 Node 296 n1 = g.addRedNode(), 298 RedNode 299 n1 = g.addRedNode(); 300 BlueNode 297 301 n2 = g.addBlueNode(), 298 302 n3 = g.addBlueNode(); 299 303 … … 396 400 397 401 for (int i = 0; i < G.redNum(); ++i) { 398 402 check(G.red(G.redNode(i)), "Wrong node"); 399 check(G. redIndex(G.redNode(i)) == i, "Wrong index");403 check(G.index(G.redNode(i)) == i, "Wrong index"); 400 404 } 401 405 402 406 for (int i = 0; i < G.blueNum(); ++i) { 403 407 check(G.blue(G.blueNode(i)), "Wrong node"); 404 check(G. blueIndex(G.blueNode(i)) == i, "Wrong index");408 check(G.index(G.blueNode(i)) == i, "Wrong index"); 405 409 } 406 410 407 411 for (NodeIt u(G); u != INVALID; ++u) { -
test/graph_copy_test.cc
diff -r 008af84a4d4a -r 434a20e74585 test/graph_copy_test.cc
a b 221 221 SmartBpGraph::ArcMap<int> fam(from); 222 222 SmartBpGraph::EdgeMap<int> fem(from); 223 223 SmartBpGraph::Node fn = INVALID; 224 SmartBpGraph::RedNode frn = INVALID; 225 SmartBpGraph::BlueNode fbn = INVALID; 224 226 SmartBpGraph::Arc fa = INVALID; 225 227 SmartBpGraph::Edge fe = INVALID; 226 228 227 std::vector<SmartBpGraph:: Node> frnv;229 std::vector<SmartBpGraph::RedNode> frnv; 228 230 for (int i = 0; i < nn; ++i) { 229 SmartBpGraph:: Node node = from.addRedNode();231 SmartBpGraph::RedNode node = from.addRedNode(); 230 232 frnv.push_back(node); 231 233 fnm[node] = i * i; 232 234 frnm[node] = i + i; 233 if (i == 0) fn = node; 235 if (i == 0) { 236 fn = node; 237 frn = node; 238 } 234 239 } 235 240 236 std::vector<SmartBpGraph:: Node> fbnv;241 std::vector<SmartBpGraph::BlueNode> fbnv; 237 242 for (int i = 0; i < nn; ++i) { 238 SmartBpGraph:: Node node = from.addBlueNode();243 SmartBpGraph::BlueNode node = from.addBlueNode(); 239 244 fbnv.push_back(node); 240 245 fnm[node] = i * i; 241 246 fbnm[node] = i + i; 247 if (i == 0) fbn = node; 242 248 } 243 249 244 250 for (int i = 0; i < nn; ++i) { … … 260 266 typename GR::template ArcMap<int> tam(to); 261 267 typename GR::template EdgeMap<int> tem(to); 262 268 typename GR::Node tn; 269 typename GR::RedNode trn; 270 typename GR::BlueNode tbn; 263 271 typename GR::Arc ta; 264 272 typename GR::Edge te; 265 273 266 274 SmartBpGraph::NodeMap<typename GR::Node> nr(from); 267 SmartBpGraph::RedMap<typename GR:: Node> rnr(from);268 SmartBpGraph::BlueMap<typename GR:: Node> bnr(from);275 SmartBpGraph::RedMap<typename GR::RedNode> rnr(from); 276 SmartBpGraph::BlueMap<typename GR::BlueNode> bnr(from); 269 277 SmartBpGraph::ArcMap<typename GR::Arc> ar(from); 270 278 SmartBpGraph::EdgeMap<typename GR::Edge> er(from); 271 279 272 280 typename GR::template NodeMap<SmartBpGraph::Node> ncr(to); 273 typename GR::template RedMap<SmartBpGraph:: Node> rncr(to);274 typename GR::template BlueMap<SmartBpGraph:: Node> bncr(to);281 typename GR::template RedMap<SmartBpGraph::RedNode> rncr(to); 282 typename GR::template BlueMap<SmartBpGraph::BlueNode> bncr(to); 275 283 typename GR::template ArcMap<SmartBpGraph::Arc> acr(to); 276 284 typename GR::template EdgeMap<SmartBpGraph::Edge> ecr(to); 277 285 … … 282 290 arcRef(ar).edgeRef(er). 283 291 nodeCrossRef(ncr).redCrossRef(rncr).blueCrossRef(bncr). 284 292 arcCrossRef(acr).edgeCrossRef(ecr). 285 node(fn, tn).arc(fa, ta).edge(fe, te).run(); 293 node(fn, tn).redNode(frn, trn).blueNode(fbn, tbn). 294 arc(fa, ta).edge(fe, te).run(); 286 295 287 296 check(countNodes(from) == countNodes(to), "Wrong copy."); 288 297 check(countRedNodes(from) == countRedNodes(to), "Wrong copy."); … … 293 302 for (SmartBpGraph::NodeIt it(from); it != INVALID; ++it) { 294 303 check(ncr[nr[it]] == it, "Wrong copy."); 295 304 check(fnm[it] == tnm[nr[it]], "Wrong copy."); 296 if (from.red(it)) { 297 check(rnr[it] == nr[it], "Wrong copy."); 298 check(rncr[rnr[it]] == it, "Wrong copy."); 299 check(frnm[it] == trnm[rnr[it]], "Wrong copy."); 300 check(to.red(rnr[it]), "Wrong copy."); 301 } else { 302 check(bnr[it] == nr[it], "Wrong copy."); 303 check(bncr[bnr[it]] == it, "Wrong copy."); 304 check(fbnm[it] == tbnm[bnr[it]], "Wrong copy."); 305 check(to.blue(bnr[it]), "Wrong copy."); 306 } 305 } 306 307 for (SmartBpGraph::RedIt it(from); it != INVALID; ++it) { 308 check(ncr[nr[it]] == it, "Wrong copy."); 309 check(fnm[it] == tnm[nr[it]], "Wrong copy."); 310 check(rnr[it] == nr[it], "Wrong copy."); 311 check(rncr[rnr[it]] == it, "Wrong copy."); 312 check(frnm[it] == trnm[rnr[it]], "Wrong copy."); 313 check(to.red(rnr[it]), "Wrong copy."); 314 } 315 316 for (SmartBpGraph::BlueIt it(from); it != INVALID; ++it) { 317 check(ncr[nr[it]] == it, "Wrong copy."); 318 check(fnm[it] == tnm[nr[it]], "Wrong copy."); 319 check(bnr[it] == nr[it], "Wrong copy."); 320 check(bncr[bnr[it]] == it, "Wrong copy."); 321 check(fbnm[it] == tbnm[bnr[it]], "Wrong copy."); 322 check(to.blue(bnr[it]), "Wrong copy."); 307 323 } 308 324 309 325 for (SmartBpGraph::ArcIt it(from); it != INVALID; ++it) { … … 342 358 check(er[ecr[it]] == it, "Wrong copy."); 343 359 } 344 360 check(tn == nr[fn], "Wrong copy."); 361 check(trn == rnr[frn], "Wrong copy."); 362 check(tbn == bnr[fbn], "Wrong copy."); 345 363 check(ta == ar[fa], "Wrong copy."); 346 364 check(te == er[fe], "Wrong copy."); 347 365 -
test/graph_test.h
diff -r 008af84a4d4a -r 434a20e74585 test/graph_test.h
a b 46 46 typename Graph::RedIt n(G); 47 47 for(int i=0;i<cnt;i++) { 48 48 check(n!=INVALID,"Wrong red Node list linking."); 49 check(G.red(n),"Wrong node set check."); 50 check(!G.blue(n),"Wrong node set check."); 51 typename Graph::Node nn = n; 52 check(G.asRedNodeUnsafe(nn) == n,"Wrong node conversion."); 53 check(G.asRedNode(nn) == n,"Wrong node conversion."); 54 check(G.asBlueNode(nn) == INVALID,"Wrong node conversion."); 55 std::pair<typename Graph::RedNode, typename Graph::BlueNode> rbn = 56 G.asRedBlueNode(nn); 57 check(rbn.first == n,"Wrong node conversion."); 58 check(rbn.second == INVALID,"Wrong node conversion."); 49 59 ++n; 50 60 } 51 61 check(n==INVALID,"Wrong red Node list linking."); … … 58 68 typename Graph::BlueIt n(G); 59 69 for(int i=0;i<cnt;i++) { 60 70 check(n!=INVALID,"Wrong blue Node list linking."); 71 check(G.blue(n),"Wrong node set check."); 72 check(!G.red(n),"Wrong node set check."); 73 typename Graph::Node nn = n; 74 check(G.asBlueNodeUnsafe(nn) == n,"Wrong node conversion."); 75 check(G.asBlueNode(nn) == n,"Wrong node conversion."); 76 check(G.asRedNode(nn) == INVALID,"Wrong node conversion."); 77 std::pair<typename Graph::RedNode, typename Graph::BlueNode> rbn = 78 G.asRedBlueNode(nn); 79 check(rbn.first == INVALID,"Wrong node conversion."); 80 check(rbn.second == n,"Wrong node conversion."); 61 81 ++n; 62 82 } 63 83 check(n==INVALID,"Wrong blue Node list linking."); … … 207 227 std::set<int> values; 208 228 for (typename Graph::RedIt n(G); n != INVALID; ++n) { 209 229 check(G.red(n), "Wrong partition"); 210 check(G.redId(n) == G.id(RedNode(n)), "Wrong id"); 211 check(values.find(G.redId(n)) == values.end(), "Wrong id"); 212 check(G.redId(n) <= G.maxRedId(), "Wrong maximum id"); 230 check(values.find(G.id(n)) == values.end(), "Wrong id"); 231 check(G.id(n) <= G.maxRedId(), "Wrong maximum id"); 213 232 values.insert(G.id(n)); 214 233 } 215 234 check(G.maxId(RedNode()) == G.maxRedId(), "Wrong maximum id"); … … 221 240 std::set<int> values; 222 241 for (typename Graph::BlueIt n(G); n != INVALID; ++n) { 223 242 check(G.blue(n), "Wrong partition"); 224 check(G.blueId(n) == G.id(BlueNode(n)), "Wrong id"); 225 check(values.find(G.blueId(n)) == values.end(), "Wrong id"); 226 check(G.blueId(n) <= G.maxBlueId(), "Wrong maximum id"); 243 check(values.find(G.id(n)) == values.end(), "Wrong id"); 244 check(G.id(n) <= G.maxBlueId(), "Wrong maximum id"); 227 245 values.insert(G.id(n)); 228 246 } 229 247 check(G.maxId(BlueNode()) == G.maxBlueId(), "Wrong maximum id");