1*0b57cec5SDimitry Andric //===--- DeltaAlgorithm.cpp - A Set Minimization Algorithm -----*- C++ -*--===//
2*0b57cec5SDimitry Andric //
3*0b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*0b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*0b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*0b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
7*0b57cec5SDimitry Andric
8*0b57cec5SDimitry Andric #include "llvm/ADT/DeltaAlgorithm.h"
9*0b57cec5SDimitry Andric #include <algorithm>
10*0b57cec5SDimitry Andric #include <iterator>
11*0b57cec5SDimitry Andric #include <set>
12*0b57cec5SDimitry Andric using namespace llvm;
13*0b57cec5SDimitry Andric
14*0b57cec5SDimitry Andric DeltaAlgorithm::~DeltaAlgorithm() = default;
15*0b57cec5SDimitry Andric
GetTestResult(const changeset_ty & Changes)16*0b57cec5SDimitry Andric bool DeltaAlgorithm::GetTestResult(const changeset_ty &Changes) {
17*0b57cec5SDimitry Andric if (FailedTestsCache.count(Changes))
18*0b57cec5SDimitry Andric return false;
19*0b57cec5SDimitry Andric
20*0b57cec5SDimitry Andric bool Result = ExecuteOneTest(Changes);
21*0b57cec5SDimitry Andric if (!Result)
22*0b57cec5SDimitry Andric FailedTestsCache.insert(Changes);
23*0b57cec5SDimitry Andric
24*0b57cec5SDimitry Andric return Result;
25*0b57cec5SDimitry Andric }
26*0b57cec5SDimitry Andric
Split(const changeset_ty & S,changesetlist_ty & Res)27*0b57cec5SDimitry Andric void DeltaAlgorithm::Split(const changeset_ty &S, changesetlist_ty &Res) {
28*0b57cec5SDimitry Andric // FIXME: Allow clients to provide heuristics for improved splitting.
29*0b57cec5SDimitry Andric
30*0b57cec5SDimitry Andric // FIXME: This is really slow.
31*0b57cec5SDimitry Andric changeset_ty LHS, RHS;
32*0b57cec5SDimitry Andric unsigned idx = 0, N = S.size() / 2;
33*0b57cec5SDimitry Andric for (changeset_ty::const_iterator it = S.begin(),
34*0b57cec5SDimitry Andric ie = S.end(); it != ie; ++it, ++idx)
35*0b57cec5SDimitry Andric ((idx < N) ? LHS : RHS).insert(*it);
36*0b57cec5SDimitry Andric if (!LHS.empty())
37*0b57cec5SDimitry Andric Res.push_back(LHS);
38*0b57cec5SDimitry Andric if (!RHS.empty())
39*0b57cec5SDimitry Andric Res.push_back(RHS);
40*0b57cec5SDimitry Andric }
41*0b57cec5SDimitry Andric
42*0b57cec5SDimitry Andric DeltaAlgorithm::changeset_ty
Delta(const changeset_ty & Changes,const changesetlist_ty & Sets)43*0b57cec5SDimitry Andric DeltaAlgorithm::Delta(const changeset_ty &Changes,
44*0b57cec5SDimitry Andric const changesetlist_ty &Sets) {
45*0b57cec5SDimitry Andric // Invariant: union(Res) == Changes
46*0b57cec5SDimitry Andric UpdatedSearchState(Changes, Sets);
47*0b57cec5SDimitry Andric
48*0b57cec5SDimitry Andric // If there is nothing left we can remove, we are done.
49*0b57cec5SDimitry Andric if (Sets.size() <= 1)
50*0b57cec5SDimitry Andric return Changes;
51*0b57cec5SDimitry Andric
52*0b57cec5SDimitry Andric // Look for a passing subset.
53*0b57cec5SDimitry Andric changeset_ty Res;
54*0b57cec5SDimitry Andric if (Search(Changes, Sets, Res))
55*0b57cec5SDimitry Andric return Res;
56*0b57cec5SDimitry Andric
57*0b57cec5SDimitry Andric // Otherwise, partition the sets if possible; if not we are done.
58*0b57cec5SDimitry Andric changesetlist_ty SplitSets;
59*0b57cec5SDimitry Andric for (const changeset_ty &Set : Sets)
60*0b57cec5SDimitry Andric Split(Set, SplitSets);
61*0b57cec5SDimitry Andric if (SplitSets.size() == Sets.size())
62*0b57cec5SDimitry Andric return Changes;
63*0b57cec5SDimitry Andric
64*0b57cec5SDimitry Andric return Delta(Changes, SplitSets);
65*0b57cec5SDimitry Andric }
66*0b57cec5SDimitry Andric
Search(const changeset_ty & Changes,const changesetlist_ty & Sets,changeset_ty & Res)67*0b57cec5SDimitry Andric bool DeltaAlgorithm::Search(const changeset_ty &Changes,
68*0b57cec5SDimitry Andric const changesetlist_ty &Sets,
69*0b57cec5SDimitry Andric changeset_ty &Res) {
70*0b57cec5SDimitry Andric // FIXME: Parallelize.
71*0b57cec5SDimitry Andric for (changesetlist_ty::const_iterator it = Sets.begin(),
72*0b57cec5SDimitry Andric ie = Sets.end(); it != ie; ++it) {
73*0b57cec5SDimitry Andric // If the test passes on this subset alone, recurse.
74*0b57cec5SDimitry Andric if (GetTestResult(*it)) {
75*0b57cec5SDimitry Andric changesetlist_ty Sets;
76*0b57cec5SDimitry Andric Split(*it, Sets);
77*0b57cec5SDimitry Andric Res = Delta(*it, Sets);
78*0b57cec5SDimitry Andric return true;
79*0b57cec5SDimitry Andric }
80*0b57cec5SDimitry Andric
81*0b57cec5SDimitry Andric // Otherwise, if we have more than two sets, see if test passes on the
82*0b57cec5SDimitry Andric // complement.
83*0b57cec5SDimitry Andric if (Sets.size() > 2) {
84*0b57cec5SDimitry Andric // FIXME: This is really slow.
85*0b57cec5SDimitry Andric changeset_ty Complement;
86*0b57cec5SDimitry Andric std::set_difference(
87*0b57cec5SDimitry Andric Changes.begin(), Changes.end(), it->begin(), it->end(),
88*0b57cec5SDimitry Andric std::insert_iterator<changeset_ty>(Complement, Complement.begin()));
89*0b57cec5SDimitry Andric if (GetTestResult(Complement)) {
90*0b57cec5SDimitry Andric changesetlist_ty ComplementSets;
91*0b57cec5SDimitry Andric ComplementSets.insert(ComplementSets.end(), Sets.begin(), it);
92*0b57cec5SDimitry Andric ComplementSets.insert(ComplementSets.end(), it + 1, Sets.end());
93*0b57cec5SDimitry Andric Res = Delta(Complement, ComplementSets);
94*0b57cec5SDimitry Andric return true;
95*0b57cec5SDimitry Andric }
96*0b57cec5SDimitry Andric }
97*0b57cec5SDimitry Andric }
98*0b57cec5SDimitry Andric
99*0b57cec5SDimitry Andric return false;
100*0b57cec5SDimitry Andric }
101*0b57cec5SDimitry Andric
Run(const changeset_ty & Changes)102*0b57cec5SDimitry Andric DeltaAlgorithm::changeset_ty DeltaAlgorithm::Run(const changeset_ty &Changes) {
103*0b57cec5SDimitry Andric // Check empty set first to quickly find poor test functions.
104*0b57cec5SDimitry Andric if (GetTestResult(changeset_ty()))
105*0b57cec5SDimitry Andric return changeset_ty();
106*0b57cec5SDimitry Andric
107*0b57cec5SDimitry Andric // Otherwise run the real delta algorithm.
108*0b57cec5SDimitry Andric changesetlist_ty Sets;
109*0b57cec5SDimitry Andric Split(Changes, Sets);
110*0b57cec5SDimitry Andric
111*0b57cec5SDimitry Andric return Delta(Changes, Sets);
112*0b57cec5SDimitry Andric }
113*0b57cec5SDimitry Andric