10b57cec5SDimitry Andric //===--- DeltaAlgorithm.cpp - A Set Minimization Algorithm -----*- C++ -*--===// 20b57cec5SDimitry Andric // 30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information. 50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 60b57cec5SDimitry Andric //===----------------------------------------------------------------------===// 70b57cec5SDimitry Andric 80b57cec5SDimitry Andric #include "llvm/ADT/DeltaAlgorithm.h" 90b57cec5SDimitry Andric #include <algorithm> 100b57cec5SDimitry Andric #include <iterator> 110b57cec5SDimitry Andric #include <set> 120b57cec5SDimitry Andric using namespace llvm; 130b57cec5SDimitry Andric 1481ad6265SDimitry Andric DeltaAlgorithm::~DeltaAlgorithm() = default; 150b57cec5SDimitry Andric 160b57cec5SDimitry Andric bool DeltaAlgorithm::GetTestResult(const changeset_ty &Changes) { 170b57cec5SDimitry Andric if (FailedTestsCache.count(Changes)) 180b57cec5SDimitry Andric return false; 190b57cec5SDimitry Andric 200b57cec5SDimitry Andric bool Result = ExecuteOneTest(Changes); 210b57cec5SDimitry Andric if (!Result) 220b57cec5SDimitry Andric FailedTestsCache.insert(Changes); 230b57cec5SDimitry Andric 240b57cec5SDimitry Andric return Result; 250b57cec5SDimitry Andric } 260b57cec5SDimitry Andric 270b57cec5SDimitry Andric void DeltaAlgorithm::Split(const changeset_ty &S, changesetlist_ty &Res) { 280b57cec5SDimitry Andric // FIXME: Allow clients to provide heuristics for improved splitting. 290b57cec5SDimitry Andric 300b57cec5SDimitry Andric // FIXME: This is really slow. 310b57cec5SDimitry Andric changeset_ty LHS, RHS; 320b57cec5SDimitry Andric unsigned idx = 0, N = S.size() / 2; 330b57cec5SDimitry Andric for (changeset_ty::const_iterator it = S.begin(), 340b57cec5SDimitry Andric ie = S.end(); it != ie; ++it, ++idx) 350b57cec5SDimitry Andric ((idx < N) ? LHS : RHS).insert(*it); 360b57cec5SDimitry Andric if (!LHS.empty()) 370b57cec5SDimitry Andric Res.push_back(LHS); 380b57cec5SDimitry Andric if (!RHS.empty()) 390b57cec5SDimitry Andric Res.push_back(RHS); 400b57cec5SDimitry Andric } 410b57cec5SDimitry Andric 420b57cec5SDimitry Andric DeltaAlgorithm::changeset_ty 430b57cec5SDimitry Andric DeltaAlgorithm::Delta(const changeset_ty &Changes, 440b57cec5SDimitry Andric const changesetlist_ty &Sets) { 450b57cec5SDimitry Andric // Invariant: union(Res) == Changes 460b57cec5SDimitry Andric UpdatedSearchState(Changes, Sets); 470b57cec5SDimitry Andric 480b57cec5SDimitry Andric // If there is nothing left we can remove, we are done. 490b57cec5SDimitry Andric if (Sets.size() <= 1) 500b57cec5SDimitry Andric return Changes; 510b57cec5SDimitry Andric 520b57cec5SDimitry Andric // Look for a passing subset. 530b57cec5SDimitry Andric changeset_ty Res; 540b57cec5SDimitry Andric if (Search(Changes, Sets, Res)) 550b57cec5SDimitry Andric return Res; 560b57cec5SDimitry Andric 570b57cec5SDimitry Andric // Otherwise, partition the sets if possible; if not we are done. 580b57cec5SDimitry Andric changesetlist_ty SplitSets; 590eae32dcSDimitry Andric for (const changeset_ty &Set : Sets) 600eae32dcSDimitry Andric Split(Set, SplitSets); 610b57cec5SDimitry Andric if (SplitSets.size() == Sets.size()) 620b57cec5SDimitry Andric return Changes; 630b57cec5SDimitry Andric 640b57cec5SDimitry Andric return Delta(Changes, SplitSets); 650b57cec5SDimitry Andric } 660b57cec5SDimitry Andric 670b57cec5SDimitry Andric bool DeltaAlgorithm::Search(const changeset_ty &Changes, 680b57cec5SDimitry Andric const changesetlist_ty &Sets, 690b57cec5SDimitry Andric changeset_ty &Res) { 700b57cec5SDimitry Andric // FIXME: Parallelize. 710b57cec5SDimitry Andric for (changesetlist_ty::const_iterator it = Sets.begin(), 720b57cec5SDimitry Andric ie = Sets.end(); it != ie; ++it) { 730b57cec5SDimitry Andric // If the test passes on this subset alone, recurse. 740b57cec5SDimitry Andric if (GetTestResult(*it)) { 750b57cec5SDimitry Andric changesetlist_ty Sets; 760b57cec5SDimitry Andric Split(*it, Sets); 770b57cec5SDimitry Andric Res = Delta(*it, Sets); 780b57cec5SDimitry Andric return true; 790b57cec5SDimitry Andric } 800b57cec5SDimitry Andric 810b57cec5SDimitry Andric // Otherwise, if we have more than two sets, see if test passes on the 820b57cec5SDimitry Andric // complement. 830b57cec5SDimitry Andric if (Sets.size() > 2) { 840b57cec5SDimitry Andric // FIXME: This is really slow. 850b57cec5SDimitry Andric changeset_ty Complement; 86*0fca6ea1SDimitry Andric std::set_difference(Changes.begin(), Changes.end(), it->begin(), 87*0fca6ea1SDimitry Andric it->end(), 88*0fca6ea1SDimitry Andric std::inserter(Complement, Complement.begin())); 890b57cec5SDimitry Andric if (GetTestResult(Complement)) { 900b57cec5SDimitry Andric changesetlist_ty ComplementSets; 910b57cec5SDimitry Andric ComplementSets.insert(ComplementSets.end(), Sets.begin(), it); 920b57cec5SDimitry Andric ComplementSets.insert(ComplementSets.end(), it + 1, Sets.end()); 930b57cec5SDimitry Andric Res = Delta(Complement, ComplementSets); 940b57cec5SDimitry Andric return true; 950b57cec5SDimitry Andric } 960b57cec5SDimitry Andric } 970b57cec5SDimitry Andric } 980b57cec5SDimitry Andric 990b57cec5SDimitry Andric return false; 1000b57cec5SDimitry Andric } 1010b57cec5SDimitry Andric 1020b57cec5SDimitry Andric DeltaAlgorithm::changeset_ty DeltaAlgorithm::Run(const changeset_ty &Changes) { 1030b57cec5SDimitry Andric // Check empty set first to quickly find poor test functions. 1040b57cec5SDimitry Andric if (GetTestResult(changeset_ty())) 1050b57cec5SDimitry Andric return changeset_ty(); 1060b57cec5SDimitry Andric 1070b57cec5SDimitry Andric // Otherwise run the real delta algorithm. 1080b57cec5SDimitry Andric changesetlist_ty Sets; 1090b57cec5SDimitry Andric Split(Changes, Sets); 1100b57cec5SDimitry Andric 1110b57cec5SDimitry Andric return Delta(Changes, Sets); 1120b57cec5SDimitry Andric } 113