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"
15 DeltaAlgorithm::~DeltaAlgorithm() {
18 bool DeltaAlgorithm::GetTestResult(const changeset_ty &Changes) {
19 if (FailedTestsCache.count(Changes))
22 bool Result = ExecuteOneTest(Changes);
24 FailedTestsCache.insert(Changes);
29 void DeltaAlgorithm::Split(const changeset_ty &S, changesetlist_ty &Res) {
30 // FIXME: Allow clients to provide heuristics for improved splitting.
32 // FIXME: This is really slow.
33 changeset_ty LHS, RHS;
34 unsigned idx = 0, N = S.size() / 2;
35 for (changeset_ty::const_iterator it = S.begin(),
36 ie = S.end(); it != ie; ++it, ++idx)
37 ((idx < N) ? LHS : RHS).insert(*it);
44 DeltaAlgorithm::changeset_ty
45 DeltaAlgorithm::Delta(const changeset_ty &Changes,
46 const changesetlist_ty &Sets) {
47 // Invariant: union(Res) == Changes
48 UpdatedSearchState(Changes, Sets);
50 // If there is nothing left we can remove, we are done.
54 // Look for a passing subset.
56 if (Search(Changes, Sets, Res))
59 // Otherwise, partition the sets if possible; if not we are done.
60 changesetlist_ty SplitSets;
61 for (changesetlist_ty::const_iterator it = Sets.begin(),
62 ie = Sets.end(); it != ie; ++it)
63 Split(*it, SplitSets);
64 if (SplitSets.size() == Sets.size())
67 return Delta(Changes, SplitSets);
70 bool DeltaAlgorithm::Search(const changeset_ty &Changes,
71 const changesetlist_ty &Sets,
73 // FIXME: Parallelize.
74 for (changesetlist_ty::const_iterator it = Sets.begin(),
75 ie = Sets.end(); it != ie; ++it) {
76 // If the test passes on this subset alone, recurse.
77 if (GetTestResult(*it)) {
78 changesetlist_ty Sets;
80 Res = Delta(*it, Sets);
84 // Otherwise, if we have more than two sets, see if test passes on the
86 if (Sets.size() > 2) {
87 // FIXME: This is really slow.
88 changeset_ty Complement;
90 Changes.begin(), Changes.end(), it->begin(), it->end(),
91 std::insert_iterator<changeset_ty>(Complement, Complement.begin()));
92 if (GetTestResult(Complement)) {
93 changesetlist_ty ComplementSets;
94 ComplementSets.insert(ComplementSets.end(), Sets.begin(), it);
95 ComplementSets.insert(ComplementSets.end(), it + 1, Sets.end());
96 Res = Delta(Complement, ComplementSets);
105 DeltaAlgorithm::changeset_ty DeltaAlgorithm::Run(const changeset_ty &Changes) {
106 // Check empty set first to quickly find poor test functions.
107 if (GetTestResult(changeset_ty()))
108 return changeset_ty();
110 // Otherwise run the real delta algorithm.
111 changesetlist_ty Sets;
112 Split(Changes, Sets);
114 return Delta(Changes, Sets);