1 //===--- DeltaAlgorithm.cpp - 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"
14 bool DeltaAlgorithm::GetTestResult(const changeset_ty &Changes) {
15 if (FailedTestsCache.count(Changes))
18 bool Result = ExecuteOneTest(Changes);
20 FailedTestsCache.insert(Changes);
25 void DeltaAlgorithm::Split(const changeset_ty &S, changesetlist_ty &Res) {
26 // FIXME: Allow clients to provide heuristics for improved splitting.
28 // FIXME: This is really slow.
29 changeset_ty LHS, RHS;
31 for (changeset_ty::const_iterator it = S.begin(),
32 ie = S.end(); it != ie; ++it, ++idx)
33 ((idx & 1) ? LHS : RHS).insert(*it);
40 DeltaAlgorithm::changeset_ty
41 DeltaAlgorithm::Delta(const changeset_ty &Changes,
42 const changesetlist_ty &Sets) {
43 // Invariant: union(Res) == Changes
44 UpdatedSearchState(Changes, Sets);
46 // If there is nothing left we can remove, we are done.
50 // Look for a passing subset.
52 if (Search(Changes, Sets, Res))
55 // Otherwise, partition the sets if possible; if not we are done.
56 changesetlist_ty SplitSets;
57 for (changesetlist_ty::const_iterator it = Sets.begin(),
58 ie = Sets.end(); it != ie; ++it)
59 Split(*it, SplitSets);
60 if (SplitSets.size() == Sets.size())
63 return Delta(Changes, SplitSets);
66 bool DeltaAlgorithm::Search(const changeset_ty &Changes,
67 const changesetlist_ty &Sets,
69 // FIXME: Parallelize.
70 for (changesetlist_ty::const_iterator it = Sets.begin(),
71 ie = Sets.end(); it != ie; ++it) {
72 // If the test passes on this subset alone, recurse.
73 if (GetTestResult(*it)) {
74 changesetlist_ty Sets;
76 Res = Delta(*it, Sets);
80 // Otherwise, if we have more than two sets, see if test passes on the
82 if (Sets.size() > 2) {
83 // FIXME: This is really slow.
84 changeset_ty Complement;
86 Changes.begin(), Changes.end(), it->begin(), it->end(),
87 std::insert_iterator<changeset_ty>(Complement, Complement.begin()));
88 if (GetTestResult(Complement)) {
89 changesetlist_ty ComplementSets;
90 ComplementSets.insert(ComplementSets.end(), Sets.begin(), it);
91 ComplementSets.insert(ComplementSets.end(), it + 1, Sets.end());
92 Res = Delta(Complement, ComplementSets);
101 DeltaAlgorithm::changeset_ty DeltaAlgorithm::Run(const changeset_ty &Changes) {
102 // Check empty set first to quickly find poor test functions.
103 if (GetTestResult(changeset_ty()))
104 return changeset_ty();
106 // Otherwise run the real delta algorithm.
107 changesetlist_ty Sets;
108 Split(Changes, Sets);
110 return Delta(Changes, Sets);