1 //===--- DeltaAlgorithm.h - A Set Minimization Algorithm -------*- C++ -*--===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/DeltaAlgorithm.h"
13 bool DeltaAlgorithm::GetTestResult(const changeset_ty &Changes) {
14 if (FailedTestsCache.count(Changes))
17 bool Result = ExecuteOneTest(Changes);
19 FailedTestsCache.insert(Changes);
24 void DeltaAlgorithm::Split(const changeset_ty &S, changesetlist_ty &Res) {
25 // FIXME: Allow clients to provide heuristics for improved splitting.
27 // FIXME: This is really slow.
28 changeset_ty LHS, RHS;
30 for (changeset_ty::const_iterator it = S.begin(),
31 ie = S.end(); it != ie; ++it, ++idx)
32 ((idx & 1) ? LHS : RHS).insert(*it);
39 DeltaAlgorithm::changeset_ty
40 DeltaAlgorithm::Delta(const changeset_ty &Changes,
41 const changesetlist_ty &Sets) {
42 // Invariant: union(Res) == Changes
43 UpdatedSearchState(Changes, Sets);
45 // If there is nothing left we can remove, we are done.
49 // Look for a passing subset.
51 if (Search(Changes, Sets, Res))
54 // Otherwise, partition the sets if possible; if not we are done.
55 changesetlist_ty SplitSets;
56 for (changesetlist_ty::const_iterator it = Sets.begin(),
57 ie = Sets.end(); it != ie; ++it)
58 Split(*it, SplitSets);
59 if (SplitSets.size() == Sets.size())
62 return Delta(Changes, SplitSets);
65 bool DeltaAlgorithm::Search(const changeset_ty &Changes,
66 const changesetlist_ty &Sets,
68 // FIXME: Parallelize.
69 for (changesetlist_ty::const_iterator it = Sets.begin(),
70 ie = Sets.end(); it != ie; ++it) {
71 // If the test passes on this subset alone, recurse.
72 if (GetTestResult(*it)) {
73 changesetlist_ty Sets;
75 Res = Delta(*it, Sets);
79 // Otherwise, if we have more than two sets, see if test passes on the
81 if (Sets.size() > 2) {
82 // FIXME: This is really slow.
83 changeset_ty Complement;
85 Changes.begin(), Changes.end(), it->begin(), it->end(),
86 std::insert_iterator<changeset_ty>(Complement, Complement.begin()));
87 if (GetTestResult(Complement)) {
88 changesetlist_ty ComplementSets;
89 ComplementSets.insert(ComplementSets.end(), Sets.begin(), it);
90 ComplementSets.insert(ComplementSets.end(), it + 1, Sets.end());
91 Res = Delta(Complement, ComplementSets);
100 DeltaAlgorithm::changeset_ty DeltaAlgorithm::Run(const changeset_ty &Changes) {
101 // Check empty set first to quickly find poor test functions.
102 if (GetTestResult(changeset_ty()))
103 return changeset_ty();
105 // Otherwise run the real delta algorithm.
106 changesetlist_ty Sets;
107 Split(Changes, Sets);
109 return Delta(Changes, Sets);