#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/None.h"
+#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include <array>
#include <vector>
return std::equal(begin(), end(), RHS.begin());
}
- /// slice(n) - Chop off the first N elements of the array.
- ArrayRef<T> slice(size_t N) const {
- assert(N <= size() && "Invalid specifier");
- return ArrayRef<T>(data()+N, size()-N);
- }
-
/// slice(n, m) - Chop off the first N elements of the array, and keep M
/// elements in the array.
ArrayRef<T> slice(size_t N, size_t M) const {
return ArrayRef<T>(data()+N, M);
}
+ /// slice(n) - Chop off the first N elements of the array.
+ ArrayRef<T> slice(size_t N) const { return slice(N, size() - N); }
+
/// \brief Drop the first \p N elements of the array.
ArrayRef<T> drop_front(size_t N = 1) const {
assert(size() >= N && "Dropping more elements than exist");
return slice(0, size() - N);
}
+ /// \brief Return a copy of *this with the first N elements satisfying the
+ /// given predicate removed.
+ template <class PredicateT> ArrayRef<T> drop_while(PredicateT Pred) const {
+ return ArrayRef<T>(find_if_not(*this, Pred), end());
+ }
+
+ /// \brief Return a copy of *this with the first N elements not satisfying
+ /// the given predicate removed.
+ template <class PredicateT> ArrayRef<T> drop_until(PredicateT Pred) const {
+ return ArrayRef<T>(find_if(*this, Pred), end());
+ }
+
/// \brief Return a copy of *this with only the first \p N elements.
ArrayRef<T> take_front(size_t N = 1) const {
if (N >= size())
return drop_front(size() - N);
}
+ /// \brief Return the first N elements of this Array that satisfy the given
+ /// predicate.
+ template <class PredicateT> ArrayRef<T> take_while(PredicateT Pred) const {
+ return ArrayRef<T>(begin(), find_if_not(*this, Pred));
+ }
+
+ /// \brief Return the first N elements of this Array that don't satisfy the
+ /// given predicate.
+ template <class PredicateT> ArrayRef<T> take_until(PredicateT Pred) const {
+ return ArrayRef<T>(begin(), find_if(*this, Pred));
+ }
+
/// @}
/// @name Operator Overloads
/// @{
return data()[this->size()-1];
}
- /// slice(n) - Chop off the first N elements of the array.
- MutableArrayRef<T> slice(size_t N) const {
- assert(N <= this->size() && "Invalid specifier");
- return MutableArrayRef<T>(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<T> slice(size_t N, size_t M) const {
- assert(N+M <= this->size() && "Invalid specifier");
- return MutableArrayRef<T>(data()+N, M);
+ assert(N + M <= this->size() && "Invalid specifier");
+ return MutableArrayRef<T>(this->data() + N, M);
+ }
+
+ /// slice(n) - Chop off the first N elements of the array.
+ MutableArrayRef<T> slice(size_t N) const {
+ return slice(N, this->size() - N);
}
/// \brief Drop the first \p N elements of the array.
return slice(0, this->size() - N);
}
+ /// \brief Return a copy of *this with the first N elements satisfying the
+ /// given predicate removed.
+ template <class PredicateT>
+ MutableArrayRef<T> drop_while(PredicateT Pred) const {
+ return MutableArrayRef<T>(find_if_not(*this, Pred), end());
+ }
+
+ /// \brief Return a copy of *this with the first N elements not satisfying
+ /// the given predicate removed.
+ template <class PredicateT>
+ MutableArrayRef<T> drop_until(PredicateT Pred) const {
+ return MutableArrayRef<T>(find_if(*this, Pred), end());
+ }
+
/// \brief Return a copy of *this with only the first \p N elements.
MutableArrayRef<T> take_front(size_t N = 1) const {
if (N >= this->size())
return drop_front(this->size() - N);
}
+ /// \brief Return the first N elements of this Array that satisfy the given
+ /// predicate.
+ template <class PredicateT>
+ MutableArrayRef<T> take_while(PredicateT Pred) const {
+ return MutableArrayRef<T>(begin(), find_if_not(*this, Pred));
+ }
+
+ /// \brief Return the first N elements of this Array that don't satisfy the
+ /// given predicate.
+ template <class PredicateT>
+ MutableArrayRef<T> take_until(PredicateT Pred) const {
+ return MutableArrayRef<T>(begin(), find_if(*this, Pred));
+ }
+
/// @}
/// @name Operator Overloads
/// @{
EXPECT_EQ(1U, AR3.drop_front(AR3.size() - 1).size());
}
+TEST(ArrayRefTest, DropWhile) {
+ static const int TheNumbers[] = {1, 3, 5, 8, 10, 11};
+ ArrayRef<int> AR1(TheNumbers);
+ ArrayRef<int> 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<int>(),
+ AR1.drop_while([](const int &N) { return N > 0; }));
+}
+
+TEST(ArrayRefTest, DropUntil) {
+ static const int TheNumbers[] = {1, 3, 5, 8, 10, 11};
+ ArrayRef<int> AR1(TheNumbers);
+ ArrayRef<int> Expected = AR1.drop_front(3);
+ EXPECT_EQ(Expected, AR1.drop_until([](const int &N) { return N % 2 == 0; }));
+
+ EXPECT_EQ(ArrayRef<int>(),
+ 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<int> AR1(TheNumbers);
EXPECT_TRUE(AR1.take_front(2).equals(AR2));
}
+TEST(ArrayRefTest, TakeWhile) {
+ static const int TheNumbers[] = {1, 3, 5, 8, 10, 11};
+ ArrayRef<int> AR1(TheNumbers);
+ ArrayRef<int> Expected = AR1.take_front(3);
+ EXPECT_EQ(Expected, AR1.take_while([](const int &N) { return N % 2 == 1; }));
+
+ EXPECT_EQ(ArrayRef<int>(),
+ 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<int> AR1(TheNumbers);
+ ArrayRef<int> 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<int>(),
+ 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<int> AR1(A1);