DeltaAlgorithm.h 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //===- DeltaAlgorithm.h - 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. #ifndef LLVM_ADT_DELTAALGORITHM_H
  8. #define LLVM_ADT_DELTAALGORITHM_H
  9. #include <set>
  10. #include <vector>
  11. namespace llvm {
  12. /// DeltaAlgorithm - Implements the delta debugging algorithm (A. Zeller '99)
  13. /// for minimizing arbitrary sets using a predicate function.
  14. ///
  15. /// The result of the algorithm is a subset of the input change set which is
  16. /// guaranteed to satisfy the predicate, assuming that the input set did. For
  17. /// well formed predicates, the result set is guaranteed to be such that
  18. /// removing any single element would falsify the predicate.
  19. ///
  20. /// For best results the predicate function *should* (but need not) satisfy
  21. /// certain properties, in particular:
  22. /// (1) The predicate should return false on an empty set and true on the full
  23. /// set.
  24. /// (2) If the predicate returns true for a set of changes, it should return
  25. /// true for all supersets of that set.
  26. ///
  27. /// It is not an error to provide a predicate that does not satisfy these
  28. /// requirements, and the algorithm will generally produce reasonable
  29. /// results. However, it may run substantially more tests than with a good
  30. /// predicate.
  31. class DeltaAlgorithm {
  32. public:
  33. using change_ty = unsigned;
  34. // FIXME: Use a decent data structure.
  35. using changeset_ty = std::set<change_ty>;
  36. using changesetlist_ty = std::vector<changeset_ty>;
  37. private:
  38. /// Cache of failed test results. Successful test results are never cached
  39. /// since we always reduce following a success.
  40. std::set<changeset_ty> FailedTestsCache;
  41. /// GetTestResult - Get the test result for the \p Changes from the
  42. /// cache, executing the test if necessary.
  43. ///
  44. /// \param Changes - The change set to test.
  45. /// \return - The test result.
  46. bool GetTestResult(const changeset_ty &Changes);
  47. /// Split - Partition a set of changes \p S into one or two subsets.
  48. void Split(const changeset_ty &S, changesetlist_ty &Res);
  49. /// Delta - Minimize a set of \p Changes which has been partitioned into
  50. /// smaller sets, by attempting to remove individual subsets.
  51. changeset_ty Delta(const changeset_ty &Changes,
  52. const changesetlist_ty &Sets);
  53. /// Search - Search for a subset (or subsets) in \p Sets which can be
  54. /// removed from \p Changes while still satisfying the predicate.
  55. ///
  56. /// \param Res - On success, a subset of Changes which satisfies the
  57. /// predicate.
  58. /// \return - True on success.
  59. bool Search(const changeset_ty &Changes, const changesetlist_ty &Sets,
  60. changeset_ty &Res);
  61. protected:
  62. /// UpdatedSearchState - Callback used when the search state changes.
  63. virtual void UpdatedSearchState(const changeset_ty &Changes,
  64. const changesetlist_ty &Sets) {}
  65. /// ExecuteOneTest - Execute a single test predicate on the change set \p S.
  66. virtual bool ExecuteOneTest(const changeset_ty &S) = 0;
  67. DeltaAlgorithm& operator=(const DeltaAlgorithm&) = default;
  68. public:
  69. virtual ~DeltaAlgorithm();
  70. /// Run - Minimize the set \p Changes by executing \see ExecuteOneTest() on
  71. /// subsets of changes and returning the smallest set which still satisfies
  72. /// the test predicate.
  73. changeset_ty Run(const changeset_ty &Changes);
  74. };
  75. } // end namespace llvm
  76. #endif // LLVM_ADT_DELTAALGORITHM_H