llvm.org GIT mirror llvm / a597103 lib / CodeGen / PBQP / GraphBase.h
a597103

Tree @a597103 (Download .tar.gz)

GraphBase.h @a597103raw · history · blame

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
//===-- GraphBase.h - Abstract Base PBQP Graph -----------------*- C++ --*-===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// Base class for PBQP Graphs.
//
//===----------------------------------------------------------------------===//


#ifndef LLVM_CODEGEN_PBQP_GRAPHBASE_H
#define LLVM_CODEGEN_PBQP_GRAPHBASE_H

#include "PBQPMath.h"

#include <list>
#include <vector>

namespace PBQP {

// UGLY, but I'm not sure there's a good way around this: We need to be able to
// look up a Node's "adjacent edge list" structure type before the Node type is
// fully constructed.  We can enable this by pushing the choice of data type
// out into this traits class.
template <typename Graph>
class NodeBaseTraits {
  public:
    typedef std::list<typename Graph::EdgeIterator> AdjEdgeList;
    typedef typename AdjEdgeList::iterator AdjEdgeIterator;
    typedef typename AdjEdgeList::const_iterator ConstAdjEdgeIterator;
};

/// \brief Base for concrete graph classes. Provides a basic set of graph
///        operations which are useful for PBQP solvers.
template <typename NodeEntry, typename EdgeEntry>
class GraphBase {
private:

  typedef GraphBase<NodeEntry, EdgeEntry> ThisGraphT;

  typedef std::list<NodeEntry> NodeList;
  typedef std::list<EdgeEntry> EdgeList;

  NodeList nodeList;
  unsigned nodeListSize;

  EdgeList edgeList;
  unsigned edgeListSize;

  GraphBase(const ThisGraphT &other) { abort(); }
  void operator=(const ThisGraphT &other) { abort(); } 
  
public:

  /// \brief Iterates over the nodes of a graph.
  typedef typename NodeList::iterator NodeIterator;
  /// \brief Iterates over the nodes of a const graph.
  typedef typename NodeList::const_iterator ConstNodeIterator;
  /// \brief Iterates over the edges of a graph.
  typedef typename EdgeList::iterator EdgeIterator;
  /// \brief Iterates over the edges of a const graph.
  typedef typename EdgeList::const_iterator ConstEdgeIterator;

  /// \brief Iterates over the edges attached to a node.
  typedef typename NodeBaseTraits<ThisGraphT>::AdjEdgeIterator
    AdjEdgeIterator;

  /// \brief Iterates over the edges attached to a node in a const graph.
  typedef typename NodeBaseTraits<ThisGraphT>::ConstAdjEdgeIterator
    ConstAdjEdgeIterator;

private:

  typedef std::vector<NodeIterator> IDToNodeMap;

  IDToNodeMap idToNodeMap;
  bool nodeIDsValid;

  void invalidateNodeIDs() {
    if (nodeIDsValid) {
      idToNodeMap.clear();
      nodeIDsValid = false;
    }
  }

  template <typename ItrT>
  bool iteratorInRange(ItrT itr, const ItrT &begin, const ItrT &end) {
    for (ItrT t = begin; t != end; ++t) {
      if (itr == t)
        return true;
    }

    return false;
  }

protected:

  GraphBase() : nodeListSize(0), edgeListSize(0), nodeIDsValid(false) {}
  
  NodeEntry& getNodeEntry(const NodeIterator &nodeItr) { return *nodeItr; }
  const NodeEntry& getNodeEntry(const ConstNodeIterator &nodeItr) const {
    return *nodeItr;
  }

  EdgeEntry& getEdgeEntry(const EdgeIterator &edgeItr) { return *edgeItr; }
  const EdgeEntry& getEdgeEntry(const ConstEdgeIterator &edgeItr) const {
    return *edgeItr;
  }

  NodeIterator addConstructedNode(const NodeEntry &nodeEntry) {
    ++nodeListSize;

    invalidateNodeIDs();

    NodeIterator newNodeItr = nodeList.insert(nodeList.end(), nodeEntry);

    return newNodeItr;
  }

  EdgeIterator addConstructedEdge(const EdgeEntry &edgeEntry) {

    assert((findEdge(edgeEntry.getNode1Itr(), edgeEntry.getNode2Itr())
          == edgeList.end()) && "Attempt to add duplicate edge.");

    ++edgeListSize;

    // Add the edge to the graph.
    EdgeIterator edgeItr = edgeList.insert(edgeList.end(), edgeEntry);

    // Get a reference to the version in the graph.
    EdgeEntry &newEdgeEntry = getEdgeEntry(edgeItr);

    // Node entries:
    NodeEntry &node1Entry = getNodeEntry(newEdgeEntry.getNode1Itr()),
              &node2Entry = getNodeEntry(newEdgeEntry.getNode2Itr());

    // Sanity check on matrix dimensions.
    assert((node1Entry.getCosts().getLength() == 
            newEdgeEntry.getCosts().getRows()) && 
           (node2Entry.getCosts().getLength() == 
            newEdgeEntry.getCosts().getCols()) &&
        "Matrix dimensions do not match cost vector dimensions.");

    // Create links between nodes and edges.
    newEdgeEntry.setNode1ThisEdgeItr(
        node1Entry.addAdjEdge(edgeItr));
    newEdgeEntry.setNode2ThisEdgeItr(
        node2Entry.addAdjEdge(edgeItr));

    return edgeItr;
  }

public:

  /// \brief Returns the number of nodes in this graph.
  unsigned getNumNodes() const { return nodeListSize; }

  /// \brief Returns the number of edges in this graph.
  unsigned getNumEdges() const { return edgeListSize; } 

  /// \brief Return the cost vector for the given node.
  Vector& getNodeCosts(const NodeIterator &nodeItr) {
    return getNodeEntry(nodeItr).getCosts();
  }

  /// \brief Return the cost vector for the give node. 
  const Vector& getNodeCosts(const ConstNodeIterator &nodeItr) const {
    return getNodeEntry(nodeItr).getCosts();
  }

  /// \brief Return the degree of the given node.
  unsigned getNodeDegree(const NodeIterator &nodeItr) const {
    return getNodeEntry(nodeItr).getDegree();
  }

  /// \brief Assigns sequential IDs to the nodes, starting at 0, which
  /// remain valid until the next addition or removal of a node.
  void assignNodeIDs() {
    unsigned curID = 0;
    idToNodeMap.resize(getNumNodes());
    for (NodeIterator nodeItr = nodesBegin(), nodeEnd = nodesEnd();
         nodeItr != nodeEnd; ++nodeItr, ++curID) {
      getNodeEntry(nodeItr).setID(curID);
      idToNodeMap[curID] = nodeItr;
    }
    nodeIDsValid = true;
  }

  /// \brief Assigns sequential IDs to the nodes using the ordering of the
  /// given vector.
  void assignNodeIDs(const std::vector<NodeIterator> &nodeOrdering) {
    assert((getNumNodes() == nodeOrdering.size()) && 
           "Wrong number of nodes in node ordering.");
    idToNodeMap = nodeOrdering;
    for (unsigned nodeID = 0; nodeID < idToNodeMap.size(); ++nodeID) {
      getNodeEntry(idToNodeMap[nodeID]).setID(nodeID);
    }
    nodeIDsValid = true;
  }

  /// \brief Returns true if valid node IDs are assigned, false otherwise.
  bool areNodeIDsValid() const { return nodeIDsValid; }

  /// \brief Return the numeric ID of the given node.
  ///
  /// Calls to this method will result in an assertion failure if there have
  /// been any node additions or removals since the last call to
  /// assignNodeIDs().
  unsigned getNodeID(const ConstNodeIterator &nodeItr) const {
    assert(nodeIDsValid && "Attempt to retrieve invalid ID.");
    return getNodeEntry(nodeItr).getID();
  }

  /// \brief Returns the iterator associated with the given node ID.
  NodeIterator getNodeItr(unsigned nodeID) {
    assert(nodeIDsValid && "Attempt to retrieve iterator with invalid ID.");
    return idToNodeMap[nodeID];
  }

  /// \brief Returns the iterator associated with the given node ID.
  ConstNodeIterator getNodeItr(unsigned nodeID) const {
    assert(nodeIDsValid && "Attempt to retrieve iterator with invalid ID.");
    return idToNodeMap[nodeID];
  }

  /// \brief Removes the given node (and all attached edges) from the graph.
  void removeNode(const NodeIterator &nodeItr) {
    assert(iteratorInRange(nodeItr, nodeList.begin(), nodeList.end()) &&
           "Iterator does not belong to this graph!");

    invalidateNodeIDs();
    
    NodeEntry &nodeEntry = getNodeEntry(nodeItr);

    // We need to copy this out because it will be destroyed as the edges are
    // removed.
    typedef std::vector<EdgeIterator> AdjEdgeList;
    typedef typename AdjEdgeList::iterator AdjEdgeListItr;

    AdjEdgeList adjEdges;
    adjEdges.reserve(nodeEntry.getDegree());
    std::copy(nodeEntry.adjEdgesBegin(), nodeEntry.adjEdgesEnd(),
              std::back_inserter(adjEdges));

    // Iterate over the copied out edges and remove them from the graph.
    for (AdjEdgeListItr itr = adjEdges.begin(), end = adjEdges.end();
         itr != end; ++itr) {
      removeEdge(*itr);
    }

    // Erase the node from the nodelist.
    nodeList.erase(nodeItr);
    --nodeListSize;
  }

  NodeIterator nodesBegin() { return nodeList.begin(); }
  ConstNodeIterator nodesBegin() const { return nodeList.begin(); }
  NodeIterator nodesEnd() { return nodeList.end(); }
  ConstNodeIterator nodesEnd() const { return nodeList.end(); }

  AdjEdgeIterator adjEdgesBegin(const NodeIterator &nodeItr) {
    return getNodeEntry(nodeItr).adjEdgesBegin();
  }

  ConstAdjEdgeIterator adjEdgesBegin(const ConstNodeIterator &nodeItr) const {
    return getNodeEntry(nodeItr).adjEdgesBegin();
  }

  AdjEdgeIterator adjEdgesEnd(const NodeIterator &nodeItr) {
    return getNodeEntry(nodeItr).adjEdgesEnd();
  }
  
  ConstAdjEdgeIterator adjEdgesEnd(const ConstNodeIterator &nodeItr) const {
    getNodeEntry(nodeItr).adjEdgesEnd();
  }

  EdgeIterator findEdge(const NodeIterator &node1Itr,
                        const NodeIterator &node2Itr) {

    for (AdjEdgeIterator adjEdgeItr = adjEdgesBegin(node1Itr),
         adjEdgeEnd = adjEdgesEnd(node1Itr);
         adjEdgeItr != adjEdgeEnd; ++adjEdgeItr) {
      if ((getEdgeNode1Itr(*adjEdgeItr) == node2Itr) ||
          (getEdgeNode2Itr(*adjEdgeItr) == node2Itr)) {
        return *adjEdgeItr;
      }
    }

    return edgeList.end();
  }

  ConstEdgeIterator findEdge(const ConstNodeIterator &node1Itr,
                             const ConstNodeIterator &node2Itr) const {

    for (ConstAdjEdgeIterator adjEdgeItr = adjEdgesBegin(node1Itr),
         adjEdgeEnd = adjEdgesEnd(node1Itr);
         adjEdgeItr != adjEdgesEnd; ++adjEdgeItr) {
      if ((getEdgeNode1Itr(*adjEdgeItr) == node2Itr) ||
          (getEdgeNode2Itr(*adjEdgeItr) == node2Itr)) {
        return *adjEdgeItr;
      }
    }

    return edgeList.end();
  }

  Matrix& getEdgeCosts(const EdgeIterator &edgeItr) {
    return getEdgeEntry(edgeItr).getCosts();
  }

  const Matrix& getEdgeCosts(const ConstEdgeIterator &edgeItr) const {
    return getEdgeEntry(edgeItr).getCosts();
  }

  NodeIterator getEdgeNode1Itr(const EdgeIterator &edgeItr) {
    return getEdgeEntry(edgeItr).getNode1Itr();
  }

  ConstNodeIterator getEdgeNode1Itr(const ConstEdgeIterator &edgeItr) const {
    return getEdgeEntry(edgeItr).getNode1Itr();
  }

  NodeIterator getEdgeNode2Itr(const EdgeIterator &edgeItr) {
    return getEdgeEntry(edgeItr).getNode2Itr();
  }

  ConstNodeIterator getEdgeNode2Itr(const ConstEdgeIterator &edgeItr) const {
    return getEdgeEntry(edgeItr).getNode2Itr();
  }

  NodeIterator getEdgeOtherNode(const EdgeIterator &edgeItr,
                                const NodeIterator &nodeItr) {

    EdgeEntry &edgeEntry = getEdgeEntry(edgeItr);
    if (nodeItr == edgeEntry.getNode1Itr()) {
      return edgeEntry.getNode2Itr();
    }
    //else
    return edgeEntry.getNode1Itr();
  }

  ConstNodeIterator getEdgeOtherNode(const ConstEdgeIterator &edgeItr,
                                     const ConstNodeIterator &nodeItr) const {

    const EdgeEntry &edgeEntry = getEdgeEntry(edgeItr);
    if (nodeItr == edgeEntry.getNode1Itr()) {
      return edgeEntry.getNode2Itr();
    }
    //else
    return edgeEntry.getNode1Itr();
  }

  void removeEdge(const EdgeIterator &edgeItr) {
    assert(iteratorInRange(edgeItr, edgeList.begin(), edgeList.end()) &&
           "Iterator does not belong to this graph!");

    --edgeListSize;

    // Get the edge entry.
    EdgeEntry &edgeEntry = getEdgeEntry(edgeItr);

    // Get the nodes entry.
    NodeEntry &node1Entry(getNodeEntry(edgeEntry.getNode1Itr())),
              &node2Entry(getNodeEntry(edgeEntry.getNode2Itr()));

    // Disconnect the edge from the nodes.
    node1Entry.removeAdjEdge(edgeEntry.getNode1ThisEdgeItr());
    node2Entry.removeAdjEdge(edgeEntry.getNode2ThisEdgeItr());

    // Remove the edge from the graph.
    edgeList.erase(edgeItr);
  }

  EdgeIterator edgesBegin() { return edgeList.begin(); }
  ConstEdgeIterator edgesBegin() const { return edgeList.begin(); }
  EdgeIterator edgesEnd() { return edgeList.end(); }
  ConstEdgeIterator edgesEnd() const { return edgeList.end(); }

  void clear() {
    nodeList.clear();
    nodeListSize = 0;
    edgeList.clear();
    edgeListSize = 0;
    idToNodeMap.clear();
  }

  template <typename OStream>
  void printDot(OStream &os) const {
    
    assert(areNodeIDsValid() &&
           "Cannot print a .dot of a graph unless IDs have been assigned.");
    
    os << "graph {\n";

    for (ConstNodeIterator nodeItr = nodesBegin(), nodeEnd = nodesEnd();
         nodeItr != nodeEnd; ++nodeItr) {

      os << "  node" << getNodeID(nodeItr) << " [ label=\""
         << getNodeID(nodeItr) << ": " << getNodeCosts(nodeItr) << "\" ]\n";
    }

    os << "  edge [ len=" << getNumNodes() << " ]\n";

    for (ConstEdgeIterator edgeItr = edgesBegin(), edgeEnd = edgesEnd();
         edgeItr != edgeEnd; ++edgeItr) {

      os << "  node" << getNodeID(getEdgeNode1Itr(edgeItr))
         << " -- node" << getNodeID(getEdgeNode2Itr(edgeItr))
         << " [ label=\"";

      const Matrix &edgeCosts = getEdgeCosts(edgeItr);

      for (unsigned i = 0; i < edgeCosts.getRows(); ++i) {
        os << edgeCosts.getRowAsVector(i) << "\\n";
      }

      os << "\" ]\n";
    }

    os << "}\n";
  }

  template <typename OStream>
  void printDot(OStream &os) {
    if (!areNodeIDsValid()) {
      assignNodeIDs();
    }

    const_cast<const ThisGraphT*>(this)->printDot(os);
  }

  template <typename OStream>
  void dumpTo(OStream &os) const {
    typedef ConstNodeIterator ConstNodeID;
    
    assert(areNodeIDsValid() &&
           "Cannot dump a graph unless IDs have been assigned.");

    for (ConstNodeIterator nItr = nodesBegin(), nEnd = nodesEnd();
         nItr != nEnd; ++nItr) {
      os << getNodeID(nItr) << "\n";
    }

    unsigned edgeNumber = 1;
    for (ConstEdgeIterator eItr = edgesBegin(), eEnd = edgesEnd();
         eItr != eEnd; ++eItr) {

      os << edgeNumber++ << ": { "
         << getNodeID(getEdgeNode1Itr(eItr)) << ", "
         << getNodeID(getEdgeNode2Itr(eItr)) << " }\n";
    }

  }

  template <typename OStream>
  void dumpTo(OStream &os) {
    if (!areNodeIDsValid()) {
      assignNodeIDs();
    }

    const_cast<const ThisGraphT*>(this)->dumpTo(os);
  }

};

/// \brief Provides a base from which to derive nodes for GraphBase.
template <typename NodeImpl, typename EdgeImpl>
class NodeBase {
private:

  typedef GraphBase<NodeImpl, EdgeImpl> GraphBaseT;
  typedef NodeBaseTraits<GraphBaseT> ThisNodeBaseTraits;

public:
  typedef typename GraphBaseT::EdgeIterator EdgeIterator;

private:
  typedef typename ThisNodeBaseTraits::AdjEdgeList AdjEdgeList;

  unsigned degree, id;
  Vector costs;
  AdjEdgeList adjEdges;

  void operator=(const NodeBase& other) {
    assert(false && "Can't assign NodeEntrys.");
  }

public:

  typedef typename ThisNodeBaseTraits::AdjEdgeIterator AdjEdgeIterator;
  typedef typename ThisNodeBaseTraits::ConstAdjEdgeIterator
    ConstAdjEdgeIterator;

  NodeBase(const Vector &costs) : degree(0), costs(costs) {
    assert((costs.getLength() > 0) && "Can't have zero-length cost vector.");
  }

  Vector& getCosts() { return costs; }
  const Vector& getCosts() const { return costs; }

  unsigned getDegree() const { return degree;  }

  void setID(unsigned id) { this->id = id; }
  unsigned getID() const { return id; }

  AdjEdgeIterator addAdjEdge(const EdgeIterator &edgeItr) {
    ++degree;
    return adjEdges.insert(adjEdges.end(), edgeItr);
  }

  void removeAdjEdge(const AdjEdgeIterator &adjEdgeItr) {
    --degree;
    adjEdges.erase(adjEdgeItr);
  }

  AdjEdgeIterator adjEdgesBegin() { return adjEdges.begin(); } 
  ConstAdjEdgeIterator adjEdgesBegin() const { return adjEdges.begin(); }
  AdjEdgeIterator adjEdgesEnd() { return adjEdges.end(); }
  ConstAdjEdgeIterator adjEdgesEnd() const { return adjEdges.end(); }

};

template <typename NodeImpl, typename EdgeImpl>
class EdgeBase {
public:
  typedef typename GraphBase<NodeImpl, EdgeImpl>::NodeIterator NodeIterator;
  typedef typename GraphBase<NodeImpl, EdgeImpl>::EdgeIterator EdgeIterator;

  typedef typename NodeImpl::AdjEdgeIterator NodeAdjEdgeIterator;

private:

  NodeIterator node1Itr, node2Itr;
  NodeAdjEdgeIterator node1ThisEdgeItr, node2ThisEdgeItr;
  Matrix costs;

  void operator=(const EdgeBase &other) {
    assert(false && "Can't assign EdgeEntrys.");
  }

public:

  EdgeBase(const NodeIterator &node1Itr, const NodeIterator &node2Itr,
           const Matrix &costs) :
    node1Itr(node1Itr), node2Itr(node2Itr), costs(costs) {

    assert((costs.getRows() > 0) && (costs.getCols() > 0) &&
           "Can't have zero-dimensioned cost matrices");
  }

  Matrix& getCosts() { return costs; }
  const Matrix& getCosts() const { return costs; }

  const NodeIterator& getNode1Itr() const { return node1Itr; }
  const NodeIterator& getNode2Itr() const { return node2Itr; }

  void setNode1ThisEdgeItr(const NodeAdjEdgeIterator &node1ThisEdgeItr) {
    this->node1ThisEdgeItr = node1ThisEdgeItr;
  }

  const NodeAdjEdgeIterator& getNode1ThisEdgeItr() const {
    return node1ThisEdgeItr;
  }

  void setNode2ThisEdgeItr(const NodeAdjEdgeIterator &node2ThisEdgeItr) {
    this->node2ThisEdgeItr = node2ThisEdgeItr;
  }

  const NodeAdjEdgeIterator& getNode2ThisEdgeItr() const {
    return node2ThisEdgeItr;
  }

};


}

#endif // LLVM_CODEGEN_PBQP_GRAPHBASE_HPP