Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:03

0001 //===- DeltaAlgorithm.h - A Set Minimization Algorithm ---------*- C++ -*--===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //===----------------------------------------------------------------------===//
0007 
0008 #ifndef LLVM_ADT_DELTAALGORITHM_H
0009 #define LLVM_ADT_DELTAALGORITHM_H
0010 
0011 #include <set>
0012 #include <vector>
0013 
0014 namespace llvm {
0015 
0016 /// DeltaAlgorithm - Implements the delta debugging algorithm (A. Zeller '99)
0017 /// for minimizing arbitrary sets using a predicate function.
0018 ///
0019 /// The result of the algorithm is a subset of the input change set which is
0020 /// guaranteed to satisfy the predicate, assuming that the input set did. For
0021 /// well formed predicates, the result set is guaranteed to be such that
0022 /// removing any single element would falsify the predicate.
0023 ///
0024 /// For best results the predicate function *should* (but need not) satisfy
0025 /// certain properties, in particular:
0026 ///  (1) The predicate should return false on an empty set and true on the full
0027 ///  set.
0028 ///  (2) If the predicate returns true for a set of changes, it should return
0029 ///  true for all supersets of that set.
0030 ///
0031 /// It is not an error to provide a predicate that does not satisfy these
0032 /// requirements, and the algorithm will generally produce reasonable
0033 /// results. However, it may run substantially more tests than with a good
0034 /// predicate.
0035 class DeltaAlgorithm {
0036 public:
0037   using change_ty = unsigned;
0038   // FIXME: Use a decent data structure.
0039   using changeset_ty = std::set<change_ty>;
0040   using changesetlist_ty = std::vector<changeset_ty>;
0041 
0042 private:
0043   /// Cache of failed test results. Successful test results are never cached
0044   /// since we always reduce following a success.
0045   std::set<changeset_ty> FailedTestsCache;
0046 
0047   /// GetTestResult - Get the test result for the \p Changes from the
0048   /// cache, executing the test if necessary.
0049   ///
0050   /// \param Changes - The change set to test.
0051   /// \return - The test result.
0052   bool GetTestResult(const changeset_ty &Changes);
0053 
0054   /// Split - Partition a set of changes \p S into one or two subsets.
0055   void Split(const changeset_ty &S, changesetlist_ty &Res);
0056 
0057   /// Delta - Minimize a set of \p Changes which has been partitioned into
0058   /// smaller sets, by attempting to remove individual subsets.
0059   changeset_ty Delta(const changeset_ty &Changes,
0060                      const changesetlist_ty &Sets);
0061 
0062   /// Search - Search for a subset (or subsets) in \p Sets which can be
0063   /// removed from \p Changes while still satisfying the predicate.
0064   ///
0065   /// \param Res - On success, a subset of Changes which satisfies the
0066   /// predicate.
0067   /// \return - True on success.
0068   bool Search(const changeset_ty &Changes, const changesetlist_ty &Sets,
0069               changeset_ty &Res);
0070 
0071 protected:
0072   /// UpdatedSearchState - Callback used when the search state changes.
0073   virtual void UpdatedSearchState(const changeset_ty &Changes,
0074                                   const changesetlist_ty &Sets) {}
0075 
0076   /// ExecuteOneTest - Execute a single test predicate on the change set \p S.
0077   virtual bool ExecuteOneTest(const changeset_ty &S) = 0;
0078 
0079   DeltaAlgorithm& operator=(const DeltaAlgorithm&) = default;
0080 
0081 public:
0082   virtual ~DeltaAlgorithm();
0083 
0084   /// Run - Minimize the set \p Changes by executing \see ExecuteOneTest() on
0085   /// subsets of changes and returning the smallest set which still satisfies
0086   /// the test predicate.
0087   changeset_ty Run(const changeset_ty &Changes);
0088 };
0089 
0090 } // end namespace llvm
0091 
0092 #endif // LLVM_ADT_DELTAALGORITHM_H