2 #ifndef ANDRES_GRAPH_COMPLETE_GRAPH_HXX
3 #define ANDRES_GRAPH_COMPLETE_GRAPH_HXX
10 #include "adjacency.hxx"
11 #include "visitor.hxx"
17 template<
typename VISITOR = IdleGraphVisitor<std::
size_t> >
24 class AdjacencyIterator
25 :
public std::iterator <
26 std::random_access_iterator_tag,
31 typedef std::iterator <
32 std::random_access_iterator_tag,
35 typedef typename Base::iterator_category iterator_category;
36 typedef typename Base::value_type value_type;
37 typedef typename Base::difference_type difference_type;
38 typedef typename Base::pointer pointer;
39 typedef typename Base::reference reference;
42 AdjacencyIterator(
const GraphType&);
43 AdjacencyIterator(
const GraphType&,
const std::size_t);
44 AdjacencyIterator(
const GraphType&,
const std::size_t,
const std::size_t);
47 AdjacencyIterator& operator+=(
const difference_type);
48 AdjacencyIterator& operator-=(
const difference_type);
49 AdjacencyIterator& operator++();
50 AdjacencyIterator& operator--();
51 AdjacencyIterator operator++(
int);
52 AdjacencyIterator operator--(
int);
53 AdjacencyIterator operator+(
const difference_type)
const;
54 AdjacencyIterator operator-(
const difference_type)
const;
55 difference_type operator-(
const AdjacencyIterator&)
const;
58 bool operator==(
const AdjacencyIterator&)
const;
59 bool operator!=(
const AdjacencyIterator&)
const;
60 bool operator<(
const AdjacencyIterator&)
const;
61 bool operator<=(
const AdjacencyIterator&)
const;
62 bool operator>(
const AdjacencyIterator&)
const;
63 bool operator>=(
const AdjacencyIterator&)
const;
66 reference operator*();
68 reference operator[](
const std::size_t);
71 const GraphType* graph_;
73 std::size_t adjacencyIndex_;
78 :
public AdjacencyIterator {
81 typedef AdjacencyIterator Base;
82 typedef typename Base::iterator_category iterator_category;
83 typedef typename Base::difference_type difference_type;
84 typedef const std::size_t value_type;
85 typedef value_type* pointer;
86 typedef value_type& reference;
89 VertexIterator(
const VertexIterator&);
90 VertexIterator(
const AdjacencyIterator&);
91 VertexIterator(
const GraphType&);
92 VertexIterator(
const GraphType&,
const std::size_t);
93 VertexIterator(
const GraphType&,
const std::size_t,
const std::size_t);
96 value_type operator*()
const;
97 value_type operator[](
const std::size_t)
const;
99 pointer operator->()
const;
103 :
public AdjacencyIterator {
105 typedef CompleteGraph<Visitor> GraphType;
106 typedef AdjacencyIterator Base;
107 typedef typename Base::iterator_category iterator_category;
108 typedef typename Base::difference_type difference_type;
109 typedef const std::size_t value_type;
110 typedef value_type* pointer;
111 typedef value_type& reference;
114 EdgeIterator(
const EdgeIterator&);
115 EdgeIterator(
const AdjacencyIterator&);
116 EdgeIterator(
const GraphType&);
117 EdgeIterator(
const GraphType&,
const std::size_t);
118 EdgeIterator(
const GraphType&,
const std::size_t,
const std::size_t);
121 value_type operator*()
const;
122 value_type operator[](
const std::size_t)
const;
124 pointer operator->()
const;
153 std::size_t
vertexOfEdge(
const std::size_t,
const std::size_t)
const;
154 std::size_t
edgeFromVertex(
const std::size_t,
const std::size_t)
const;
155 std::size_t
edgeToVertex(
const std::size_t,
const std::size_t)
const;
157 std::size_t
vertexToVertex(
const std::size_t,
const std::size_t)
const;
160 std::pair<bool, std::size_t>
findEdge(
const std::size_t,
const std::size_t)
const;
164 std::size_t edgeOfStrictlyIncreasingPairOfVertices(
const std::size_t,
const std::size_t)
const;
166 std::size_t numberOfVertices_;
174 template<
typename VISITOR>
179 : numberOfVertices_(0),
188 template<
typename VISITOR>
191 const std::size_t numberOfVertices,
194 : numberOfVertices_(numberOfVertices),
202 template<
typename VISITOR>
207 numberOfVertices_ = 0;
216 template<
typename VISITOR>
219 const std::size_t numberOfVertices,
222 numberOfVertices_ = numberOfVertices;
233 template<
typename VISITOR>
236 const std::size_t vertex
238 return VertexIterator(*
this, vertex, 0);
248 template<
typename VISITOR>
251 const std::size_t vertex
253 return VertexIterator(*
this, vertex, numberOfEdgesFromVertex(vertex));
263 template<
typename VISITOR>
266 const std::size_t vertex
268 return VertexIterator(*
this, vertex, 0);
278 template<
typename VISITOR>
281 const std::size_t vertex
283 return VertexIterator(*
this, vertex, numberOfEdgesFromVertex(vertex));
293 template<
typename VISITOR>
296 const std::size_t vertex
298 return EdgeIterator(*
this, vertex, 0);
308 template<
typename VISITOR>
311 const std::size_t vertex
313 return EdgeIterator(*
this, vertex, numberOfEdgesFromVertex(vertex));
323 template<
typename VISITOR>
326 const std::size_t vertex
328 return EdgeIterator(*
this, vertex, 0);
338 template<
typename VISITOR>
341 const std::size_t vertex
343 return EdgeIterator(*
this, vertex, numberOfEdgesFromVertex(vertex));
353 template<
typename VISITOR>
356 const std::size_t vertex
358 return AdjacencyIterator(*
this, vertex, 0);
368 template<
typename VISITOR>
371 const std::size_t vertex
373 return AdjacencyIterator(*
this, vertex, numberOfEdgesFromVertex(vertex));
383 template<
typename VISITOR>
386 const std::size_t vertex
388 return AdjacencyIterator(*
this, vertex, 0);
398 template<
typename VISITOR>
401 const std::size_t vertex
403 return AdjacencyIterator(*
this, vertex, numberOfEdgesFromVertex(vertex));
408 template<
typename VISITOR>
411 return numberOfVertices_;
416 template<
typename VISITOR>
419 return numberOfVertices() * (numberOfVertices() - 1) / 2;
428 template<
typename VISITOR>
431 const std::size_t vertex
433 assert(vertex < numberOfVertices());
434 return numberOfVertices() - 1;
443 template<
typename VISITOR>
446 const std::size_t vertex
448 assert(vertex < numberOfVertices());
449 return numberOfVertices() - 1;
457 template<
typename VISITOR>
460 const std::size_t edge,
463 assert(edge < numberOfEdges());
465 double p =
static_cast<double>(numberOfVertices() * 2 - 1) / 2;
466 double q =
static_cast<double>(edge) * 2;
467 std::size_t vertex0 =
static_cast<std::size_t
>(p - std::sqrt(p * p - q));
472 return edge + vertex0 * (vertex0 + 1) / 2 - (numberOfVertices() - 1) * vertex0 + 1;
483 template<
typename VISITOR>
486 const std::size_t vertex,
489 assert(j < numberOfEdgesFromVertex(vertex));
491 const std::size_t vertexAdjacent = j;
492 const std::size_t edgeAdjacent = edgeOfStrictlyIncreasingPairOfVertices(vertexAdjacent, vertex);
496 const std::size_t vertexAdjacent = j + 1;
497 const std::size_t edgeAdjacent = edgeOfStrictlyIncreasingPairOfVertices(vertex, vertexAdjacent);
509 template<
typename VISITOR>
512 const std::size_t vertex,
515 assert(j < numberOfEdgesToVertex(vertex));
516 return edgeFromVertex(vertex, j);
526 template<
typename VISITOR>
529 const std::size_t vertex,
532 assert(j < numberOfEdgesFromVertex(vertex));
548 template<
typename VISITOR>
551 const std::size_t vertex,
554 assert(j < numberOfEdgesToVertex(vertex));
555 return vertexFromVertex(vertex, j);
563 template<
typename VISITOR>
566 const std::size_t vertex,
569 assert(j < numberOfEdgesToVertex(vertex));
571 const std::size_t vertexAdjacent = j;
572 const std::size_t edgeAdjacent = edgeOfStrictlyIncreasingPairOfVertices(vertexAdjacent, vertex);
576 const std::size_t vertexAdjacent = j + 1;
577 const std::size_t edgeAdjacent = edgeOfStrictlyIncreasingPairOfVertices(vertex, vertexAdjacent);
587 template<
typename VISITOR>
590 const std::size_t vertex,
593 return adjacencyFromVertex(vertex, j);
610 template<
typename VISITOR>
611 inline std::pair<bool, std::size_t>
613 const std::size_t vertex0,
614 const std::size_t vertex1
616 assert(vertex0 < numberOfVertices());
617 assert(vertex1 < numberOfVertices());
618 if(vertex0 == vertex1) {
619 return std::pair<bool, std::size_t>(
false, 0);
621 else if(vertex0 < vertex1) {
622 return std::pair<bool, std::size_t>(
true, edgeOfStrictlyIncreasingPairOfVertices(vertex0, vertex1));
625 return std::pair<bool, std::size_t>(
true, edgeOfStrictlyIncreasingPairOfVertices(vertex1, vertex0));
633 template<
typename VISITOR>
640 template<
typename VISITOR>
643 const std::size_t vertex0,
644 const std::size_t vertex1
646 assert(vertex1 < numberOfVertices());
647 assert(vertex0 < vertex1);
648 return (numberOfVertices() - 1) * vertex0 - vertex0 * (vertex0 + 1) / 2 + vertex1 - 1;
655 template<
typename VISITOR>
657 CompleteGraph<VISITOR>::AdjacencyIterator::AdjacencyIterator()
664 template<
typename VISITOR>
666 CompleteGraph<VISITOR>::AdjacencyIterator::AdjacencyIterator(
667 const GraphType& graph
675 template<
typename VISITOR>
677 CompleteGraph<VISITOR>::AdjacencyIterator::AdjacencyIterator(
678 const GraphType& graph,
679 const std::size_t vertex
686 assert(vertex < graph.numberOfVertices());
689 template<
typename VISITOR>
691 CompleteGraph<VISITOR>::AdjacencyIterator::AdjacencyIterator(
692 const GraphType& graph,
693 const std::size_t vertex,
694 const std::size_t adjacencyIndex
698 adjacencyIndex_(adjacencyIndex),
701 assert(vertex < graph.numberOfVertices());
702 assert(adjacencyIndex <= graph.numberOfVertices());
705 template<
typename VISITOR>
706 inline typename CompleteGraph<VISITOR>::AdjacencyIterator&
707 CompleteGraph<VISITOR>::AdjacencyIterator::operator+=(
708 const difference_type d
710 adjacencyIndex_ += d;
714 template<
typename VISITOR>
715 inline typename CompleteGraph<VISITOR>::AdjacencyIterator&
716 CompleteGraph<VISITOR>::AdjacencyIterator::operator-=(
717 const difference_type d
719 adjacencyIndex_ -= d;
723 template<
typename VISITOR>
724 inline typename CompleteGraph<VISITOR>::AdjacencyIterator&
725 CompleteGraph<VISITOR>::AdjacencyIterator::operator++() {
730 template<
typename VISITOR>
731 inline typename CompleteGraph<VISITOR>::AdjacencyIterator&
732 CompleteGraph<VISITOR>::AdjacencyIterator::operator--() {
737 template<
typename VISITOR>
738 inline typename CompleteGraph<VISITOR>::AdjacencyIterator
739 CompleteGraph<VISITOR>::AdjacencyIterator::operator++(
int) {
740 AdjacencyIterator copy = *
this;
745 template<
typename VISITOR>
746 inline typename CompleteGraph<VISITOR>::AdjacencyIterator
747 CompleteGraph<VISITOR>::AdjacencyIterator::operator--(
int) {
748 AdjacencyIterator copy = *
this;
753 template<
typename VISITOR>
754 inline typename CompleteGraph<VISITOR>::AdjacencyIterator
755 CompleteGraph<VISITOR>::AdjacencyIterator::operator+(
756 const difference_type d
758 AdjacencyIterator copy = *
this;
763 template<
typename VISITOR>
764 inline typename CompleteGraph<VISITOR>::AdjacencyIterator
765 CompleteGraph<VISITOR>::AdjacencyIterator::operator-(
766 const difference_type d
768 AdjacencyIterator copy = *
this;
773 template<
typename VISITOR>
774 inline typename CompleteGraph<VISITOR>::AdjacencyIterator::difference_type
775 CompleteGraph<VISITOR>::AdjacencyIterator::operator-(
776 const AdjacencyIterator& adjacencyIterator
778 return adjacencyIndex_ - adjacencyIterator.adjacencyIndex_;
781 template<
typename VISITOR>
783 CompleteGraph<VISITOR>::AdjacencyIterator::operator==(
784 const AdjacencyIterator& other
786 return adjacencyIndex_ == other.adjacencyIndex_
787 && vertex_ == other.vertex_
788 && graph_ == other.graph_;
791 template<
typename VISITOR>
793 CompleteGraph<VISITOR>::AdjacencyIterator::operator!=(
794 const AdjacencyIterator& other
796 return adjacencyIndex_ != other.adjacencyIndex_
797 || vertex_ != other.vertex_
798 || graph_ != other.graph_;
801 template<
typename VISITOR>
803 CompleteGraph<VISITOR>::AdjacencyIterator::operator<(
804 const AdjacencyIterator& other
806 return adjacencyIndex_ < other.adjacencyIndex_
807 && vertex_ == other.vertex_
808 && graph_ == other.graph_;
811 template<
typename VISITOR>
813 CompleteGraph<VISITOR>::AdjacencyIterator::operator<=(
814 const AdjacencyIterator& other
816 return adjacencyIndex_ <= other.adjacencyIndex_
817 && vertex_ == other.vertex_
818 && graph_ == other.graph_;
822 template<
typename VISITOR>
824 CompleteGraph<VISITOR>::AdjacencyIterator::operator>(
825 const AdjacencyIterator& other
827 return adjacencyIndex_ > other.adjacencyIndex_
828 && vertex_ == other.vertex_
829 && graph_ == other.graph_;
832 template<
typename VISITOR>
834 CompleteGraph<VISITOR>::AdjacencyIterator::operator>=(
835 const AdjacencyIterator& other
837 return adjacencyIndex_ >= other.adjacencyIndex_
838 && vertex_ == other.vertex_
839 && graph_ == other.graph_;
842 template<
typename VISITOR>
843 inline typename CompleteGraph<VISITOR>::AdjacencyIterator::reference
844 CompleteGraph<VISITOR>::AdjacencyIterator::operator*() {
845 adjacency_ = graph_->adjacencyFromVertex(vertex_, adjacencyIndex_);
849 template<
typename VISITOR>
850 inline typename CompleteGraph<VISITOR>::AdjacencyIterator::pointer
851 CompleteGraph<VISITOR>::AdjacencyIterator::operator->() {
852 adjacency_ = graph_->adjacencyFromVertex(vertex_, adjacencyIndex_);
856 template<
typename VISITOR>
857 inline typename CompleteGraph<VISITOR>::AdjacencyIterator::reference
858 CompleteGraph<VISITOR>::AdjacencyIterator::operator[](
861 adjacency_ = graph_->adjacencyFromVertex(vertex_, adjacencyIndex_ + j);
867 template<
typename VISITOR>
869 CompleteGraph<VISITOR>::VertexIterator::VertexIterator()
873 template<
typename VISITOR>
875 CompleteGraph<VISITOR>::VertexIterator::VertexIterator(
876 const GraphType& graph
881 template<
typename VISITOR>
883 CompleteGraph<VISITOR>::VertexIterator::VertexIterator(
884 const GraphType& graph,
885 const std::size_t vertex
887 : Base(graph, vertex)
890 template<
typename VISITOR>
892 CompleteGraph<VISITOR>::VertexIterator::VertexIterator(
893 const GraphType& graph,
894 const std::size_t vertex,
895 const std::size_t adjacencyIndex
897 : Base(graph, vertex, adjacencyIndex)
900 template<
typename VISITOR>
902 CompleteGraph<VISITOR>::VertexIterator::VertexIterator(
903 const VertexIterator& it
905 : Base(*(it.graph_), it.vertex_, it.adjacencyIndex_)
908 template<
typename VISITOR>
910 CompleteGraph<VISITOR>::VertexIterator::VertexIterator(
911 const AdjacencyIterator& it
916 template<
typename VISITOR>
917 inline typename CompleteGraph<VISITOR>::VertexIterator::value_type
918 CompleteGraph<VISITOR>::VertexIterator::operator*()
const {
919 return Base::graph_->vertexFromVertex(Base::vertex_, Base::adjacencyIndex_);
922 template<
typename VISITOR>
923 inline typename CompleteGraph<VISITOR>::VertexIterator::value_type
924 CompleteGraph<VISITOR>::VertexIterator::operator[](
927 return Base::graph_->vertexFromVertex(Base::vertex_, Base::adjacencyIndex_ + j);
932 template<
typename VISITOR>
934 CompleteGraph<VISITOR>::EdgeIterator::EdgeIterator()
938 template<
typename VISITOR>
940 CompleteGraph<VISITOR>::EdgeIterator::EdgeIterator(
941 const GraphType& graph
946 template<
typename VISITOR>
948 CompleteGraph<VISITOR>::EdgeIterator::EdgeIterator(
949 const GraphType& graph,
950 const std::size_t vertex
952 : Base(graph, vertex)
955 template<
typename VISITOR>
957 CompleteGraph<VISITOR>::EdgeIterator::EdgeIterator(
958 const GraphType& graph,
959 const std::size_t vertex,
960 const std::size_t adjacencyIndex
962 : Base(graph, vertex, adjacencyIndex)
965 template<
typename VISITOR>
967 CompleteGraph<VISITOR>::EdgeIterator::EdgeIterator(
968 const EdgeIterator& it
970 : Base(*(it.graph_), it.vertex_, it.adjacencyIndex_)
973 template<
typename VISITOR>
975 CompleteGraph<VISITOR>::EdgeIterator::EdgeIterator(
976 const AdjacencyIterator& it
981 template<
typename VISITOR>
982 inline typename CompleteGraph<VISITOR>::EdgeIterator::value_type
983 CompleteGraph<VISITOR>::EdgeIterator::operator*()
const {
984 return Base::graph_->edgeFromVertex(Base::vertex_, Base::adjacencyIndex_);
987 template<
typename VISITOR>
988 inline typename CompleteGraph<VISITOR>::EdgeIterator::value_type
989 CompleteGraph<VISITOR>::EdgeIterator::operator[](
992 return Base::graph_->edgeFromVertex(Base::vertex_, Base::adjacencyIndex_ + j);
1000 #endif // #ifndef ANDRES_GRAPH_COMPLETE_GRAPH_HXX