annotate include/llvm/XRay/Graph.h @ 121:803732b1fca8

LLVM 5.0
author kono
date Fri, 27 Oct 2017 17:07:41 +0900
parents
children c2174574ed3a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
121
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
1 //===-- Graph.h - XRay Graph Class ------------------------------*- C++ -*-===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
2 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
3 // The LLVM Compiler Infrastructure
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
4 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
5 // This file is distributed under the University of Illinois Open Source
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
6 // License. See LICENSE.TXT for details.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
7 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
8 //===----------------------------------------------------------------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
9 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
10 // A Graph Datatype for XRay.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
11 //
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
12 //===----------------------------------------------------------------------===//
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
13
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
14 #ifndef LLVM_XRAY_GRAPH_T_H
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
15 #define LLVM_XRAY_GRAPH_T_H
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
16
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
17 #include <initializer_list>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
18 #include <stdint.h>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
19 #include <type_traits>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
20 #include <utility>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
21
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
22 #include "llvm/ADT/DenseMap.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
23 #include "llvm/ADT/DenseSet.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
24 #include "llvm/ADT/iterator.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
25 #include "llvm/Support/Error.h"
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
26
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
27 namespace llvm {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
28 namespace xray {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
29
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
30 /// A Graph object represents a Directed Graph and is used in XRay to compute
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
31 /// and store function call graphs and associated statistical information.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
32 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
33 /// The graph takes in four template parameters, these are:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
34 /// - VertexAttribute, this is a structure which is stored for each vertex.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
35 /// Must be DefaultConstructible, CopyConstructible, CopyAssignable and
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
36 /// Destructible.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
37 /// - EdgeAttribute, this is a structure which is stored for each edge
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
38 /// Must be DefaultConstructible, CopyConstructible, CopyAssignable and
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
39 /// Destructible.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
40 /// - EdgeAttribute, this is a structure which is stored for each variable
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
41 /// - VI, this is a type over which DenseMapInfo is defined and is the type
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
42 /// used look up strings, available as VertexIdentifier.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
43 /// - If the built in DenseMapInfo is not defined, provide a specialization
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
44 /// class type here.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
45 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
46 /// Graph is CopyConstructible, CopyAssignable, MoveConstructible and
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
47 /// MoveAssignable but is not EqualityComparible or LessThanComparible.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
48 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
49 /// Usage Example Graph with weighted edges and vertices:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
50 /// Graph<int, int, int> G;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
51 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
52 /// G[1] = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
53 /// G[2] = 2;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
54 /// G[{1,2}] = 1;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
55 /// G[{2,1}] = -1;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
56 /// for(const auto &v : G.vertices()){
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
57 /// // Do something with the vertices in the graph;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
58 /// }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
59 /// for(const auto &e : G.edges()){
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
60 /// // Do something with the edges in the graph;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
61 /// }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
62 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
63 /// Usage Example with StrRef keys.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
64 /// Graph<int, double, StrRef> StrG;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
65 /// char va[] = "Vertex A";
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
66 /// char vaa[] = "Vertex A";
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
67 /// char vb[] = "Vertex B"; // Vertices are referenced by String Refs.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
68 /// G[va] = 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
69 /// G[vb] = 1;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
70 /// G[{va, vb}] = 1.0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
71 /// cout() << G[vaa] << " " << G[{vaa, vb}]; //prints "0 1.0".
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
72 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
73 template <typename VertexAttribute, typename EdgeAttribute,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
74 typename VI = int32_t>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
75 class Graph {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
76 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
77 /// These objects are used to name edges and vertices in the graph.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
78 typedef VI VertexIdentifier;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
79 typedef std::pair<VI, VI> EdgeIdentifier;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
80
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
81 /// This type is the value_type of all iterators which range over vertices,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
82 /// Determined by the Vertices DenseMap
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
83 using VertexValueType =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
84 detail::DenseMapPair<VertexIdentifier, VertexAttribute>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
85
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
86 /// This type is the value_type of all iterators which range over edges,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
87 /// Determined by the Edges DenseMap.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
88 using EdgeValueType = detail::DenseMapPair<EdgeIdentifier, EdgeAttribute>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
89
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
90 using size_type = std::size_t;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
91
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
92 private:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
93 /// The type used for storing the EdgeAttribute for each edge in the graph
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
94 using EdgeMapT = DenseMap<EdgeIdentifier, EdgeAttribute>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
95
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
96 /// The type used for storing the VertexAttribute for each vertex in
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
97 /// the graph.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
98 using VertexMapT = DenseMap<VertexIdentifier, VertexAttribute>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
99
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
100 /// The type used for storing the edges entering a vertex. Indexed by
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
101 /// the VertexIdentifier of the start of the edge. Only used to determine
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
102 /// where the incoming edges are, the EdgeIdentifiers are stored in an
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
103 /// InnerEdgeMapT.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
104 using NeighborSetT = DenseSet<VertexIdentifier>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
105
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
106 /// The type storing the InnerInvGraphT corresponding to each vertex in
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
107 /// the graph (When a vertex has an incoming edge incident to it)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
108 using NeighborLookupT = DenseMap<VertexIdentifier, NeighborSetT>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
109
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
110 private:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
111 /// Stores the map from the start and end vertex of an edge to it's
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
112 /// EdgeAttribute
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
113 EdgeMapT Edges;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
114
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
115 /// Stores the map from VertexIdentifier to VertexAttribute
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
116 VertexMapT Vertices;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
117
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
118 /// Allows fast lookup for the incoming edge set of any given vertex.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
119 NeighborLookupT InNeighbors;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
120
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
121 /// Allows fast lookup for the outgoing edge set of any given vertex.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
122 NeighborLookupT OutNeighbors;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
123
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
124 /// An Iterator adapter using an InnerInvGraphT::iterator as a base iterator,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
125 /// and storing the VertexIdentifier the iterator range comes from. The
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
126 /// dereference operator is then performed using a pointer to the graph's edge
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
127 /// set.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
128 template <bool IsConst, bool IsOut,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
129 typename BaseIt = typename NeighborSetT::const_iterator,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
130 typename T = typename std::conditional<IsConst, const EdgeValueType,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
131 EdgeValueType>::type>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
132 class NeighborEdgeIteratorT
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
133 : public iterator_adaptor_base<
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
134 NeighborEdgeIteratorT<IsConst, IsOut>, BaseIt,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
135 typename std::iterator_traits<BaseIt>::iterator_category, T> {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
136 using InternalEdgeMapT =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
137 typename std::conditional<IsConst, const EdgeMapT, EdgeMapT>::type;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
138
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
139 friend class NeighborEdgeIteratorT<false, IsOut, BaseIt, EdgeValueType>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
140 friend class NeighborEdgeIteratorT<true, IsOut, BaseIt,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
141 const EdgeValueType>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
142
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
143 InternalEdgeMapT *MP;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
144 VertexIdentifier SI;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
145
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
146 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
147 template <bool IsConstDest,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
148 typename = typename std::enable_if<IsConstDest && !IsConst>::type>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
149 operator NeighborEdgeIteratorT<IsConstDest, IsOut, BaseIt,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
150 const EdgeValueType>() const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
151 return NeighborEdgeIteratorT<IsConstDest, IsOut, BaseIt,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
152 const EdgeValueType>(this->I, MP, SI);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
153 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
154
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
155 NeighborEdgeIteratorT() = default;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
156 NeighborEdgeIteratorT(BaseIt _I, InternalEdgeMapT *_MP,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
157 VertexIdentifier _SI)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
158 : iterator_adaptor_base<
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
159 NeighborEdgeIteratorT<IsConst, IsOut>, BaseIt,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
160 typename std::iterator_traits<BaseIt>::iterator_category, T>(_I),
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
161 MP(_MP), SI(_SI) {}
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
162
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
163 T &operator*() const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
164 if (!IsOut)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
165 return *(MP->find({*(this->I), SI}));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
166 else
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
167 return *(MP->find({SI, *(this->I)}));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
168 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
169 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
170
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
171 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
172 /// A const iterator type for iterating through the set of edges entering a
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
173 /// vertex.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
174 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
175 /// Has a const EdgeValueType as its value_type
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
176 using ConstInEdgeIterator = NeighborEdgeIteratorT<true, false>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
177
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
178 /// An iterator type for iterating through the set of edges leaving a vertex.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
179 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
180 /// Has an EdgeValueType as its value_type
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
181 using InEdgeIterator = NeighborEdgeIteratorT<false, false>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
182
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
183 /// A const iterator type for iterating through the set of edges entering a
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
184 /// vertex.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
185 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
186 /// Has a const EdgeValueType as its value_type
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
187 using ConstOutEdgeIterator = NeighborEdgeIteratorT<true, true>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
188
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
189 /// An iterator type for iterating through the set of edges leaving a vertex.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
190 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
191 /// Has an EdgeValueType as its value_type
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
192 using OutEdgeIterator = NeighborEdgeIteratorT<false, true>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
193
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
194 /// A class for ranging over the incoming edges incident to a vertex.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
195 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
196 /// Like all views in this class it provides methods to get the beginning and
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
197 /// past the range iterators for the range, as well as methods to determine
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
198 /// the number of elements in the range and whether the range is empty.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
199 template <bool isConst, bool isOut> class InOutEdgeView {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
200 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
201 using iterator = NeighborEdgeIteratorT<isConst, isOut>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
202 using const_iterator = NeighborEdgeIteratorT<true, isOut>;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
203 using GraphT = typename std::conditional<isConst, const Graph, Graph>::type;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
204 using InternalEdgeMapT =
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
205 typename std::conditional<isConst, const EdgeMapT, EdgeMapT>::type;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
206
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
207 private:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
208 InternalEdgeMapT &M;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
209 const VertexIdentifier A;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
210 const NeighborLookupT &NL;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
211
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
212 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
213 iterator begin() {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
214 auto It = NL.find(A);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
215 if (It == NL.end())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
216 return iterator();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
217 return iterator(It->second.begin(), &M, A);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
218 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
219
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
220 const_iterator cbegin() const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
221 auto It = NL.find(A);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
222 if (It == NL.end())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
223 return const_iterator();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
224 return const_iterator(It->second.begin(), &M, A);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
225 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
226
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
227 const_iterator begin() const { return cbegin(); }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
228
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
229 iterator end() {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
230 auto It = NL.find(A);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
231 if (It == NL.end())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
232 return iterator();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
233 return iterator(It->second.end(), &M, A);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
234 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
235 const_iterator cend() const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
236 auto It = NL.find(A);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
237 if (It == NL.end())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
238 return const_iterator();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
239 return const_iterator(It->second.end(), &M, A);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
240 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
241
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
242 const_iterator end() const { return cend(); }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
243
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
244 size_type size() const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
245 auto I = NL.find(A);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
246 if (I == NL.end())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
247 return 0;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
248 else
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
249 return I->second.size();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
250 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
251
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
252 bool empty() const { return NL.count(A) == 0; };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
253
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
254 InOutEdgeView(GraphT &G, VertexIdentifier A)
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
255 : M(G.Edges), A(A), NL(isOut ? G.OutNeighbors : G.InNeighbors) {}
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
256 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
257
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
258 /// A const iterator type for iterating through the whole vertex set of the
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
259 /// graph.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
260 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
261 /// Has a const VertexValueType as its value_type
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
262 using ConstVertexIterator = typename VertexMapT::const_iterator;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
263
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
264 /// An iterator type for iterating through the whole vertex set of the graph.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
265 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
266 /// Has a VertexValueType as its value_type
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
267 using VertexIterator = typename VertexMapT::iterator;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
268
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
269 /// A class for ranging over the vertices in the graph.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
270 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
271 /// Like all views in this class it provides methods to get the beginning and
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
272 /// past the range iterators for the range, as well as methods to determine
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
273 /// the number of elements in the range and whether the range is empty.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
274 template <bool isConst> class VertexView {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
275 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
276 using iterator = typename std::conditional<isConst, ConstVertexIterator,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
277 VertexIterator>::type;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
278 using const_iterator = ConstVertexIterator;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
279 using GraphT = typename std::conditional<isConst, const Graph, Graph>::type;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
280
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
281 private:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
282 GraphT &G;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
283
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
284 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
285 iterator begin() { return G.Vertices.begin(); }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
286 iterator end() { return G.Vertices.end(); }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
287 const_iterator cbegin() const { return G.Vertices.cbegin(); }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
288 const_iterator cend() const { return G.Vertices.cend(); }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
289 const_iterator begin() const { return G.Vertices.begin(); }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
290 const_iterator end() const { return G.Vertices.end(); }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
291 size_type size() const { return G.Vertices.size(); }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
292 bool empty() const { return G.Vertices.empty(); }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
293 VertexView(GraphT &_G) : G(_G) {}
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
294 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
295
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
296 /// A const iterator for iterating through the entire edge set of the graph.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
297 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
298 /// Has a const EdgeValueType as its value_type
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
299 using ConstEdgeIterator = typename EdgeMapT::const_iterator;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
300
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
301 /// An iterator for iterating through the entire edge set of the graph.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
302 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
303 /// Has an EdgeValueType as its value_type
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
304 using EdgeIterator = typename EdgeMapT::iterator;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
305
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
306 /// A class for ranging over all the edges in the graph.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
307 ///
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
308 /// Like all views in this class it provides methods to get the beginning and
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
309 /// past the range iterators for the range, as well as methods to determine
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
310 /// the number of elements in the range and whether the range is empty.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
311 template <bool isConst> class EdgeView {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
312 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
313 using iterator = typename std::conditional<isConst, ConstEdgeIterator,
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
314 EdgeIterator>::type;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
315 using const_iterator = ConstEdgeIterator;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
316 using GraphT = typename std::conditional<isConst, const Graph, Graph>::type;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
317
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
318 private:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
319 GraphT &G;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
320
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
321 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
322 iterator begin() { return G.Edges.begin(); }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
323 iterator end() { return G.Edges.end(); }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
324 const_iterator cbegin() const { return G.Edges.cbegin(); }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
325 const_iterator cend() const { return G.Edges.cend(); }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
326 const_iterator begin() const { return G.Edges.begin(); }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
327 const_iterator end() const { return G.Edges.end(); }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
328 size_type size() const { return G.Edges.size(); }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
329 bool empty() const { return G.Edges.empty(); }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
330 EdgeView(GraphT &_G) : G(_G) {}
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
331 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
332
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
333 public:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
334 // TODO: implement constructor to enable Graph Initialisation.\
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
335 // Something like:
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
336 // Graph<int, int, int> G(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
337 // {1, 2, 3, 4, 5},
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
338 // {{1, 2}, {2, 3}, {3, 4}});
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
339
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
340 /// Empty the Graph
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
341 void clear() {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
342 Edges.clear();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
343 Vertices.clear();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
344 InNeighbors.clear();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
345 OutNeighbors.clear();
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
346 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
347
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
348 /// Returns a view object allowing iteration over the vertices of the graph.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
349 /// also allows access to the size of the vertex set.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
350 VertexView<false> vertices() { return VertexView<false>(*this); }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
351
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
352 VertexView<true> vertices() const { return VertexView<true>(*this); }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
353
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
354 /// Returns a view object allowing iteration over the edges of the graph.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
355 /// also allows access to the size of the edge set.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
356 EdgeView<false> edges() { return EdgeView<false>(*this); }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
357
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
358 EdgeView<true> edges() const { return EdgeView<true>(*this); }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
359
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
360 /// Returns a view object allowing iteration over the edges which start at
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
361 /// a vertex I.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
362 InOutEdgeView<false, true> outEdges(const VertexIdentifier I) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
363 return InOutEdgeView<false, true>(*this, I);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
364 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
365
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
366 InOutEdgeView<true, true> outEdges(const VertexIdentifier I) const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
367 return InOutEdgeView<true, true>(*this, I);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
368 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
369
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
370 /// Returns a view object allowing iteration over the edges which point to
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
371 /// a vertex I.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
372 InOutEdgeView<false, false> inEdges(const VertexIdentifier I) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
373 return InOutEdgeView<false, false>(*this, I);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
374 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
375
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
376 InOutEdgeView<true, false> inEdges(const VertexIdentifier I) const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
377 return InOutEdgeView<true, false>(*this, I);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
378 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
379
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
380 /// Looks up the vertex with identifier I, if it does not exist it default
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
381 /// constructs it.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
382 VertexAttribute &operator[](const VertexIdentifier &I) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
383 return Vertices.FindAndConstruct(I).second;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
384 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
385
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
386 /// Looks up the edge with identifier I, if it does not exist it default
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
387 /// constructs it, if it's endpoints do not exist it also default constructs
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
388 /// them.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
389 EdgeAttribute &operator[](const EdgeIdentifier &I) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
390 auto &P = Edges.FindAndConstruct(I);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
391 Vertices.FindAndConstruct(I.first);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
392 Vertices.FindAndConstruct(I.second);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
393 InNeighbors[I.second].insert(I.first);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
394 OutNeighbors[I.first].insert(I.second);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
395 return P.second;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
396 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
397
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
398 /// Looks up a vertex with Identifier I, or an error if it does not exist.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
399 Expected<VertexAttribute &> at(const VertexIdentifier &I) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
400 auto It = Vertices.find(I);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
401 if (It == Vertices.end())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
402 return make_error<StringError>(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
403 "Vertex Identifier Does Not Exist",
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
404 std::make_error_code(std::errc::invalid_argument));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
405 return It->second;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
406 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
407
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
408 Expected<const VertexAttribute &> at(const VertexIdentifier &I) const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
409 auto It = Vertices.find(I);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
410 if (It == Vertices.end())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
411 return make_error<StringError>(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
412 "Vertex Identifier Does Not Exist",
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
413 std::make_error_code(std::errc::invalid_argument));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
414 return It->second;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
415 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
416
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
417 /// Looks up an edge with Identifier I, or an error if it does not exist.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
418 Expected<EdgeAttribute &> at(const EdgeIdentifier &I) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
419 auto It = Edges.find(I);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
420 if (It == Edges.end())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
421 return make_error<StringError>(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
422 "Edge Identifier Does Not Exist",
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
423 std::make_error_code(std::errc::invalid_argument));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
424 return It->second;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
425 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
426
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
427 Expected<const EdgeAttribute &> at(const EdgeIdentifier &I) const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
428 auto It = Edges.find(I);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
429 if (It == Edges.end())
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
430 return make_error<StringError>(
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
431 "Edge Identifier Does Not Exist",
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
432 std::make_error_code(std::errc::invalid_argument));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
433 return It->second;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
434 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
435
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
436 /// Looks for a vertex with identifier I, returns 1 if one exists, and
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
437 /// 0 otherwise
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
438 size_type count(const VertexIdentifier &I) const {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
439 return Vertices.count(I);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
440 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
441
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
442 /// Looks for an edge with Identifier I, returns 1 if one exists and 0
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
443 /// otherwise
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
444 size_type count(const EdgeIdentifier &I) const { return Edges.count(I); }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
445
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
446 /// Inserts a vertex into the graph with Identifier Val.first, and
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
447 /// Attribute Val.second.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
448 std::pair<VertexIterator, bool>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
449 insert(const std::pair<VertexIdentifier, VertexAttribute> &Val) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
450 return Vertices.insert(Val);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
451 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
452
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
453 std::pair<VertexIterator, bool>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
454 insert(std::pair<VertexIdentifier, VertexAttribute> &&Val) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
455 return Vertices.insert(std::move(Val));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
456 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
457
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
458 /// Inserts an edge into the graph with Identifier Val.first, and
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
459 /// Attribute Val.second. If the key is already in the map, it returns false
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
460 /// and doesn't update the value.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
461 std::pair<EdgeIterator, bool>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
462 insert(const std::pair<EdgeIdentifier, EdgeAttribute> &Val) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
463 const auto &p = Edges.insert(Val);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
464 if (p.second) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
465 const auto &EI = Val.first;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
466 Vertices.FindAndConstruct(EI.first);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
467 Vertices.FindAndConstruct(EI.second);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
468 InNeighbors[EI.second].insert(EI.first);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
469 OutNeighbors[EI.first].insert(EI.second);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
470 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
471
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
472 return p;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
473 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
474
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
475 /// Inserts an edge into the graph with Identifier Val.first, and
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
476 /// Attribute Val.second. If the key is already in the map, it returns false
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
477 /// and doesn't update the value.
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
478 std::pair<EdgeIterator, bool>
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
479 insert(std::pair<EdgeIdentifier, EdgeAttribute> &&Val) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
480 auto EI = Val.first;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
481 const auto &p = Edges.insert(std::move(Val));
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
482 if (p.second) {
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
483 Vertices.FindAndConstruct(EI.first);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
484 Vertices.FindAndConstruct(EI.second);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
485 InNeighbors[EI.second].insert(EI.first);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
486 OutNeighbors[EI.first].insert(EI.second);
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
487 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
488
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
489 return p;
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
490 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
491 };
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
492 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
493 }
803732b1fca8 LLVM 5.0
kono
parents:
diff changeset
494 #endif