[llvm] r268434 - [ADT] Add drop_front method to ArrayRef
Reid Kleckner via llvm-commits
llvm-commits at lists.llvm.org
Tue May 3 13:53:21 PDT 2016
Author: rnk
Date: Tue May 3 15:53:20 2016
New Revision: 268434
URL: http://llvm.org/viewvc/llvm-project?rev=268434&view=rev
Log:
[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.
Modified:
llvm/trunk/include/llvm/ADT/ArrayRef.h
llvm/trunk/unittests/ADT/ArrayRefTest.cpp
Modified: llvm/trunk/include/llvm/ADT/ArrayRef.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/ADT/ArrayRef.h?rev=268434&r1=268433&r2=268434&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/ArrayRef.h (original)
+++ llvm/trunk/include/llvm/ADT/ArrayRef.h Tue May 3 15:53:20 2016
@@ -173,7 +173,13 @@ namespace llvm {
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 {
assert(size() >= N && "Dropping more elements than exist");
return slice(0, size() - N);
@@ -285,7 +291,13 @@ namespace llvm {
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");
return slice(0, this->size() - N);
}
Modified: llvm/trunk/unittests/ADT/ArrayRefTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/ArrayRefTest.cpp?rev=268434&r1=268433&r2=268434&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/ArrayRefTest.cpp (original)
+++ llvm/trunk/unittests/ADT/ArrayRefTest.cpp Tue May 3 15:53:20 2016
@@ -67,6 +67,13 @@ TEST(ArrayRefTest, DropBack) {
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) {
static const int A1[] = {1, 2, 3, 4, 5, 6, 7, 8};
ArrayRef<int> AR1(A1);
More information about the llvm-commits
mailing list