DeltaAlgorithm.cpp 3.4 KB

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