[llvm] r212370 - ADT: Add a drop_back() helper to ArrayRef
David Majnemer
david.majnemer at gmail.com
Fri Jul 4 23:12:32 PDT 2014
Author: majnemer
Date: Sat Jul 5 01:12:30 2014
New Revision: 212370
URL: http://llvm.org/viewvc/llvm-project?rev=212370&view=rev
Log:
ADT: Add a drop_back() helper to ArrayRef
The slice(N, M) interface is powerful but not concise when wanting to
drop a few elements off of an ArrayRef, fix this by adding a drop_back
method.
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=212370&r1=212369&r2=212370&view=diff
==============================================================================
--- llvm/trunk/include/llvm/ADT/ArrayRef.h (original)
+++ llvm/trunk/include/llvm/ADT/ArrayRef.h Sat Jul 5 01:12:30 2014
@@ -147,6 +147,12 @@ namespace llvm {
return ArrayRef<T>(data()+N, M);
}
+ // \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);
+ }
+
/// @}
/// @name Operator Overloads
/// @{
Modified: llvm/trunk/unittests/ADT/ArrayRefTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/ADT/ArrayRefTest.cpp?rev=212370&r1=212369&r2=212370&view=diff
==============================================================================
--- llvm/trunk/unittests/ADT/ArrayRefTest.cpp (original)
+++ llvm/trunk/unittests/ADT/ArrayRefTest.cpp Sat Jul 5 01:12:30 2014
@@ -29,5 +29,12 @@ TEST(ArrayRefTest, AllocatorCopy) {
EXPECT_NE(Array2.data(), Array2c.data());
}
+TEST(ArrayRefTest, DropBack) {
+ static const int TheNumbers[] = {4, 8, 15, 16, 23, 42};
+ ArrayRef<int> AR1(TheNumbers);
+ ArrayRef<int> AR2(TheNumbers, AR1.size() - 1);
+ EXPECT_TRUE(AR1.drop_back().equals(AR2));
+}
+
} // end anonymous namespace
More information about the llvm-commits
mailing list