/3rd_party/llvm/lib/Support/DeltaAlgorithm.cpp

https://code.google.com/p/softart/ · C++ · 114 lines · 76 code · 18 blank · 20 comment · 18 complexity · 56af99590ed9b9b4d618490972f5f0d5 MD5 · raw file

  1. //===--- DeltaAlgorithm.cpp - A Set Minimization Algorithm -----*- 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. #include "llvm/ADT/DeltaAlgorithm.h"
  9. #include <algorithm>
  10. #include <iterator>
  11. using namespace llvm;
  12. DeltaAlgorithm::~DeltaAlgorithm() {
  13. }
  14. bool DeltaAlgorithm::GetTestResult(const changeset_ty &Changes) {
  15. if (FailedTestsCache.count(Changes))
  16. return false;
  17. bool Result = ExecuteOneTest(Changes);
  18. if (!Result)
  19. FailedTestsCache.insert(Changes);
  20. return Result;
  21. }
  22. void DeltaAlgorithm::Split(const changeset_ty &S, changesetlist_ty &Res) {
  23. // FIXME: Allow clients to provide heuristics for improved splitting.
  24. // FIXME: This is really slow.
  25. changeset_ty LHS, RHS;
  26. unsigned idx = 0, N = S.size() / 2;
  27. for (changeset_ty::const_iterator it = S.begin(),
  28. ie = S.end(); it != ie; ++it, ++idx)
  29. ((idx < N) ? LHS : RHS).insert(*it);
  30. if (!LHS.empty())
  31. Res.push_back(LHS);
  32. if (!RHS.empty())
  33. Res.push_back(RHS);
  34. }
  35. DeltaAlgorithm::changeset_ty
  36. DeltaAlgorithm::Delta(const changeset_ty &Changes,
  37. const changesetlist_ty &Sets) {
  38. // Invariant: union(Res) == Changes
  39. UpdatedSearchState(Changes, Sets);
  40. // If there is nothing left we can remove, we are done.
  41. if (Sets.size() <= 1)
  42. return Changes;
  43. // Look for a passing subset.
  44. changeset_ty Res;
  45. if (Search(Changes, Sets, Res))
  46. return Res;
  47. // Otherwise, partition the sets if possible; if not we are done.
  48. changesetlist_ty SplitSets;
  49. for (changesetlist_ty::const_iterator it = Sets.begin(),
  50. ie = Sets.end(); it != ie; ++it)
  51. Split(*it, SplitSets);
  52. if (SplitSets.size() == Sets.size())
  53. return Changes;
  54. return Delta(Changes, SplitSets);
  55. }
  56. bool DeltaAlgorithm::Search(const changeset_ty &Changes,
  57. const changesetlist_ty &Sets,
  58. changeset_ty &Res) {
  59. // FIXME: Parallelize.
  60. for (changesetlist_ty::const_iterator it = Sets.begin(),
  61. ie = Sets.end(); it != ie; ++it) {
  62. // If the test passes on this subset alone, recurse.
  63. if (GetTestResult(*it)) {
  64. changesetlist_ty Sets;
  65. Split(*it, Sets);
  66. Res = Delta(*it, Sets);
  67. return true;
  68. }
  69. // Otherwise, if we have more than two sets, see if test passes on the
  70. // complement.
  71. if (Sets.size() > 2) {
  72. // FIXME: This is really slow.
  73. changeset_ty Complement;
  74. std::set_difference(
  75. Changes.begin(), Changes.end(), it->begin(), it->end(),
  76. std::insert_iterator<changeset_ty>(Complement, Complement.begin()));
  77. if (GetTestResult(Complement)) {
  78. changesetlist_ty ComplementSets;
  79. ComplementSets.insert(ComplementSets.end(), Sets.begin(), it);
  80. ComplementSets.insert(ComplementSets.end(), it + 1, Sets.end());
  81. Res = Delta(Complement, ComplementSets);
  82. return true;
  83. }
  84. }
  85. }
  86. return false;
  87. }
  88. DeltaAlgorithm::changeset_ty DeltaAlgorithm::Run(const changeset_ty &Changes) {
  89. // Check empty set first to quickly find poor test functions.
  90. if (GetTestResult(changeset_ty()))
  91. return changeset_ty();
  92. // Otherwise run the real delta algorithm.
  93. changesetlist_ty Sets;
  94. Split(Changes, Sets);
  95. return Delta(Changes, Sets);
  96. }