llvm.org GIT mirror llvm / ca6a4d9
[STLExtras] Add size() for ranges, and remove distance() r332057 introduced distance() for ranges. Based on post-commit feedback, this renames distance() to size(). The new size() is also only enabled when the operation is O(1). Differential Revision: https://reviews.llvm.org/D46976 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@332551 91177308-0d34-0410-b5e6-96231b3b80d8 Vedant Kumar 1 year, 4 months ago
8 changed file(s) with 23 addition(s) and 14 deletion(s). Raw diff Collapse all Expand all
10251025 C.erase(remove_if(C, P), C.end());
10261026 }
10271027
1028 /// Wrapper function around std::distance which works with ranges.
1028 /// Get the size of a range. This is a wrapper function around std::distance
1029 /// which is only enabled when the operation is O(1).
10291030 template
1030 auto distance(R &&Range)
1031 auto size(R &&Range, typename std::enable_if<
1032 std::is_same
1033 Range.begin())>::iterator_category,
1034 std::random_access_iterator_tag>::value,
1035 void>::type * = nullptr)
10311036 -> decltype(std::distance(Range.begin(), Range.end())) {
10321037 return std::distance(Range.begin(), Range.end());
10331038 }
12721272 // the removal hasn't changed the structure at all. This is an important
12731273 // special case and we can directly exit the entire routine more
12741274 // efficiently as soon as we discover it.
1275 if (distance(RefSCCNodes) == NumRefSCCNodes) {
1275 if (llvm::size(RefSCCNodes) == NumRefSCCNodes) {
12761276 // Clear out the low link field as we won't need it.
12771277 for (Node *N : RefSCCNodes)
12781278 N->LowLink = -1;
17381738 }
17391739
17401740 static void printSCC(raw_ostream &OS, LazyCallGraph::SCC &C) {
1741 ptrdiff_t Size = distance(C);
1741 ptrdiff_t Size = size(C);
17421742 OS << " SCC with " << Size << " functions:\n";
17431743
17441744 for (LazyCallGraph::Node &N : C)
17461746 }
17471747
17481748 static void printRefSCC(raw_ostream &OS, LazyCallGraph::RefSCC &C) {
1749 ptrdiff_t Size = distance(C);
1749 ptrdiff_t Size = size(C);
17501750 OS << " RefSCC with " << Size << " call SCCs:\n";
17511751
17521752 for (LazyCallGraph::SCC &InnerC : C)
596596 unsigned DefReg = NoRegister;
597597 if (NumDefs != 0) {
598598 DefReg = MI->defs().begin()->getReg();
599 assert(distance(MI->defs()) == 1 && "expected exactly one def!");
599 assert(NumDefs == 1 && "expected exactly one def!");
600600 }
601601
602602 FaultMaps::FaultKind FK;
166166 return false;
167167 }
168168
169 unsigned Value::getNumUses() const { return (unsigned)distance(uses()); }
169 unsigned Value::getNumUses() const {
170 return (unsigned)std::distance(use_begin(), use_end());
171 }
170172
171173 static bool getSymTab(Value *V, ValueSymbolTable *&ST) {
172174 ST = nullptr;
577577
578578 // Returns true when the values are flowing out to each edge.
579579 bool valueAnticipable(CHIArgs C, TerminatorInst *TI) const {
580 if (TI->getNumSuccessors() > (unsigned)distance(C))
580 if (TI->getNumSuccessors() > (unsigned)size(C))
581581 return false; // Not enough args in this CHI.
582582
583583 for (auto CHI : C) {
284284 return false; // No. More than 2 predecessors.
285285
286286 // #Instructions in Succ1 for Compile Time Control
287 int Size1 = distance(Pred1->instructionsWithoutDebug());
287 auto InstsNoDbg = Pred1->instructionsWithoutDebug();
288 int Size1 = std::distance(InstsNoDbg.begin(), InstsNoDbg.end());
288289 int NStores = 0;
289290
290291 for (BasicBlock::reverse_iterator RBI = Pred0->rbegin(), RBE = Pred0->rend();
368368 std::vector v1;
369369 std::vector v2{1, 2, 3};
370370
371 EXPECT_EQ(std::distance(v1.begin(), v1.end()), distance(v1));
372 EXPECT_EQ(std::distance(v2.begin(), v2.end()), distance(v2));
371 EXPECT_EQ(std::distance(v1.begin(), v1.end()), size(v1));
372 EXPECT_EQ(std::distance(v2.begin(), v2.end()), size(v2));
373373 }
374374
375375 } // anonymous namespace
7272 auto isPhi = [](Instruction &I) { return isa(&I); };
7373 auto Phis = make_filter_range(*BB, isPhi);
7474 auto ReversedPhis = reverse(make_filter_range(*BB, isPhi));
75 EXPECT_EQ(distance(Phis), 3);
75 EXPECT_EQ(std::distance(Phis.begin(), Phis.end()), 3);
7676 EXPECT_EQ(&*Phis.begin(), P1);
77 EXPECT_EQ(distance(ReversedPhis), 3);
77 EXPECT_EQ(std::distance(ReversedPhis.begin(), ReversedPhis.end()), 3);
7878 EXPECT_EQ(&*ReversedPhis.begin(), P3);
7979
8080 // And iterate a const range.
8686 }
8787
8888 #define CHECK_ITERATORS(Range1, Range2) \
89 EXPECT_EQ(distance(Range1), distance(Range2)); \
89 EXPECT_EQ(std::distance(Range1.begin(), Range1.end()), \
90 std::distance(Range2.begin(), Range2.end())); \
9091 for (auto Pair : zip(Range1, Range2)) \
9192 EXPECT_EQ(&std::get<0>(Pair), std::get<1>(Pair));
9293