15 #ifndef LLVM_CODEGEN_PBQP_GRAPH_H
16 #define LLVM_CODEGEN_PBQP_GRAPH_H
37 typedef std::set<NodeId> AdjEdgeList;
50 NodeEntry() : costs(0, 0) {}
52 NodeEntry(
const Vector &costs) : costs(costs), data(0) {}
53 Vector& getCosts() {
return costs; }
54 const Vector& getCosts()
const {
return costs; }
55 unsigned getDegree()
const {
return adjEdges.size(); }
59 return adjEdges.insert(adjEdges.end(), e);
64 void setData(
void *data) { this->data = data; }
65 void* getData() {
return data; }
74 EdgeEntry() : costs(0, 0, 0), data(0) {}
77 : node1(node1), node2(node2), costs(costs) {}
78 NodeId getNode1()
const {
return node1; }
79 NodeId getNode2()
const {
return node2; }
80 Matrix& getCosts() {
return costs; }
81 const Matrix& getCosts()
const {
return costs; }
82 void setNode1AEItr(
AdjEdgeItr ae) { node1AEItr = ae; }
83 AdjEdgeItr getNode1AEItr() {
return node1AEItr; }
84 void setNode2AEItr(
AdjEdgeItr ae) { node2AEItr = ae; }
85 AdjEdgeItr getNode2AEItr() {
return node2AEItr; }
86 void setData(
void *data) { this->data = data; }
87 void *getData() {
return data; }
92 typedef std::vector<NodeEntry> NodeVector;
93 typedef std::vector<NodeId> FreeNodeVector;
95 FreeNodeVector freeNodes;
97 typedef std::vector<EdgeEntry> EdgeVector;
98 typedef std::vector<EdgeId> FreeEdgeVector;
100 FreeEdgeVector freeEdges;
104 NodeEntry& getNode(
NodeId nId) {
return nodes[nId]; }
105 const NodeEntry& getNode(
NodeId nId)
const {
return nodes[nId]; }
107 EdgeEntry& getEdge(
EdgeId eId) {
return edges[eId]; }
108 const EdgeEntry& getEdge(
EdgeId eId)
const {
return edges[eId]; }
110 NodeId addConstructedNode(
const NodeEntry &n) {
112 if (!freeNodes.empty()) {
113 nodeId = freeNodes.back();
114 freeNodes.pop_back();
117 nodeId = nodes.size();
123 EdgeId addConstructedEdge(
const EdgeEntry &e) {
125 "Attempt to add duplicate edge.");
127 if (!freeEdges.empty()) {
128 edgeId = freeEdges.back();
129 freeEdges.pop_back();
132 edgeId = edges.size();
136 EdgeEntry &ne = getEdge(edgeId);
137 NodeEntry &n1 = getNode(ne.getNode1());
138 NodeEntry &n2 = getNode(ne.getNode2());
141 assert((n1.getCosts().getLength() == ne.getCosts().getRows()) &&
142 (n2.getCosts().getLength() == ne.getCosts().getCols()) &&
143 "Edge cost dimensions do not match node costs dimensions.");
145 ne.setNode1AEItr(n1.addEdge(edgeId));
146 ne.setNode2AEItr(n2.addEdge(edgeId));
151 void operator=(
const Graph &other) {}
158 : nodeId(nodeId), endNodeId(g.nodes.size()), freeNodes(g.freeNodes) {
159 this->nodeId = findNextInUse(nodeId);
169 while (n < endNodeId &&
170 std::find(freeNodes.begin(), freeNodes.end(), n) !=
178 const FreeNodeVector& freeNodes;
184 : edgeId(edgeId), endEdgeId(g.edges.size()), freeEdges(g.freeEdges) {
185 this->edgeId = findNextInUse(edgeId);
195 while (n < endEdgeId &&
196 std::find(freeEdges.begin(), freeEdges.end(), n) !=
204 const FreeEdgeVector& freeEdges;
214 return addConstructedNode(NodeEntry(costs));
224 "Matrix dimensions mismatch.");
225 return addConstructedEdge(EdgeEntry(n1Id, n2Id, costs));
230 unsigned getNumNodes()
const {
return nodes.size() - freeNodes.size(); }
234 unsigned getNumEdges()
const {
return edges.size() - freeEdges.size(); }
245 return getNode(nId).getCosts();
269 return getEdge(eId).getCosts();
288 return getNode(nId).getDegree();
307 return getNode(nId).edgesBegin();
314 return getNode(nId).edgesEnd();
321 return getEdge(eId).getNode1();
328 return getEdge(eId).getNode2();
336 EdgeEntry &e = getEdge(eId);
337 if (e.getNode1() == nId) {
344 return std::numeric_limits<EdgeId>::max();
354 aeItr != aeEnd; ++aeItr) {
366 NodeEntry &n = getNode(nId);
367 for (
AdjEdgeItr itr = n.edgesBegin(),
end = n.edgesEnd(); itr !=
end; ++itr) {
371 freeNodes.push_back(nId);
377 EdgeEntry &e = getEdge(eId);
378 NodeEntry &n1 = getNode(e.getNode1());
379 NodeEntry &n2 = getNode(e.getNode2());
380 n1.removeEdge(e.getNode1AEItr());
381 n2.removeEdge(e.getNode2AEItr());
382 freeEdges.push_back(eId);
394 template <
typename OStream>
399 nodeItr != nodeEnd; ++nodeItr) {
402 assert(v.
getLength() != 0 &&
"Empty vector in graph.");
404 for (
unsigned i = 1; i < v.getLength(); ++i) {
411 edgeItr != edgeEnd; ++edgeItr) {
414 assert(n1 != n2 &&
"PBQP graphs shound not have self-edges.");
416 os <<
"\n" << n1 <<
" " << n2 <<
"\n"
418 assert(m.
getRows() != 0 &&
"No rows in matrix.");
419 assert(m.
getCols() != 0 &&
"No cols in matrix.");
420 for (
unsigned i = 0; i < m.
getRows(); ++i) {
422 for (
unsigned j = 1; j < m.getCols(); ++j) {
423 os <<
" " << m[i][j];
432 template <
typename OStream>
438 nodeItr != nodeEnd; ++nodeItr) {
440 os <<
" node" << nodeItr <<
" [ label=\""
441 << nodeItr <<
": " <<
getNodeCosts(*nodeItr) <<
"\" ]\n";
447 edgeItr != edgeEnd; ++edgeItr) {
455 for (
unsigned i = 0; i < edgeCosts.
getRows(); ++i) {
478 #endif // LLVM_CODEGEN_PBQP_GRAPH_HPP
const_iterator end(StringRef path)
Get end iterator over path.
EdgeId invalidEdgeId() const
const Matrix & getEdgeCosts(EdgeId eId) const
Get an edge's cost matrix (const version).
Vector & getNodeCosts(NodeId nId)
Get a node's cost vector.
NodeItr nodesEnd() const
End iterator for node set.
NodeId getEdgeNode1(EdgeId eId)
Get the first node connected to this edge.
bool operator!=(const NodeItr &n) const
AdjEdgeList::iterator AdjEdgeItr
unsigned getLength() const
Return the length of the vector.
bool operator==(const EdgeItr &n) const
unsigned getNodeDegree(NodeId nId) const
Get a node's degree.
EdgeItr edgesBegin() const
Begin iterator for edge set.
NodeId addNode(const Vector &costs)
Add a node with the given costs.
AdjEdgeItr adjEdgesEnd(NodeId nId)
Get end iterator for adjacent edge set.
void removeEdge(EdgeId eId)
Remove an edge from the graph.
Graph()
Construct an empty PBQP graph.
unsigned getRows() const
Return the number of rows in this matrix.
void removeNode(NodeId nId)
Remove a node from the graph.
EdgeId findEdge(NodeId n1Id, NodeId n2Id)
Get the edge connecting two nodes.
NodeItr(NodeId nodeId, const Graph &g)
const Vector & getNodeCosts(NodeId nId) const
Get a node's cost vector (const version).
NodeId getEdgeNode2(EdgeId eId)
Get the second node connected to this edge.
void setNodeData(NodeId nId, void *data)
Set a node's data pointer.
void clear()
Remove all nodes and edges from the graph.
void printDot(OStream &os)
Print a representation of this graph in DOT format.
EdgeItr(EdgeId edgeId, const Graph &g)
unsigned getNumNodes() const
Get the number of nodes in the graph.
Vector getRowAsVector(unsigned r) const
Returns the given row as a vector.
NodeId getEdgeOtherNode(EdgeId eId, NodeId nId)
Get the "other" node connected to this edge.
AdjEdgeItr adjEdgesBegin(NodeId nId)
Get begin iterator for adjacent edge set.
Matrix & getEdgeCosts(EdgeId eId)
Get an edge's cost matrix.
EdgeItr edgesEnd() const
End iterator for edge set.
bool operator==(const NodeItr &n) const
void dump(OStream &os)
Dump a graph to an output stream.
bool operator!=(const EdgeItr &n) const
void * getEdgeData(EdgeId eId)
Get an edge's data pointer.
unsigned getNumEdges() const
Get the number of edges in the graph.
EdgeId addEdge(NodeId n1Id, NodeId n2Id, const Matrix &costs)
Add an edge between the given nodes with the given costs.
void setEdgeData(EdgeId eId, void *data)
Set an edge's data pointer.
NodeItr nodesBegin() const
Begin iterator for node set.
unsigned getCols() const
Return the number of cols in this matrix.
void * getNodeData(NodeId nId)
Get the node's data pointer.