LLVM API Documentation
00001 //===--- DeltaAlgorithm.h - A Set Minimization Algorithm -------*- C++ -*--===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 //===----------------------------------------------------------------------===// 00008 00009 #ifndef LLVM_ADT_DELTAALGORITHM_H 00010 #define LLVM_ADT_DELTAALGORITHM_H 00011 00012 #include <set> 00013 #include <vector> 00014 00015 namespace llvm { 00016 00017 /// DeltaAlgorithm - Implements the delta debugging algorithm (A. Zeller '99) 00018 /// for minimizing arbitrary sets using a predicate function. 00019 /// 00020 /// The result of the algorithm is a subset of the input change set which is 00021 /// guaranteed to satisfy the predicate, assuming that the input set did. For 00022 /// well formed predicates, the result set is guaranteed to be such that 00023 /// removing any single element would falsify the predicate. 00024 /// 00025 /// For best results the predicate function *should* (but need not) satisfy 00026 /// certain properties, in particular: 00027 /// (1) The predicate should return false on an empty set and true on the full 00028 /// set. 00029 /// (2) If the predicate returns true for a set of changes, it should return 00030 /// true for all supersets of that set. 00031 /// 00032 /// It is not an error to provide a predicate that does not satisfy these 00033 /// requirements, and the algorithm will generally produce reasonable 00034 /// results. However, it may run substantially more tests than with a good 00035 /// predicate. 00036 class DeltaAlgorithm { 00037 public: 00038 typedef unsigned change_ty; 00039 // FIXME: Use a decent data structure. 00040 typedef std::set<change_ty> changeset_ty; 00041 typedef std::vector<changeset_ty> changesetlist_ty; 00042 00043 private: 00044 /// Cache of failed test results. Successful test results are never cached 00045 /// since we always reduce following a success. 00046 std::set<changeset_ty> FailedTestsCache; 00047 00048 /// GetTestResult - Get the test result for the \p Changes from the 00049 /// cache, executing the test if necessary. 00050 /// 00051 /// \param Changes - The change set to test. 00052 /// \return - The test result. 00053 bool GetTestResult(const changeset_ty &Changes); 00054 00055 /// Split - Partition a set of changes \p S into one or two subsets. 00056 void Split(const changeset_ty &S, changesetlist_ty &Res); 00057 00058 /// Delta - Minimize a set of \p Changes which has been partioned into 00059 /// smaller sets, by attempting to remove individual subsets. 00060 changeset_ty Delta(const changeset_ty &Changes, 00061 const changesetlist_ty &Sets); 00062 00063 /// Search - Search for a subset (or subsets) in \p Sets which can be 00064 /// removed from \p Changes while still satisfying the predicate. 00065 /// 00066 /// \param Res - On success, a subset of Changes which satisfies the 00067 /// predicate. 00068 /// \return - True on success. 00069 bool Search(const changeset_ty &Changes, const changesetlist_ty &Sets, 00070 changeset_ty &Res); 00071 00072 protected: 00073 /// UpdatedSearchState - Callback used when the search state changes. 00074 virtual void UpdatedSearchState(const changeset_ty &Changes, 00075 const changesetlist_ty &Sets) {} 00076 00077 /// ExecuteOneTest - Execute a single test predicate on the change set \p S. 00078 virtual bool ExecuteOneTest(const changeset_ty &S) = 0; 00079 00080 public: 00081 virtual ~DeltaAlgorithm(); 00082 00083 /// Run - Minimize the set \p Changes by executing \see ExecuteOneTest() on 00084 /// subsets of changes and returning the smallest set which still satisfies 00085 /// the test predicate. 00086 changeset_ty Run(const changeset_ty &Changes); 00087 }; 00088 00089 } // end namespace llvm 00090 00091 #endif