DeltaAlgorithm.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. //===--- DeltaAlgorithm.cpp - A Set Minimization Algorithm -----*- C++ -*--===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //===----------------------------------------------------------------------===//
  7. #include "llvm/ADT/DeltaAlgorithm.h"
  8. #include <algorithm>
  9. #include <iterator>
  10. #include <set>
  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 (const changeset_ty &Set : Sets)
  50. Split(Set, SplitSets);
  51. if (SplitSets.size() == Sets.size())
  52. return Changes;
  53. return Delta(Changes, SplitSets);
  54. }
  55. bool DeltaAlgorithm::Search(const changeset_ty &Changes,
  56. const changesetlist_ty &Sets,
  57. changeset_ty &Res) {
  58. // FIXME: Parallelize.
  59. for (changesetlist_ty::const_iterator it = Sets.begin(),
  60. ie = Sets.end(); it != ie; ++it) {
  61. // If the test passes on this subset alone, recurse.
  62. if (GetTestResult(*it)) {
  63. changesetlist_ty Sets;
  64. Split(*it, Sets);
  65. Res = Delta(*it, Sets);
  66. return true;
  67. }
  68. // Otherwise, if we have more than two sets, see if test passes on the
  69. // complement.
  70. if (Sets.size() > 2) {
  71. // FIXME: This is really slow.
  72. changeset_ty Complement;
  73. std::set_difference(
  74. Changes.begin(), Changes.end(), it->begin(), it->end(),
  75. std::insert_iterator<changeset_ty>(Complement, Complement.begin()));
  76. if (GetTestResult(Complement)) {
  77. changesetlist_ty ComplementSets;
  78. ComplementSets.insert(ComplementSets.end(), Sets.begin(), it);
  79. ComplementSets.insert(ComplementSets.end(), it + 1, Sets.end());
  80. Res = Delta(Complement, ComplementSets);
  81. return true;
  82. }
  83. }
  84. }
  85. return false;
  86. }
  87. DeltaAlgorithm::changeset_ty DeltaAlgorithm::Run(const changeset_ty &Changes) {
  88. // Check empty set first to quickly find poor test functions.
  89. if (GetTestResult(changeset_ty()))
  90. return changeset_ty();
  91. // Otherwise run the real delta algorithm.
  92. changesetlist_ty Sets;
  93. Split(Changes, Sets);
  94. return Delta(Changes, Sets);
  95. }