2 #ifndef ANDRES_GRAPH_DIGRAPH_HXX
3 #define ANDRES_GRAPH_DIGRAPH_HXX
14 #include "adjacency.hxx"
15 #include "subgraph.hxx"
16 #include "visitor.hxx"
17 #include "detail/graph.hxx"
23 template<
typename VISITOR = IdleGraphVisitor<std::
size_t> >
60 std::size_t
vertexOfEdge(
const std::size_t,
const std::size_t)
const;
61 std::size_t
edgeFromVertex(
const std::size_t,
const std::size_t)
const;
62 std::size_t
edgeToVertex(
const std::size_t,
const std::size_t)
const;
64 std::size_t
vertexToVertex(
const std::size_t,
const std::size_t)
const;
67 std::pair<bool, std::size_t>
findEdge(
const std::size_t,
const std::size_t)
const;
73 std::size_t
insertEdge(
const std::size_t,
const std::size_t);
79 typedef detail::Adjacencies Adjacencies;
87 typedef detail::Edge<true> Edge;
89 void insertAdjacenciesForEdge(
const std::size_t);
90 void eraseAdjacenciesForEdge(
const std::size_t);
92 std::vector<Vertex> vertices_;
93 std::vector<Edge> edges_;
94 bool multipleEdgesEnabled_;
104 template<
typename VISITOR>
111 multipleEdgesEnabled_(false),
120 template<
typename VISITOR>
123 const std::size_t numberOfVertices,
126 : vertices_(numberOfVertices),
128 multipleEdgesEnabled_(false),
131 visitor_.insertVertices(0, numberOfVertices);
138 template<
typename VISITOR>
145 multipleEdgesEnabled_ =
false;
154 template<
typename VISITOR>
157 const std::size_t numberOfVertices,
160 vertices_.resize(numberOfVertices);
161 std::fill(vertices_.begin(), vertices_.end(), Vertex());
163 multipleEdgesEnabled_ =
false;
165 visitor_.insertVertices(0, numberOfVertices);
170 template<
typename VISITOR>
173 return vertices_.size();
178 template<
typename VISITOR>
181 return edges_.size();
190 template<
typename VISITOR>
193 const std::size_t vertex
195 return vertices_[vertex].to_.size();
204 template<
typename VISITOR>
207 const std::size_t vertex
209 return vertices_[vertex].from_.size();
217 template<
typename VISITOR>
220 const std::size_t edge,
225 return edges_[edge][j];
235 template<
typename VISITOR>
238 const std::size_t vertex,
241 return vertices_[vertex].to_[j].edge();
251 template<
typename VISITOR>
254 const std::size_t vertex,
257 return vertices_[vertex].from_[j].edge();
267 template<
typename VISITOR>
270 const std::size_t vertex,
273 return vertices_[vertex].to_[j].vertex();
283 template<
typename VISITOR>
286 const std::size_t vertex,
289 return vertices_[vertex].from_[j].vertex();
298 template<
typename VISITOR>
301 vertices_.push_back(Vertex());
302 visitor_.insertVertex(vertices_.size() - 1);
303 return vertices_.size() - 1;
313 template<
typename VISITOR>
316 const std::size_t number
318 std::size_t position = vertices_.size();
319 vertices_.insert(vertices_.end(), number, Vertex());
320 visitor_.insertVertices(position, number);
330 template<
typename VISITOR>
333 const std::size_t vertexIndex0,
334 const std::size_t vertexIndex1
336 assert(vertexIndex0 < numberOfVertices());
337 assert(vertexIndex1 < numberOfVertices());
339 if(multipleEdgesEnabled()) {
341 edges_.push_back(Edge(vertexIndex0, vertexIndex1));
342 std::size_t edgeIndex = edges_.size() - 1;
343 insertAdjacenciesForEdge(edgeIndex);
344 visitor_.insertEdge(edgeIndex);
348 std::pair<bool, std::size_t> p = findEdge(vertexIndex0, vertexIndex1);
362 template<
typename VISITOR>
365 const std::size_t vertexIndex
367 assert(vertexIndex < numberOfVertices());
370 while(vertices_[vertexIndex].from_.size() != 0) {
371 eraseEdge(vertices_[vertexIndex].from_.begin()->edge());
373 while(vertices_[vertexIndex].to_.size() != 0) {
374 eraseEdge(vertices_[vertexIndex].to_.begin()->edge());
377 if(vertexIndex == numberOfVertices() - 1) {
378 vertices_.pop_back();
379 visitor_.eraseVertex(vertexIndex);
385 std::size_t movingVertexIndex = numberOfVertices() - 1;
386 std::set<std::size_t> affectedEdgeIndices;
387 for(Adjacencies::const_iterator it = vertices_[movingVertexIndex].from_.begin();
388 it != vertices_[movingVertexIndex].from_.end(); ++it) {
389 affectedEdgeIndices.insert(it->edge());
391 for(Adjacencies::const_iterator it = vertices_[movingVertexIndex].to_.begin();
392 it != vertices_[movingVertexIndex].to_.end(); ++it) {
393 affectedEdgeIndices.insert(it->edge());
397 for(std::set<std::size_t>::const_iterator it = affectedEdgeIndices.begin();
398 it != affectedEdgeIndices.end(); ++it) {
400 eraseAdjacenciesForEdge(*it);
403 for(std::size_t j=0; j<2; ++j) {
404 if(edges_[*it][j] == movingVertexIndex) {
405 edges_[*it][j] = vertexIndex;
411 vertices_[vertexIndex] = vertices_[movingVertexIndex];
412 vertices_.pop_back();
415 for(std::set<std::size_t>::const_iterator it = affectedEdgeIndices.begin();
416 it != affectedEdgeIndices.end(); ++it) {
417 insertAdjacenciesForEdge(*it);
420 visitor_.eraseVertex(vertexIndex);
421 visitor_.relabelVertex(movingVertexIndex, vertexIndex);
429 template<
typename VISITOR>
432 const std::size_t edgeIndex
434 assert(edgeIndex < numberOfEdges());
436 eraseAdjacenciesForEdge(edgeIndex);
437 if(edgeIndex == numberOfEdges() - 1) {
439 visitor_.eraseEdge(edgeIndex);
442 std::size_t movingEdgeIndex = numberOfEdges() - 1;
443 eraseAdjacenciesForEdge(movingEdgeIndex);
444 edges_[edgeIndex] = edges_[movingEdgeIndex];
446 insertAdjacenciesForEdge(edgeIndex);
447 visitor_.eraseEdge(edgeIndex);
448 visitor_.relabelEdge(movingEdgeIndex, edgeIndex);
459 template<
typename VISITOR>
462 const std::size_t vertex
464 return vertices_[vertex].to_.begin();
474 template<
typename VISITOR>
477 const std::size_t vertex
479 return vertices_[vertex].to_.end();
489 template<
typename VISITOR>
492 const std::size_t vertex
494 return vertices_[vertex].from_.begin();
504 template<
typename VISITOR>
507 const std::size_t vertex
509 return vertices_[vertex].from_.end();
519 template<
typename VISITOR>
522 const std::size_t vertex
524 return vertices_[vertex].to_.begin();
534 template<
typename VISITOR>
537 const std::size_t vertex
539 return vertices_[vertex].to_.end();
549 template<
typename VISITOR>
552 const std::size_t vertex
554 return vertices_[vertex].from_.begin();
564 template<
typename VISITOR>
567 const std::size_t vertex
569 return vertices_[vertex].from_.end();
579 template<
typename VISITOR>
582 const std::size_t vertex
584 return vertices_[vertex].to_.begin();
594 template<
typename VISITOR>
597 const std::size_t vertex
599 return vertices_[vertex].to_.end();
609 template<
typename VISITOR>
612 const std::size_t vertex
614 return vertices_[vertex].from_.begin();
624 template<
typename VISITOR>
627 const std::size_t vertex
629 return vertices_[vertex].from_.end();
636 template<
typename VISITOR>
639 const std::size_t number
641 vertices_.reserve(number);
648 template<
typename VISITOR>
651 const std::size_t number
653 edges_.reserve(number);
661 template<
typename VISITOR>
664 const std::size_t vertex,
667 return vertices_[vertex].to_[j];
675 template<
typename VISITOR>
678 const std::size_t vertex,
681 return vertices_[vertex].from_[j];
692 template<
typename VISITOR>
693 inline std::pair<bool, std::size_t>
695 const std::size_t vertex0,
696 const std::size_t vertex1
698 assert(vertex0 < numberOfVertices());
699 assert(vertex1 < numberOfVertices());
702 verticesFromVertexBegin(vertex0),
703 verticesFromVertexEnd(vertex0),
706 if(it != verticesFromVertexEnd(vertex0) && *it == vertex1) {
708 const std::size_t j = std::distance(verticesFromVertexBegin(vertex0), it);
709 return std::make_pair(
true, edgeFromVertex(vertex0, j));
712 return std::make_pair(
false, 0);
720 template<
typename VISITOR>
723 return multipleEdgesEnabled_;
732 template<
typename VISITOR>
735 return multipleEdgesEnabled_;
738 template<
typename VISITOR>
741 const std::size_t edgeIndex
743 const Edge& edge = edges_[edgeIndex];
744 const std::size_t vertexIndex0 = edge[0];
745 const std::size_t vertexIndex1 = edge[1];
746 vertices_[vertexIndex0].to_.insert(
747 AdjacencyType(vertexIndex1, edgeIndex)
749 vertices_[vertexIndex1].from_.insert(
750 AdjacencyType(vertexIndex0, edgeIndex)
754 template<
typename VISITOR>
756 Digraph<VISITOR>::eraseAdjacenciesForEdge(
757 const std::size_t edgeIndex
759 const Edge& edge = edges_[edgeIndex];
760 const std::size_t vertexIndex0 = edge[0];
761 const std::size_t vertexIndex1 = edge[1];
762 Vertex& vertex0 = vertices_[vertexIndex0];
763 Vertex& vertex1 = vertices_[vertexIndex1];
765 AdjacencyType adj(vertexIndex1, edgeIndex);
766 RandomAccessSet<AdjacencyType>::iterator it = vertex0.to_.find(adj);
767 assert(it != vertex0.to_.end());
768 vertex0.to_.erase(it);
770 adj.vertex() = vertexIndex0;
771 it = vertex1.from_.find(adj);
772 assert(it != vertex1.from_.end());
773 vertex1.from_.erase(it);
779 #endif // #ifndef ANDRES_GRAPH_DIGRAPH_HXX