MaximumSpanningTree.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. //===- llvm/Analysis/MaximumSpanningTree.h - Interface ----------*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This module provides means for calculating a maximum spanning tree for a
  11. // given set of weighted edges. The type parameter T is the type of a node.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_ANALYSIS_MAXIMUMSPANNINGTREE_H
  15. #define LLVM_ANALYSIS_MAXIMUMSPANNINGTREE_H
  16. #include "llvm/ADT/EquivalenceClasses.h"
  17. #include "llvm/IR/BasicBlock.h"
  18. #include <algorithm>
  19. #include <vector>
  20. namespace llvm {
  21. /// MaximumSpanningTree - A MST implementation.
  22. /// The type parameter T determines the type of the nodes of the graph.
  23. template <typename T>
  24. class MaximumSpanningTree {
  25. public:
  26. typedef std::pair<const T*, const T*> Edge;
  27. typedef std::pair<Edge, double> EdgeWeight;
  28. typedef std::vector<EdgeWeight> EdgeWeights;
  29. protected:
  30. typedef std::vector<Edge> MaxSpanTree;
  31. MaxSpanTree MST;
  32. private:
  33. // A comparing class for comparing weighted edges.
  34. struct EdgeWeightCompare {
  35. static bool getBlockSize(const T *X) {
  36. const BasicBlock *BB = dyn_cast_or_null<BasicBlock>(X);
  37. return BB ? BB->size() : 0;
  38. }
  39. bool operator()(EdgeWeight X, EdgeWeight Y) const {
  40. if (X.second > Y.second) return true;
  41. if (X.second < Y.second) return false;
  42. // Equal edge weights: break ties by comparing block sizes.
  43. size_t XSizeA = getBlockSize(X.first.first);
  44. size_t YSizeA = getBlockSize(Y.first.first);
  45. if (XSizeA > YSizeA) return true;
  46. if (XSizeA < YSizeA) return false;
  47. size_t XSizeB = getBlockSize(X.first.second);
  48. size_t YSizeB = getBlockSize(Y.first.second);
  49. if (XSizeB > YSizeB) return true;
  50. if (XSizeB < YSizeB) return false;
  51. return false;
  52. }
  53. };
  54. public:
  55. static char ID; // Class identification, replacement for typeinfo
  56. /// MaximumSpanningTree() - Takes a vector of weighted edges and returns a
  57. /// spanning tree.
  58. MaximumSpanningTree(EdgeWeights &EdgeVector) {
  59. std::stable_sort(EdgeVector.begin(), EdgeVector.end(), EdgeWeightCompare());
  60. // Create spanning tree, Forest contains a special data structure
  61. // that makes checking if two nodes are already in a common (sub-)tree
  62. // fast and cheap.
  63. EquivalenceClasses<const T*> Forest;
  64. for (typename EdgeWeights::iterator EWi = EdgeVector.begin(),
  65. EWe = EdgeVector.end(); EWi != EWe; ++EWi) {
  66. Edge e = (*EWi).first;
  67. Forest.insert(e.first);
  68. Forest.insert(e.second);
  69. }
  70. // Iterate over the sorted edges, biggest first.
  71. for (typename EdgeWeights::iterator EWi = EdgeVector.begin(),
  72. EWe = EdgeVector.end(); EWi != EWe; ++EWi) {
  73. Edge e = (*EWi).first;
  74. if (Forest.findLeader(e.first) != Forest.findLeader(e.second)) {
  75. Forest.unionSets(e.first, e.second);
  76. // So we know now that the edge is not already in a subtree, so we push
  77. // the edge to the MST.
  78. MST.push_back(e);
  79. }
  80. }
  81. }
  82. typename MaxSpanTree::iterator begin() {
  83. return MST.begin();
  84. }
  85. typename MaxSpanTree::iterator end() {
  86. return MST.end();
  87. }
  88. };
  89. } // End llvm namespace
  90. #endif