annotate include/llvm/XRay/Graph.h @ 147:c2174574ed3a

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