[llvm] r212370 - ADT: Add a drop_back() helper to ArrayRef
David Blaikie
dblaikie at gmail.com
Mon Jul 7 15:51:36 PDT 2014
Test pedantry: Might be nice to have a test for the non-default value.
On Fri, Jul 4, 2014 at 11:12 PM, David Majnemer
<david.majnemer at gmail.com> wrote:
> 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
>
>
> _______________________________________________
> llvm-commits mailing list
> llvm-commits at cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvm-commits
More information about the llvm-commits
mailing list