92 class NodeIt; |
92 class NodeIt; |
93 class EdgeIt; |
93 class EdgeIt; |
94 class OutEdgeIt; |
94 class OutEdgeIt; |
95 class InEdgeIt; |
95 class InEdgeIt; |
96 |
96 |
97 template <typename T> class NodeMap; |
|
98 template <typename T> class EdgeMap; |
|
99 |
|
100 public: |
97 public: |
101 |
98 |
102 ListGraph() : nodes(), first_node(-1), |
99 ListGraph() : nodes(), first_node(-1), |
103 first_free_node(-1), edges(), first_free_edge(-1) {} |
100 first_free_node(-1), edges(), first_free_edge(-1) {} |
104 ListGraph(const ListGraph &_g) : nodes(_g.nodes), first_node(_g.first_node), |
101 ListGraph(const ListGraph &_g) : nodes(_g.nodes), first_node(_g.first_node), |
326 friend class ListGraph; |
323 friend class ListGraph; |
327 public: |
324 public: |
328 NodeIt() : Node() { } |
325 NodeIt() : Node() { } |
329 NodeIt(Invalid i) : Node(i) { } |
326 NodeIt(Invalid i) : Node(i) { } |
330 NodeIt(const ListGraph& G) : Node(G.first_node) { } |
327 NodeIt(const ListGraph& G) : Node(G.first_node) { } |
|
328 ///\todo Undocumented conversion Node -\> NodeIt. |
|
329 NodeIt(const ListGraph& G, const Node &n) : Node(n) { } |
331 }; |
330 }; |
332 |
331 |
333 class Edge { |
332 class Edge { |
334 friend class ListGraph; |
333 friend class ListGraph; |
335 template <typename T> friend class EdgeMap; |
334 template <typename T> friend class EdgeMap; |
942 }; |
942 }; |
943 |
943 |
944 class NodeIt : public Node { |
944 class NodeIt : public Node { |
945 friend class NodeSet; |
945 friend class NodeSet; |
946 public: |
946 public: |
|
947 NodeIt() : Node() { } |
|
948 NodeIt(Invalid i) : Node(i) { } |
947 NodeIt(const NodeSet& G) : Node(G.first_node) { } |
949 NodeIt(const NodeSet& G) : Node(G.first_node) { } |
948 NodeIt() : Node() { } |
950 ///\todo Undocumented conversion Node -\> NodeIt. |
|
951 NodeIt(const NodeSet& G, const Node &n) : Node(n) { } |
|
952 |
949 }; |
953 }; |
950 |
954 |
951 class Edge { |
955 class Edge { |
952 //friend class NodeSet; |
956 //friend class NodeSet; |
953 //template <typename T> friend class EdgeMap; |
957 //template <typename T> friend class EdgeMap; |
1180 NodeIt() : NodeGraphType::NodeIt() { } |
1184 NodeIt() : NodeGraphType::NodeIt() { } |
1181 NodeIt (Invalid i) : NodeGraphType::NodeIt(i) {} |
1185 NodeIt (Invalid i) : NodeGraphType::NodeIt(i) {} |
1182 NodeIt(const EdgeSet& _G) : NodeGraphType::NodeIt(_G.G) { } |
1186 NodeIt(const EdgeSet& _G) : NodeGraphType::NodeIt(_G.G) { } |
1183 NodeIt(const typename NodeGraphType::NodeIt &n) |
1187 NodeIt(const typename NodeGraphType::NodeIt &n) |
1184 : NodeGraphType::NodeIt(n) {} |
1188 : NodeGraphType::NodeIt(n) {} |
|
1189 ///\todo Undocumented conversion Node -\> NodeIt. |
|
1190 NodeIt(const EdgeSet& _G, const Node &n) |
|
1191 : NodeGraphType::NodeIt(_G.G,n) { } |
|
1192 |
1185 operator Node() { return Node(*this);} |
1193 operator Node() { return Node(*this);} |
1186 }; |
1194 }; |
1187 |
1195 |
1188 private: |
1196 private: |
1189 //Edges are double linked. |
1197 //Edges are double linked. |
1324 EdgeIt& next(EdgeIt& it) const { |
1332 EdgeIt& next(EdgeIt& it) const { |
1325 if(edges[it.n].next_in!=-1) { |
1333 if(edges[it.n].next_in!=-1) { |
1326 it.n=edges[it.n].next_in; |
1334 it.n=edges[it.n].next_in; |
1327 } |
1335 } |
1328 else { |
1336 else { |
1329 NodeIt n; |
1337 NodeIt n(*this,edges[it.n].head); |
1330 for(n=next(edges[it.n].head); |
1338 for(n=next(n); |
1331 valid(n) && nodes[n].first_in == -1; |
1339 valid(n) && nodes[n].first_in == -1; |
1332 next(n)) ; |
1340 next(n)) ; |
1333 it.n = (valid(n))?-1:nodes[n].first_in; |
1341 it.n = (valid(n))?-1:nodes[n].first_in; |
1334 } |
1342 } |
1335 return it; |
1343 return it; |
1336 } |
1344 } |
1337 |
1345 |
1338 int id(Edge e) const { return e.n; } |
1346 int id(Edge e) const { return e.n; } |
1339 |
1347 |
1340 /// Adds a new node to the graph. |
1348 /// Adds a new node to the graph. |
1341 Node addNode() { return G.AddNode(); } |
1349 Node addNode() { return G.addNode(); } |
1342 |
1350 |
1343 Edge addEdge(Node u, Node v) { |
1351 Edge addEdge(Node u, Node v) { |
1344 int n; |
1352 int n; |
1345 |
1353 |
1346 if(first_free_edge==-1) |
1354 if(first_free_edge==-1) |
1407 // while((m=nodes[n].first_out)!=-1) eraseEdge(m); |
1415 // while((m=nodes[n].first_out)!=-1) eraseEdge(m); |
1408 // } |
1416 // } |
1409 |
1417 |
1410 void erase(Edge e) { eraseEdge(e.n); } |
1418 void erase(Edge e) { eraseEdge(e.n); } |
1411 |
1419 |
|
1420 ///Clear all edges. (Doesn't clear the nodes!) |
|
1421 void clear() { |
|
1422 edges.clear(); |
|
1423 first_free_edge=-1; |
|
1424 } |
|
1425 |
|
1426 |
1412 // //\bug Dynamic maps must be updated! |
1427 // //\bug Dynamic maps must be updated! |
1413 // // |
1428 // // |
1414 // void clear() { |
1429 // void clear() { |
1415 // nodes.clear();edges.clear(); |
1430 // nodes.clear();edges.clear(); |
1416 // first_node=first_free_node=first_free_edge=-1; |
1431 // first_node=first_free_node=first_free_edge=-1; |
1417 // } |
1432 // } |
1418 |
1433 |
|
1434 public: |
|
1435 template <typename T> class EdgeMap; |
|
1436 |
|
1437 /// |
1419 class Edge { |
1438 class Edge { |
|
1439 public: |
1420 friend class EdgeSet; |
1440 friend class EdgeSet; |
1421 template <typename T> friend class EdgeMap; |
1441 template <typename T> friend class EdgeMap; |
1422 |
1442 |
1423 //template <typename T> friend class SymEdgeSet::SymEdgeMap; |
|
1424 //friend Edge SymEdgeSet::opposite(Edge) const; |
|
1425 |
|
1426 friend class Node; |
1443 friend class Node; |
1427 friend class NodeIt; |
1444 friend class NodeIt; |
|
1445 public: |
|
1446 ///\bug It shoud be at least protected |
|
1447 /// |
|
1448 int n; |
1428 protected: |
1449 protected: |
1429 int n; |
|
1430 friend int EdgeSet::id(Edge e) const; |
1450 friend int EdgeSet::id(Edge e) const; |
1431 |
1451 |
1432 Edge(int nn) {n=nn;} |
1452 Edge(int nn) {n=nn;} |
1433 public: |
1453 public: |
1434 Edge() { } |
1454 Edge() { } |
1464 friend class EdgeSet; |
1487 friend class EdgeSet; |
1465 public: |
1488 public: |
1466 OutEdgeIt() : Edge() { } |
1489 OutEdgeIt() : Edge() { } |
1467 OutEdgeIt (Invalid i) : Edge(i) { } |
1490 OutEdgeIt (Invalid i) : Edge(i) { } |
1468 |
1491 |
1469 OutEdgeIt(const EdgeSet& G,const Node v) : Edge(nodes[v].first_out) { } |
1492 OutEdgeIt(const EdgeSet& G,const Node v) : Edge(G.nodes[v].first_out) { } |
1470 }; |
1493 }; |
1471 |
1494 |
1472 class InEdgeIt : public Edge { |
1495 class InEdgeIt : public Edge { |
1473 friend class EdgeSet; |
1496 friend class EdgeSet; |
1474 public: |
1497 public: |
1475 InEdgeIt() : Edge() { } |
1498 InEdgeIt() : Edge() { } |
1476 InEdgeIt (Invalid i) : Edge(i) { } |
1499 InEdgeIt (Invalid i) : Edge(i) { } |
1477 InEdgeIt(const EdgeSet& G,Node v) :Edge(nodes[v].first_in) { } |
1500 InEdgeIt(const EdgeSet& G,Node v) :Edge(G.nodes[v].first_in) { } |
1478 }; |
1501 }; |
1479 |
1502 |
1480 template <typename T> class NodeMap : |
1503 template <typename T> class NodeMap : |
1481 public NodeGraphType::template NodeMap<T> |
1504 public NodeGraphType::template NodeMap<T> |
1482 { |
1505 { |
1483 public: |
1506 //This is a must, the constructors need it. |
1484 NodeMap(const EdgeSet &_G) : |
1507 typedef typename NodeGraphType::template NodeMap<T> ParentNodeMap; |
1485 NodeGraphType::NodeMap(_G.G) { } //AJAJJ <T> would be wrong!!! |
1508 public: |
1486 NodeMap(const EdgeSet &_G,const T &t) : |
1509 NodeMap(const EdgeSet &_G) : ParentNodeMap(_G.G) { } |
1487 NodeGraphType::NodeMap(_G.G,t) { } |
1510 NodeMap(const EdgeSet &_G,const T &t) : ParentNodeMap(_G.G,t) { } |
1488 //It is unnecessary |
1511 //It is unnecessary |
1489 NodeMap(const typename NodeGraphType::template NodeMap<T> &m) : |
1512 NodeMap(const typename NodeGraphType::template NodeMap<T> &m) : |
1490 NodeGraphType::NodeMap(m) { } |
1513 ParentNodeMap(m) { } |
1491 |
1514 |
1492 ///\todo It can copy between different types. |
1515 ///\todo It can copy between different types. |
1493 /// |
1516 /// |
1494 template<typename TT> |
1517 template<typename TT> |
1495 NodeMap(const typename NodeGraphType::template NodeMap<TT> &m) |
1518 NodeMap(const typename NodeGraphType::template NodeMap<TT> &m) |
1496 : NodeGraphType::NodeMap(m) { } |
1519 : ParentNodeMap(m) { } |
1497 }; |
1520 }; |
1498 |
1521 |
|
1522 /// |
1499 template <typename T> class EdgeMap : public DynMapBase<Edge> |
1523 template <typename T> class EdgeMap : public DynMapBase<Edge> |
1500 { |
1524 { |
|
1525 protected: |
|
1526 public: |
|
1527 ///\bug It should be at least protected |
|
1528 /// |
1501 std::vector<T> container; |
1529 std::vector<T> container; |
1502 |
1530 |
1503 public: |
1531 public: |
1504 typedef T ValueType; |
1532 typedef T ValueType; |
1505 typedef Edge KeyType; |
1533 typedef Edge KeyType; |
1555 { |
1583 { |
1556 if(k.n>=int(container.size())) container.resize(k.n+1); |
1584 if(k.n>=int(container.size())) container.resize(k.n+1); |
1557 } |
1585 } |
1558 void erase(const Edge) { } |
1586 void erase(const Edge) { } |
1559 |
1587 |
1560 void set(Edge n, T a) { container[n.n]=a; } |
1588 ///\bug This doesn't work. Why? |
|
1589 /// void set(Edge n, T a) { container[n.n]=a; } |
|
1590 void set(Edge n, T a) { container[G->id(n)]=a; } |
1561 //T get(Edge n) const { return container[n.n]; } |
1591 //T get(Edge n) const { return container[n.n]; } |
1562 typename std::vector<T>::reference |
1592 typename std::vector<T>::reference |
1563 operator[](Edge n) { return container[n.n]; } |
1593 ///\bug This doesn't work. Why? |
|
1594 /// operator[](Edge n) { return container[n.n]; } |
|
1595 operator[](Edge n) { return container[G->id(n)]; } |
1564 typename std::vector<T>::const_reference |
1596 typename std::vector<T>::const_reference |
1565 operator[](Edge n) const { return container[n.n]; } |
1597 ///\bug This doesn't work. Why? |
|
1598 /// operator[](Edge n) const { return container[n.n]; } |
|
1599 operator[](Edge n) const { return container[G->id(n)]; } |
1566 |
1600 |
1567 ///\warning There is no safety check at all! |
1601 ///\warning There is no safety check at all! |
1568 ///Using operator = between maps attached to different graph may |
1602 ///Using operator = between maps attached to different graph may |
1569 ///cause serious problem. |
1603 ///cause serious problem. |
1570 ///\todo Is this really so? |
1604 ///\todo Is this really so? |
1572 const EdgeMap<T>& operator=(const EdgeMap<T> &m) |
1606 const EdgeMap<T>& operator=(const EdgeMap<T> &m) |
1573 { |
1607 { |
1574 container = m.container; |
1608 container = m.container; |
1575 return *this; |
1609 return *this; |
1576 } |
1610 } |
|
1611 |
|
1612 template<typename TT> friend class EdgeMap; |
|
1613 |
1577 template<typename TT> |
1614 template<typename TT> |
1578 const EdgeMap<T>& operator=(const EdgeMap<TT> &m) |
1615 const EdgeMap<T>& operator=(const EdgeMap<TT> &m) |
1579 { |
1616 { |
1580 std::copy(m.container.begin(), m.container.end(), container.begin()); |
1617 std::copy(m.container.begin(), m.container.end(), container.begin()); |
1581 return *this; |
1618 return *this; |