Index: include/llvm/ADT/ArrayRef.h =================================================================== --- include/llvm/ADT/ArrayRef.h +++ include/llvm/ADT/ArrayRef.h @@ -31,6 +31,7 @@ template class LLVM_NODISCARD ArrayRef { public: + static const size_t npos = ~size_t(0); typedef const T *iterator; typedef const T *const_iterator; typedef size_t size_type; @@ -165,19 +166,43 @@ return std::equal(begin(), end(), RHS.begin()); } - /// slice(n) - Chop off the first N elements of the array. - ArrayRef slice(size_t N) const { - assert(N <= size() && "Invalid specifier"); - return ArrayRef(data()+N, size()-N); + /// Search for the first element satisfying the predicate \p F + /// + /// \returns The index of the first element satisfying \p F starting from + /// \p From, or npos if not found. + size_t find_if(function_ref F, size_t From = 0) const { + ArrayRef S = drop_front(From); + while (!S.empty()) { + if (F(S.front())) + return size() - S.size(); + S = S.drop_front(); + } + return npos; + } + + /// Search for the first element not satisfying the predicate \p F + /// + /// \returns The index of the first element not satisfying \p F starting + /// from \p From, or npos if not found. + size_t find_if_not(function_ref F, size_t From = 0) const { + return find_if([F](const T &c) { return !F(c); }, From); } /// slice(n, m) - Chop off the first N elements of the array, and keep M /// elements in the array. ArrayRef slice(size_t N, size_t M) const { + if (N == npos) + return ArrayRef(); + if (M == npos) + return *this; + assert(N+M <= size() && "Invalid specifier"); return ArrayRef(data()+N, M); } + /// slice(n) - Chop off the first N elements of the array. + ArrayRef slice(size_t N) const { return slice(N, size() - N); } + /// \brief Drop the first \p N elements of the array. ArrayRef drop_front(size_t N = 1) const { assert(size() >= N && "Dropping more elements than exist"); @@ -190,6 +215,18 @@ return slice(0, size() - N); } + /// \brief Return a copy of *this with the first N elements satisfying the + /// given predicate removed. + ArrayRef drop_while(function_ref F) const { + return slice(find_if_not(F)); + } + + /// \brief Return a copy of *this with the first N elements not satisfying + /// the given predicate removed. + ArrayRef drop_until(function_ref F) const { + return slice(find_if(F)); + } + /// \brief Return a copy of *this with only the first \p N elements. ArrayRef take_front(size_t N = 1) const { if (N >= size()) @@ -204,6 +241,18 @@ return drop_front(size() - N); } + /// \brief Return the first N elements of this Array that satisfy the given + /// predicate. + ArrayRef take_while(function_ref F) const { + return slice(0, find_if_not(F)); + } + + /// \brief Return the first N elements of this Array that don't satisfy the + /// given predicate. + ArrayRef take_until(function_ref F) const { + return slice(0, find_if(F)); + } + /// @} /// @name Operator Overloads /// @{ @@ -317,17 +366,21 @@ return data()[this->size()-1]; } - /// slice(n) - Chop off the first N elements of the array. - MutableArrayRef slice(size_t N) const { - assert(N <= this->size() && "Invalid specifier"); - return MutableArrayRef(data()+N, this->size()-N); - } - /// slice(n, m) - Chop off the first N elements of the array, and keep M /// elements in the array. MutableArrayRef slice(size_t N, size_t M) const { - assert(N+M <= this->size() && "Invalid specifier"); - return MutableArrayRef(data()+N, M); + if (N == npos) + return MutableArrayRef(); + if (M == npos) + return *this; + + assert(N + M <= this->size() && "Invalid specifier"); + return MutableArrayRef(this->data() + N, M); + } + + /// slice(n) - Chop off the first N elements of the array. + MutableArrayRef slice(size_t N) const { + return slice(N, this->size() - N); } /// \brief Drop the first \p N elements of the array. @@ -341,6 +394,18 @@ return slice(0, this->size() - N); } + /// \brief Return a copy of *this with the first N elements satisfying the + /// given predicate removed. + MutableArrayRef drop_while(function_ref F) const { + return slice(find_if_not(F)); + } + + /// \brief Return a copy of *this with the first N elements not satisfying + /// the given predicate removed. + MutableArrayRef drop_until(function_ref F) const { + return slice(find_if(F)); + } + /// \brief Return a copy of *this with only the first \p N elements. MutableArrayRef take_front(size_t N = 1) const { if (N >= this->size()) @@ -355,6 +420,18 @@ return drop_front(this->size() - N); } + /// \brief Return the first N elements of this Array that satisfy the given + /// predicate. + MutableArrayRef take_while(function_ref F) const { + return slice(0, find_if_not(F)); + } + + /// \brief Return the first N elements of this Array that don't satisfy the + /// given predicate. + MutableArrayRef take_until(function_ref F) const { + return slice(0, find_if(F)); + } + /// @} /// @name Operator Overloads /// @{ Index: unittests/ADT/ArrayRefTest.cpp =================================================================== --- unittests/ADT/ArrayRefTest.cpp +++ unittests/ADT/ArrayRefTest.cpp @@ -102,6 +102,28 @@ EXPECT_EQ(1U, AR3.drop_front(AR3.size() - 1).size()); } +TEST(ArrayRefTest, DropWhile) { + static const int TheNumbers[] = {1, 3, 5, 8, 10, 11}; + ArrayRef AR1(TheNumbers); + ArrayRef Expected = AR1.drop_front(3); + EXPECT_EQ(Expected, AR1.drop_while([](const int &N) { return N % 2 == 1; })); + + EXPECT_EQ(AR1, AR1.drop_while([](const int &N) { return N < 0; })); + EXPECT_EQ(ArrayRef(), + AR1.drop_while([](const int &N) { return N > 0; })); +} + +TEST(ArrayRefTest, DropUntil) { + static const int TheNumbers[] = {1, 3, 5, 8, 10, 11}; + ArrayRef AR1(TheNumbers); + ArrayRef Expected = AR1.drop_front(3); + EXPECT_EQ(Expected, AR1.drop_until([](const int &N) { return N % 2 == 0; })); + + EXPECT_EQ(ArrayRef(), + AR1.drop_until([](const int &N) { return N < 0; })); + EXPECT_EQ(AR1, AR1.drop_until([](const int &N) { return N > 0; })); +} + TEST(ArrayRefTest, TakeBack) { static const int TheNumbers[] = {4, 8, 15, 16, 23, 42}; ArrayRef AR1(TheNumbers); @@ -116,6 +138,28 @@ EXPECT_TRUE(AR1.take_front(2).equals(AR2)); } +TEST(ArrayRefTest, TakeWhile) { + static const int TheNumbers[] = {1, 3, 5, 8, 10, 11}; + ArrayRef AR1(TheNumbers); + ArrayRef Expected = AR1.take_front(3); + EXPECT_EQ(Expected, AR1.take_while([](const int &N) { return N % 2 == 1; })); + + EXPECT_EQ(ArrayRef(), + AR1.take_while([](const int &N) { return N < 0; })); + EXPECT_EQ(AR1, AR1.take_while([](const int &N) { return N > 0; })); +} + +TEST(ArrayRefTest, TakeUntil) { + static const int TheNumbers[] = {1, 3, 5, 8, 10, 11}; + ArrayRef AR1(TheNumbers); + ArrayRef Expected = AR1.take_front(3); + EXPECT_EQ(Expected, AR1.take_until([](const int &N) { return N % 2 == 0; })); + + EXPECT_EQ(AR1, AR1.take_until([](const int &N) { return N < 0; })); + EXPECT_EQ(ArrayRef(), + AR1.take_until([](const int &N) { return N > 0; })); +} + TEST(ArrayRefTest, Equals) { static const int A1[] = {1, 2, 3, 4, 5, 6, 7, 8}; ArrayRef AR1(A1);