Skip to content

Commit 8b2b4a7

Browse files
committed
Small simplification resulting from using std::min_element.
1 parent 6fa403d commit 8b2b4a7

File tree

1 file changed

+21
-33
lines changed

1 file changed

+21
-33
lines changed

lib/Sema/CSSolver.cpp

Lines changed: 21 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@
1313
// This file implements the constraint solver used in the type checker.
1414
//
1515
//===----------------------------------------------------------------------===//
16-
#include "ConstraintSystem.h"
1716
#include "ConstraintGraph.h"
17+
#include "ConstraintSystem.h"
1818
#include "swift/AST/ParameterList.h"
1919
#include "swift/AST/TypeWalker.h"
2020
#include "llvm/ADT/Statistic.h"
2121
#include "llvm/Support/Compiler.h"
22-
#include "llvm/Support/raw_ostream.h"
2322
#include "llvm/Support/SaveAndRestore.h"
23+
#include "llvm/Support/raw_ostream.h"
24+
#include <algorithm>
2425
#include <memory>
2526
#include <tuple>
2627

@@ -1814,6 +1815,9 @@ static bool shouldSkipDisjunctionChoice(ConstraintSystem &cs,
18141815
static Constraint *selectBestBindingDisjunction(
18151816
ConstraintSystem &cs, SmallVectorImpl<Constraint *> &disjunctions) {
18161817

1818+
if (disjunctions.empty())
1819+
return nullptr;
1820+
18171821
// Collect any disjunctions that simply attempt bindings for a
18181822
// type variable.
18191823
SmallVector<Constraint *, 8> bindingDisjunctions;
@@ -1884,44 +1888,28 @@ Constraint *ConstraintSystem::selectDisjunction() {
18841888
SmallVector<Constraint *, 4> disjunctions;
18851889

18861890
collectDisjunctions(disjunctions);
1887-
1888-
if (disjunctions.empty())
1889-
return nullptr;
1890-
1891-
auto *disjunction =
1892-
selectBestBindingDisjunction(*this, disjunctions);
1893-
1894-
if (disjunction)
1891+
if (auto *disjunction = selectBestBindingDisjunction(*this, disjunctions))
18951892
return disjunction;
18961893

18971894
// Pick the disjunction with the lowest disjunction number in order
18981895
// to solve them in the order they were created (which should be
18991896
// stable within an expression).
1900-
disjunction = disjunctions[0];
1901-
auto found = DisjunctionNumber.find(disjunction);
1902-
assert(found != DisjunctionNumber.end());
1903-
auto lowestNumber = found->second;
1904-
if (lowestNumber > 0) {
1905-
for (auto contender : llvm::makeArrayRef(disjunctions).slice(1)) {
1906-
auto found = DisjunctionNumber.find(contender);
1907-
assert(found != DisjunctionNumber.end());
1908-
unsigned newNumber = found->second;
1909-
if (newNumber < lowestNumber) {
1910-
lowestNumber = newNumber;
1911-
disjunction = contender;
1912-
1913-
if (lowestNumber == 0)
1914-
break;
1915-
}
1916-
}
1917-
}
1897+
auto minDisjunction =
1898+
std::min_element(disjunctions.begin(), disjunctions.end(),
1899+
[&](Constraint *first, Constraint *second) -> bool {
1900+
auto firstFound = DisjunctionNumber.find(first);
1901+
auto secondFound = DisjunctionNumber.find(second);
1902+
1903+
assert(firstFound != DisjunctionNumber.end() &&
1904+
secondFound != DisjunctionNumber.end());
19181905

1919-
// If there are no active constraints in the disjunction, there is
1920-
// no solution.
1921-
// if (bestSize == 0)
1922-
// return nullptr;
1906+
return firstFound->second < secondFound->second;
1907+
});
19231908

1924-
return disjunction;
1909+
if (minDisjunction != disjunctions.end())
1910+
return *minDisjunction;
1911+
1912+
return nullptr;
19251913
}
19261914

19271915
bool ConstraintSystem::solveSimplified(

0 commit comments

Comments
 (0)