00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00020
00021
00022 #ifndef LEMON_CONCEPT_GRAPH_COMPONENT_H
00023 #define LEMON_CONCEPT_GRAPH_COMPONENT_H
00024
00025 #include <lemon/invalid.h>
00026 #include <lemon/concept/maps.h>
00027
00028 #include <lemon/bits/alteration_notifier.h>
00029
00030 namespace lemon {
00031 namespace concept {
00032
00033
00034
00036
00045
00046 #ifndef DOXYGEN
00047 template <char _selector = '0'>
00048 #endif
00049 class GraphItem {
00050 public:
00052
00056 GraphItem() {}
00058
00061 GraphItem(GraphItem const&) {}
00063
00066 GraphItem(Invalid) {}
00068
00071 GraphItem& operator=(GraphItem const&) { return *this; }
00073
00076 bool operator==(GraphItem) const { return false; }
00078
00081 bool operator!=(GraphItem) const { return false; }
00082
00084
00093 bool operator<(GraphItem) const { return false; }
00094
00095 template<typename _GraphItem>
00096 struct Constraints {
00097 void constraints() {
00098 _GraphItem i1;
00099 _GraphItem i2 = i1;
00100 _GraphItem i3 = INVALID;
00101
00102 i1 = i2 = i3;
00103
00104 bool b;
00105
00106 b = (ia == ib) && (ia != ib);
00107 b = (ia == INVALID) && (ib != INVALID);
00108
00109 }
00110
00111 const _GraphItem &ia;
00112 const _GraphItem &ib;
00113 };
00114 };
00115
00117
00120 typedef GraphItem<'n'> GraphNode;
00121
00123
00126 typedef GraphItem<'e'> GraphEdge;
00127
00128
00129
00130
00132
00139
00140 class BaseGraphComponent {
00141 public:
00142
00143 typedef BaseGraphComponent Graph;
00144
00146
00149 typedef GraphItem<'n'> Node;
00150
00152
00155 typedef GraphItem<'e'> Edge;
00156
00158
00161 Node target(const Edge&) const { return INVALID;}
00162
00164
00167 Node source(const Edge&) const { return INVALID;}
00168
00169
00170 template <typename _Graph>
00171 struct Constraints {
00172 typedef typename _Graph::Node Node;
00173 typedef typename _Graph::Edge Edge;
00174
00175 void constraints() {
00176 checkConcept<GraphItem<'n'>, Node>();
00177 checkConcept<GraphItem<'e'>, Edge>();
00178 {
00179 Node n;
00180 Edge e(INVALID);
00181 n = graph.source(e);
00182 n = graph.target(e);
00183 }
00184 }
00185
00186 const _Graph& graph;
00187 };
00188 };
00189
00191
00195
00196 class BaseIterableGraphComponent : virtual public BaseGraphComponent {
00197 public:
00198
00199 typedef BaseGraphComponent::Node Node;
00200 typedef BaseGraphComponent::Edge Edge;
00201
00203
00206 void first(Node&) const {}
00207
00209
00212 void next(Node&) const {}
00213
00215
00218 void first(Edge&) const {}
00220
00223 void next(Edge&) const {}
00224
00225
00227
00230 void firstIn(Edge&, const Node&) const {}
00231
00233
00234
00237 void nextIn(Edge&) const {}
00238
00240
00243 void firstOut(Edge&, const Node&) const {}
00244
00246
00249 void nextOut(Edge&) const {}
00250
00251
00252 template <typename _Graph>
00253 struct Constraints {
00254
00255 void constraints() {
00256 checkConcept< BaseGraphComponent, _Graph >();
00257 typename _Graph::Node node;
00258 typename _Graph::Edge edge;
00259 {
00260 graph.first(node);
00261 graph.next(node);
00262 }
00263 {
00264 graph.first(edge);
00265 graph.next(edge);
00266 }
00267 {
00268 graph.firstIn(edge, node);
00269 graph.nextIn(edge);
00270 }
00271 {
00272 graph.firstOut(edge, node);
00273 graph.nextOut(edge);
00274 }
00275 }
00276
00277 const _Graph& graph;
00278 };
00279 };
00280
00282
00287 class IDableGraphComponent : virtual public BaseGraphComponent {
00288 public:
00289
00290 typedef BaseGraphComponent::Node Node;
00291 typedef BaseGraphComponent::Edge Edge;
00292
00294
00297 int id(const Node&) const { return -1;}
00298
00304 Node fromId(int , Node) const { return INVALID;}
00305
00310 int id(const Edge&) const { return -1;}
00311
00317 Edge fromId(int, Edge) const { return INVALID;}
00318
00319 template <typename _Graph>
00320 struct Constraints {
00321
00322 void constraints() {
00323 checkConcept< BaseGraphComponent, _Graph >();
00324 typename _Graph::Node node;
00325 int nid = graph.id(node);
00326 nid = graph.id(node);
00327 node = graph.fromId(nid, Node());
00328 typename _Graph::Edge edge;
00329 int eid = graph.id(edge);
00330 eid = graph.id(edge);
00331 edge = graph.fromId(eid, Edge());
00332 }
00333
00334 const _Graph& graph;
00335 };
00336 };
00337
00338
00340
00345 class MaxIDableGraphComponent : virtual public BaseGraphComponent {
00346 public:
00347
00349
00352 int maxId(Node = INVALID) const { return -1;}
00353
00355
00358 int maxId(Edge = INVALID) const { return -1;}
00359
00360 template <typename _Graph>
00361 struct Constraints {
00362
00363 void constraints() {
00364 checkConcept<BaseGraphComponent, _Graph>();
00365 int nid = graph.maxId(typename _Graph::Node());
00366 ignore_unused_variable_warning(nid);
00367 int eid = graph.maxId(typename _Graph::Edge());
00368 ignore_unused_variable_warning(eid);
00369 }
00370
00371 const _Graph& graph;
00372 };
00373 };
00374
00376
00380 class BaseExtendableGraphComponent : virtual public BaseGraphComponent {
00381 public:
00382
00383 typedef BaseGraphComponent::Node Node;
00384 typedef BaseGraphComponent::Edge Edge;
00385
00387
00390 Node addNode() {
00391 return INVALID;
00392 }
00393
00395
00398 Edge addEdge(const Node&, const Node&) {
00399 return INVALID;
00400 }
00401
00402 template <typename _Graph>
00403 struct Constraints {
00404 void constraints() {
00405 checkConcept<BaseGraphComponent, _Graph >();
00406 typename _Graph::Node node_a, node_b;
00407 node_a = graph.addNode();
00408 node_b = graph.addNode();
00409 typename _Graph::Edge edge;
00410 edge = graph.addEdge(node_a, node_b);
00411 }
00412
00413 _Graph& graph;
00414 };
00415 };
00416
00418
00422 class BaseErasableGraphComponent : virtual public BaseGraphComponent {
00423 public:
00424
00425 typedef BaseGraphComponent::Node Node;
00426 typedef BaseGraphComponent::Edge Edge;
00427
00429
00432 void erase(const Node&) {}
00433
00435
00438 void erase(const Edge&) {}
00439
00440 template <typename _Graph>
00441 struct Constraints {
00442 void constraints() {
00443 checkConcept<BaseGraphComponent, _Graph>();
00444 typename _Graph::Node node;
00445 graph.erase(node);
00446 typename _Graph::Edge edge;
00447 graph.erase(edge);
00448 }
00449
00450 _Graph& graph;
00451 };
00452 };
00453
00455
00459 class ClearableGraphComponent : virtual public BaseGraphComponent {
00460 public:
00461
00463
00466 void clear() {}
00467
00468 template <typename _Graph>
00469 struct Constraints {
00470 void constraints() {
00471 checkConcept<BaseGraphComponent, _Graph>();
00472 graph.clear();
00473 }
00474
00475 _Graph graph;
00476 };
00477 };
00478
00479
00481
00484 template <typename _Graph, typename _Item>
00485 class GraphIterator : public _Item {
00486 public:
00488
00490
00493 GraphIterator() {}
00495
00498 GraphIterator(GraphIterator const&) {}
00500
00503 explicit GraphIterator(const _Graph&) {}
00505
00508 GraphIterator(Invalid) {}
00510
00513 GraphIterator& operator=(GraphIterator const&) { return *this; }
00515
00518 GraphIterator& operator++() { return *this; }
00519
00521
00524 bool operator==(const GraphIterator&) const { return true;}
00526
00529 bool operator!=(const GraphIterator&) const { return true;}
00530
00531 template<typename _GraphIterator>
00532 struct Constraints {
00533 void constraints() {
00534
00535 _GraphIterator it1(g);
00536
00538
00539 _GraphIterator it2;
00540
00541 it2 = ++it1;
00542 ++it2 = it1;
00543 ++(++it1);
00545 _Item bi = it1;
00546 bi = it2;
00547 }
00548 _Graph& g;
00549 };
00550 };
00551
00553
00559 template <typename Graph,
00560 typename Edge = typename Graph::Edge,
00561 char _selector = '0'>
00562 class GraphIncIterator : public Edge {
00563 public:
00565
00568 GraphIncIterator() {}
00570
00573 GraphIncIterator(GraphIncIterator const& gi) :Edge(gi) {}
00576
00580 explicit GraphIncIterator(const Graph&, const typename Graph::Node&) {}
00582
00585 GraphIncIterator(Invalid) {}
00587
00590 GraphIncIterator& operator=(GraphIncIterator const&) { return *this; }
00592
00595 GraphIncIterator& operator++() { return *this; }
00596
00597
00598
00600
00603 bool operator==(const GraphIncIterator&) const { return true;}
00604
00606
00609 bool operator!=(const GraphIncIterator&) const { return true;}
00610
00611 template <typename _GraphIncIterator>
00612 struct Constraints {
00613 typedef typename Graph::Node Node;
00614 void constraints() {
00615 checkConcept<GraphItem<'e'>, _GraphIncIterator>();
00616 _GraphIncIterator it1(graph, node);
00618
00619 _GraphIncIterator it2;
00620
00621 it2 = ++it1;
00622 ++it2 = it1;
00623 ++(++it1);
00624 Edge e = it1;
00625 e = it2;
00626
00627 const_constraits();
00628 }
00629
00630 void const_constraits() {
00631 Node n = graph.baseNode(it);
00632 n = graph.runningNode(it);
00633 }
00634
00635 Edge edge;
00636 Node node;
00637 Graph graph;
00638 _GraphIncIterator it;
00639 };
00640 };
00641
00642
00644
00648 class IterableGraphComponent : virtual public BaseGraphComponent {
00649
00650 public:
00651
00652 typedef IterableGraphComponent Graph;
00653
00654 typedef BaseGraphComponent::Node Node;
00655 typedef BaseGraphComponent::Edge Edge;
00656
00658
00661 typedef GraphIterator<Graph, Node> NodeIt;
00663
00666 typedef GraphIterator<Graph, Edge> EdgeIt;
00668
00671 typedef GraphIncIterator<Graph, Edge, 'i'> InEdgeIt;
00673
00676 typedef GraphIncIterator<Graph, Edge, 'o'> OutEdgeIt;
00677
00682 Node baseNode(const InEdgeIt&) const { return INVALID; }
00683
00688 Node runningNode(const InEdgeIt&) const { return INVALID; }
00689
00694 Node baseNode(const OutEdgeIt&) const { return INVALID; }
00695
00700 Node runningNode(const OutEdgeIt&) const { return INVALID; }
00701
00706 Node oppositeNode(const Node&, const Edge&) const { return INVALID; }
00707
00708
00709 template <typename _Graph>
00710 struct Constraints {
00711 void constraints() {
00712 checkConcept< BaseGraphComponent, _Graph>();
00713
00714 checkConcept<GraphIterator<_Graph, typename _Graph::Edge>,
00715 typename _Graph::EdgeIt >();
00716 checkConcept<GraphIterator<_Graph, typename _Graph::Node>,
00717 typename _Graph::NodeIt >();
00718 checkConcept<GraphIncIterator<_Graph>, typename _Graph::InEdgeIt>();
00719 checkConcept<GraphIncIterator<_Graph>, typename _Graph::OutEdgeIt>();
00720
00721 typename _Graph::Node n(INVALID);
00722 typename _Graph::Edge e(INVALID);
00723 n = graph.oppositeNode(n, e);
00724 }
00725
00726 const _Graph& graph;
00727
00728 };
00729 };
00730
00732
00738 class AlterableGraphComponent : virtual public BaseGraphComponent {
00739 public:
00740
00742 typedef AlterationNotifier<Edge> EdgeNotifier;
00744 typedef AlterationNotifier<Node> NodeNotifier;
00745
00749 EdgeNotifier getNotifier(Edge) const {
00750 return EdgeNotifier();
00751 }
00752
00756 NodeNotifier getNotifier(Node) const {
00757 return NodeNotifier();
00758 }
00759
00760 };
00761
00762
00764
00768 template <typename Graph, typename Item, typename _Value>
00769 class GraphMap : public ReadWriteMap<Item, _Value> {
00770 protected:
00771 GraphMap() {}
00772 public:
00776 explicit GraphMap(const Graph&) {}
00780 GraphMap(const Graph&, const _Value&) {}
00784 GraphMap(const GraphMap& gm) :ReadWriteMap<Item, _Value>(gm) {}
00785
00789 GraphMap& operator=(const GraphMap&) { return *this;}
00790
00791 template<typename _Map>
00792 struct Constraints {
00793 void constraints() {
00794 checkConcept<ReadWriteMap<Item, _Value>, _Map >();
00795
00796 _Map a(g);
00797
00798 _Map a2(g,t);
00799
00800 _Map b=c;
00801
00802 a=b;
00803
00804 ignore_unused_variable_warning(a2);
00805 }
00806
00807 const _Map &c;
00808 const Graph &g;
00809 const typename GraphMap::Value &t;
00810 };
00811
00812 };
00813
00815
00819 class MappableGraphComponent : virtual public BaseGraphComponent {
00820 public:
00821
00822 typedef MappableGraphComponent Graph;
00823
00824 typedef BaseGraphComponent::Node Node;
00825 typedef BaseGraphComponent::Edge Edge;
00826
00828
00831 template <typename _Value>
00832 class NodeMap : public GraphMap<Graph, Node, _Value> {
00833 private:
00834 NodeMap();
00835 public:
00840 explicit NodeMap(const Graph&) {}
00844 NodeMap(const Graph&, const _Value&) {}
00848 NodeMap(const NodeMap& nm) : GraphMap<Graph, Node, _Value>(nm) {}
00849
00853 NodeMap& operator=(const NodeMap&) { return *this;}
00854
00855 };
00856
00858
00861 template <typename _Value>
00862 class EdgeMap : public GraphMap<Graph, Edge, _Value> {
00863 private:
00864 EdgeMap();
00865 public:
00870 explicit EdgeMap(const Graph&) {}
00874 EdgeMap(const Graph&, const _Value&) {}
00878 EdgeMap(const EdgeMap& em) :GraphMap<Graph, Edge, _Value>(em) {}
00879
00883 EdgeMap& operator=(const EdgeMap&) { return *this;}
00884
00885 };
00886
00887 template <typename _Graph>
00888 struct Constraints {
00889
00890 struct Type {
00891 int value;
00892 Type() : value(0) {}
00893 Type(int _v) : value(_v) {}
00894 };
00895
00896 void constraints() {
00897 checkConcept<BaseGraphComponent, _Graph>();
00898 {
00899 typedef typename _Graph::template NodeMap<int> IntNodeMap;
00900 checkConcept<GraphMap<_Graph, typename _Graph::Node, int>,
00901 IntNodeMap >();
00902 } {
00903 typedef typename _Graph::template NodeMap<bool> BoolNodeMap;
00904 checkConcept<GraphMap<_Graph, typename _Graph::Node, bool>,
00905 BoolNodeMap >();
00906 } {
00907 typedef typename _Graph::template NodeMap<Type> TypeNodeMap;
00908 checkConcept<GraphMap<_Graph, typename _Graph::Node, Type>,
00909 TypeNodeMap >();
00910 }
00911
00912 {
00913 typedef typename _Graph::template EdgeMap<int> IntEdgeMap;
00914 checkConcept<GraphMap<_Graph, typename _Graph::Edge, int>,
00915 IntEdgeMap >();
00916 } {
00917 typedef typename _Graph::template EdgeMap<bool> BoolEdgeMap;
00918 checkConcept<GraphMap<_Graph, typename _Graph::Edge, bool>,
00919 BoolEdgeMap >();
00920 } {
00921 typedef typename _Graph::template EdgeMap<Type> TypeEdgeMap;
00922 checkConcept<GraphMap<_Graph, typename _Graph::Edge, Type>,
00923 TypeEdgeMap >();
00924 }
00925 }
00926
00927 _Graph& graph;
00928 };
00929 };
00930
00938 class ExtendableGraphComponent : virtual public BaseGraphComponent {
00939 public:
00940
00941 typedef ExtendableGraphComponent Graph;
00942
00943 typedef BaseGraphComponent::Node Node;
00944 typedef BaseGraphComponent::Edge Edge;
00945
00949 Node addNode() {
00950 return INVALID;
00951 }
00952
00956 Edge addEdge(const Node&, const Node&) {
00957 return INVALID;
00958 }
00959
00960 template <typename _Graph>
00961 struct Constraints {
00962 void constraints() {
00963 checkConcept<BaseGraphComponent, _Graph >();
00964 typename _Graph::Node node_a, node_b;
00965 node_a = graph.addNode();
00966 node_b = graph.addNode();
00967 typename _Graph::Edge edge;
00968 edge = graph.addEdge(node_a, node_b);
00969 }
00970 _Graph& graph;
00971 };
00972 };
00973
00981 class ErasableGraphComponent : virtual public BaseGraphComponent {
00982 public:
00983
00984 typedef ErasableGraphComponent Graph;
00985
00986 typedef BaseGraphComponent::Node Node;
00987 typedef BaseGraphComponent::Edge Edge;
00988
00992 void erase(const Node&) {}
00993
00997 void erase(const Edge&) {}
00998
00999 template <typename _Graph>
01000 struct Constraints {
01001 void constraints() {
01002 checkConcept<BaseGraphComponent, _Graph >();
01003 typename _Graph::Node node;
01004 graph.erase(node);
01005 typename _Graph::Edge edge;
01006 graph.erase(edge);
01007 }
01008
01009 _Graph& graph;
01010 };
01011 };
01012
01013 }
01014
01015 }
01016
01017 #endif