[ADT] Add drop_front method to ArrayRef

We have it for StringRef but not ArrayRef, and ArrayRef has drop_back,
so I see no reason it shouldn't have drop_front. Splitting this out of a
change that I have that will use this funcitonality.

llvm-svn: 268434
This commit is contained in:
Reid Kleckner 2016-05-03 20:53:20 +00:00
parent ed75c93233
commit 9a635e6090
2 changed files with 21 additions and 2 deletions

View File

@ -173,7 +173,13 @@ namespace llvm {
return ArrayRef<T>(data()+N, M); return ArrayRef<T>(data()+N, M);
} }
// \brief Drop the last \p N elements of the array. /// \brief Drop the first \p N elements of the array.
ArrayRef<T> drop_front(unsigned N = 1) const {
assert(size() >= N && "Dropping more elements than exist");
return slice(N, size() - N);
}
/// \brief Drop the last \p N elements of the array.
ArrayRef<T> drop_back(unsigned N = 1) const { ArrayRef<T> drop_back(unsigned N = 1) const {
assert(size() >= N && "Dropping more elements than exist"); assert(size() >= N && "Dropping more elements than exist");
return slice(0, size() - N); return slice(0, size() - N);
@ -285,7 +291,13 @@ namespace llvm {
return MutableArrayRef<T>(data()+N, M); return MutableArrayRef<T>(data()+N, M);
} }
MutableArrayRef<T> drop_back(unsigned N) const { /// \brief Drop the first \p N elements of the array.
MutableArrayRef<T> drop_front(unsigned N = 1) const {
assert(this->size() >= N && "Dropping more elements than exist");
return slice(N, this->size() - N);
}
MutableArrayRef<T> drop_back(unsigned N = 1) const {
assert(this->size() >= N && "Dropping more elements than exist"); assert(this->size() >= N && "Dropping more elements than exist");
return slice(0, this->size() - N); return slice(0, this->size() - N);
} }

View File

@ -67,6 +67,13 @@ TEST(ArrayRefTest, DropBack) {
EXPECT_TRUE(AR1.drop_back().equals(AR2)); EXPECT_TRUE(AR1.drop_back().equals(AR2));
} }
TEST(ArrayRefTest, DropFront) {
static const int TheNumbers[] = {4, 8, 15, 16, 23, 42};
ArrayRef<int> AR1(TheNumbers);
ArrayRef<int> AR2(&TheNumbers[2], AR1.size() - 2);
EXPECT_TRUE(AR1.drop_front(2).equals(AR2));
}
TEST(ArrayRefTest, Equals) { TEST(ArrayRefTest, Equals) {
static const int A1[] = {1, 2, 3, 4, 5, 6, 7, 8}; static const int A1[] = {1, 2, 3, 4, 5, 6, 7, 8};
ArrayRef<int> AR1(A1); ArrayRef<int> AR1(A1);